Mercurial > cpython-lru_cache-weakref
annotate Lib/tarfile.py @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 30e4ea423369 |
| children | fa2dac3926ff |
| 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 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
36 version = "0.8.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 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
52 |
|
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
|
53 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
|
54 # 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
|
55 # 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
|
56 # 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
|
57 # 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
|
58 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
|
59 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
60 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
61 import grp, pwd |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
62 except ImportError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
63 grp = pwd = None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
64 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
65 # from tarfile import * |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
66 __all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError"] |
|
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 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
69 # tar constants |
|
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 NUL = "\0" # the null character |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
72 BLOCKSIZE = 512 # length of processing blocks |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
73 RECORDSIZE = BLOCKSIZE * 20 # length of records |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
74 MAGIC = "ustar" # magic tar string |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
75 VERSION = "00" # version number |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
76 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
77 LENGTH_NAME = 100 # maximum length of a filename |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
78 LENGTH_LINK = 100 # maximum length of a linkname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
79 LENGTH_PREFIX = 155 # maximum length of the prefix field |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
80 MAXSIZE_MEMBER = 077777777777L # maximum size of a file (11 octal digits) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
81 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
82 REGTYPE = "0" # regular file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
83 AREGTYPE = "\0" # regular file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
84 LNKTYPE = "1" # link (inside tarfile) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
85 SYMTYPE = "2" # symbolic link |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
86 CHRTYPE = "3" # character special device |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
87 BLKTYPE = "4" # block special device |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
88 DIRTYPE = "5" # directory |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
89 FIFOTYPE = "6" # fifo special device |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
90 CONTTYPE = "7" # contiguous file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
91 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
92 GNUTYPE_LONGNAME = "L" # GNU tar extension for longnames |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
93 GNUTYPE_LONGLINK = "K" # GNU tar extension for longlink |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
94 GNUTYPE_SPARSE = "S" # GNU tar extension for sparse file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
95 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
96 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
97 # tarfile constants |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
98 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
99 SUPPORTED_TYPES = (REGTYPE, AREGTYPE, LNKTYPE, # file types that tarfile |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
100 SYMTYPE, DIRTYPE, FIFOTYPE, # can cope with. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
101 CONTTYPE, CHRTYPE, BLKTYPE, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
102 GNUTYPE_LONGNAME, GNUTYPE_LONGLINK, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
103 GNUTYPE_SPARSE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
104 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
105 REGULAR_TYPES = (REGTYPE, AREGTYPE, # file types that somehow |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
106 CONTTYPE, GNUTYPE_SPARSE) # represent regular files |
|
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 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
109 # 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
|
110 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
111 S_IFLNK = 0120000 # symbolic link |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
112 S_IFREG = 0100000 # regular file |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
113 S_IFBLK = 0060000 # block device |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
114 S_IFDIR = 0040000 # directory |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
115 S_IFCHR = 0020000 # character device |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
116 S_IFIFO = 0010000 # fifo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
117 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
118 TSUID = 04000 # set UID on execution |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
119 TSGID = 02000 # set GID on execution |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
120 TSVTX = 01000 # reserved |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
121 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
122 TUREAD = 0400 # read by owner |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
123 TUWRITE = 0200 # write by owner |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
124 TUEXEC = 0100 # execute/search by owner |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
125 TGREAD = 0040 # read by group |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
126 TGWRITE = 0020 # write by group |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
127 TGEXEC = 0010 # execute/search by group |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
128 TOREAD = 0004 # read by other |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
129 TOWRITE = 0002 # write by other |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
130 TOEXEC = 0001 # execute/search by other |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
131 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
132 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
133 # Some useful functions |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
134 #--------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
135 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
136 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
|
137 """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
|
138 """ |
|
38436
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
139 return s[:length-1] + (length - len(s) - 1) * NUL + NUL |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
140 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
141 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
|
142 """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
|
143 """ |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
144 # 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
|
145 # itn() below. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
146 if s[0] != chr(0200): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
147 n = int(s.rstrip(NUL) or "0", 8) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
148 else: |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
149 n = 0L |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
150 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
|
151 n <<= 8 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
152 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
|
153 return n |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
154 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
155 def itn(n, digits=8, posix=False): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
156 """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
|
157 """ |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
158 # 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
|
159 # 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
|
160 # (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
|
161 # 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
|
162 # 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
|
163 # 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
|
164 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
|
165 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
|
166 else: |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
167 if posix: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
168 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
|
169 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
170 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
|
171 # 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
|
172 # 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
|
173 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
|
174 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
175 s = "" |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
176 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
|
177 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
|
178 n >>= 8 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
179 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
|
180 return s |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
181 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
182 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
|
183 """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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 signed. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
190 """ |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
191 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
|
192 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
|
193 return unsigned_chksum, signed_chksum |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
194 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
195 def copyfileobj(src, dst, length=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
196 """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
|
197 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
|
198 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
199 if length == 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
200 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
201 if length is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
202 shutil.copyfileobj(src, dst) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
203 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
204 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
205 BUFSIZE = 16 * 1024 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
206 blocks, remainder = divmod(length, BUFSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
207 for b in xrange(blocks): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
208 buf = src.read(BUFSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
209 if len(buf) < BUFSIZE: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
210 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
|
211 dst.write(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
212 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
213 if remainder != 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
214 buf = src.read(remainder) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
215 if len(buf) < remainder: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
216 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
|
217 dst.write(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
218 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
219 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
220 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
|
221 ((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
|
222 (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
|
223 (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
|
224 (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
|
225 (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
|
226 (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
|
227 |
|
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
|
228 ((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
|
229 ((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
|
230 ((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
|
231 (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
|
232 (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
|
233 |
|
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
|
234 ((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
|
235 ((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
|
236 ((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
|
237 (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
|
238 (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
|
239 |
|
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
|
240 ((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
|
241 ((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
|
242 ((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
|
243 (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
|
244 (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
|
245 ) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
246 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
247 def filemode(mode): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
248 """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
|
249 -rwxrwxrwx. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
250 Used by TarFile.list() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
251 """ |
|
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
|
252 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 return "".join(perm) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
261 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
262 if os.sep != "/": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
263 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
|
264 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
265 normpath = os.path.normpath |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
266 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
267 class TarError(Exception): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
268 """Base exception.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
269 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
270 class ExtractError(TarError): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
271 """General exception for extract errors.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
272 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
273 class ReadError(TarError): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
274 """Exception for unreadble tar archives.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
275 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
276 class CompressionError(TarError): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
277 """Exception for unavailable compression methods.""" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
278 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
279 class StreamError(TarError): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
280 """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
|
281 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
282 |
|
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 # internal stream interface |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
285 #--------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
286 class _LowLevelFile: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
287 """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
|
288 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
|
289 access. |
|
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 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
292 def __init__(self, name, mode): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
293 mode = { |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
294 "r": os.O_RDONLY, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
295 "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
|
296 }[mode] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
297 if hasattr(os, "O_BINARY"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
298 mode |= os.O_BINARY |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
299 self.fd = os.open(name, mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
300 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
301 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
302 os.close(self.fd) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
303 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
304 def read(self, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
305 return os.read(self.fd, size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
306 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
307 def write(self, s): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
308 os.write(self.fd, s) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
309 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
310 class _Stream: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
311 """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
|
312 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
318 _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
|
319 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
320 |
|
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
|
321 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
|
322 """Construct a _Stream object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
323 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
324 self._extfileobj = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
325 if fileobj is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
326 fileobj = _LowLevelFile(name, mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
327 self._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
328 |
|
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
|
329 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
|
330 # 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
|
331 # 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
|
332 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
|
333 comptype = fileobj.getcomptype() |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
334 |
|
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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 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
|
340 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
|
341 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
|
342 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
|
343 |
|
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
|
344 if comptype == "gz": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
345 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
346 import zlib |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
347 except ImportError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
348 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
|
349 self.zlib = zlib |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
350 self.crc = zlib.crc32("") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
351 if mode == "r": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
352 self._init_read_gz() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
353 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
354 self._init_write_gz() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
355 |
|
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
|
356 if comptype == "bz2": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
357 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
358 import bz2 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
359 except ImportError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
360 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
|
361 if mode == "r": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
362 self.dbuf = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
363 self.cmp = bz2.BZ2Decompressor() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
364 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
365 self.cmp = bz2.BZ2Compressor() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
366 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
367 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
|
368 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
|
369 self.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
370 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
371 def _init_write_gz(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
372 """Initialize for writing with gzip compression. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
373 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
374 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
|
375 -self.zlib.MAX_WBITS, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
376 self.zlib.DEF_MEM_LEVEL, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
377 0) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
378 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
|
379 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
|
380 if self.name.endswith(".gz"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
381 self.name = self.name[:-3] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
382 self.__write(self.name + NUL) |
|
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 def write(self, s): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
385 """Write string s to the stream. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
386 """ |
|
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
|
387 if self.comptype == "gz": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
388 self.crc = self.zlib.crc32(s, self.crc) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
389 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
|
390 if self.comptype != "tar": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
391 s = self.cmp.compress(s) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
392 self.__write(s) |
|
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 def __write(self, s): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
395 """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
|
396 is ready to be written. |
|
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.buf += s |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
399 while len(self.buf) > self.bufsize: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
400 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
|
401 self.buf = self.buf[self.bufsize:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
402 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
403 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
404 """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
|
405 done on it afterwards. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
406 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
407 if self.closed: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
408 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
409 |
|
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
|
410 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
|
411 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
|
412 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
413 if self.mode == "w" and 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
|
414 blocks, remainder = divmod(len(self.buf), self.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
|
415 if remainder > 0: |
|
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.buf += NUL * (self.bufsize - remainder) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
417 self.fileobj.write(self.buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
418 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
|
419 if self.comptype == "gz": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
420 self.fileobj.write(struct.pack("<l", self.crc)) |
|
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
|
421 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
|
422 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
423 if not self._extfileobj: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
424 self.fileobj.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
425 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
426 self.closed = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
427 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
428 def _init_read_gz(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
429 """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
|
430 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
431 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
|
432 self.dbuf = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
433 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
434 # 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
|
435 if self.__read(2) != "\037\213": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
436 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
|
437 if self.__read(1) != "\010": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
438 raise CompressionError("unsupported compression method") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
439 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
440 flag = ord(self.__read(1)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
441 self.__read(6) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
442 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
443 if flag & 4: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
444 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
|
445 self.read(xlen) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
446 if flag & 8: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
447 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
448 s = self.__read(1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
449 if not s or s == NUL: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
450 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
451 if flag & 16: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
452 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
453 s = self.__read(1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
454 if not s or s == NUL: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
455 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
456 if flag & 2: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
457 self.__read(2) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
458 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
459 def tell(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
460 """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
|
461 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
462 return self.pos |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
463 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
464 def seek(self, pos=0): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
465 """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
|
466 is forbidden. |
|
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 if pos - self.pos >= 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
469 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
|
470 for i in xrange(blocks): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
471 self.read(self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
472 self.read(remainder) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
473 else: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
474 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
|
475 return self.pos |
|
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 read(self, size=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
478 """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
|
479 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
|
480 up to EOF. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
481 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
482 if size is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
483 t = [] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
484 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
485 buf = self._read(self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
486 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
487 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
488 t.append(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
489 buf = "".join(t) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
490 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
491 buf = self._read(size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
492 self.pos += len(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
493 return buf |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
494 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
495 def _read(self, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
496 """Return size bytes from the stream. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
497 """ |
|
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
|
498 if self.comptype == "tar": |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
499 return self.__read(size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
500 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
501 c = len(self.dbuf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
502 t = [self.dbuf] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
503 while c < size: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
504 buf = self.__read(self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
505 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
506 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
507 buf = self.cmp.decompress(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
508 t.append(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
509 c += len(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
510 t = "".join(t) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
511 self.dbuf = t[size:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
512 return t[:size] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
513 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
514 def __read(self, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
515 """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
|
516 read another block from the stream. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
517 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
518 c = len(self.buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
519 t = [self.buf] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
520 while c < size: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
521 buf = self.fileobj.read(self.bufsize) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
522 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
523 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
524 t.append(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
525 c += len(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
526 t = "".join(t) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
527 self.buf = t[size:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
528 return t[:size] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
529 # class _Stream |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
530 |
|
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
|
531 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
|
532 """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
|
533 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
|
534 """ |
|
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
|
535 |
|
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
|
536 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
|
537 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
|
538 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
|
539 |
|
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
|
540 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
|
541 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
|
542 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
|
543 |
|
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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 |
|
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
|
551 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
|
552 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
|
553 # 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
|
554 |
|
38185
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
555 class _BZ2Proxy(object): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
556 """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
|
557 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
|
558 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
|
559 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
|
560 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
|
561 """ |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
562 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
563 blocksize = 16 * 1024 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
564 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
565 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
|
566 self.fileobj = fileobj |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
567 self.mode = mode |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
568 self.init() |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
569 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
570 def init(self): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
571 import bz2 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
572 self.pos = 0 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
573 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
|
574 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
|
575 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
|
576 self.buf = "" |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
577 else: |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
578 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
|
579 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
580 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
|
581 b = [self.buf] |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
582 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
|
583 while x < size: |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
584 try: |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
585 raw = self.fileobj.read(self.blocksize) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
586 data = self.bz2obj.decompress(raw) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
587 b.append(data) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
588 except EOFError: |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
589 break |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
590 x += len(data) |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
591 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
|
592 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
593 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
|
594 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
|
595 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
|
596 return buf |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
597 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
598 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
|
599 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
|
600 self.init() |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
601 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
|
602 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
603 def tell(self): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
604 return self.pos |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
605 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
606 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
|
607 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
|
608 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
|
609 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
|
610 |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
611 def close(self): |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
612 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
|
613 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
|
614 self.fileobj.write(raw) |
|
38436
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
615 self.fileobj.close() |
|
38185
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
616 # class _BZ2Proxy |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
617 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
618 #------------------------ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
619 # Extraction file object |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
620 #------------------------ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
621 class ExFileObject(object): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
622 """File-like object for reading an archive member. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
623 Is returned by TarFile.extractfile(). Support for |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
624 sparse files included. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
625 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
626 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
627 def __init__(self, tarfile, tarinfo): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
628 self.fileobj = tarfile.fileobj |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
629 self.name = tarinfo.name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
630 self.mode = "r" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
631 self.closed = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
632 self.offset = tarinfo.offset_data |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
633 self.size = tarinfo.size |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
634 self.pos = 0L |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
635 self.linebuffer = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
636 if tarinfo.issparse(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
637 self.sparse = tarinfo.sparse |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
638 self.read = self._readsparse |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
639 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
640 self.read = self._readnormal |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
641 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
642 def __read(self, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
643 """Overloadable read method. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
644 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
645 return self.fileobj.read(size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
646 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
647 def readline(self, size=-1): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
648 """Read a line with approx. size. If size is negative, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
649 read a whole line. readline() and read() must not |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
650 be mixed up (!). |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
651 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
652 if size < 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
653 size = sys.maxint |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
654 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
655 nl = self.linebuffer.find("\n") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
656 if nl >= 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
657 nl = min(nl, size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
658 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
659 size -= len(self.linebuffer) |
|
33263
8cd57796a9cf
Patch #1014992: Never return more than a line from readline.
Martin v. Löwis <martin@v.loewis.de>
parents:
33234
diff
changeset
|
660 while (nl < 0 and size > 0): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
661 buf = self.read(min(size, 100)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
662 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
663 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
664 self.linebuffer += buf |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
665 size -= len(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
666 nl = self.linebuffer.find("\n") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
667 if nl == -1: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
668 s = self.linebuffer |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
669 self.linebuffer = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
670 return s |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
671 buf = self.linebuffer[:nl] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
672 self.linebuffer = self.linebuffer[nl + 1:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
673 while buf[-1:] == "\r": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
674 buf = buf[:-1] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
675 return buf + "\n" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
676 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
677 def readlines(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
678 """Return a list with all (following) lines. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
679 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
680 result = [] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
681 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
682 line = self.readline() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
683 if not line: break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
684 result.append(line) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
685 return result |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
686 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
687 def _readnormal(self, size=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
688 """Read operation for regular files. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
689 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
690 if self.closed: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
691 raise ValueError("file is closed") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
692 self.fileobj.seek(self.offset + self.pos) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
693 bytesleft = self.size - self.pos |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
694 if size is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
695 bytestoread = bytesleft |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
696 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
697 bytestoread = min(size, bytesleft) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
698 self.pos += bytestoread |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
699 return self.__read(bytestoread) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
700 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
701 def _readsparse(self, size=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
702 """Read operation for sparse files. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
703 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
704 if self.closed: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
705 raise ValueError("file is closed") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
706 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
707 if size is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
708 size = self.size - self.pos |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
709 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
710 data = [] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
711 while size > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
712 buf = self._readsparsesection(size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
713 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
714 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
715 size -= len(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
716 data.append(buf) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
717 return "".join(data) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
718 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
719 def _readsparsesection(self, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
720 """Read a single section of a sparse file. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
721 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
722 section = self.sparse.find(self.pos) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
723 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
724 if section is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
725 return "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
726 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
727 toread = min(size, section.offset + section.size - self.pos) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
728 if isinstance(section, _data): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
729 realpos = section.realpos + self.pos - section.offset |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
730 self.pos += toread |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
731 self.fileobj.seek(self.offset + realpos) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
732 return self.__read(toread) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
733 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
734 self.pos += toread |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
735 return NUL * toread |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
736 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
737 def tell(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
738 """Return the current file position. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
739 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
740 return self.pos |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
741 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
742 def seek(self, pos, whence=0): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
743 """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
|
744 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
745 self.linebuffer = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
746 if whence == 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
747 self.pos = min(max(pos, 0), self.size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
748 if whence == 1: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
749 if pos < 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
750 self.pos = max(self.pos + pos, 0) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
751 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
752 self.pos = min(self.pos + pos, self.size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
753 if whence == 2: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
754 self.pos = max(min(self.size + pos, self.size), 0) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
755 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
756 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
757 """Close the file object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
758 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
759 self.closed = True |
|
34649
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
760 |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
761 def __iter__(self): |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
762 """Get an iterator over the file object. |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
763 """ |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
764 if self.closed: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
765 raise ValueError("I/O operation on closed file") |
|
34649
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
766 return self |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
767 |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
768 def next(self): |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
769 """Get the next item from the file iterator. |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
770 """ |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
771 result = self.readline() |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
772 if not result: |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
773 raise StopIteration |
|
13a950a3624b
Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents:
34421
diff
changeset
|
774 return result |
|
34818
4eaab38566c9
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34697
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 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
777 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
778 #------------------ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
779 # Exported Classes |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
780 #------------------ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
781 class TarInfo(object): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
782 """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
|
783 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
|
784 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
|
785 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
|
786 usually created internally. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
787 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
788 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
789 def __init__(self, name=""): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
790 """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
|
791 of the member. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
792 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
793 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
794 self.name = name # member name (dirnames must end with '/') |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
795 self.mode = 0666 # file permissions |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
796 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
|
797 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
|
798 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
|
799 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
|
800 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
|
801 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
|
802 self.linkname = "" # link name |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
803 self.uname = "user" # user name |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
804 self.gname = "group" # group name |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
805 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
|
806 self.devminor = 0 # device minor number |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
807 self.prefix = "" # prefix to filename or information |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
808 # about sparse files |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
809 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
810 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
|
811 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
|
812 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
813 def __repr__(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
814 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
|
815 |
| 34421 | 816 @classmethod |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
817 def frombuf(cls, buf): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
818 """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
|
819 """ |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
820 if len(buf) != BLOCKSIZE: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
821 raise ValueError("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
|
822 if buf.count(NUL) == BLOCKSIZE: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
823 raise ValueError("empty header") |
|
32770
a3b1d6c99e46
SF #918101, allow files >= 8 GB using GNU extension
Neal Norwitz <nnorwitz@gmail.com>
parents:
32769
diff
changeset
|
824 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
825 tarinfo = cls() |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
826 tarinfo.buf = buf |
|
38436
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
827 tarinfo.name = buf[0:100].rstrip(NUL) |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
828 tarinfo.mode = nti(buf[100:108]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
829 tarinfo.uid = nti(buf[108:116]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
830 tarinfo.gid = nti(buf[116:124]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
831 tarinfo.size = nti(buf[124:136]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
832 tarinfo.mtime = nti(buf[136:148]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
833 tarinfo.chksum = nti(buf[148:156]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
834 tarinfo.type = buf[156:157] |
|
38436
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
835 tarinfo.linkname = buf[157:257].rstrip(NUL) |
|
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
836 tarinfo.uname = buf[265:297].rstrip(NUL) |
|
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
837 tarinfo.gname = buf[297:329].rstrip(NUL) |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
838 tarinfo.devmajor = nti(buf[329:337]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
839 tarinfo.devminor = nti(buf[337:345]) |
|
28731
86f4933fa429
Fix by Lars Gustaebel for bug 721871: don't get upset if the prefix
Jack Jansen <jack.jansen@cwi.nl>
parents:
28576
diff
changeset
|
840 tarinfo.prefix = buf[345:500] |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
841 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
842 if tarinfo.chksum not in calc_chksums(buf): |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
843 raise ValueError("invalid header") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
844 return tarinfo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
845 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
846 def tobuf(self, posix=False): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
847 """Return a tar header block as a 512 byte string. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
848 """ |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
849 parts = [ |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
850 stn(self.name, 100), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
851 itn(self.mode & 07777, 8, posix), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
852 itn(self.uid, 8, posix), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
853 itn(self.gid, 8, posix), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
854 itn(self.size, 12, posix), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
855 itn(self.mtime, 12, posix), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
856 " ", # checksum field |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
857 self.type, |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
858 stn(self.linkname, 100), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
859 stn(MAGIC, 6), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
860 stn(VERSION, 2), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
861 stn(self.uname, 32), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
862 stn(self.gname, 32), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
863 itn(self.devmajor, 8, posix), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
864 itn(self.devminor, 8, posix), |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
865 stn(self.prefix, 155) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
866 ] |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
867 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
868 buf = struct.pack("%ds" % BLOCKSIZE, "".join(parts)) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
869 chksum = calc_chksums(buf)[0] |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
870 buf = buf[:148] + "%06o\0" % chksum + buf[155:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
871 self.buf = buf |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
872 return buf |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
873 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
874 def isreg(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
875 return self.type in REGULAR_TYPES |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
876 def isfile(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
877 return self.isreg() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
878 def isdir(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
879 return self.type == DIRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
880 def issym(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
881 return self.type == SYMTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
882 def islnk(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
883 return self.type == LNKTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
884 def ischr(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
885 return self.type == CHRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
886 def isblk(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
887 return self.type == BLKTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
888 def isfifo(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
889 return self.type == FIFOTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
890 def issparse(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
891 return self.type == GNUTYPE_SPARSE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
892 def isdev(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
893 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
|
894 # class TarInfo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
895 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
896 class TarFile(object): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
897 """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
|
898 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
899 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
900 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
|
901 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
902 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
|
903 # tar file, else the link. |
|
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 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
|
906 # continues processing. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
907 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
908 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
|
909 # 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
|
910 # 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
|
911 |
|
33179
a2514e2d1c74
Patch #995126: Correct directory size, and generate GNU tarfiles by default.
Martin v. Löwis <martin@v.loewis.de>
parents:
32771
diff
changeset
|
912 posix = False # If True, generates POSIX.1-1990-compliant |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
913 # archives (no GNU extensions!) |
|
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 fileobject = ExFileObject |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
916 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
917 def __init__(self, name=None, mode="r", fileobj=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
918 """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
|
919 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
|
920 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
|
921 defaults to 'r'. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
922 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
|
923 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
|
924 `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
|
925 """ |
|
35403
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
926 self.name = name |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
927 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
928 if len(mode) > 1 or mode not in "raw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
929 raise ValueError("mode must be 'r', 'a' or 'w'") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
930 self._mode = mode |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
931 self.mode = {"r": "rb", "a": "r+b", "w": "wb"}[mode] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
932 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
933 if not fileobj: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
934 fileobj = file(self.name, self.mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
935 self._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
936 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
937 if self.name is None and hasattr(fileobj, "name"): |
|
35403
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
938 self.name = fileobj.name |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
939 if hasattr(fileobj, "mode"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
940 self.mode = fileobj.mode |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
941 self._extfileobj = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
942 self.fileobj = fileobj |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
943 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
944 # 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
|
945 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
|
946 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
|
947 self._loaded = False # flag if all members have been read |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
948 self.offset = 0L # current position in the archive file |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
949 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
|
950 # archive members already added |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
951 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
952 if self._mode == "r": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
953 self.firstmember = None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
954 self.firstmember = self.next() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
955 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
956 if self._mode == "a": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
957 # 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
|
958 # before the first empty block. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
959 self.firstmember = None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
960 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
961 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
962 tarinfo = self.next() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
963 except ReadError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
964 self.fileobj.seek(0) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
965 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
966 if tarinfo is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
967 self.fileobj.seek(- BLOCKSIZE, 1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
968 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
969 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
970 if self._mode in "aw": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
971 self._loaded = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
972 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
973 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
974 # 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
|
975 # 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
|
976 # 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
|
977 # 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
|
978 # from OPEN_METH. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
979 # |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
980 # 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
|
981 # 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
|
982 # 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
|
983 |
| 34421 | 984 @classmethod |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
985 def open(cls, name=None, mode="r", fileobj=None, bufsize=20*512): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
986 """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
|
987 an appropriate TarFile class. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
988 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
989 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
|
990 '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
|
991 'r:' open for reading exclusively uncompressed |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
992 '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
|
993 'r:bz2' open for reading with bzip2 compression |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
994 'a' or 'a:' open for appending |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
995 '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
|
996 '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
|
997 '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
|
998 |
|
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
|
999 '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
|
1000 '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
|
1001 '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
|
1002 '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
|
1003 '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
|
1004 '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
|
1005 '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
|
1006 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1007 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1008 if not name and not fileobj: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1009 raise ValueError("nothing to open") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1010 |
|
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
|
1011 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
|
1012 # 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
|
1013 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
|
1014 func = getattr(cls, cls.OPEN_METH[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
|
1015 try: |
|
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
|
1016 return func(name, "r", 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
|
1017 except (ReadError, CompressionError): |
|
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
|
1018 continue |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1019 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
|
1020 |
|
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
|
1021 elif ":" in mode: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1022 filemode, comptype = mode.split(":", 1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1023 filemode = filemode or "r" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1024 comptype = comptype or "tar" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1025 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1026 # Select the *open() function according to |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1027 # given compression. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1028 if comptype in cls.OPEN_METH: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1029 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
|
1030 else: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1031 raise CompressionError("unknown compression type %r" % comptype) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1032 return func(name, filemode, fileobj) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1033 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1034 elif "|" in mode: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1035 filemode, comptype = mode.split("|", 1) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1036 filemode = filemode or "r" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1037 comptype = comptype or "tar" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1038 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1039 if filemode not in "rw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1040 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
|
1041 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1042 t = cls(name, filemode, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1043 _Stream(name, filemode, comptype, fileobj, bufsize)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1044 t._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1045 return t |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1046 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1047 elif mode in "aw": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1048 return cls.taropen(name, mode, fileobj) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1049 |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1050 raise ValueError("undiscernible mode") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1051 |
| 34421 | 1052 @classmethod |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1053 def taropen(cls, name, mode="r", fileobj=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1054 """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
|
1055 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1056 if len(mode) > 1 or mode not in "raw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1057 raise ValueError("mode must be 'r', 'a' or 'w'") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1058 return cls(name, mode, fileobj) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1059 |
| 34421 | 1060 @classmethod |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1061 def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1062 """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
|
1063 Appending is not allowed. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1064 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1065 if len(mode) > 1 or mode not in "rw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1066 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
|
1067 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1068 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1069 import gzip |
|
28576
1bd50c28203f
Fix test_tarfile failure when gzip is not available
Neal Norwitz <nnorwitz@gmail.com>
parents:
28222
diff
changeset
|
1070 gzip.GzipFile |
|
1bd50c28203f
Fix test_tarfile failure when gzip is not available
Neal Norwitz <nnorwitz@gmail.com>
parents:
28222
diff
changeset
|
1071 except (ImportError, AttributeError): |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1072 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
|
1073 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1074 pre, ext = os.path.splitext(name) |
|
35403
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1075 pre = os.path.basename(pre) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1076 if ext == ".tgz": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1077 ext = ".tar" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1078 if ext == ".gz": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1079 ext = "" |
|
35403
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1080 tarname = pre + ext |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1081 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1082 if fileobj is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1083 fileobj = file(name, mode + "b") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1084 |
|
35403
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1085 if mode != "r": |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1086 name = tarname |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1087 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1088 try: |
|
35403
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1089 t = cls.taropen(tarname, mode, |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1090 gzip.GzipFile(name, mode, compresslevel, fileobj) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1091 ) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1092 except IOError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1093 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
|
1094 t._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1095 return t |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1096 |
| 34421 | 1097 @classmethod |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1098 def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1099 """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
|
1100 Appending is not allowed. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1101 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1102 if len(mode) > 1 or mode not in "rw": |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1103 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
|
1104 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1105 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1106 import bz2 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1107 except ImportError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1108 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
|
1109 |
|
35403
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1110 pre, ext = os.path.splitext(name) |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1111 pre = os.path.basename(pre) |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1112 if ext == ".tbz2": |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1113 ext = ".tar" |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1114 if ext == ".bz2": |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1115 ext = "" |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1116 tarname = pre + ext |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1117 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1118 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
|
1119 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
|
1120 else: |
|
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
1121 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
|
1122 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1123 try: |
|
38185
ad8b76e99261
[ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents:
38146
diff
changeset
|
1124 t = cls.taropen(tarname, mode, fileobj) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1125 except IOError: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1126 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
|
1127 t._extfileobj = False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1128 return t |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1129 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1130 # All *open() methods are registered here. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1131 OPEN_METH = { |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1132 "tar": "taropen", # uncompressed tar |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1133 "gz": "gzopen", # gzip compressed tar |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1134 "bz2": "bz2open" # bzip2 compressed tar |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1135 } |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1136 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1137 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1138 # The public methods which TarFile provides: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1139 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1140 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1141 """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
|
1142 appended to the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1143 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1144 if self.closed: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1145 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1146 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1147 if self._mode in "aw": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1148 self.fileobj.write(NUL * (BLOCKSIZE * 2)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1149 self.offset += (BLOCKSIZE * 2) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1150 # 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
|
1151 # (like option -b20 for tar does) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1152 blocks, remainder = divmod(self.offset, RECORDSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1153 if remainder > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1154 self.fileobj.write(NUL * (RECORDSIZE - remainder)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1155 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1156 if not self._extfileobj: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1157 self.fileobj.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1158 self.closed = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1159 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1160 def getmember(self, name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1161 """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
|
1162 found in the archive, KeyError is raised. If a member occurs more |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1163 than once in the archive, its last occurence is assumed to be the |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1164 most up-to-date version. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1165 """ |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1166 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
|
1167 if tarinfo is None: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1168 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
|
1169 return tarinfo |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1170 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1171 def getmembers(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1172 """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
|
1173 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
|
1174 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1175 self._check() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1176 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
|
1177 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
|
1178 # scan the whole archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1179 return self.members |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1180 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1181 def getnames(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1182 """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
|
1183 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
|
1184 """ |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1185 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
|
1186 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1187 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
|
1188 """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
|
1189 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
|
1190 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
|
1191 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
|
1192 file in the archive. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1193 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1194 self._check("aw") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1195 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1196 # 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
|
1197 # fileobj's real name. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1198 if fileobj is not None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1199 name = fileobj.name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1200 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1201 # 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
|
1202 # 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
|
1203 # 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
|
1204 if arcname is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1205 arcname = name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1206 arcname = normpath(arcname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1207 drv, arcname = os.path.splitdrive(arcname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1208 while arcname[0:1] == "/": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1209 arcname = arcname[1:] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1210 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1211 # Now, fill the TarInfo object with |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1212 # information specific for the file. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1213 tarinfo = TarInfo() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1214 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1215 # 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
|
1216 # and if symlinks shall be resolved. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1217 if fileobj is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1218 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
|
1219 statres = os.lstat(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1220 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1221 statres = os.stat(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1222 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1223 statres = os.fstat(fileobj.fileno()) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1224 linkname = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1225 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1226 stmd = statres.st_mode |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1227 if stat.S_ISREG(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1228 inode = (statres.st_ino, statres.st_dev) |
|
35735
ea6808ba1637
Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35532
diff
changeset
|
1229 if not self.dereference and \ |
|
ea6808ba1637
Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35532
diff
changeset
|
1230 statres.st_nlink > 1 and inode in self.inodes: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1231 # 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
|
1232 # archived file? |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1233 type = LNKTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1234 linkname = self.inodes[inode] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1235 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1236 # 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
|
1237 # For win32 it is always 0. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1238 type = REGTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1239 if inode[0]: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1240 self.inodes[inode] = arcname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1241 elif stat.S_ISDIR(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1242 type = DIRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1243 if arcname[-1:] != "/": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1244 arcname += "/" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1245 elif stat.S_ISFIFO(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1246 type = FIFOTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1247 elif stat.S_ISLNK(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1248 type = SYMTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1249 linkname = os.readlink(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1250 elif stat.S_ISCHR(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1251 type = CHRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1252 elif stat.S_ISBLK(stmd): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1253 type = BLKTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1254 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1255 return None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1256 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1257 # Fill the TarInfo object with all |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1258 # 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
|
1259 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
|
1260 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
|
1261 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
|
1262 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
|
1263 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
|
1264 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
|
1265 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
|
1266 tarinfo.size = 0L |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1267 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
|
1268 tarinfo.type = type |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1269 tarinfo.linkname = linkname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1270 if pwd: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1271 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1272 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
|
1273 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1274 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1275 if grp: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1276 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1277 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
|
1278 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1279 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1280 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1281 if type in (CHRTYPE, BLKTYPE): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1282 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
|
1283 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
|
1284 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
|
1285 return tarinfo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1286 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1287 def list(self, verbose=True): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1288 """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
|
1289 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
|
1290 output is produced. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1291 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1292 self._check() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1293 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1294 for tarinfo in self: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1295 if verbose: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1296 print filemode(tarinfo.mode), |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1297 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
|
1298 tarinfo.gname or tarinfo.gid), |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1299 if tarinfo.ischr() or tarinfo.isblk(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1300 print "%10s" % ("%d,%d" \ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1301 % (tarinfo.devmajor, tarinfo.devminor)), |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1302 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1303 print "%10d" % tarinfo.size, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1304 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
|
1305 % time.localtime(tarinfo.mtime)[:6], |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1306 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1307 print tarinfo.name, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1308 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1309 if verbose: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1310 if tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1311 print "->", tarinfo.linkname, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1312 if tarinfo.islnk(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1313 print "link to", tarinfo.linkname, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1314 print |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1315 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1316 def add(self, name, arcname=None, recursive=True): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1317 """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
|
1318 (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
|
1319 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
|
1320 Directories are added recursively by default. This can be avoided by |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1321 setting `recursive' to False. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1322 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1323 self._check("aw") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1324 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1325 if arcname is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1326 arcname = name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1327 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1328 # Skip if somebody tries to archive the archive... |
|
35403
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1329 if self.name is not None \ |
|
8837ddfaad45
Revert previous checkin.
Martin v. Löwis <martin@v.loewis.de>
parents:
35401
diff
changeset
|
1330 and os.path.abspath(name) == os.path.abspath(self.name): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1331 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
|
1332 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1333 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1334 # 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
|
1335 # working directory. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1336 if name == ".": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1337 if recursive: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1338 if arcname == ".": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1339 arcname = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1340 for f in os.listdir("."): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1341 self.add(f, os.path.join(arcname, f)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1342 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1343 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1344 self._dbg(1, name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1345 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1346 # 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
|
1347 tarinfo = self.gettarinfo(name, arcname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1348 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1349 if tarinfo is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1350 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
|
1351 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1352 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1353 # 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
|
1354 if tarinfo.isreg(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1355 f = file(name, "rb") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1356 self.addfile(tarinfo, f) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1357 f.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1358 |
|
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
|
1359 elif tarinfo.isdir(): |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1360 self.addfile(tarinfo) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1361 if recursive: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1362 for f in os.listdir(name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1363 self.add(os.path.join(name, f), os.path.join(arcname, f)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1364 |
|
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
|
1365 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
|
1366 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
|
1367 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1368 def addfile(self, tarinfo, fileobj=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1369 """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
|
1370 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
|
1371 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
|
1372 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
|
1373 '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
|
1374 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1375 self._check("aw") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1376 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1377 tarinfo.name = normpath(tarinfo.name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1378 if tarinfo.isdir(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1379 # directories should end with '/' |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1380 tarinfo.name += "/" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1381 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1382 if tarinfo.linkname: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1383 tarinfo.linkname = normpath(tarinfo.linkname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1384 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1385 if tarinfo.size > MAXSIZE_MEMBER: |
|
32770
a3b1d6c99e46
SF #918101, allow files >= 8 GB using GNU extension
Neal Norwitz <nnorwitz@gmail.com>
parents:
32769
diff
changeset
|
1386 if self.posix: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1387 raise ValueError("file is too large (>= 8 GB)") |
|
32770
a3b1d6c99e46
SF #918101, allow files >= 8 GB using GNU extension
Neal Norwitz <nnorwitz@gmail.com>
parents:
32769
diff
changeset
|
1388 else: |
|
a3b1d6c99e46
SF #918101, allow files >= 8 GB using GNU extension
Neal Norwitz <nnorwitz@gmail.com>
parents:
32769
diff
changeset
|
1389 self._dbg(2, "tarfile: Created GNU tar largefile header") |
|
a3b1d6c99e46
SF #918101, allow files >= 8 GB using GNU extension
Neal Norwitz <nnorwitz@gmail.com>
parents:
32769
diff
changeset
|
1390 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1391 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1392 if len(tarinfo.linkname) > LENGTH_LINK: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1393 if self.posix: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1394 raise ValueError("linkname is too long (>%d)" % (LENGTH_LINK)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1395 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1396 self._create_gnulong(tarinfo.linkname, GNUTYPE_LONGLINK) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1397 tarinfo.linkname = tarinfo.linkname[:LENGTH_LINK -1] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1398 self._dbg(2, "tarfile: Created GNU tar extension LONGLINK") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1399 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1400 if len(tarinfo.name) > LENGTH_NAME: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1401 if self.posix: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1402 prefix = tarinfo.name[:LENGTH_PREFIX + 1] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1403 while prefix and prefix[-1] != "/": |
|
27507
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27098
diff
changeset
|
1404 prefix = prefix[:-1] |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1405 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1406 name = tarinfo.name[len(prefix):] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1407 prefix = prefix[:-1] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1408 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1409 if not prefix or len(name) > LENGTH_NAME: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1410 raise ValueError("name is too long (>%d)" % (LENGTH_NAME)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1411 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1412 tarinfo.name = name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1413 tarinfo.prefix = prefix |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1414 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1415 self._create_gnulong(tarinfo.name, GNUTYPE_LONGNAME) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1416 tarinfo.name = tarinfo.name[:LENGTH_NAME - 1] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1417 self._dbg(2, "tarfile: Created GNU tar extension LONGNAME") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1418 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1419 self.fileobj.write(tarinfo.tobuf(self.posix)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1420 self.offset += BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1421 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1422 # 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
|
1423 if fileobj is not None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1424 copyfileobj(fileobj, self.fileobj, tarinfo.size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1425 blocks, remainder = divmod(tarinfo.size, BLOCKSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1426 if remainder > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1427 self.fileobj.write(NUL * (BLOCKSIZE - remainder)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1428 blocks += 1 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1429 self.offset += blocks * BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1430 |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1431 self.members.append(tarinfo) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1432 |
|
34688
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1433 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
|
1434 """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
|
1435 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
|
1436 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
|
1437 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
|
1438 list returned by getmembers(). |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1439 """ |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1440 directories = [] |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1441 |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1442 if members is None: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1443 members = self |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1444 |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1445 for tarinfo in members: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1446 if tarinfo.isdir(): |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1447 # Extract directory with a safe mode, so that |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1448 # all files below can be extracted as well. |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1449 try: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1450 os.makedirs(os.path.join(path, tarinfo.name), 0777) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1451 except EnvironmentError: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1452 pass |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1453 directories.append(tarinfo) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1454 else: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1455 self.extract(tarinfo, path) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1456 |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1457 # Reverse sort directories. |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1458 directories.sort(lambda a, b: cmp(a.name, b.name)) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1459 directories.reverse() |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1460 |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1461 # 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
|
1462 for tarinfo in directories: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1463 path = os.path.join(path, tarinfo.name) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1464 try: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1465 self.chown(tarinfo, path) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1466 self.utime(tarinfo, path) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1467 self.chmod(tarinfo, path) |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1468 except ExtractError, e: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1469 if self.errorlevel > 1: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1470 raise |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1471 else: |
|
1ac24e9183e5
Patch #1043890: tarfile: add extractall() method.
Martin v. Löwis <martin@v.loewis.de>
parents:
34680
diff
changeset
|
1472 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
|
1473 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1474 def extract(self, member, path=""): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1475 """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
|
1476 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
|
1477 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
|
1478 specify a different directory using `path'. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1479 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1480 self._check("r") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1481 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1482 if isinstance(member, TarInfo): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1483 tarinfo = member |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1484 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1485 tarinfo = self.getmember(member) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1486 |
|
32769
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
1487 # 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
|
1488 if tarinfo.islnk(): |
|
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
1489 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
|
1490 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1491 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1492 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
|
1493 except EnvironmentError, e: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1494 if self.errorlevel > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1495 raise |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1496 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1497 if e.filename is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1498 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
|
1499 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1500 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
|
1501 except ExtractError, e: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1502 if self.errorlevel > 1: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1503 raise |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1504 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1505 self._dbg(1, "tarfile: %s" % e) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1506 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1507 def extractfile(self, member): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1508 """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
|
1509 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
|
1510 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
|
1511 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
|
1512 the above, None is returned. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1513 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
|
1514 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
|
1515 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1516 self._check("r") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1517 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1518 if isinstance(member, TarInfo): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1519 tarinfo = member |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1520 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1521 tarinfo = self.getmember(member) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1522 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1523 if tarinfo.isreg(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1524 return self.fileobject(self, tarinfo) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1525 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1526 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
|
1527 # 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
|
1528 # regular file. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1529 return self.fileobject(self, tarinfo) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1530 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1531 elif tarinfo.islnk() or tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1532 if isinstance(self.fileobj, _Stream): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1533 # 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
|
1534 # 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
|
1535 # stream of tar blocks. |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1536 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
|
1537 else: |
|
35276
4adbd331d335
Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents:
34818
diff
changeset
|
1538 # 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
|
1539 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
|
1540 tarinfo)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1541 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1542 # 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
|
1543 # 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
|
1544 return None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1545 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1546 def _extract_member(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1547 """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
|
1548 file called targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1549 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1550 # 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
|
1551 # and build the destination pathname, replacing |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1552 # forward slashes to platform specific separators. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1553 if targetpath[-1:] == "/": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1554 targetpath = targetpath[:-1] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1555 targetpath = os.path.normpath(targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1556 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1557 # Create all upper directories. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1558 upperdirs = os.path.dirname(targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1559 if upperdirs and not os.path.exists(upperdirs): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1560 ti = TarInfo() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1561 ti.name = upperdirs |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1562 ti.type = DIRTYPE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1563 ti.mode = 0777 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1564 ti.mtime = tarinfo.mtime |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1565 ti.uid = tarinfo.uid |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1566 ti.gid = tarinfo.gid |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1567 ti.uname = tarinfo.uname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1568 ti.gname = tarinfo.gname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1569 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1570 self._extract_member(ti, ti.name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1571 except: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1572 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1573 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1574 if tarinfo.islnk() or tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1575 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
|
1576 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1577 self._dbg(1, tarinfo.name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1578 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1579 if tarinfo.isreg(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1580 self.makefile(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1581 elif tarinfo.isdir(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1582 self.makedir(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1583 elif tarinfo.isfifo(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1584 self.makefifo(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1585 elif tarinfo.ischr() or tarinfo.isblk(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1586 self.makedev(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1587 elif tarinfo.islnk() or tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1588 self.makelink(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1589 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
|
1590 self.makeunknown(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1591 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1592 self.makefile(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1593 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1594 self.chown(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1595 if not tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1596 self.chmod(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1597 self.utime(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1598 |
|
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 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
|
1601 # _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
|
1602 # subclass to implement other functionality. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1603 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1604 def makedir(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1605 """Make a directory called targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1606 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1607 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1608 os.mkdir(targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1609 except EnvironmentError, e: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1610 if e.errno != errno.EEXIST: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1611 raise |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1612 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1613 def makefile(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1614 """Make a file called targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1615 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1616 source = self.extractfile(tarinfo) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1617 target = file(targetpath, "wb") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1618 copyfileobj(source, target) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1619 source.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1620 target.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1621 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1622 def makeunknown(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1623 """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
|
1624 at targetpath. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1625 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1626 self.makefile(tarinfo, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1627 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
|
1628 "extracted as regular file." % tarinfo.type) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1629 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1630 def makefifo(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1631 """Make a fifo called targetpath. |
|
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 if hasattr(os, "mkfifo"): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1634 os.mkfifo(targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1635 else: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1636 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
|
1637 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1638 def makedev(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1639 """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
|
1640 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1641 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
|
1642 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
|
1643 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1644 mode = tarinfo.mode |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1645 if tarinfo.isblk(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1646 mode |= stat.S_IFBLK |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1647 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1648 mode |= stat.S_IFCHR |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1649 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1650 os.mknod(targetpath, mode, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1651 os.makedev(tarinfo.devmajor, tarinfo.devminor)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1652 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1653 def makelink(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1654 """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
|
1655 (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
|
1656 instead of a link. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1657 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1658 linkpath = tarinfo.linkname |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1659 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1660 if tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1661 os.symlink(linkpath, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1662 else: |
|
32769
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
1663 # See extract(). |
|
983a91b50f6e
SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents:
32768
diff
changeset
|
1664 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
|
1665 except AttributeError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1666 if tarinfo.issym(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1667 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
|
1668 linkpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1669 linkpath = normpath(linkpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1670 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1671 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1672 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
|
1673 except (EnvironmentError, KeyError), e: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1674 linkpath = os.path.normpath(linkpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1675 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1676 shutil.copy2(linkpath, targetpath) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1677 except EnvironmentError, e: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1678 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
|
1679 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1680 def chown(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1681 """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
|
1682 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1683 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
|
1684 # 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
|
1685 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1686 g = grp.getgrnam(tarinfo.gname)[2] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1687 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1688 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1689 g = grp.getgrgid(tarinfo.gid)[2] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1690 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1691 g = os.getgid() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1692 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1693 u = pwd.getpwnam(tarinfo.uname)[2] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1694 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1695 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1696 u = pwd.getpwuid(tarinfo.uid)[2] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1697 except KeyError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1698 u = os.getuid() |
|
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 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
|
1701 os.lchown(targetpath, u, g) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1702 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
|
1703 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
|
1704 os.chown(targetpath, u, g) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1705 except EnvironmentError, e: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1706 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
|
1707 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1708 def chmod(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1709 """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
|
1710 """ |
|
28214
d4b262fbd04a
Test that os.utime and os.chmod actually exist before using them.
Jack Jansen <jack.jansen@cwi.nl>
parents:
27972
diff
changeset
|
1711 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
|
1712 try: |
|
d4b262fbd04a
Test that os.utime and os.chmod actually exist before using them.
Jack Jansen <jack.jansen@cwi.nl>
parents:
27972
diff
changeset
|
1713 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
|
1714 except EnvironmentError, e: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1715 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
|
1716 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1717 def utime(self, tarinfo, targetpath): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1718 """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
|
1719 """ |
|
28214
d4b262fbd04a
Test that os.utime and os.chmod actually exist before using them.
Jack Jansen <jack.jansen@cwi.nl>
parents:
27972
diff
changeset
|
1720 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
|
1721 return |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1722 if sys.platform == "win32" and tarinfo.isdir(): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1723 # According to msdn.microsoft.com, it is an error (EACCES) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1724 # to use utime() on directories. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1725 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1726 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1727 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
|
1728 except EnvironmentError, e: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1729 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
|
1730 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1731 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1732 def next(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1733 """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
|
1734 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
|
1735 available. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1736 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1737 self._check("ra") |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1738 if self.firstmember is not None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1739 m = self.firstmember |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1740 self.firstmember = None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1741 return m |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1742 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1743 # Read the next block. |
| 32604 | 1744 self.fileobj.seek(self.offset) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1745 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1746 buf = self.fileobj.read(BLOCKSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1747 if not buf: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1748 return None |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1749 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1750 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1751 tarinfo = TarInfo.frombuf(buf) |
|
38222
b0ba4e8229f7
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38208
diff
changeset
|
1752 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1753 # Set the TarInfo object's offset to the current position of the |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1754 # TarFile and set self.offset to the position where the data blocks |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1755 # should begin. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1756 tarinfo.offset = self.offset |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1757 self.offset += BLOCKSIZE |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1758 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1759 tarinfo = self.proc_member(tarinfo) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1760 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1761 except ValueError, e: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1762 if self.ignore_zeros: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1763 self._dbg(2, "0x%X: empty or invalid block: %s" % |
|
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1764 (self.offset, e)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1765 self.offset += BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1766 continue |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1767 else: |
| 32604 | 1768 if self.offset == 0: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1769 raise ReadError("empty, unreadable or compressed " |
|
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1770 "file: %s" % e) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1771 return None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1772 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1773 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1774 # Some old tar programs represent a directory as a regular |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1775 # file with a trailing slash. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1776 if tarinfo.isreg() and tarinfo.name.endswith("/"): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1777 tarinfo.type = DIRTYPE |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1778 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1779 # The prefix field is used for filenames > 100 in |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1780 # the POSIX standard. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1781 # name = prefix + '/' + name |
|
38436
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
1782 tarinfo.name = normpath(os.path.join(tarinfo.prefix.rstrip(NUL), |
|
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
1783 tarinfo.name)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1784 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1785 # Directory names should have a '/' at the end. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1786 if tarinfo.isdir(): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1787 tarinfo.name += "/" |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1788 |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1789 self.members.append(tarinfo) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1790 return tarinfo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1791 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1792 #-------------------------------------------------------------------------- |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1793 # The following are methods that are called depending on the type of a |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1794 # member. The entry point is proc_member() which is called with a TarInfo |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1795 # object created from the header block from the current offset. The |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1796 # proc_member() method can be overridden in a subclass to add custom |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1797 # proc_*() methods. A proc_*() method MUST implement the following |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1798 # operations: |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1799 # 1. Set tarinfo.offset_data to the position where the data blocks begin, |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1800 # if there is data that follows. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1801 # 2. Set self.offset to the position where the next member's header will |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1802 # begin. |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1803 # 3. Return tarinfo or another valid TarInfo object. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1804 def proc_member(self, tarinfo): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1805 """Choose the right processing method for tarinfo depending |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1806 on its type and call it. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1807 """ |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1808 if tarinfo.type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1809 return self.proc_gnulong(tarinfo) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1810 elif tarinfo.type == GNUTYPE_SPARSE: |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1811 return self.proc_sparse(tarinfo) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1812 else: |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1813 return self.proc_builtin(tarinfo) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1814 |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1815 def proc_builtin(self, tarinfo): |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1816 """Process a builtin type member or an unknown member |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1817 which will be treated as a regular file. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1818 """ |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1819 tarinfo.offset_data = self.offset |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1820 if tarinfo.isreg() or tarinfo.type not in SUPPORTED_TYPES: |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1821 # Skip the following data blocks. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1822 self.offset += self._block(tarinfo.size) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1823 return tarinfo |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1824 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1825 def proc_gnulong(self, tarinfo): |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1826 """Process the blocks that hold a GNU longname |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1827 or longlink member. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1828 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1829 buf = "" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1830 count = tarinfo.size |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1831 while count > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1832 block = self.fileobj.read(BLOCKSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1833 buf += block |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1834 self.offset += BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1835 count -= BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1836 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1837 # Fetch the next header and process it. |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1838 b = self.fileobj.read(BLOCKSIZE) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1839 t = TarInfo.frombuf(b) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1840 t.offset = self.offset |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1841 self.offset += BLOCKSIZE |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1842 next = self.proc_member(t) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1843 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1844 # Patch the TarInfo object from the next header with |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1845 # the longname information. |
| 32604 | 1846 next.offset = tarinfo.offset |
| 1847 if tarinfo.type == GNUTYPE_LONGNAME: | |
|
38436
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
1848 next.name = buf.rstrip(NUL) |
| 32604 | 1849 elif tarinfo.type == GNUTYPE_LONGLINK: |
|
38436
30e4ea423369
Always close BZ2Proxy object. Remove unnecessary struct usage.
Georg Brandl <georg@python.org>
parents:
38222
diff
changeset
|
1850 next.linkname = buf.rstrip(NUL) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1851 |
| 32604 | 1852 return next |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1853 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1854 def proc_sparse(self, tarinfo): |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1855 """Process a GNU sparse header plus extra headers. |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1856 """ |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1857 buf = tarinfo.buf |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1858 sp = _ringbuffer() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1859 pos = 386 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1860 lastpos = 0L |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1861 realpos = 0L |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1862 # There are 4 possible sparse structs in the |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1863 # first header. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1864 for i in xrange(4): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1865 try: |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1866 offset = nti(buf[pos:pos + 12]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1867 numbytes = nti(buf[pos + 12:pos + 24]) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1868 except ValueError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1869 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1870 if offset > lastpos: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1871 sp.append(_hole(lastpos, offset - lastpos)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1872 sp.append(_data(offset, numbytes, realpos)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1873 realpos += numbytes |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1874 lastpos = offset + numbytes |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1875 pos += 24 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1876 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1877 isextended = ord(buf[482]) |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1878 origsize = nti(buf[483:495]) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1879 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1880 # If the isextended flag is given, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1881 # there are extra headers to process. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1882 while isextended == 1: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1883 buf = self.fileobj.read(BLOCKSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1884 self.offset += BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1885 pos = 0 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1886 for i in xrange(21): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1887 try: |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1888 offset = nti(buf[pos:pos + 12]) |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1889 numbytes = nti(buf[pos + 12:pos + 24]) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1890 except ValueError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1891 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1892 if offset > lastpos: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1893 sp.append(_hole(lastpos, offset - lastpos)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1894 sp.append(_data(offset, numbytes, realpos)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1895 realpos += numbytes |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1896 lastpos = offset + numbytes |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1897 pos += 24 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1898 isextended = ord(buf[504]) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1899 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1900 if lastpos < origsize: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1901 sp.append(_hole(lastpos, origsize - lastpos)) |
|
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 tarinfo.sparse = sp |
|
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 tarinfo.offset_data = self.offset |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1906 self.offset += self._block(tarinfo.size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1907 tarinfo.size = origsize |
| 32604 | 1908 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1909 # Clear the prefix field so that it is not used |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1910 # as a pathname in next(). |
|
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1911 tarinfo.prefix = "" |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1912 |
|
38146
f61c892cadac
Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents:
35804
diff
changeset
|
1913 return tarinfo |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1914 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1915 #-------------------------------------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1916 # Little helper methods: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1917 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1918 def _block(self, count): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1919 """Round up a byte count by BLOCKSIZE and return it, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1920 e.g. _block(834) => 1024. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1921 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1922 blocks, remainder = divmod(count, BLOCKSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1923 if remainder: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1924 blocks += 1 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1925 return blocks * BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1926 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1927 def _getmember(self, name, tarinfo=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1928 """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
|
1929 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
|
1930 """ |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1931 # 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
|
1932 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
|
1933 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1934 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
|
1935 end = len(members) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1936 else: |
|
33507
725c44421a42
Patch #1029061: Always extract member names from the tarinfo.
Martin v. Löwis <martin@v.loewis.de>
parents:
33263
diff
changeset
|
1937 end = members.index(tarinfo) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1938 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1939 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
|
1940 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
|
1941 return members[i] |
| 32604 | 1942 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1943 def _load(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1944 """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
|
1945 members. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1946 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1947 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1948 tarinfo = self.next() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1949 if tarinfo is None: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1950 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1951 self._loaded = True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1952 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1953 def _check(self, mode=None): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1954 """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
|
1955 corresponds to TarFile's mode. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1956 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1957 if self.closed: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1958 raise IOError("%s is closed" % self.__class__.__name__) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1959 if mode is not None and self._mode not in mode: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
1960 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
|
1961 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1962 def __iter__(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1963 """Provide an iterator object. |
|
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 self._loaded: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1966 return iter(self.members) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1967 else: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1968 return TarIter(self) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1969 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1970 def _create_gnulong(self, name, type): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1971 """Write a GNU longname/longlink member to the TarFile. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1972 It consists of an extended tar header, with the length |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1973 of the longname as size, followed by data blocks, |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1974 which contain the longname as a null terminated string. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1975 """ |
|
32768
ffa8ab0081f2
SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents:
32604
diff
changeset
|
1976 name += NUL |
|
ffa8ab0081f2
SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents:
32604
diff
changeset
|
1977 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1978 tarinfo = TarInfo() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1979 tarinfo.name = "././@LongLink" |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1980 tarinfo.type = type |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1981 tarinfo.mode = 0 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1982 tarinfo.size = len(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1983 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1984 # write extended header |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1985 self.fileobj.write(tarinfo.tobuf()) |
|
32768
ffa8ab0081f2
SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents:
32604
diff
changeset
|
1986 self.offset += BLOCKSIZE |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1987 # write name blocks |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1988 self.fileobj.write(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1989 blocks, remainder = divmod(tarinfo.size, BLOCKSIZE) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1990 if remainder > 0: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1991 self.fileobj.write(NUL * (BLOCKSIZE - remainder)) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1992 blocks += 1 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1993 self.offset += blocks * BLOCKSIZE |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1994 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1995 def _dbg(self, level, msg): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1996 """Write debugging output to sys.stderr. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1997 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1998 if level <= self.debug: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
1999 print >> sys.stderr, msg |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2000 # class TarFile |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2001 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2002 class TarIter: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2003 """Iterator Class. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2004 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2005 for tarinfo in TarFile(...): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2006 suite... |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2007 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2008 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2009 def __init__(self, tarfile): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2010 """Construct a TarIter object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2011 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2012 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
|
2013 self.index = 0 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2014 def __iter__(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2015 """Return iterator object. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2016 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2017 return self |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2018 def next(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2019 """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
|
2020 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
|
2021 """ |
|
34680
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2022 # 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
|
2023 # 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
|
2024 # 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
|
2025 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
|
2026 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
|
2027 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
|
2028 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
|
2029 raise StopIteration |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2030 else: |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2031 try: |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2032 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
|
2033 except IndexError: |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2034 raise StopIteration |
|
481397683eac
Patch #1103407: Properly deal with tarfile iterators when untarring
Martin v. Löwis <martin@v.loewis.de>
parents:
34649
diff
changeset
|
2035 self.index += 1 |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2036 return tarinfo |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2037 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2038 # Helper classes for sparse file support |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2039 class _section: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2040 """Base class for _data and _hole. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2041 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2042 def __init__(self, offset, size): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2043 self.offset = offset |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2044 self.size = size |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2045 def __contains__(self, offset): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2046 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
|
2047 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2048 class _data(_section): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2049 """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
|
2050 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2051 def __init__(self, offset, size, realpos): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2052 _section.__init__(self, offset, size) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2053 self.realpos = realpos |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2054 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2055 class _hole(_section): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2056 """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
|
2057 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2058 pass |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2059 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2060 class _ringbuffer(list): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2061 """Ringbuffer class which increases performance |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2062 over a regular list. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2063 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2064 def __init__(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2065 self.idx = 0 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2066 def find(self, offset): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2067 idx = self.idx |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2068 while True: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2069 item = self[idx] |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2070 if offset in item: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2071 break |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2072 idx += 1 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2073 if idx == len(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2074 idx = 0 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2075 if idx == self.idx: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2076 # End of File |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2077 return None |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2078 self.idx = idx |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2079 return item |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2080 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2081 #--------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2082 # zipfile compatible TarFile class |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2083 #--------------------------------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2084 TAR_PLAIN = 0 # zipfile.ZIP_STORED |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2085 TAR_GZIPPED = 8 # zipfile.ZIP_DEFLATED |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2086 class TarFileCompat: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2087 """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
|
2088 ZipFile class. |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2089 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2090 def __init__(self, file, mode="r", compression=TAR_PLAIN): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2091 if compression == TAR_PLAIN: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2092 self.tarfile = TarFile.taropen(file, mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2093 elif compression == TAR_GZIPPED: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2094 self.tarfile = TarFile.gzopen(file, mode) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2095 else: |
|
38208
b286ed45439d
Amendments to patch #1484695.
Georg Brandl <georg@python.org>
parents:
38185
diff
changeset
|
2096 raise ValueError("unknown compression constant") |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2097 if mode[0:1] == "r": |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2098 members = self.tarfile.getmembers() |
| 35532 | 2099 for m in members: |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2100 m.filename = m.name |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2101 m.file_size = m.size |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2102 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
|
2103 def namelist(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2104 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
|
2105 def infolist(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2106 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
|
2107 self.tarfile.getmembers()) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2108 def printdir(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2109 self.tarfile.list() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2110 def testzip(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2111 return |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2112 def getinfo(self, name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2113 return self.tarfile.getmember(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2114 def read(self, name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2115 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
|
2116 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
|
2117 self.tarfile.add(filename, arcname) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2118 def writestr(self, zinfo, bytes): |
|
34293
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2119 try: |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2120 from cStringIO import StringIO |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2121 except ImportError: |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2122 from StringIO import StringIO |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2123 import calendar |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2124 zinfo.name = zinfo.filename |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2125 zinfo.size = zinfo.file_size |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2126 zinfo.mtime = calendar.timegm(zinfo.date_time) |
|
34293
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
33810
diff
changeset
|
2127 self.tarfile.addfile(zinfo, StringIO(bytes)) |
|
27098
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2128 def close(self): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2129 self.tarfile.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2130 #class TarFileCompat |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2131 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2132 #-------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2133 # exported functions |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2134 #-------------------- |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2135 def is_tarfile(name): |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2136 """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
|
2137 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
|
2138 """ |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2139 try: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2140 t = open(name) |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2141 t.close() |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2142 return True |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2143 except TarError: |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2144 return False |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2145 |
|
f9a5ed092660
SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
2146 open = TarFile.open |
