summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2010-11-01 21:39:13 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2010-11-01 21:39:13 (GMT)
commit16f344df36a8a7d7c609eb7036bb52b8a9a47a23 (patch)
tree31a0a830b06d94a759541064c58080c768d9c4c7 /Lib
parentbbea35f194c6a2a64ce96526bd5eee37a75e8c0a (diff)
downloadcpython-16f344df36a8a7d7c609eb7036bb52b8a9a47a23.zip
cpython-16f344df36a8a7d7c609eb7036bb52b8a9a47a23.tar.gz
cpython-16f344df36a8a7d7c609eb7036bb52b8a9a47a23.tar.bz2
Issue #10184: Touch directories only once when extracting a tarfile.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tarfile.py22
-rw-r--r--Lib/test/test_tarfile.py9
2 files changed, 22 insertions, 9 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index d49e82f..fd898c7 100644
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -2131,7 +2131,8 @@ class TarFile(object):
directories.append(tarinfo)
tarinfo = copy.copy(tarinfo)
tarinfo.mode = 0o700
- self.extract(tarinfo, path)
+ # Do not set_attrs directories, as we will do that further down
+ self.extract(tarinfo, path, set_attrs=not tarinfo.isdir())
# Reverse sort directories.
directories.sort(key=lambda a: a.name)
@@ -2150,11 +2151,12 @@ class TarFile(object):
else:
self._dbg(1, "tarfile: %s" % e)
- def extract(self, member, path=""):
+ def extract(self, member, path="", set_attrs=True):
"""Extract a member from the archive to the current working directory,
using its full name. Its file information is extracted as accurately
as possible. `member' may be a filename or a TarInfo object. You can
- specify a different directory using `path'.
+ specify a different directory using `path'. File attributes (owner,
+ mtime, mode) are set unless `set_attrs' is False.
"""
self._check("r")
@@ -2168,7 +2170,8 @@ class TarFile(object):
tarinfo._link_target = os.path.join(path, tarinfo.linkname)
try:
- self._extract_member(tarinfo, os.path.join(path, tarinfo.name))
+ self._extract_member(tarinfo, os.path.join(path, tarinfo.name),
+ set_attrs=set_attrs)
except EnvironmentError as e:
if self.errorlevel > 0:
raise
@@ -2221,7 +2224,7 @@ class TarFile(object):
# blkdev, etc.), return None instead of a file object.
return None
- def _extract_member(self, tarinfo, targetpath):
+ def _extract_member(self, tarinfo, targetpath, set_attrs=True):
"""Extract the TarInfo object tarinfo to a physical
file called targetpath.
"""
@@ -2258,10 +2261,11 @@ class TarFile(object):
else:
self.makefile(tarinfo, targetpath)
- self.chown(tarinfo, targetpath)
- if not tarinfo.issym():
- self.chmod(tarinfo, targetpath)
- self.utime(tarinfo, targetpath)
+ if set_attrs:
+ self.chown(tarinfo, targetpath)
+ if not tarinfo.issym():
+ self.chmod(tarinfo, targetpath)
+ self.utime(tarinfo, targetpath)
#--------------------------------------------------------------------------
# Below are the different file methods. They are called via
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 302ee85..daf46f7 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -377,6 +377,15 @@ class MiscReadTest(CommonReadTest):
finally:
tar.close()
+ def test_extract_directory(self):
+ dirtype = "ustar/dirtype"
+ with tarfile.open(tarname, encoding="iso8859-1") as tar:
+ tarinfo = tar.getmember(dirtype)
+ tar.extract(tarinfo)
+ self.assertEqual(os.path.getmtime(dirtype), tarinfo.mtime)
+ if sys.platform != "win32":
+ self.assertEqual(os.stat(dirtype).st_mode & 0o777, 0o755)
+
def test_init_close_fobj(self):
# Issue #7341: Close the internal file object in the TarFile
# constructor in case of an error. For the test we rely on