Mercurial > cpython-lru_cache-weakref
annotate Lib/tarfile.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | a015a298543d |
| children | 5f9e13066d36 |
| rev | line source |
|---|---|
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2 # -*- coding: iso-8859-1 -*- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
3 #------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
4 # tarfile.py |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
5 #------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
6 # Copyright (C) 2002 Lars Gustäbel <lars@gustaebel.de> |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
7 # All rights reserved. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
8 # |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
9 # Permission is hereby granted, free of charge, to any person |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
10 # obtaining a copy of this software and associated documentation |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
11 # files (the "Software"), to deal in the Software without |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
12 # restriction, including without limitation the rights to use, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
13 # copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
14 # copies of the Software, and to permit persons to whom the |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
15 # Software is furnished to do so, subject to the following |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
16 # conditions: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
17 # |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
18 # The above copyright notice and this permission notice shall be |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
19 # included in all copies or substantial portions of the Software. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
20 # |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
22 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
23 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
25 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
26 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
27 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
28 # OTHER DEALINGS IN THE SOFTWARE. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
29 # |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
30 """Read from and write to tar format archives. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
31 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
32 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
33 __version__ = "$Revision$" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
34 # $Source$ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
35 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
36 version = "0.9.0" |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
37 __author__ = "Lars Gustäbel (lars@gustaebel.de)" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
38 __date__ = "$Date$" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
39 __cvsid__ = "$Id$" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
40 __credits__ = "Gustavo Niemeyer, Niels Gustäbel, Richard Townsend." |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
41 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
42 #--------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
43 # Imports |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
44 #--------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
45 import sys |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
46 import os |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
47 import shutil |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
48 import stat |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
49 import errno |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
50 import time |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
51 import struct |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
52 import copy |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
53 import re |
|
48175
979f0a89903f
Remove a dict.has_key() and list.sort(cmp=) usage from tarfile to silence
Brett Cannon <bcannon@gmail.com>
parents:
48138
diff
changeset
|
54 import operator |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
55 |
|
28217
73b0dc28c91a
Make tarfile raise ImportError on MacOS9. The pathname handling needs work, and I don't have time to fix it. I'll file a bug report.
Jack Jansen <jack.jansen@cwi.nl>
parents:
28214
diff
changeset
|
56 if sys.platform == 'mac': |
|
73b0dc28c91a
Make tarfile raise ImportError on MacOS9. The pathname handling needs work, and I don't have time to fix it. I'll file a bug report.
Jack Jansen <jack.jansen@cwi.nl>
parents:
28214
diff
changeset
|
57 # This module needs work for MacOS9, especially in the area of pathname |
|
73b0dc28c91a
Make tarfile raise ImportError on MacOS9. The pathname handling needs work, and I don't have time to fix it. I'll file a bug report.
Jack Jansen <jack.jansen@cwi.nl>
parents:
28214
diff
changeset
|
58 # handling. In many places it is assumed a simple substitution of / by the |
|
73b0dc28c91a
Make tarfile raise ImportError on MacOS9. The pathname handling needs work, and I don't have time to fix it. I'll file a bug report.
Jack Jansen <jack.jansen@cwi.nl>
parents:
28214
diff
changeset
|
59 # local os.path.sep is good enough to convert pathnames, but this does not |
|
73b0dc28c91a
Make tarfile raise ImportError on MacOS9. The pathname handling needs work, and I don't have time to fix it. I'll file a bug report.
Jack Jansen <jack.jansen@cwi.nl>
parents:
28214
diff
changeset
|
60 # work with the mac rooted:path:name versus :nonrooted:path:name syntax |
|
73b0dc28c91a
Make tarfile raise ImportError on MacOS9. The pathname handling needs work, and I don't have time to fix it. I'll file a bug report.
Jack Jansen <jack.jansen@cwi.nl>
parents:
28214
diff
changeset
|
61 raise ImportError, "tarfile does not work for platform==mac" |
|
73b0dc28c91a
Make tarfile raise ImportError on MacOS9. The pathname handling needs work, and I don't have time to fix it. I'll file a bug report.
Jack Jansen <jack.jansen@cwi.nl>
parents:
28214
diff
changeset
|
62 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
63 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
64 import grp, pwd |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
65 except ImportError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
66 grp = pwd = None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
67 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
68 # from tarfile import * |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
69 __all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
70 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
71 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
72 # tar constants |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
73 #--------------------------------------------------------- |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
74 NUL = "\0" # the null character |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
75 BLOCKSIZE = 512 # length of processing blocks |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
76 RECORDSIZE = BLOCKSIZE * 20 # length of records |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
77 GNU_MAGIC = "ustar \0" # magic gnu tar string |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
78 POSIX_MAGIC = "ustar\x0000" # magic posix tar string |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
79 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
80 LENGTH_NAME = 100 # maximum length of a filename |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
81 LENGTH_LINK = 100 # maximum length of a linkname |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
82 LENGTH_PREFIX = 155 # maximum length of the prefix field |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
83 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
84 REGTYPE = "0" # regular file |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
85 AREGTYPE = "\0" # regular file |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
86 LNKTYPE = "1" # link (inside tarfile) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
87 SYMTYPE = "2" # symbolic link |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
88 CHRTYPE = "3" # character special device |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
89 BLKTYPE = "4" # block special device |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
90 DIRTYPE = "5" # directory |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
91 FIFOTYPE = "6" # fifo special device |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
92 CONTTYPE = "7" # contiguous file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
93 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
94 GNUTYPE_LONGNAME = "L" # GNU tar longname |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
95 GNUTYPE_LONGLINK = "K" # GNU tar longlink |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
96 GNUTYPE_SPARSE = "S" # GNU tar sparse file |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
97 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
98 XHDTYPE = "x" # POSIX.1-2001 extended header |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
99 XGLTYPE = "g" # POSIX.1-2001 global header |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
100 SOLARIS_XHDTYPE = "X" # Solaris extended header |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
101 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
102 USTAR_FORMAT = 0 # POSIX.1-1988 (ustar) format |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
103 GNU_FORMAT = 1 # GNU tar format |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
104 PAX_FORMAT = 2 # POSIX.1-2001 (pax) format |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
105 DEFAULT_FORMAT = GNU_FORMAT |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
106 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
107 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
108 # tarfile constants |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
109 #--------------------------------------------------------- |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
110 # File types that tarfile supports: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
111 SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
112 SYMTYPE, DIRTYPE, FIFOTYPE, |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
113 CONTTYPE, CHRTYPE, BLKTYPE, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
114 GNUTYPE_LONGNAME, GNUTYPE_LONGLINK, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
115 GNUTYPE_SPARSE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
116 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
117 # File types that will be treated as a regular file. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
118 REGULAR_TYPES = (REGTYPE, AREGTYPE, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
119 CONTTYPE, GNUTYPE_SPARSE) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
120 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
121 # File types that are part of the GNU tar format. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
122 GNU_TYPES = (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
123 GNUTYPE_SPARSE) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
124 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
125 # Fields from a pax header that override a TarInfo attribute. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
126 PAX_FIELDS = ("path", "linkpath", "size", "mtime", |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
127 "uid", "gid", "uname", "gname") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
128 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
129 # Fields in a pax header that are numbers, all other fields |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
130 # are treated as strings. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
131 PAX_NUMBER_FIELDS = { |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
132 "atime": float, |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
133 "ctime": float, |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
134 "mtime": float, |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
135 "uid": int, |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
136 "gid": int, |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
137 "size": int |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
138 } |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
139 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
140 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
141 # Bits used in the mode field, values in octal. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
142 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
143 S_IFLNK = 0120000 # symbolic link |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
144 S_IFREG = 0100000 # regular file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
145 S_IFBLK = 0060000 # block device |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
146 S_IFDIR = 0040000 # directory |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
147 S_IFCHR = 0020000 # character device |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
148 S_IFIFO = 0010000 # fifo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
149 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
150 TSUID = 04000 # set UID on execution |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
151 TSGID = 02000 # set GID on execution |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
152 TSVTX = 01000 # reserved |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
153 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
154 TUREAD = 0400 # read by owner |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
155 TUWRITE = 0200 # write by owner |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
156 TUEXEC = 0100 # execute/search by owner |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
157 TGREAD = 0040 # read by group |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
158 TGWRITE = 0020 # write by group |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
159 TGEXEC = 0010 # execute/search by group |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
160 TOREAD = 0004 # read by other |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
161 TOWRITE = 0002 # write by other |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
162 TOEXEC = 0001 # execute/search by other |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
163 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
164 #--------------------------------------------------------- |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
165 # initialization |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
166 #--------------------------------------------------------- |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
167 ENCODING = sys.getfilesystemencoding() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
168 if ENCODING is None: |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
169 ENCODING = sys.getdefaultencoding() |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
170 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
171 #--------------------------------------------------------- |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
172 # Some useful functions |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
173 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
174 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
175 def stn(s, length): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
176 """Convert a python string to a null-terminated string buffer. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
177 """ |
|
40170
e82e696c03dc
Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents:
40111
diff
changeset
|
178 return s[:length] + (length - len(s)) * NUL |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
179 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
180 def nts(s): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
181 """Convert a null-terminated string field to a python string. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
182 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
183 # Use the string up to the first null char. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
184 p = s.find("\0") |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
185 if p == -1: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
186 return s |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
187 return s[:p] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
188 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
189 def nti(s): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
190 """Convert a number field to a python number. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
191 """ |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
192 # There are two possible encodings for a number field, see |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
193 # itn() below. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
194 if s[0] != chr(0200): |
|
40491
1250fe42ce5c
Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents:
40487
diff
changeset
|
195 try: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
196 n = int(nts(s) or "0", 8) |
|
40491
1250fe42ce5c
Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents:
40487
diff
changeset
|
197 except ValueError: |
|
1250fe42ce5c
Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents:
40487
diff
changeset
|
198 raise HeaderError("invalid header") |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
199 else: |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
200 n = 0L |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
201 for i in xrange(len(s) - 1): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
202 n <<= 8 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
203 n += ord(s[i + 1]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
204 return n |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
205 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
206 def itn(n, digits=8, format=DEFAULT_FORMAT): |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
207 """Convert a python number to a number field. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
208 """ |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
209 # POSIX 1003.1-1988 requires numbers to be encoded as a string of |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
210 # octal digits followed by a null-byte, this allows values up to |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
211 # (8**(digits-1))-1. GNU tar allows storing numbers greater than |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
212 # that if necessary. A leading 0200 byte indicates this particular |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
213 # encoding, the following digits-1 bytes are a big-endian |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
214 # representation. This allows values up to (256**(digits-1))-1. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
215 if 0 <= n < 8 ** (digits - 1): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
216 s = "%0*o" % (digits - 1, n) + NUL |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
217 else: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
218 if format != GNU_FORMAT or n >= 256 ** (digits - 1): |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
219 raise ValueError("overflow in number field") |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
220 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
221 if n < 0: |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
222 # XXX We mimic GNU tar's behaviour with negative numbers, |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
223 # this could raise OverflowError. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
224 n = struct.unpack("L", struct.pack("l", n))[0] |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
225 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
226 s = "" |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
227 for i in xrange(digits - 1): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
228 s = chr(n & 0377) + s |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
229 n >>= 8 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
230 s = chr(0200) + s |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
231 return s |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
232 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
233 def uts(s, encoding, errors): |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
234 """Convert a unicode object to a string. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
235 """ |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
236 if errors == "utf-8": |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
237 # An extra error handler similar to the -o invalid=UTF-8 option |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
238 # in POSIX.1-2001. Replace untranslatable characters with their |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
239 # UTF-8 representation. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
240 try: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
241 return s.encode(encoding, "strict") |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
242 except UnicodeEncodeError: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
243 x = [] |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
244 for c in s: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
245 try: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
246 x.append(c.encode(encoding, "strict")) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
247 except UnicodeEncodeError: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
248 x.append(c.encode("utf8")) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
249 return "".join(x) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
250 else: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
251 return s.encode(encoding, errors) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
252 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
253 def calc_chksums(buf): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
254 """Calculate the checksum for a member's header by summing up all |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
255 characters except for the chksum field which is treated as if |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
256 it was filled with spaces. According to the GNU tar sources, |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
257 some tars (Sun and NeXT) calculate chksum with signed char, |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
258 which will be different if there are chars in the buffer with |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
259 the high bit set. So we calculate two checksums, unsigned and |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
260 signed. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
261 """ |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
262 unsigned_chksum = 256 + sum(struct.unpack("148B", buf[:148]) + struct.unpack("356B", buf[156:512])) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
263 signed_chksum = 256 + sum(struct.unpack("148b", buf[:148]) + struct.unpack("356b", buf[156:512])) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
264 return unsigned_chksum, signed_chksum |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
265 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
266 def copyfileobj(src, dst, length=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
267 """Copy length bytes from fileobj src to fileobj dst. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
268 If length is None, copy the entire content. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
269 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
270 if length == 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
271 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
272 if length is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
273 shutil.copyfileobj(src, dst) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
274 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
275 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
276 BUFSIZE = 16 * 1024 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
277 blocks, remainder = divmod(length, BUFSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
278 for b in xrange(blocks): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
279 buf = src.read(BUFSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
280 if len(buf) < BUFSIZE: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
281 raise IOError("end of file reached") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
282 dst.write(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
283 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
284 if remainder != 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
285 buf = src.read(remainder) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
286 if len(buf) < remainder: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
287 raise IOError("end of file reached") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
288 dst.write(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
289 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
290 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
291 filemode_table = ( |
|
33810
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
292 ((S_IFLNK, "l"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
293 (S_IFREG, "-"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
294 (S_IFBLK, "b"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
295 (S_IFDIR, "d"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
296 (S_IFCHR, "c"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
297 (S_IFIFO, "p")), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
298 |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
299 ((TUREAD, "r"),), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
300 ((TUWRITE, "w"),), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
301 ((TUEXEC|TSUID, "s"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
302 (TSUID, "S"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
303 (TUEXEC, "x")), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
304 |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
305 ((TGREAD, "r"),), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
306 ((TGWRITE, "w"),), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
307 ((TGEXEC|TSGID, "s"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
308 (TSGID, "S"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
309 (TGEXEC, "x")), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
310 |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
311 ((TOREAD, "r"),), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
312 ((TOWRITE, "w"),), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
313 ((TOEXEC|TSVTX, "t"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
314 (TSVTX, "T"), |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
315 (TOEXEC, "x")) |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
316 ) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
317 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
318 def filemode(mode): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
319 """Convert a file's mode to a string of the form |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
320 -rwxrwxrwx. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
321 Used by TarFile.list() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
322 """ |
|
33810
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
323 perm = [] |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
324 for table in filemode_table: |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
325 for bit, char in table: |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
326 if mode & bit == bit: |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
327 perm.append(char) |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
328 break |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
329 else: |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
330 perm.append("-") |
|
3ff252715d93
[Patch #1043972, for bug #1017553] filemode() returns an incorrect value for the mode 07111
Andrew M. Kuchling <amk@amk.ca>
parents:
33507
diff
changeset
|
331 return "".join(perm) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
332 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
333 if os.sep != "/": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
334 normpath = lambda path: os.path.normpath(path).replace(os.sep, "/") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
335 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
336 normpath = os.path.normpath |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
337 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
338 class TarError(Exception): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
339 """Base exception.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
340 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
341 class ExtractError(TarError): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
342 """General exception for extract errors.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
343 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
344 class ReadError(TarError): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
345 """Exception for unreadble tar archives.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
346 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
347 class CompressionError(TarError): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
348 """Exception for unavailable compression methods.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
349 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
350 class StreamError(TarError): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
351 """Exception for unsupported operations on stream-like TarFiles.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
352 pass |
|
40487
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
353 class HeaderError(TarError): |
|
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
354 """Exception for invalid headers.""" |
|
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
355 pass |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
356 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
357 #--------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
358 # internal stream interface |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
359 #--------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
360 class _LowLevelFile: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
361 """Low-level file object. Supports reading and writing. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
362 It is used instead of a regular file object for streaming |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
363 access. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
364 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
365 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
366 def __init__(self, name, mode): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
367 mode = { |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
368 "r": os.O_RDONLY, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
369 "w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
370 }[mode] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
371 if hasattr(os, "O_BINARY"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
372 mode |= os.O_BINARY |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
373 self.fd = os.open(name, mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
374 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
375 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
376 os.close(self.fd) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
377 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
378 def read(self, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
379 return os.read(self.fd, size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
380 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
381 def write(self, s): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
382 os.write(self.fd, s) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
383 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
384 class _Stream: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
385 """Class that serves as an adapter between TarFile and |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
386 a stream-like object. The stream-like object only |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
387 needs to have a read() or write() method and is accessed |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
388 blockwise. Use of gzip or bzip2 compression is possible. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
389 A stream-like object could be for example: sys.stdin, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
390 sys.stdout, a socket, a tape device etc. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
391 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
392 _Stream is intended to be used only internally. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
393 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
394 |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
395 def __init__(self, name, mode, comptype, fileobj, bufsize): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
396 """Construct a _Stream object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
397 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
398 self._extfileobj = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
399 if fileobj is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
400 fileobj = _LowLevelFile(name, mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
401 self._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
402 |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
403 if comptype == '*': |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
404 # Enable transparent compression detection for the |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
405 # stream interface |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
406 fileobj = _StreamProxy(fileobj) |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
407 comptype = fileobj.getcomptype() |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
408 |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
409 self.name = name or "" |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
410 self.mode = mode |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
411 self.comptype = comptype |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
412 self.fileobj = fileobj |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
413 self.bufsize = bufsize |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
414 self.buf = "" |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
415 self.pos = 0L |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
416 self.closed = False |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
417 |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
418 if comptype == "gz": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
419 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
420 import zlib |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
421 except ImportError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
422 raise CompressionError("zlib module is not available") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
423 self.zlib = zlib |
|
46063
ce45fd3a7a3f
A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents:
45164
diff
changeset
|
424 self.crc = zlib.crc32("") & 0xffffffffL |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
425 if mode == "r": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
426 self._init_read_gz() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
427 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
428 self._init_write_gz() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
429 |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
430 if comptype == "bz2": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
431 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
432 import bz2 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
433 except ImportError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
434 raise CompressionError("bz2 module is not available") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
435 if mode == "r": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
436 self.dbuf = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
437 self.cmp = bz2.BZ2Decompressor() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
438 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
439 self.cmp = bz2.BZ2Compressor() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
440 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
441 def __del__(self): |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
442 if hasattr(self, "closed") and not self.closed: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
443 self.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
444 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
445 def _init_write_gz(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
446 """Initialize for writing with gzip compression. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
447 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
448 self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
449 -self.zlib.MAX_WBITS, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
450 self.zlib.DEF_MEM_LEVEL, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
451 0) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
452 timestamp = struct.pack("<L", long(time.time())) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
453 self.__write("\037\213\010\010%s\002\377" % timestamp) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
454 if self.name.endswith(".gz"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
455 self.name = self.name[:-3] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
456 self.__write(self.name + NUL) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
457 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
458 def write(self, s): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
459 """Write string s to the stream. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
460 """ |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
461 if self.comptype == "gz": |
|
46063
ce45fd3a7a3f
A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents:
45164
diff
changeset
|
462 self.crc = self.zlib.crc32(s, self.crc) & 0xffffffffL |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
463 self.pos += len(s) |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
464 if self.comptype != "tar": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
465 s = self.cmp.compress(s) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
466 self.__write(s) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
467 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
468 def __write(self, s): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
469 """Write string s to the stream if a whole new block |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
470 is ready to be written. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
471 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
472 self.buf += s |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
473 while len(self.buf) > self.bufsize: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
474 self.fileobj.write(self.buf[:self.bufsize]) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
475 self.buf = self.buf[self.bufsize:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
476 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
477 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
478 """Close the _Stream object. No operation should be |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
479 done on it afterwards. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
480 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
481 if self.closed: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
482 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
483 |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
484 if self.mode == "w" and self.comptype != "tar": |
|
33234
ab34980ffa16
Flush bz2 data even if nothing had been written so far. Fixes #1013882.
Martin v. Löwis <martin@v.loewis.de>
parents:
33208
diff
changeset
|
485 self.buf += self.cmp.flush() |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
486 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
487 if self.mode == "w" and self.buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
488 self.fileobj.write(self.buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
489 self.buf = "" |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
490 if self.comptype == "gz": |
|
39349
fa2dac3926ff
_Stream.close(): Try to kill struct.pack() warnings when
Tim Peters <tim.peters@gmail.com>
parents:
39042
diff
changeset
|
491 # The native zlib crc is an unsigned 32-bit integer, but |
|
fa2dac3926ff
_Stream.close(): Try to kill struct.pack() warnings when
Tim Peters <tim.peters@gmail.com>
parents:
39042
diff
changeset
|
492 # the Python wrapper implicitly casts that to a signed C |
|
fa2dac3926ff
_Stream.close(): Try to kill struct.pack() warnings when
Tim Peters <tim.peters@gmail.com>
parents:
39042
diff
changeset
|
493 # long. So, on a 32-bit box self.crc may "look negative", |
|
fa2dac3926ff
_Stream.close(): Try to kill struct.pack() warnings when
Tim Peters <tim.peters@gmail.com>
parents:
39042
diff
changeset
|
494 # while the same crc on a 64-bit box may "look positive". |
|
fa2dac3926ff
_Stream.close(): Try to kill struct.pack() warnings when
Tim Peters <tim.peters@gmail.com>
parents:
39042
diff
changeset
|
495 # To avoid irksome warnings from the `struct` module, force |
|
fa2dac3926ff
_Stream.close(): Try to kill struct.pack() warnings when
Tim Peters <tim.peters@gmail.com>
parents:
39042
diff
changeset
|
496 # it to look positive on all boxes. |
|
fa2dac3926ff
_Stream.close(): Try to kill struct.pack() warnings when
Tim Peters <tim.peters@gmail.com>
parents:
39042
diff
changeset
|
497 self.fileobj.write(struct.pack("<L", self.crc & 0xffffffffL)) |
|
30769
2bc95d49fc7a
[Backport fix for bug #822668] gzip filesize should be written out mod 2**32
Andrew M. Kuchling <amk@amk.ca>
parents:
28731
diff
changeset
|
498 self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFFL)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
499 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
500 if not self._extfileobj: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
501 self.fileobj.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
502 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
503 self.closed = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
504 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
505 def _init_read_gz(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
506 """Initialize for reading a gzip compressed fileobj. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
507 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
508 self.cmp = self.zlib.decompressobj(-self.zlib.MAX_WBITS) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
509 self.dbuf = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
510 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
511 # taken from gzip.GzipFile with some alterations |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
512 if self.__read(2) != "\037\213": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
513 raise ReadError("not a gzip file") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
514 if self.__read(1) != "\010": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
515 raise CompressionError("unsupported compression method") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
516 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
517 flag = ord(self.__read(1)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
518 self.__read(6) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
519 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
520 if flag & 4: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
521 xlen = ord(self.__read(1)) + 256 * ord(self.__read(1)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
522 self.read(xlen) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
523 if flag & 8: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
524 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
525 s = self.__read(1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
526 if not s or s == NUL: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
527 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
528 if flag & 16: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
529 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
530 s = self.__read(1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
531 if not s or s == NUL: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
532 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
533 if flag & 2: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
534 self.__read(2) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
535 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
536 def tell(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
537 """Return the stream's file pointer position. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
538 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
539 return self.pos |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
540 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
541 def seek(self, pos=0): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
542 """Set the stream's file pointer to pos. Negative seeking |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
543 is forbidden. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
544 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
545 if pos - self.pos >= 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
546 blocks, remainder = divmod(pos - self.pos, self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
547 for i in xrange(blocks): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
548 self.read(self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
549 self.read(remainder) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
550 else: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
551 raise StreamError("seeking backwards is not allowed") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
552 return self.pos |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
553 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
554 def read(self, size=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
555 """Return the next size number of bytes from the stream. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
556 If size is not defined, return all bytes of the stream |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
557 up to EOF. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
558 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
559 if size is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
560 t = [] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
561 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
562 buf = self._read(self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
563 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
564 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
565 t.append(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
566 buf = "".join(t) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
567 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
568 buf = self._read(size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
569 self.pos += len(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
570 return buf |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
571 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
572 def _read(self, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
573 """Return size bytes from the stream. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
574 """ |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
575 if self.comptype == "tar": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
576 return self.__read(size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
577 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
578 c = len(self.dbuf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
579 t = [self.dbuf] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
580 while c < size: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
581 buf = self.__read(self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
582 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
583 break |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
584 try: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
585 buf = self.cmp.decompress(buf) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
586 except IOError: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
587 raise ReadError("invalid compressed data") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
588 t.append(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
589 c += len(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
590 t = "".join(t) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
591 self.dbuf = t[size:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
592 return t[:size] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
593 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
594 def __read(self, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
595 """Return size bytes from stream. If internal buffer is empty, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
596 read another block from the stream. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
597 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
598 c = len(self.buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
599 t = [self.buf] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
600 while c < size: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
601 buf = self.fileobj.read(self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
602 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
603 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
604 t.append(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
605 c += len(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
606 t = "".join(t) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
607 self.buf = t[size:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
608 return t[:size] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
609 # class _Stream |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
610 |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
611 class _StreamProxy(object): |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
612 """Small proxy class that enables transparent compression |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
613 detection for the Stream interface (mode 'r|*'). |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
614 """ |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
615 |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
616 def __init__(self, fileobj): |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
617 self.fileobj = fileobj |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
618 self.buf = self.fileobj.read(BLOCKSIZE) |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
619 |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
620 def read(self, size): |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
621 self.read = self.fileobj.read |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
622 return self.buf |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
623 |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
624 def getcomptype(self): |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
625 if self.buf.startswith("\037\213\010"): |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
626 return "gz" |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
627 if self.buf.startswith("BZh91"): |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
628 return "bz2" |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
629 return "tar" |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
630 |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
631 def close(self): |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
632 self.fileobj.close() |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
633 # class StreamProxy |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
634 |
|
38185
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
635 class _BZ2Proxy(object): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
636 """Small proxy class that enables external file object |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
637 support for "r:bz2" and "w:bz2" modes. This is actually |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
638 a workaround for a limitation in bz2 module's BZ2File |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
639 class which (unlike gzip.GzipFile) has no support for |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
640 a file object argument. |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
641 """ |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
642 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
643 blocksize = 16 * 1024 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
644 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
645 def __init__(self, fileobj, mode): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
646 self.fileobj = fileobj |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
647 self.mode = mode |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
648 self.name = getattr(self.fileobj, "name", None) |
|
38185
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
649 self.init() |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
650 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
651 def init(self): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
652 import bz2 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
653 self.pos = 0 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
654 if self.mode == "r": |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
655 self.bz2obj = bz2.BZ2Decompressor() |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
656 self.fileobj.seek(0) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
657 self.buf = "" |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
658 else: |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
659 self.bz2obj = bz2.BZ2Compressor() |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
660 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
661 def read(self, size): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
662 b = [self.buf] |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
663 x = len(self.buf) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
664 while x < size: |
|
52267
445d404270ea
Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents:
51775
diff
changeset
|
665 raw = self.fileobj.read(self.blocksize) |
|
445d404270ea
Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents:
51775
diff
changeset
|
666 if not raw: |
|
38185
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
667 break |
|
52267
445d404270ea
Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents:
51775
diff
changeset
|
668 data = self.bz2obj.decompress(raw) |
|
445d404270ea
Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents:
51775
diff
changeset
|
669 b.append(data) |
|
38185
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
670 x += len(data) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
671 self.buf = "".join(b) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
672 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
673 buf = self.buf[:size] |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
674 self.buf = self.buf[size:] |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
675 self.pos += len(buf) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
676 return buf |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
677 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
678 def seek(self, pos): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
679 if pos < self.pos: |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
680 self.init() |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
681 self.read(pos - self.pos) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
682 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
683 def tell(self): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
684 return self.pos |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
685 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
686 def write(self, data): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
687 self.pos += len(data) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
688 raw = self.bz2obj.compress(data) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
689 self.fileobj.write(raw) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
690 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
691 def close(self): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
692 if self.mode == "w": |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
693 raw = self.bz2obj.flush() |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
694 self.fileobj.write(raw) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
695 # class _BZ2Proxy |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
696 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
697 #------------------------ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
698 # Extraction file object |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
699 #------------------------ |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
700 class _FileInFile(object): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
701 """A thin wrapper around an existing file object that |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
702 provides a part of its data as an individual file |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
703 object. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
704 """ |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
705 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
706 def __init__(self, fileobj, offset, size, sparse=None): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
707 self.fileobj = fileobj |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
708 self.offset = offset |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
709 self.size = size |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
710 self.sparse = sparse |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
711 self.position = 0 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
712 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
713 def tell(self): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
714 """Return the current file position. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
715 """ |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
716 return self.position |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
717 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
718 def seek(self, position): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
719 """Seek to a position in the file. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
720 """ |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
721 self.position = position |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
722 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
723 def read(self, size=None): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
724 """Read data from the file. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
725 """ |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
726 if size is None: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
727 size = self.size - self.position |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
728 else: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
729 size = min(size, self.size - self.position) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
730 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
731 if self.sparse is None: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
732 return self.readnormal(size) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
733 else: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
734 return self.readsparse(size) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
735 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
736 def readnormal(self, size): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
737 """Read operation for regular files. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
738 """ |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
739 self.fileobj.seek(self.offset + self.position) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
740 self.position += size |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
741 return self.fileobj.read(size) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
742 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
743 def readsparse(self, size): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
744 """Read operation for sparse files. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
745 """ |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
746 data = [] |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
747 while size > 0: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
748 buf = self.readsparsesection(size) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
749 if not buf: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
750 break |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
751 size -= len(buf) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
752 data.append(buf) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
753 return "".join(data) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
754 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
755 def readsparsesection(self, size): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
756 """Read a single section of a sparse file. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
757 """ |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
758 section = self.sparse.find(self.position) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
759 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
760 if section is None: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
761 return "" |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
762 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
763 size = min(size, section.offset + section.size - self.position) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
764 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
765 if isinstance(section, _data): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
766 realpos = section.realpos + self.position - section.offset |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
767 self.fileobj.seek(self.offset + realpos) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
768 self.position += size |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
769 return self.fileobj.read(size) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
770 else: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
771 self.position += size |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
772 return NUL * size |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
773 #class _FileInFile |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
774 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
775 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
776 class ExFileObject(object): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
777 """File-like object for reading an archive member. |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
778 Is returned by TarFile.extractfile(). |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
779 """ |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
780 blocksize = 1024 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
781 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
782 def __init__(self, tarfile, tarinfo): |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
783 self.fileobj = _FileInFile(tarfile.fileobj, |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
784 tarinfo.offset_data, |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
785 tarinfo.size, |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
786 getattr(tarinfo, "sparse", None)) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
787 self.name = tarinfo.name |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
788 self.mode = "r" |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
789 self.closed = False |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
790 self.size = tarinfo.size |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
791 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
792 self.position = 0 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
793 self.buffer = "" |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
794 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
795 def read(self, size=None): |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
796 """Read at most size bytes from the file. If size is not |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
797 present or None, read all data until EOF is reached. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
798 """ |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
799 if self.closed: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
800 raise ValueError("I/O operation on closed file") |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
801 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
802 buf = "" |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
803 if self.buffer: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
804 if size is None: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
805 buf = self.buffer |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
806 self.buffer = "" |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
807 else: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
808 buf = self.buffer[:size] |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
809 self.buffer = self.buffer[size:] |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
810 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
811 if size is None: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
812 buf += self.fileobj.read() |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
813 else: |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
814 buf += self.fileobj.read(size - len(buf)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
815 |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
816 self.position += len(buf) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
817 return buf |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
818 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
819 def readline(self, size=-1): |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
820 """Read one entire line from the file. If size is present |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
821 and non-negative, return a string with at most that |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
822 size, which may be an incomplete line. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
823 """ |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
824 if self.closed: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
825 raise ValueError("I/O operation on closed file") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
826 |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
827 if "\n" in self.buffer: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
828 pos = self.buffer.find("\n") + 1 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
829 else: |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
830 buffers = [self.buffer] |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
831 while True: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
832 buf = self.fileobj.read(self.blocksize) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
833 buffers.append(buf) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
834 if not buf or "\n" in buf: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
835 self.buffer = "".join(buffers) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
836 pos = self.buffer.find("\n") + 1 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
837 if pos == 0: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
838 # no newline found. |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
839 pos = len(self.buffer) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
840 break |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
841 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
842 if size != -1: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
843 pos = min(size, pos) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
844 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
845 buf = self.buffer[:pos] |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
846 self.buffer = self.buffer[pos:] |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
847 self.position += len(buf) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
848 return buf |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
849 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
850 def readlines(self): |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
851 """Return a list with all remaining lines. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
852 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
853 result = [] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
854 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
855 line = self.readline() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
856 if not line: break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
857 result.append(line) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
858 return result |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
859 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
860 def tell(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
861 """Return the current file position. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
862 """ |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
863 if self.closed: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
864 raise ValueError("I/O operation on closed file") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
865 |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
866 return self.position |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
867 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
868 def seek(self, pos, whence=os.SEEK_SET): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
869 """Seek to a position in the file. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
870 """ |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
871 if self.closed: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
872 raise ValueError("I/O operation on closed file") |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
873 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
874 if whence == os.SEEK_SET: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
875 self.position = min(max(pos, 0), self.size) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
876 elif whence == os.SEEK_CUR: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
877 if pos < 0: |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
878 self.position = max(self.position + pos, 0) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
879 else: |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
880 self.position = min(self.position + pos, self.size) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
881 elif whence == os.SEEK_END: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
882 self.position = max(min(self.size + pos, self.size), 0) |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
883 else: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
884 raise ValueError("Invalid argument") |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
885 |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
886 self.buffer = "" |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
887 self.fileobj.seek(self.position) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
888 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
889 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
890 """Close the file object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
891 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
892 self.closed = True |
|
34649
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
893 |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
894 def __iter__(self): |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
895 """Get an iterator over the file's lines. |
|
34649
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
896 """ |
|
40517
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
897 while True: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
898 line = self.readline() |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
899 if not line: |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
900 break |
|
5c7ea9346f12
Patch #1230446: tarfile.py: fix ExFileObject so that read() and tell()
Lars Gustäbel <lars@gustaebel.de>
parents:
40491
diff
changeset
|
901 yield line |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
902 #class ExFileObject |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
903 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
904 #------------------ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
905 # Exported Classes |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
906 #------------------ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
907 class TarInfo(object): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
908 """Informational class which holds the details about an |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
909 archive member given by a tar header block. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
910 TarInfo objects are returned by TarFile.getmember(), |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
911 TarFile.getmembers() and TarFile.gettarinfo() and are |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
912 usually created internally. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
913 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
914 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
915 def __init__(self, name=""): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
916 """Construct a TarInfo object. name is the optional name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
917 of the member. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
918 """ |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
919 self.name = name # member name |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
920 self.mode = 0644 # file permissions |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
921 self.uid = 0 # user id |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
922 self.gid = 0 # group id |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
923 self.size = 0 # file size |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
924 self.mtime = 0 # modification time |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
925 self.chksum = 0 # header checksum |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
926 self.type = REGTYPE # member type |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
927 self.linkname = "" # link name |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
928 self.uname = "root" # user name |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
929 self.gname = "root" # group name |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
930 self.devmajor = 0 # device major number |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
931 self.devminor = 0 # device minor number |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
932 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
933 self.offset = 0 # the tar header starts here |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
934 self.offset_data = 0 # the file's data starts here |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
935 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
936 self.pax_headers = {} # pax header information |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
937 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
938 # In pax headers the "name" and "linkname" field are called |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
939 # "path" and "linkpath". |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
940 def _getpath(self): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
941 return self.name |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
942 def _setpath(self, name): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
943 self.name = name |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
944 path = property(_getpath, _setpath) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
945 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
946 def _getlinkpath(self): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
947 return self.linkname |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
948 def _setlinkpath(self, linkname): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
949 self.linkname = linkname |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
950 linkpath = property(_getlinkpath, _setlinkpath) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
951 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
952 def __repr__(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
953 return "<%s %r at %#x>" % (self.__class__.__name__,self.name,id(self)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
954 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
955 def get_info(self, encoding, errors): |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
956 """Return the TarInfo's attributes as a dictionary. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
957 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
958 info = { |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
959 "name": normpath(self.name), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
960 "mode": self.mode & 07777, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
961 "uid": self.uid, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
962 "gid": self.gid, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
963 "size": self.size, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
964 "mtime": self.mtime, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
965 "chksum": self.chksum, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
966 "type": self.type, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
967 "linkname": normpath(self.linkname) if self.linkname else "", |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
968 "uname": self.uname, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
969 "gname": self.gname, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
970 "devmajor": self.devmajor, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
971 "devminor": self.devminor |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
972 } |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
973 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
974 if info["type"] == DIRTYPE and not info["name"].endswith("/"): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
975 info["name"] += "/" |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
976 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
977 for key in ("name", "linkname", "uname", "gname"): |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
978 if type(info[key]) is unicode: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
979 info[key] = info[key].encode(encoding, errors) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
980 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
981 return info |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
982 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
983 def tobuf(self, format=DEFAULT_FORMAT, encoding=ENCODING, errors="strict"): |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
984 """Return a tar header as a string of 512 byte blocks. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
985 """ |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
986 info = self.get_info(encoding, errors) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
987 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
988 if format == USTAR_FORMAT: |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
989 return self.create_ustar_header(info) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
990 elif format == GNU_FORMAT: |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
991 return self.create_gnu_header(info) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
992 elif format == PAX_FORMAT: |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
993 return self.create_pax_header(info, encoding, errors) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
994 else: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
995 raise ValueError("invalid format") |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
996 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
997 def create_ustar_header(self, info): |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
998 """Return the object as a ustar header block. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
999 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1000 info["magic"] = POSIX_MAGIC |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1001 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1002 if len(info["linkname"]) > LENGTH_LINK: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1003 raise ValueError("linkname is too long") |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1004 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1005 if len(info["name"]) > LENGTH_NAME: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1006 info["prefix"], info["name"] = self._posix_split_name(info["name"]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1007 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1008 return self._create_header(info, USTAR_FORMAT) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1009 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1010 def create_gnu_header(self, info): |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1011 """Return the object as a GNU header block sequence. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1012 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1013 info["magic"] = GNU_MAGIC |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1014 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1015 buf = "" |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1016 if len(info["linkname"]) > LENGTH_LINK: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1017 buf += self._create_gnu_long_header(info["linkname"], GNUTYPE_LONGLINK) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1018 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1019 if len(info["name"]) > LENGTH_NAME: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1020 buf += self._create_gnu_long_header(info["name"], GNUTYPE_LONGNAME) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1021 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1022 return buf + self._create_header(info, GNU_FORMAT) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1023 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1024 def create_pax_header(self, info, encoding, errors): |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1025 """Return the object as a ustar header block. If it cannot be |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1026 represented this way, prepend a pax extended header sequence |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1027 with supplement information. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1028 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1029 info["magic"] = POSIX_MAGIC |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1030 pax_headers = self.pax_headers.copy() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1031 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1032 # Test string fields for values that exceed the field length or cannot |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1033 # be represented in ASCII encoding. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1034 for name, hname, length in ( |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1035 ("name", "path", LENGTH_NAME), ("linkname", "linkpath", LENGTH_LINK), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1036 ("uname", "uname", 32), ("gname", "gname", 32)): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1037 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1038 if hname in pax_headers: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1039 # The pax header has priority. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1040 continue |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1041 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1042 val = info[name].decode(encoding, errors) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1043 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1044 # Try to encode the string as ASCII. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1045 try: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1046 val.encode("ascii") |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1047 except UnicodeEncodeError: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1048 pax_headers[hname] = val |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1049 continue |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1050 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1051 if len(info[name]) > length: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1052 pax_headers[hname] = val |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1053 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1054 # Test number fields for values that exceed the field limit or values |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1055 # that like to be stored as float. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1056 for name, digits in (("uid", 8), ("gid", 8), ("size", 12), ("mtime", 12)): |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1057 if name in pax_headers: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1058 # The pax header has priority. Avoid overflow. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1059 info[name] = 0 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1060 continue |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1061 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1062 val = info[name] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1063 if not 0 <= val < 8 ** (digits - 1) or isinstance(val, float): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1064 pax_headers[name] = unicode(val) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1065 info[name] = 0 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1066 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1067 # Create a pax extended header if necessary. |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1068 if pax_headers: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1069 buf = self._create_pax_generic_header(pax_headers) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1070 else: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1071 buf = "" |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1072 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1073 return buf + self._create_header(info, USTAR_FORMAT) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1074 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1075 @classmethod |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1076 def create_pax_global_header(cls, pax_headers): |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1077 """Return the object as a pax global header block sequence. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1078 """ |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1079 return cls._create_pax_generic_header(pax_headers, type=XGLTYPE) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1080 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1081 def _posix_split_name(self, name): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1082 """Split a name longer than 100 chars into a prefix |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1083 and a name part. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1084 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1085 prefix = name[:LENGTH_PREFIX + 1] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1086 while prefix and prefix[-1] != "/": |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1087 prefix = prefix[:-1] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1088 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1089 name = name[len(prefix):] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1090 prefix = prefix[:-1] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1091 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1092 if not prefix or len(name) > LENGTH_NAME: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1093 raise ValueError("name is too long") |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1094 return prefix, name |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1095 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1096 @staticmethod |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1097 def _create_header(info, format): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1098 """Return a header block. info is a dictionary with file |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1099 information, format must be one of the *_FORMAT constants. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1100 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1101 parts = [ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1102 stn(info.get("name", ""), 100), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1103 itn(info.get("mode", 0) & 07777, 8, format), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1104 itn(info.get("uid", 0), 8, format), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1105 itn(info.get("gid", 0), 8, format), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1106 itn(info.get("size", 0), 12, format), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1107 itn(info.get("mtime", 0), 12, format), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1108 " ", # checksum field |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1109 info.get("type", REGTYPE), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1110 stn(info.get("linkname", ""), 100), |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1111 stn(info.get("magic", POSIX_MAGIC), 8), |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1112 stn(info.get("uname", "root"), 32), |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1113 stn(info.get("gname", "root"), 32), |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1114 itn(info.get("devmajor", 0), 8, format), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1115 itn(info.get("devminor", 0), 8, format), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1116 stn(info.get("prefix", ""), 155) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1117 ] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1118 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1119 buf = struct.pack("%ds" % BLOCKSIZE, "".join(parts)) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1120 chksum = calc_chksums(buf[-BLOCKSIZE:])[0] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1121 buf = buf[:-364] + "%06o\0" % chksum + buf[-357:] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1122 return buf |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1123 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1124 @staticmethod |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1125 def _create_payload(payload): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1126 """Return the string payload filled with zero bytes |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1127 up to the next 512 byte border. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1128 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1129 blocks, remainder = divmod(len(payload), BLOCKSIZE) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1130 if remainder > 0: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1131 payload += (BLOCKSIZE - remainder) * NUL |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1132 return payload |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1133 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1134 @classmethod |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1135 def _create_gnu_long_header(cls, name, type): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1136 """Return a GNUTYPE_LONGNAME or GNUTYPE_LONGLINK sequence |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1137 for name. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1138 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1139 name += NUL |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1140 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1141 info = {} |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1142 info["name"] = "././@LongLink" |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1143 info["type"] = type |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1144 info["size"] = len(name) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1145 info["magic"] = GNU_MAGIC |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1146 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1147 # create extended header + name blocks. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1148 return cls._create_header(info, USTAR_FORMAT) + \ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1149 cls._create_payload(name) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1150 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1151 @classmethod |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1152 def _create_pax_generic_header(cls, pax_headers, type=XHDTYPE): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1153 """Return a POSIX.1-2001 extended or global header sequence |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1154 that contains a list of keyword, value pairs. The values |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1155 must be unicode objects. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1156 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1157 records = [] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1158 for keyword, value in pax_headers.iteritems(): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1159 keyword = keyword.encode("utf8") |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1160 value = value.encode("utf8") |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1161 l = len(keyword) + len(value) + 3 # ' ' + '=' + '\n' |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1162 n = p = 0 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1163 while True: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1164 n = l + len(str(p)) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1165 if n == p: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1166 break |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1167 p = n |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1168 records.append("%d %s=%s\n" % (p, keyword, value)) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1169 records = "".join(records) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1170 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1171 # We use a hardcoded "././@PaxHeader" name like star does |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1172 # instead of the one that POSIX recommends. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1173 info = {} |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1174 info["name"] = "././@PaxHeader" |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1175 info["type"] = type |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1176 info["size"] = len(records) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1177 info["magic"] = POSIX_MAGIC |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1178 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1179 # Create pax header + record blocks. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1180 return cls._create_header(info, USTAR_FORMAT) + \ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1181 cls._create_payload(records) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1182 |
| 34421 | 1183 @classmethod |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1184 def frombuf(cls, buf): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1185 """Construct a TarInfo object from a 512 byte string buffer. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1186 """ |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1187 if len(buf) != BLOCKSIZE: |
|
40487
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
1188 raise HeaderError("truncated header") |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1189 if buf.count(NUL) == BLOCKSIZE: |
|
40487
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
1190 raise HeaderError("empty header") |
|
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
1191 |
|
40491
1250fe42ce5c
Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents:
40487
diff
changeset
|
1192 chksum = nti(buf[148:156]) |
|
40487
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
1193 if chksum not in calc_chksums(buf): |
|
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
1194 raise HeaderError("bad checksum") |
|
32770
a3b1d6c99e46
SF #918101, allow files >= 8 GB using GNU extension
Neal Norwitz <nnorwitz@gmail.com>
parents:
32769
diff
changeset
|
1195 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1196 obj = cls() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1197 obj.buf = buf |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1198 obj.name = nts(buf[0:100]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1199 obj.mode = nti(buf[100:108]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1200 obj.uid = nti(buf[108:116]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1201 obj.gid = nti(buf[116:124]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1202 obj.size = nti(buf[124:136]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1203 obj.mtime = nti(buf[136:148]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1204 obj.chksum = chksum |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1205 obj.type = buf[156:157] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1206 obj.linkname = nts(buf[157:257]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1207 obj.uname = nts(buf[265:297]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1208 obj.gname = nts(buf[297:329]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1209 obj.devmajor = nti(buf[329:337]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1210 obj.devminor = nti(buf[337:345]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1211 prefix = nts(buf[345:500]) |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1212 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1213 # Old V7 tar format represents a directory as a regular |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1214 # file with a trailing slash. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1215 if obj.type == AREGTYPE and obj.name.endswith("/"): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1216 obj.type = DIRTYPE |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1217 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1218 # Remove redundant slashes from directories. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1219 if obj.isdir(): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1220 obj.name = obj.name.rstrip("/") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1221 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1222 # Reconstruct a ustar longname. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1223 if prefix and obj.type not in GNU_TYPES: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1224 obj.name = prefix + "/" + obj.name |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1225 return obj |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1226 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1227 @classmethod |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1228 def fromtarfile(cls, tarfile): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1229 """Return the next TarInfo object from TarFile object |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1230 tarfile. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1231 """ |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1232 buf = tarfile.fileobj.read(BLOCKSIZE) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1233 if not buf: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1234 return |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1235 obj = cls.frombuf(buf) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1236 obj.offset = tarfile.fileobj.tell() - BLOCKSIZE |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1237 return obj._proc_member(tarfile) |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1238 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1239 #-------------------------------------------------------------------------- |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1240 # The following are methods that are called depending on the type of a |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1241 # member. The entry point is _proc_member() which can be overridden in a |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1242 # subclass to add custom _proc_*() methods. A _proc_*() method MUST |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1243 # implement the following |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1244 # operations: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1245 # 1. Set self.offset_data to the position where the data blocks begin, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1246 # if there is data that follows. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1247 # 2. Set tarfile.offset to the position where the next member's header will |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1248 # begin. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1249 # 3. Return self or another valid TarInfo object. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1250 def _proc_member(self, tarfile): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1251 """Choose the right processing method depending on |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1252 the type and call it. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1253 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1254 if self.type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1255 return self._proc_gnulong(tarfile) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1256 elif self.type == GNUTYPE_SPARSE: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1257 return self._proc_sparse(tarfile) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1258 elif self.type in (XHDTYPE, XGLTYPE, SOLARIS_XHDTYPE): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1259 return self._proc_pax(tarfile) |
|
40424
eb30d30f22a1
Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents:
40246
diff
changeset
|
1260 else: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1261 return self._proc_builtin(tarfile) |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1262 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1263 def _proc_builtin(self, tarfile): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1264 """Process a builtin type or an unknown type which |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1265 will be treated as a regular file. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1266 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1267 self.offset_data = tarfile.fileobj.tell() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1268 offset = self.offset_data |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1269 if self.isreg() or self.type not in SUPPORTED_TYPES: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1270 # Skip the following data blocks. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1271 offset += self._block(self.size) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1272 tarfile.offset = offset |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1273 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1274 # Patch the TarInfo object with saved global |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1275 # header information. |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1276 self._apply_pax_info(tarfile.pax_headers, tarfile.encoding, tarfile.errors) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1277 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1278 return self |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1279 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1280 def _proc_gnulong(self, tarfile): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1281 """Process the blocks that hold a GNU longname |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1282 or longlink member. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1283 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1284 buf = tarfile.fileobj.read(self._block(self.size)) |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1285 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1286 # Fetch the next header and process it. |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1287 next = self.fromtarfile(tarfile) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1288 if next is None: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1289 raise HeaderError("missing subsequent header") |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1290 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1291 # Patch the TarInfo object from the next header with |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1292 # the longname information. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1293 next.offset = self.offset |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1294 if self.type == GNUTYPE_LONGNAME: |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1295 next.name = nts(buf) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1296 elif self.type == GNUTYPE_LONGLINK: |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1297 next.linkname = nts(buf) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1298 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1299 return next |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1300 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1301 def _proc_sparse(self, tarfile): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1302 """Process a GNU sparse header plus extra headers. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1303 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1304 buf = self.buf |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1305 sp = _ringbuffer() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1306 pos = 386 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1307 lastpos = 0L |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1308 realpos = 0L |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1309 # There are 4 possible sparse structs in the |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1310 # first header. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1311 for i in xrange(4): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1312 try: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1313 offset = nti(buf[pos:pos + 12]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1314 numbytes = nti(buf[pos + 12:pos + 24]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1315 except ValueError: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1316 break |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1317 if offset > lastpos: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1318 sp.append(_hole(lastpos, offset - lastpos)) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1319 sp.append(_data(offset, numbytes, realpos)) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1320 realpos += numbytes |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1321 lastpos = offset + numbytes |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1322 pos += 24 |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1323 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1324 isextended = ord(buf[482]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1325 origsize = nti(buf[483:495]) |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1326 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1327 # If the isextended flag is given, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1328 # there are extra headers to process. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1329 while isextended == 1: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1330 buf = tarfile.fileobj.read(BLOCKSIZE) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1331 pos = 0 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1332 for i in xrange(21): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1333 try: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1334 offset = nti(buf[pos:pos + 12]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1335 numbytes = nti(buf[pos + 12:pos + 24]) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1336 except ValueError: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1337 break |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1338 if offset > lastpos: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1339 sp.append(_hole(lastpos, offset - lastpos)) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1340 sp.append(_data(offset, numbytes, realpos)) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1341 realpos += numbytes |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1342 lastpos = offset + numbytes |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1343 pos += 24 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1344 isextended = ord(buf[504]) |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1345 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1346 if lastpos < origsize: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1347 sp.append(_hole(lastpos, origsize - lastpos)) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1348 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1349 self.sparse = sp |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1350 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1351 self.offset_data = tarfile.fileobj.tell() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1352 tarfile.offset = self.offset_data + self._block(self.size) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1353 self.size = origsize |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1354 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1355 return self |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1356 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1357 def _proc_pax(self, tarfile): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1358 """Process an extended or global header as described in |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1359 POSIX.1-2001. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1360 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1361 # Read the header information. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1362 buf = tarfile.fileobj.read(self._block(self.size)) |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1363 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1364 # A pax header stores supplemental information for either |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1365 # the following file (extended) or all following files |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1366 # (global). |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1367 if self.type == XGLTYPE: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1368 pax_headers = tarfile.pax_headers |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1369 else: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1370 pax_headers = tarfile.pax_headers.copy() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1371 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1372 # Parse pax header information. A record looks like that: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1373 # "%d %s=%s\n" % (length, keyword, value). length is the size |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1374 # of the complete record including the length field itself and |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1375 # the newline. keyword and value are both UTF-8 encoded strings. |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1376 regex = re.compile(r"(\d+) ([^=]+)=", re.U) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1377 pos = 0 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1378 while True: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1379 match = regex.match(buf, pos) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1380 if not match: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1381 break |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1382 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1383 length, keyword = match.groups() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1384 length = int(length) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1385 value = buf[match.end(2) + 1:match.start(1) + length - 1] |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1386 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1387 keyword = keyword.decode("utf8") |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1388 value = value.decode("utf8") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1389 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1390 pax_headers[keyword] = value |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1391 pos += length |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1392 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1393 # Fetch the next header. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1394 next = self.fromtarfile(tarfile) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1395 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1396 if self.type in (XHDTYPE, SOLARIS_XHDTYPE): |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1397 if next is None: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1398 raise HeaderError("missing subsequent header") |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1399 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1400 # Patch the TarInfo object with the extended header info. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1401 next._apply_pax_info(pax_headers, tarfile.encoding, tarfile.errors) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1402 next.offset = self.offset |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1403 |
|
48175
979f0a89903f
Remove a dict.has_key() and list.sort(cmp=) usage from tarfile to silence
Brett Cannon <bcannon@gmail.com>
parents:
48138
diff
changeset
|
1404 if "size" in pax_headers: |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1405 # If the extended header replaces the size field, |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1406 # we need to recalculate the offset where the next |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1407 # header starts. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1408 offset = next.offset_data |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1409 if next.isreg() or next.type not in SUPPORTED_TYPES: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1410 offset += next._block(next.size) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1411 tarfile.offset = offset |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1412 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1413 return next |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1414 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1415 def _apply_pax_info(self, pax_headers, encoding, errors): |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1416 """Replace fields with supplemental information from a previous |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1417 pax extended or global header. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1418 """ |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1419 for keyword, value in pax_headers.iteritems(): |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1420 if keyword not in PAX_FIELDS: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1421 continue |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1422 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1423 if keyword == "path": |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1424 value = value.rstrip("/") |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1425 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1426 if keyword in PAX_NUMBER_FIELDS: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1427 try: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1428 value = PAX_NUMBER_FIELDS[keyword](value) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1429 except ValueError: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1430 value = 0 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1431 else: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1432 value = uts(value, encoding, errors) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1433 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1434 setattr(self, keyword, value) |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1435 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1436 self.pax_headers = pax_headers.copy() |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1437 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1438 def _block(self, count): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1439 """Round up a byte count by BLOCKSIZE and return it, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1440 e.g. _block(834) => 1024. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1441 """ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1442 blocks, remainder = divmod(count, BLOCKSIZE) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1443 if remainder: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1444 blocks += 1 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1445 return blocks * BLOCKSIZE |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1446 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1447 def isreg(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1448 return self.type in REGULAR_TYPES |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1449 def isfile(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1450 return self.isreg() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1451 def isdir(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1452 return self.type == DIRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1453 def issym(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1454 return self.type == SYMTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1455 def islnk(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1456 return self.type == LNKTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1457 def ischr(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1458 return self.type == CHRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1459 def isblk(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1460 return self.type == BLKTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1461 def isfifo(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1462 return self.type == FIFOTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1463 def issparse(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1464 return self.type == GNUTYPE_SPARSE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1465 def isdev(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1466 return self.type in (CHRTYPE, BLKTYPE, FIFOTYPE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1467 # class TarInfo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1468 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1469 class TarFile(object): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1470 """The TarFile Class provides an interface to tar archives. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1471 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1472 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1473 debug = 0 # May be set from 0 (no msgs) to 3 (all msgs) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1474 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1475 dereference = False # If true, add content of linked file to the |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1476 # tar file, else the link. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1477 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1478 ignore_zeros = False # If true, skips empty or invalid blocks and |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1479 # continues processing. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1480 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1481 errorlevel = 0 # If 0, fatal errors only appear in debug |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1482 # messages (if debug >= 0). If > 0, errors |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1483 # are passed to the caller as exceptions. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1484 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1485 format = DEFAULT_FORMAT # The format to use when creating an archive. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1486 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1487 encoding = ENCODING # Encoding for 8-bit character strings. |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1488 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1489 errors = None # Error handler for unicode conversion. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1490 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1491 tarinfo = TarInfo # The default TarInfo class to use. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1492 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1493 fileobject = ExFileObject # The default ExFileObject class to use. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1494 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1495 def __init__(self, name=None, mode="r", fileobj=None, format=None, |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1496 tarinfo=None, dereference=None, ignore_zeros=None, encoding=None, |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1497 errors=None, pax_headers=None, debug=None, errorlevel=None): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1498 """Open an (uncompressed) tar archive `name'. `mode' is either 'r' to |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1499 read from an existing archive, 'a' to append data to an existing |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1500 file or 'w' to create a new file overwriting an existing one. `mode' |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1501 defaults to 'r'. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1502 If `fileobj' is given, it is used for reading or writing data. If it |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1503 can be determined, `mode' is overridden by `fileobj's mode. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1504 `fileobj' is not closed, when TarFile is closed. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1505 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1506 if len(mode) > 1 or mode not in "raw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1507 raise ValueError("mode must be 'r', 'a' or 'w'") |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1508 self.mode = mode |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1509 self._mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode] |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1510 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1511 if not fileobj: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1512 if self.mode == "a" and not os.path.exists(name): |
|
40748
09dcacc8a0bc
Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents:
40698
diff
changeset
|
1513 # Create nonexistent files in append mode. |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1514 self.mode = "w" |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1515 self._mode = "wb" |
|
41881
3877f37c9861
Remove direct call's to file's constructor and replace them with calls to
Brett Cannon <bcannon@gmail.com>
parents:
41153
diff
changeset
|
1516 fileobj = bltn_open(name, self._mode) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1517 self._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1518 else: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1519 if name is None and hasattr(fileobj, "name"): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1520 name = fileobj.name |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1521 if hasattr(fileobj, "mode"): |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1522 self._mode = fileobj.mode |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1523 self._extfileobj = True |
|
42949
3bf2cad8f22a
TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents:
42109
diff
changeset
|
1524 self.name = os.path.abspath(name) if name else None |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1525 self.fileobj = fileobj |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1526 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1527 # Init attributes. |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1528 if format is not None: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1529 self.format = format |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1530 if tarinfo is not None: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1531 self.tarinfo = tarinfo |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1532 if dereference is not None: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1533 self.dereference = dereference |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1534 if ignore_zeros is not None: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1535 self.ignore_zeros = ignore_zeros |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1536 if encoding is not None: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1537 self.encoding = encoding |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1538 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1539 if errors is not None: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1540 self.errors = errors |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1541 elif mode == "r": |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1542 self.errors = "utf-8" |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1543 else: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1544 self.errors = "strict" |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1545 |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1546 if pax_headers is not None and self.format == PAX_FORMAT: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1547 self.pax_headers = pax_headers |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1548 else: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1549 self.pax_headers = {} |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1550 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1551 if debug is not None: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1552 self.debug = debug |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1553 if errorlevel is not None: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1554 self.errorlevel = errorlevel |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1555 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1556 # Init datastructures. |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1557 self.closed = False |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1558 self.members = [] # list of members as TarInfo objects |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1559 self._loaded = False # flag if all members have been read |
|
44091
e634511b4af5
Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents:
42949
diff
changeset
|
1560 self.offset = self.fileobj.tell() |
|
e634511b4af5
Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents:
42949
diff
changeset
|
1561 # current position in the archive file |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1562 self.inodes = {} # dictionary caching the inodes of |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1563 # archive members already added |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1564 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1565 if self.mode == "r": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1566 self.firstmember = None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1567 self.firstmember = self.next() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1568 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1569 if self.mode == "a": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1570 # Move to the end of the archive, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1571 # before the first empty block. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1572 self.firstmember = None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1573 while True: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1574 if self.next() is None: |
|
40748
09dcacc8a0bc
Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents:
40698
diff
changeset
|
1575 if self.offset > 0: |
|
09dcacc8a0bc
Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents:
40698
diff
changeset
|
1576 self.fileobj.seek(- BLOCKSIZE, 1) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1577 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1578 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1579 if self.mode in "aw": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1580 self._loaded = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1581 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1582 if self.pax_headers: |
|
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1583 buf = self.tarinfo.create_pax_global_header(self.pax_headers.copy()) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1584 self.fileobj.write(buf) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1585 self.offset += len(buf) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1586 |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1587 def _getposix(self): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1588 return self.format == USTAR_FORMAT |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1589 def _setposix(self, value): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1590 import warnings |
|
53901
a015a298543d
#4351: more appropriate DeprecationWarning stacklevels
Philip Jenvey <pjenvey@underboss.org>
parents:
52267
diff
changeset
|
1591 warnings.warn("use the format attribute instead", DeprecationWarning, |
|
a015a298543d
#4351: more appropriate DeprecationWarning stacklevels
Philip Jenvey <pjenvey@underboss.org>
parents:
52267
diff
changeset
|
1592 2) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1593 if value: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1594 self.format = USTAR_FORMAT |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1595 else: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1596 self.format = GNU_FORMAT |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1597 posix = property(_getposix, _setposix) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1598 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1599 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1600 # Below are the classmethods which act as alternate constructors to the |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1601 # TarFile class. The open() method is the only one that is needed for |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1602 # public use; it is the "super"-constructor and is able to select an |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1603 # adequate "sub"-constructor for a particular compression using the mapping |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1604 # from OPEN_METH. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1605 # |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1606 # This concept allows one to subclass TarFile without losing the comfort of |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1607 # the super-constructor. A sub-constructor is registered and made available |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1608 # by adding it to the mapping in OPEN_METH. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1609 |
| 34421 | 1610 @classmethod |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1611 def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1612 """Open a tar archive for reading, writing or appending. Return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1613 an appropriate TarFile class. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1614 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1615 mode: |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1616 'r' or 'r:*' open for reading with transparent compression |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1617 'r:' open for reading exclusively uncompressed |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1618 'r:gz' open for reading with gzip compression |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1619 'r:bz2' open for reading with bzip2 compression |
|
40748
09dcacc8a0bc
Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents:
40698
diff
changeset
|
1620 'a' or 'a:' open for appending, creating the file if necessary |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1621 'w' or 'w:' open for writing without compression |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1622 'w:gz' open for writing with gzip compression |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1623 'w:bz2' open for writing with bzip2 compression |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1624 |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1625 'r|*' open a stream of tar blocks with transparent compression |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1626 'r|' open an uncompressed stream of tar blocks for reading |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1627 'r|gz' open a gzip compressed stream of tar blocks |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1628 'r|bz2' open a bzip2 compressed stream of tar blocks |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1629 'w|' open an uncompressed stream for writing |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1630 'w|gz' open a gzip compressed stream for writing |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1631 'w|bz2' open a bzip2 compressed stream for writing |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1632 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1633 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1634 if not name and not fileobj: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1635 raise ValueError("nothing to open") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1636 |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1637 if mode in ("r", "r:*"): |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1638 # Find out which *open() is appropriate for opening the file. |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1639 for comptype in cls.OPEN_METH: |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1640 func = getattr(cls, cls.OPEN_METH[comptype]) |
|
40523
e85ca3fd4bd6
Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Lars Gustäbel <lars@gustaebel.de>
parents:
40519
diff
changeset
|
1641 if fileobj is not None: |
|
e85ca3fd4bd6
Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Lars Gustäbel <lars@gustaebel.de>
parents:
40519
diff
changeset
|
1642 saved_pos = fileobj.tell() |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1643 try: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1644 return func(name, "r", fileobj, **kwargs) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1645 except (ReadError, CompressionError), e: |
|
40523
e85ca3fd4bd6
Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Lars Gustäbel <lars@gustaebel.de>
parents:
40519
diff
changeset
|
1646 if fileobj is not None: |
|
e85ca3fd4bd6
Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Lars Gustäbel <lars@gustaebel.de>
parents:
40519
diff
changeset
|
1647 fileobj.seek(saved_pos) |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1648 continue |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1649 raise ReadError("file could not be opened successfully") |
|
34697
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1650 |
|
79302e50910e
Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents:
34688
diff
changeset
|
1651 elif ":" in mode: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1652 filemode, comptype = mode.split(":", 1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1653 filemode = filemode or "r" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1654 comptype = comptype or "tar" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1655 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1656 # Select the *open() function according to |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1657 # given compression. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1658 if comptype in cls.OPEN_METH: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1659 func = getattr(cls, cls.OPEN_METH[comptype]) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1660 else: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1661 raise CompressionError("unknown compression type %r" % comptype) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1662 return func(name, filemode, fileobj, **kwargs) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1663 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1664 elif "|" in mode: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1665 filemode, comptype = mode.split("|", 1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1666 filemode = filemode or "r" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1667 comptype = comptype or "tar" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1668 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1669 if filemode not in "rw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1670 raise ValueError("mode must be 'r' or 'w'") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1671 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1672 t = cls(name, filemode, |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1673 _Stream(name, filemode, comptype, fileobj, bufsize), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1674 **kwargs) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1675 t._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1676 return t |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1677 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1678 elif mode in "aw": |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1679 return cls.taropen(name, mode, fileobj, **kwargs) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1680 |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1681 raise ValueError("undiscernible mode") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1682 |
| 34421 | 1683 @classmethod |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1684 def taropen(cls, name, mode="r", fileobj=None, **kwargs): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1685 """Open uncompressed tar archive name for reading or writing. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1686 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1687 if len(mode) > 1 or mode not in "raw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1688 raise ValueError("mode must be 'r', 'a' or 'w'") |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1689 return cls(name, mode, fileobj, **kwargs) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1690 |
| 34421 | 1691 @classmethod |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1692 def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1693 """Open gzip compressed tar archive name for reading or writing. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1694 Appending is not allowed. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1695 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1696 if len(mode) > 1 or mode not in "rw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1697 raise ValueError("mode must be 'r' or 'w'") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1698 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1699 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1700 import gzip |
|
28576
1bd50c28203f
Fix test_tarfile failure when gzip is not available
Neal Norwitz <nnorwitz@gmail.com>
parents:
28222
diff
changeset
|
1701 gzip.GzipFile |
|
1bd50c28203f
Fix test_tarfile failure when gzip is not available
Neal Norwitz <nnorwitz@gmail.com>
parents:
28222
diff
changeset
|
1702 except (ImportError, AttributeError): |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1703 raise CompressionError("gzip module is not available") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1704 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1705 if fileobj is None: |
|
41881
3877f37c9861
Remove direct call's to file's constructor and replace them with calls to
Brett Cannon <bcannon@gmail.com>
parents:
41153
diff
changeset
|
1706 fileobj = bltn_open(name, mode + "b") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1707 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1708 try: |
|
40519
6763e45c7f7d
Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents:
40517
diff
changeset
|
1709 t = cls.taropen(name, mode, |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1710 gzip.GzipFile(name, mode, compresslevel, fileobj), |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1711 **kwargs) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1712 except IOError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1713 raise ReadError("not a gzip file") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1714 t._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1715 return t |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1716 |
| 34421 | 1717 @classmethod |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1718 def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1719 """Open bzip2 compressed tar archive name for reading or writing. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1720 Appending is not allowed. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1721 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1722 if len(mode) > 1 or mode not in "rw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1723 raise ValueError("mode must be 'r' or 'w'.") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1724 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1725 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1726 import bz2 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1727 except ImportError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1728 raise CompressionError("bz2 module is not available") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1729 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1730 if fileobj is not None: |
|
38185
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
1731 fileobj = _BZ2Proxy(fileobj, mode) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
1732 else: |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
1733 fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1734 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1735 try: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1736 t = cls.taropen(name, mode, fileobj, **kwargs) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1737 except IOError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1738 raise ReadError("not a bzip2 file") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1739 t._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1740 return t |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1741 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1742 # All *open() methods are registered here. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1743 OPEN_METH = { |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1744 "tar": "taropen", # uncompressed tar |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1745 "gz": "gzopen", # gzip compressed tar |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1746 "bz2": "bz2open" # bzip2 compressed tar |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1747 } |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1748 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1749 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1750 # The public methods which TarFile provides: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1751 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1752 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1753 """Close the TarFile. In write-mode, two finishing zero blocks are |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1754 appended to the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1755 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1756 if self.closed: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1757 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1758 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1759 if self.mode in "aw": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1760 self.fileobj.write(NUL * (BLOCKSIZE * 2)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1761 self.offset += (BLOCKSIZE * 2) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1762 # fill up the end with zero-blocks |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1763 # (like option -b20 for tar does) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1764 blocks, remainder = divmod(self.offset, RECORDSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1765 if remainder > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1766 self.fileobj.write(NUL * (RECORDSIZE - remainder)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1767 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1768 if not self._extfileobj: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1769 self.fileobj.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1770 self.closed = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1771 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1772 def getmember(self, name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1773 """Return a TarInfo object for member `name'. If `name' can not be |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1774 found in the archive, KeyError is raised. If a member occurs more |
|
51775
af9b6c43804e
Issue #5341: Fix a variety of spelling errors.
Mark Dickinson <dickinsm@gmail.com>
parents:
50130
diff
changeset
|
1775 than once in the archive, its last occurrence is assumed to be the |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1776 most up-to-date version. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1777 """ |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1778 tarinfo = self._getmember(name) |
|
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1779 if tarinfo is None: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1780 raise KeyError("filename %r not found" % name) |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1781 return tarinfo |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1782 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1783 def getmembers(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1784 """Return the members of the archive as a list of TarInfo objects. The |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1785 list has the same order as the members in the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1786 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1787 self._check() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1788 if not self._loaded: # if we want to obtain a list of |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1789 self._load() # all members, we first have to |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1790 # scan the whole archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1791 return self.members |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1792 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1793 def getnames(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1794 """Return the members of the archive as a list of their names. It has |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1795 the same order as the list returned by getmembers(). |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1796 """ |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1797 return [tarinfo.name for tarinfo in self.getmembers()] |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1798 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1799 def gettarinfo(self, name=None, arcname=None, fileobj=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1800 """Create a TarInfo object for either the file `name' or the file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1801 object `fileobj' (using os.fstat on its file descriptor). You can |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1802 modify some of the TarInfo's attributes before you add it using |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1803 addfile(). If given, `arcname' specifies an alternative name for the |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1804 file in the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1805 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1806 self._check("aw") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1807 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1808 # When fileobj is given, replace name by |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1809 # fileobj's real name. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1810 if fileobj is not None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1811 name = fileobj.name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1812 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1813 # Building the name of the member in the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1814 # Backward slashes are converted to forward slashes, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1815 # Absolute paths are turned to relative paths. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1816 if arcname is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1817 arcname = name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1818 arcname = normpath(arcname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1819 drv, arcname = os.path.splitdrive(arcname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1820 while arcname[0:1] == "/": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1821 arcname = arcname[1:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1822 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1823 # Now, fill the TarInfo object with |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1824 # information specific for the file. |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1825 tarinfo = self.tarinfo() |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1826 tarinfo.tarfile = self |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1827 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1828 # Use os.stat or os.lstat, depending on platform |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1829 # and if symlinks shall be resolved. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1830 if fileobj is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1831 if hasattr(os, "lstat") and not self.dereference: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1832 statres = os.lstat(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1833 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1834 statres = os.stat(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1835 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1836 statres = os.fstat(fileobj.fileno()) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1837 linkname = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1838 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1839 stmd = statres.st_mode |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1840 if stat.S_ISREG(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1841 inode = (statres.st_ino, statres.st_dev) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1842 if not self.dereference and statres.st_nlink > 1 and \ |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1843 inode in self.inodes and arcname != self.inodes[inode]: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1844 # Is it a hardlink to an already |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1845 # archived file? |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1846 type = LNKTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1847 linkname = self.inodes[inode] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1848 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1849 # The inode is added only if its valid. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1850 # For win32 it is always 0. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1851 type = REGTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1852 if inode[0]: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1853 self.inodes[inode] = arcname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1854 elif stat.S_ISDIR(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1855 type = DIRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1856 elif stat.S_ISFIFO(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1857 type = FIFOTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1858 elif stat.S_ISLNK(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1859 type = SYMTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1860 linkname = os.readlink(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1861 elif stat.S_ISCHR(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1862 type = CHRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1863 elif stat.S_ISBLK(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1864 type = BLKTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1865 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1866 return None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1867 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1868 # Fill the TarInfo object with all |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1869 # information we can get. |
|
35466
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1870 tarinfo.name = arcname |
|
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1871 tarinfo.mode = stmd |
|
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1872 tarinfo.uid = statres.st_uid |
|
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1873 tarinfo.gid = statres.st_gid |
|
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1874 if stat.S_ISREG(stmd): |
|
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1875 tarinfo.size = statres.st_size |
|
33208
04864c39b006
Replace tricky and/or with straight-forward if:else:
Martin v. Löwis <martin@v.loewis.de>
parents:
33179
diff
changeset
|
1876 else: |
|
35466
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1877 tarinfo.size = 0L |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1878 tarinfo.mtime = statres.st_mtime |
|
35466
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1879 tarinfo.type = type |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1880 tarinfo.linkname = linkname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1881 if pwd: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1882 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1883 tarinfo.uname = pwd.getpwuid(tarinfo.uid)[0] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1884 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1885 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1886 if grp: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1887 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1888 tarinfo.gname = grp.getgrgid(tarinfo.gid)[0] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1889 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1890 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1891 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1892 if type in (CHRTYPE, BLKTYPE): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1893 if hasattr(os, "major") and hasattr(os, "minor"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1894 tarinfo.devmajor = os.major(statres.st_rdev) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1895 tarinfo.devminor = os.minor(statres.st_rdev) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1896 return tarinfo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1897 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1898 def list(self, verbose=True): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1899 """Print a table of contents to sys.stdout. If `verbose' is False, only |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1900 the names of the members are printed. If it is True, an `ls -l'-like |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1901 output is produced. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1902 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1903 self._check() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1904 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1905 for tarinfo in self: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1906 if verbose: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1907 print filemode(tarinfo.mode), |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1908 print "%s/%s" % (tarinfo.uname or tarinfo.uid, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1909 tarinfo.gname or tarinfo.gid), |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1910 if tarinfo.ischr() or tarinfo.isblk(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1911 print "%10s" % ("%d,%d" \ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1912 % (tarinfo.devmajor, tarinfo.devminor)), |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1913 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1914 print "%10d" % tarinfo.size, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1915 print "%d-%02d-%02d %02d:%02d:%02d" \ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1916 % time.localtime(tarinfo.mtime)[:6], |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1917 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1918 print tarinfo.name + ("/" if tarinfo.isdir() else ""), |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1919 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1920 if verbose: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1921 if tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1922 print "->", tarinfo.linkname, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1923 if tarinfo.islnk(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1924 print "link to", tarinfo.linkname, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1925 print |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1926 |
|
42109
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1927 def add(self, name, arcname=None, recursive=True, exclude=None): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1928 """Add the file `name' to the archive. `name' may be any type of file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1929 (directory, fifo, symbolic link, etc.). If given, `arcname' |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1930 specifies an alternative name for the file in the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1931 Directories are added recursively by default. This can be avoided by |
|
42109
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1932 setting `recursive' to False. `exclude' is a function that should |
|
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1933 return True for each filename to be excluded. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1934 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1935 self._check("aw") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1936 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1937 if arcname is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1938 arcname = name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1939 |
|
42109
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1940 # Exclude pathnames. |
|
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1941 if exclude is not None and exclude(name): |
|
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1942 self._dbg(2, "tarfile: Excluded %r" % name) |
|
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1943 return |
|
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1944 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1945 # Skip if somebody tries to archive the archive... |
|
40519
6763e45c7f7d
Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents:
40517
diff
changeset
|
1946 if self.name is not None and os.path.abspath(name) == self.name: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1947 self._dbg(2, "tarfile: Skipped %r" % name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1948 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1949 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1950 # Special case: The user wants to add the current |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1951 # working directory. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1952 if name == ".": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1953 if recursive: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1954 if arcname == ".": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1955 arcname = "" |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
1956 for f in os.listdir(name): |
|
42109
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1957 self.add(f, os.path.join(arcname, f), recursive, exclude) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1958 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1959 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1960 self._dbg(1, name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1961 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1962 # Create a TarInfo object from the file. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1963 tarinfo = self.gettarinfo(name, arcname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1964 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1965 if tarinfo is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1966 self._dbg(1, "tarfile: Unsupported type %r" % name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1967 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1968 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1969 # Append the tar header and data to the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1970 if tarinfo.isreg(): |
|
41881
3877f37c9861
Remove direct call's to file's constructor and replace them with calls to
Brett Cannon <bcannon@gmail.com>
parents:
41153
diff
changeset
|
1971 f = bltn_open(name, "rb") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1972 self.addfile(tarinfo, f) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1973 f.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1974 |
|
35466
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1975 elif tarinfo.isdir(): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1976 self.addfile(tarinfo) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1977 if recursive: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1978 for f in os.listdir(name): |
|
42109
b2e7336feb82
Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents:
41897
diff
changeset
|
1979 self.add(os.path.join(name, f), os.path.join(arcname, f), recursive, exclude) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1980 |
|
35466
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1981 else: |
|
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1982 self.addfile(tarinfo) |
|
85960ca4fc08
Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents:
35403
diff
changeset
|
1983 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1984 def addfile(self, tarinfo, fileobj=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1985 """Add the TarInfo object `tarinfo' to the archive. If `fileobj' is |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1986 given, tarinfo.size bytes are read from it and added to the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1987 You can create TarInfo objects using gettarinfo(). |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1988 On Windows platforms, `fileobj' should always be opened with mode |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1989 'rb' to avoid irritation about the file size. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1990 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1991 self._check("aw") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1992 |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1993 tarinfo = copy.copy(tarinfo) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1994 |
|
41897
9de87569181c
Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents:
41881
diff
changeset
|
1995 buf = tarinfo.tobuf(self.format, self.encoding, self.errors) |
|
40246
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1996 self.fileobj.write(buf) |
|
fd8a5f925d4d
Patch #1583880: fix tarfile's problems with long names and posix/
Georg Brandl <georg@python.org>
parents:
40170
diff
changeset
|
1997 self.offset += len(buf) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1998 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1999 # If there's data to follow, append it. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2000 if fileobj is not None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2001 copyfileobj(fileobj, self.fileobj, tarinfo.size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2002 blocks, remainder = divmod(tarinfo.size, BLOCKSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2003 if remainder > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2004 self.fileobj.write(NUL * (BLOCKSIZE - remainder)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2005 blocks += 1 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2006 self.offset += blocks * BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2007 |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
2008 self.members.append(tarinfo) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2009 |
|
34688
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2010 def extractall(self, path=".", members=None): |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2011 """Extract all members from the archive to the current working |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2012 directory and set owner, modification time and permissions on |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2013 directories afterwards. `path' specifies a different directory |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2014 to extract to. `members' is optional and must be a subset of the |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2015 list returned by getmembers(). |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2016 """ |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2017 directories = [] |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2018 |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2019 if members is None: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2020 members = self |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2021 |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2022 for tarinfo in members: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2023 if tarinfo.isdir(): |
|
45164
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2024 # Extract directories with a safe mode. |
|
34688
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2025 directories.append(tarinfo) |
|
45164
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2026 tarinfo = copy.copy(tarinfo) |
|
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2027 tarinfo.mode = 0700 |
|
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2028 self.extract(tarinfo, path) |
|
34688
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2029 |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2030 # Reverse sort directories. |
|
48175
979f0a89903f
Remove a dict.has_key() and list.sort(cmp=) usage from tarfile to silence
Brett Cannon <bcannon@gmail.com>
parents:
48138
diff
changeset
|
2031 directories.sort(key=operator.attrgetter('name')) |
|
34688
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2032 directories.reverse() |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2033 |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2034 # Set correct owner, mtime and filemode on directories. |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2035 for tarinfo in directories: |
|
44470
bd0258df330e
Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents:
44091
diff
changeset
|
2036 dirpath = os.path.join(path, tarinfo.name) |
|
34688
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2037 try: |
|
44470
bd0258df330e
Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents:
44091
diff
changeset
|
2038 self.chown(tarinfo, dirpath) |
|
bd0258df330e
Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents:
44091
diff
changeset
|
2039 self.utime(tarinfo, dirpath) |
|
bd0258df330e
Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents:
44091
diff
changeset
|
2040 self.chmod(tarinfo, dirpath) |
|
34688
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2041 except ExtractError, e: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2042 if self.errorlevel > 1: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2043 raise |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2044 else: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2045 self._dbg(1, "tarfile: %s" % e) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
2046 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2047 def extract(self, member, path=""): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2048 """Extract a member from the archive to the current working directory, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2049 using its full name. Its file information is extracted as accurately |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2050 as possible. `member' may be a filename or a TarInfo object. You can |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2051 specify a different directory using `path'. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2052 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2053 self._check("r") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2054 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2055 if isinstance(member, basestring): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2056 tarinfo = self.getmember(member) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2057 else: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2058 tarinfo = member |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2059 |
|
32769
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
2060 # Prepare the link target for makelink(). |
|
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
2061 if tarinfo.islnk(): |
|
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
2062 tarinfo._link_target = os.path.join(path, tarinfo.linkname) |
|
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
2063 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2064 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2065 self._extract_member(tarinfo, os.path.join(path, tarinfo.name)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2066 except EnvironmentError, e: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2067 if self.errorlevel > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2068 raise |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2069 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2070 if e.filename is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2071 self._dbg(1, "tarfile: %s" % e.strerror) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2072 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2073 self._dbg(1, "tarfile: %s %r" % (e.strerror, e.filename)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2074 except ExtractError, e: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2075 if self.errorlevel > 1: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2076 raise |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2077 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2078 self._dbg(1, "tarfile: %s" % e) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2079 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2080 def extractfile(self, member): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2081 """Extract a member from the archive as a file object. `member' may be |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2082 a filename or a TarInfo object. If `member' is a regular file, a |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2083 file-like object is returned. If `member' is a link, a file-like |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2084 object is constructed from the link's target. If `member' is none of |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2085 the above, None is returned. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2086 The file-like object is read-only and provides the following |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2087 methods: read(), readline(), readlines(), seek() and tell() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2088 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2089 self._check("r") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2090 |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2091 if isinstance(member, basestring): |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2092 tarinfo = self.getmember(member) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2093 else: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2094 tarinfo = member |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2095 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2096 if tarinfo.isreg(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2097 return self.fileobject(self, tarinfo) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2098 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2099 elif tarinfo.type not in SUPPORTED_TYPES: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2100 # If a member's type is unknown, it is treated as a |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2101 # regular file. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2102 return self.fileobject(self, tarinfo) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2103 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2104 elif tarinfo.islnk() or tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2105 if isinstance(self.fileobj, _Stream): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2106 # A small but ugly workaround for the case that someone tries |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2107 # to extract a (sym)link as a file-object from a non-seekable |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2108 # stream of tar blocks. |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2109 raise StreamError("cannot extract (sym)link as file object") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2110 else: |
|
35276
4adbd331d335
Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents:
34818
diff
changeset
|
2111 # A (sym)link's file object is its target's file object. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2112 return self.extractfile(self._getmember(tarinfo.linkname, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2113 tarinfo)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2114 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2115 # If there's no data associated with the member (directory, chrdev, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2116 # blkdev, etc.), return None instead of a file object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2117 return None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2118 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2119 def _extract_member(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2120 """Extract the TarInfo object tarinfo to a physical |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2121 file called targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2122 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2123 # Fetch the TarInfo object for the given name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2124 # and build the destination pathname, replacing |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2125 # forward slashes to platform specific separators. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2126 if targetpath[-1:] == "/": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2127 targetpath = targetpath[:-1] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2128 targetpath = os.path.normpath(targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2129 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2130 # Create all upper directories. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2131 upperdirs = os.path.dirname(targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2132 if upperdirs and not os.path.exists(upperdirs): |
|
45164
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2133 # Create directories that are not part of the archive with |
|
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2134 # default permissions. |
|
40698
7f4065efa44e
Patch #1507247: tarfile.py: use current umask for intermediate
Lars Gustäbel <lars@gustaebel.de>
parents:
40523
diff
changeset
|
2135 os.makedirs(upperdirs) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2136 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2137 if tarinfo.islnk() or tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2138 self._dbg(1, "%s -> %s" % (tarinfo.name, tarinfo.linkname)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2139 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2140 self._dbg(1, tarinfo.name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2141 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2142 if tarinfo.isreg(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2143 self.makefile(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2144 elif tarinfo.isdir(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2145 self.makedir(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2146 elif tarinfo.isfifo(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2147 self.makefifo(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2148 elif tarinfo.ischr() or tarinfo.isblk(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2149 self.makedev(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2150 elif tarinfo.islnk() or tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2151 self.makelink(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2152 elif tarinfo.type not in SUPPORTED_TYPES: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2153 self.makeunknown(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2154 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2155 self.makefile(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2156 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2157 self.chown(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2158 if not tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2159 self.chmod(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2160 self.utime(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2161 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2162 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2163 # Below are the different file methods. They are called via |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2164 # _extract_member() when extract() is called. They can be replaced in a |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2165 # subclass to implement other functionality. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2166 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2167 def makedir(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2168 """Make a directory called targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2169 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2170 try: |
|
45164
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2171 # Use a safe mode for the directory, the real mode is set |
|
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2172 # later in _extract_member(). |
|
94d980966056
Issue #2004: Use mode 0700 for temporary directories and default
Lars Gustäbel <lars@gustaebel.de>
parents:
44470
diff
changeset
|
2173 os.mkdir(targetpath, 0700) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2174 except EnvironmentError, e: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2175 if e.errno != errno.EEXIST: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2176 raise |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2177 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2178 def makefile(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2179 """Make a file called targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2180 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2181 source = self.extractfile(tarinfo) |
|
41881
3877f37c9861
Remove direct call's to file's constructor and replace them with calls to
Brett Cannon <bcannon@gmail.com>
parents:
41153
diff
changeset
|
2182 target = bltn_open(targetpath, "wb") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2183 copyfileobj(source, target) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2184 source.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2185 target.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2186 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2187 def makeunknown(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2188 """Make a file from a TarInfo object with an unknown type |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2189 at targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2190 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2191 self.makefile(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2192 self._dbg(1, "tarfile: Unknown file type %r, " \ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2193 "extracted as regular file." % tarinfo.type) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2194 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2195 def makefifo(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2196 """Make a fifo called targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2197 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2198 if hasattr(os, "mkfifo"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2199 os.mkfifo(targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2200 else: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2201 raise ExtractError("fifo not supported by system") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2202 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2203 def makedev(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2204 """Make a character or block device called targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2205 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2206 if not hasattr(os, "mknod") or not hasattr(os, "makedev"): |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2207 raise ExtractError("special devices not supported by system") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2208 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2209 mode = tarinfo.mode |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2210 if tarinfo.isblk(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2211 mode |= stat.S_IFBLK |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2212 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2213 mode |= stat.S_IFCHR |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2214 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2215 os.mknod(targetpath, mode, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2216 os.makedev(tarinfo.devmajor, tarinfo.devminor)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2217 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2218 def makelink(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2219 """Make a (symbolic) link called targetpath. If it cannot be created |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2220 (platform limitation), we try to make a copy of the referenced file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2221 instead of a link. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2222 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2223 linkpath = tarinfo.linkname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2224 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2225 if tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2226 os.symlink(linkpath, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2227 else: |
|
32769
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
2228 # See extract(). |
|
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
2229 os.link(tarinfo._link_target, targetpath) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2230 except AttributeError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2231 if tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2232 linkpath = os.path.join(os.path.dirname(tarinfo.name), |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2233 linkpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2234 linkpath = normpath(linkpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2235 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2236 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2237 self._extract_member(self.getmember(linkpath), targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2238 except (EnvironmentError, KeyError), e: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2239 linkpath = os.path.normpath(linkpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2240 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2241 shutil.copy2(linkpath, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2242 except EnvironmentError, e: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2243 raise IOError("link could not be created") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2244 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2245 def chown(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2246 """Set owner of targetpath according to tarinfo. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2247 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2248 if pwd and hasattr(os, "geteuid") and os.geteuid() == 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2249 # We have to be root to do so. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2250 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2251 g = grp.getgrnam(tarinfo.gname)[2] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2252 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2253 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2254 g = grp.getgrgid(tarinfo.gid)[2] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2255 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2256 g = os.getgid() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2257 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2258 u = pwd.getpwnam(tarinfo.uname)[2] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2259 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2260 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2261 u = pwd.getpwuid(tarinfo.uid)[2] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2262 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2263 u = os.getuid() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2264 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2265 if tarinfo.issym() and hasattr(os, "lchown"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2266 os.lchown(targetpath, u, g) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2267 else: |
|
27972
53c802927202
OS/2 has no concept of file ownership, like DOS & MS Windows version
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
27507
diff
changeset
|
2268 if sys.platform != "os2emx": |
|
53c802927202
OS/2 has no concept of file ownership, like DOS & MS Windows version
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
27507
diff
changeset
|
2269 os.chown(targetpath, u, g) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2270 except EnvironmentError, e: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2271 raise ExtractError("could not change owner") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2272 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2273 def chmod(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2274 """Set file permissions of targetpath according to tarinfo. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2275 """ |
|
28214
d4b262fbd04a
Test that os.utime and os.chmod actually exist before using them.
Jack Jansen <jack.jansen@cwi.nl>
parents:
27972
diff
changeset
|
2276 if hasattr(os, 'chmod'): |
|
d4b262fbd04a
Test that os.utime and os.chmod actually exist before using them.
Jack Jansen <jack.jansen@cwi.nl>
parents:
27972
diff
changeset
|
2277 try: |
|
d4b262fbd04a
Test that os.utime and os.chmod actually exist before using them.
Jack Jansen <jack.jansen@cwi.nl>
parents:
27972
diff
changeset
|
2278 os.chmod(targetpath, tarinfo.mode) |
|
d4b262fbd04a
Test that os.utime and os.chmod actually exist before using them.
Jack Jansen <jack.jansen@cwi.nl>
parents:
27972
diff
changeset
|
2279 except EnvironmentError, e: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2280 raise ExtractError("could not change mode") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2281 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2282 def utime(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2283 """Set modification time of targetpath according to tarinfo. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2284 """ |
|
28214
d4b262fbd04a
Test that os.utime and os.chmod actually exist before using them.
Jack Jansen <jack.jansen@cwi.nl>
parents:
27972
diff
changeset
|
2285 if not hasattr(os, 'utime'): |
|
28222
eca697e06a07
Somebody must not have run the test before checking this in -- it had
Tim Peters <tim.peters@gmail.com>
parents:
28217
diff
changeset
|
2286 return |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2287 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2288 os.utime(targetpath, (tarinfo.mtime, tarinfo.mtime)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2289 except EnvironmentError, e: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2290 raise ExtractError("could not change modification time") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2291 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2292 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2293 def next(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2294 """Return the next member of the archive as a TarInfo object, when |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2295 TarFile is opened for reading. Return None if there is no more |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2296 available. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2297 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2298 self._check("ra") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2299 if self.firstmember is not None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2300 m = self.firstmember |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2301 self.firstmember = None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2302 return m |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2303 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2304 # Read the next block. |
| 32604 | 2305 self.fileobj.seek(self.offset) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2306 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2307 try: |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2308 tarinfo = self.tarinfo.fromtarfile(self) |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2309 if tarinfo is None: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2310 return |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2311 self.members.append(tarinfo) |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
2312 |
|
40487
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
2313 except HeaderError, e: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2314 if self.ignore_zeros: |
|
40487
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
2315 self._dbg(2, "0x%X: %s" % (self.offset, e)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2316 self.offset += BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2317 continue |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2318 else: |
| 32604 | 2319 if self.offset == 0: |
|
40487
51395c0b5482
Patch #1484695: The tarfile module now raises a HeaderError exception
Georg Brandl <georg@python.org>
parents:
40424
diff
changeset
|
2320 raise ReadError(str(e)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2321 return None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2322 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2323 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
2324 return tarinfo |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2325 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2326 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2327 # Little helper methods: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2328 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2329 def _getmember(self, name, tarinfo=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2330 """Find an archive member by name from bottom to top. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2331 If tarinfo is given, it is used as the starting point. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2332 """ |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
2333 # Ensure that all members have been loaded. |
|
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
2334 members = self.getmembers() |
|
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
2335 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2336 if tarinfo is None: |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
2337 end = len(members) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2338 else: |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
2339 end = members.index(tarinfo) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2340 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2341 for i in xrange(end - 1, -1, -1): |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
2342 if name == members[i].name: |
|
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
2343 return members[i] |
| 32604 | 2344 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2345 def _load(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2346 """Read through the entire archive file and look for readable |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2347 members. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2348 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2349 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2350 tarinfo = self.next() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2351 if tarinfo is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2352 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2353 self._loaded = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2354 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2355 def _check(self, mode=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2356 """Check if TarFile is still open, and if the operation's mode |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2357 corresponds to TarFile's mode. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2358 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2359 if self.closed: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2360 raise IOError("%s is closed" % self.__class__.__name__) |
|
41153
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2361 if mode is not None and self.mode not in mode: |
|
5a3a1b02418a
This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents:
40748
diff
changeset
|
2362 raise IOError("bad operation for mode %r" % self.mode) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2363 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2364 def __iter__(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2365 """Provide an iterator object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2366 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2367 if self._loaded: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2368 return iter(self.members) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2369 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2370 return TarIter(self) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2371 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2372 def _dbg(self, level, msg): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2373 """Write debugging output to sys.stderr. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2374 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2375 if level <= self.debug: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2376 print >> sys.stderr, msg |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2377 # class TarFile |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2378 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2379 class TarIter: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2380 """Iterator Class. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2381 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2382 for tarinfo in TarFile(...): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2383 suite... |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2384 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2385 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2386 def __init__(self, tarfile): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2387 """Construct a TarIter object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2388 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2389 self.tarfile = tarfile |
|
34680
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2390 self.index = 0 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2391 def __iter__(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2392 """Return iterator object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2393 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2394 return self |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2395 def next(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2396 """Return the next item using TarFile's next() method. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2397 When all members have been read, set TarFile as _loaded. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2398 """ |
|
34680
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2399 # Fix for SF #1100429: Under rare circumstances it can |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2400 # happen that getmembers() is called during iteration, |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2401 # which will cause TarIter to stop prematurely. |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2402 if not self.tarfile._loaded: |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2403 tarinfo = self.tarfile.next() |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2404 if not tarinfo: |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2405 self.tarfile._loaded = True |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2406 raise StopIteration |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2407 else: |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2408 try: |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2409 tarinfo = self.tarfile.members[self.index] |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2410 except IndexError: |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2411 raise StopIteration |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2412 self.index += 1 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2413 return tarinfo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2414 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2415 # Helper classes for sparse file support |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2416 class _section: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2417 """Base class for _data and _hole. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2418 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2419 def __init__(self, offset, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2420 self.offset = offset |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2421 self.size = size |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2422 def __contains__(self, offset): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2423 return self.offset <= offset < self.offset + self.size |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2424 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2425 class _data(_section): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2426 """Represent a data section in a sparse file. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2427 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2428 def __init__(self, offset, size, realpos): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2429 _section.__init__(self, offset, size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2430 self.realpos = realpos |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2431 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2432 class _hole(_section): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2433 """Represent a hole section in a sparse file. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2434 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2435 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2436 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2437 class _ringbuffer(list): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2438 """Ringbuffer class which increases performance |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2439 over a regular list. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2440 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2441 def __init__(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2442 self.idx = 0 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2443 def find(self, offset): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2444 idx = self.idx |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2445 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2446 item = self[idx] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2447 if offset in item: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2448 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2449 idx += 1 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2450 if idx == len(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2451 idx = 0 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2452 if idx == self.idx: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2453 # End of File |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2454 return None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2455 self.idx = idx |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2456 return item |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2457 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2458 #--------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2459 # zipfile compatible TarFile class |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2460 #--------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2461 TAR_PLAIN = 0 # zipfile.ZIP_STORED |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2462 TAR_GZIPPED = 8 # zipfile.ZIP_DEFLATED |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2463 class TarFileCompat: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2464 """TarFile class compatible with standard module zipfile's |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2465 ZipFile class. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2466 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2467 def __init__(self, file, mode="r", compression=TAR_PLAIN): |
|
48138
feddaf21c2a4
Issue #3039: Fix TarFileCompat.writestr() which always raised an
Lars Gustäbel <lars@gustaebel.de>
parents:
47352
diff
changeset
|
2468 from warnings import warnpy3k |
|
feddaf21c2a4
Issue #3039: Fix TarFileCompat.writestr() which always raised an
Lars Gustäbel <lars@gustaebel.de>
parents:
47352
diff
changeset
|
2469 warnpy3k("the TarFileCompat class has been removed in Python 3.0", |
|
feddaf21c2a4
Issue #3039: Fix TarFileCompat.writestr() which always raised an
Lars Gustäbel <lars@gustaebel.de>
parents:
47352
diff
changeset
|
2470 stacklevel=2) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2471 if compression == TAR_PLAIN: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2472 self.tarfile = TarFile.taropen(file, mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2473 elif compression == TAR_GZIPPED: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2474 self.tarfile = TarFile.gzopen(file, mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2475 else: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2476 raise ValueError("unknown compression constant") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2477 if mode[0:1] == "r": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2478 members = self.tarfile.getmembers() |
| 35532 | 2479 for m in members: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2480 m.filename = m.name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2481 m.file_size = m.size |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2482 m.date_time = time.gmtime(m.mtime)[:6] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2483 def namelist(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2484 return map(lambda m: m.name, self.infolist()) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2485 def infolist(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2486 return filter(lambda m: m.type in REGULAR_TYPES, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2487 self.tarfile.getmembers()) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2488 def printdir(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2489 self.tarfile.list() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2490 def testzip(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2491 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2492 def getinfo(self, name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2493 return self.tarfile.getmember(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2494 def read(self, name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2495 return self.tarfile.extractfile(self.tarfile.getmember(name)).read() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2496 def write(self, filename, arcname=None, compress_type=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2497 self.tarfile.add(filename, arcname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2498 def writestr(self, zinfo, bytes): |
|
34293
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2499 try: |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2500 from cStringIO import StringIO |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2501 except ImportError: |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2502 from StringIO import StringIO |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2503 import calendar |
|
48138
feddaf21c2a4
Issue #3039: Fix TarFileCompat.writestr() which always raised an
Lars Gustäbel <lars@gustaebel.de>
parents:
47352
diff
changeset
|
2504 tinfo = TarInfo(zinfo.filename) |
|
feddaf21c2a4
Issue #3039: Fix TarFileCompat.writestr() which always raised an
Lars Gustäbel <lars@gustaebel.de>
parents:
47352
diff
changeset
|
2505 tinfo.size = len(bytes) |
|
feddaf21c2a4
Issue #3039: Fix TarFileCompat.writestr() which always raised an
Lars Gustäbel <lars@gustaebel.de>
parents:
47352
diff
changeset
|
2506 tinfo.mtime = calendar.timegm(zinfo.date_time) |
|
feddaf21c2a4
Issue #3039: Fix TarFileCompat.writestr() which always raised an
Lars Gustäbel <lars@gustaebel.de>
parents:
47352
diff
changeset
|
2507 self.tarfile.addfile(tinfo, StringIO(bytes)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2508 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2509 self.tarfile.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2510 #class TarFileCompat |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2511 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2512 #-------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2513 # exported functions |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2514 #-------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2515 def is_tarfile(name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2516 """Return True if name points to a tar archive that we |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2517 are able to handle, else return False. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2518 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2519 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2520 t = open(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2521 t.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2522 return True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2523 except TarError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2524 return False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2525 |
|
41881
3877f37c9861
Remove direct call's to file's constructor and replace them with calls to
Brett Cannon <bcannon@gmail.com>
parents:
41153
diff
changeset
|
2526 bltn_open = open |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2527 open = TarFile.open |
