Merged revisions 74571 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r74571 | lars.gustaebel | 2009-08-28 21:23:44 +0200 (Fri, 28 Aug 2009) | 7 lines

  Issue #6054: Do not normalize stored pathnames.

  No longer use tarfile.normpath() on pathnames. Store pathnames
  unchanged, i.e. do not remove "./", "../" and "//" occurrences.
  However, still convert absolute to relative paths.
........
This commit is contained in:
Lars Gustäbel 2009-08-28 19:59:59 +00:00
parent d314e1b929
commit bfdfdda106
3 changed files with 84 additions and 29 deletions

View File

@ -309,11 +309,6 @@ def filemode(mode):
perm.append("-") perm.append("-")
return "".join(perm) return "".join(perm)
if os.sep != "/":
normpath = lambda path: os.path.normpath(path).replace(os.sep, "/")
else:
normpath = os.path.normpath
class TarError(Exception): class TarError(Exception):
"""Base exception.""" """Base exception."""
pass pass
@ -955,7 +950,7 @@ class TarInfo(object):
"""Return the TarInfo's attributes as a dictionary. """Return the TarInfo's attributes as a dictionary.
""" """
info = { info = {
"name": normpath(self.name), "name": self.name,
"mode": self.mode & 0o7777, "mode": self.mode & 0o7777,
"uid": self.uid, "uid": self.uid,
"gid": self.gid, "gid": self.gid,
@ -963,7 +958,7 @@ class TarInfo(object):
"mtime": self.mtime, "mtime": self.mtime,
"chksum": self.chksum, "chksum": self.chksum,
"type": self.type, "type": self.type,
"linkname": normpath(self.linkname) if self.linkname else "", "linkname": self.linkname,
"uname": self.uname, "uname": self.uname,
"gname": self.gname, "gname": self.gname,
"devmajor": self.devmajor, "devmajor": self.devmajor,
@ -1795,10 +1790,9 @@ class TarFile(object):
# Absolute paths are turned to relative paths. # Absolute paths are turned to relative paths.
if arcname is None: if arcname is None:
arcname = name arcname = name
arcname = normpath(arcname)
drv, arcname = os.path.splitdrive(arcname) drv, arcname = os.path.splitdrive(arcname)
while arcname[0:1] == "/": arcname = arcname.replace(os.sep, "/")
arcname = arcname[1:] arcname = arcname.lstrip("/")
# Now, fill the TarInfo object with # Now, fill the TarInfo object with
# information specific for the file. # information specific for the file.
@ -1927,16 +1921,6 @@ class TarFile(object):
self._dbg(2, "tarfile: Skipped %r" % name) self._dbg(2, "tarfile: Skipped %r" % name)
return return
# Special case: The user wants to add the current
# working directory.
if name == ".":
if recursive:
if arcname == ".":
arcname = ""
for f in os.listdir(name):
self.add(f, os.path.join(arcname, f), recursive, exclude)
return
self._dbg(1, name) self._dbg(1, name)
# Create a TarInfo object from the file. # Create a TarInfo object from the file.
@ -2103,9 +2087,8 @@ class TarFile(object):
# Fetch the TarInfo object for the given name # Fetch the TarInfo object for the given name
# and build the destination pathname, replacing # and build the destination pathname, replacing
# forward slashes to platform specific separators. # forward slashes to platform specific separators.
if targetpath[-1:] == "/": targetpath = targetpath.rstrip("/")
targetpath = targetpath[:-1] targetpath = targetpath.replace("/", os.sep)
targetpath = os.path.normpath(targetpath)
# Create all upper directories. # Create all upper directories.
upperdirs = os.path.dirname(targetpath) upperdirs = os.path.dirname(targetpath)
@ -2200,23 +2183,23 @@ class TarFile(object):
(platform limitation), we try to make a copy of the referenced file (platform limitation), we try to make a copy of the referenced file
instead of a link. instead of a link.
""" """
linkpath = tarinfo.linkname
try: try:
if tarinfo.issym(): if tarinfo.issym():
os.symlink(linkpath, targetpath) os.symlink(tarinfo.linkname, targetpath)
else: else:
# See extract(). # See extract().
os.link(tarinfo._link_target, targetpath) os.link(tarinfo._link_target, targetpath)
except AttributeError: except AttributeError:
if tarinfo.issym(): if tarinfo.issym():
linkpath = os.path.join(os.path.dirname(tarinfo.name), linkpath = os.path.dirname(tarinfo.name) + "/" + \
linkpath) tarinfo.linkname
linkpath = normpath(linkpath) else:
linkpath = tarinfo.linkname
try: try:
self._extract_member(self.getmember(linkpath), targetpath) self._extract_member(self.getmember(linkpath), targetpath)
except (EnvironmentError, KeyError) as e: except (EnvironmentError, KeyError) as e:
linkpath = os.path.normpath(linkpath) linkpath = linkpath.replace("/", os.sep)
try: try:
shutil.copy2(linkpath, targetpath) shutil.copy2(linkpath, targetpath)
except EnvironmentError as e: except EnvironmentError as e:

View File

@ -659,6 +659,76 @@ class WriteTest(WriteTestBase):
finally: finally:
shutil.rmtree(tempdir) shutil.rmtree(tempdir)
# Guarantee that stored pathnames are not modified. Don't
# remove ./ or ../ or double slashes. Still make absolute
# pathnames relative.
# For details see bug #6054.
def _test_pathname(self, path, cmp_path=None, dir=False):
# Create a tarfile with an empty member named path
# and compare the stored name with the original.
foo = os.path.join(TEMPDIR, "foo")
if not dir:
open(foo, "w").close()
else:
os.mkdir(foo)
tar = tarfile.open(tmpname, self.mode)
tar.add(foo, arcname=path)
tar.close()
tar = tarfile.open(tmpname, "r")
t = tar.next()
tar.close()
if not dir:
os.remove(foo)
else:
os.rmdir(foo)
self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
def test_pathnames(self):
self._test_pathname("foo")
self._test_pathname(os.path.join("foo", ".", "bar"))
self._test_pathname(os.path.join("foo", "..", "bar"))
self._test_pathname(os.path.join(".", "foo"))
self._test_pathname(os.path.join(".", "foo", "."))
self._test_pathname(os.path.join(".", "foo", ".", "bar"))
self._test_pathname(os.path.join(".", "foo", "..", "bar"))
self._test_pathname(os.path.join(".", "foo", "..", "bar"))
self._test_pathname(os.path.join("..", "foo"))
self._test_pathname(os.path.join("..", "foo", ".."))
self._test_pathname(os.path.join("..", "foo", ".", "bar"))
self._test_pathname(os.path.join("..", "foo", "..", "bar"))
self._test_pathname("foo" + os.sep + os.sep + "bar")
self._test_pathname("foo" + os.sep + os.sep, "foo", dir=True)
def test_abs_pathnames(self):
if sys.platform == "win32":
self._test_pathname("C:\\foo", "foo")
else:
self._test_pathname("/foo", "foo")
self._test_pathname("///foo", "foo")
def test_cwd(self):
# Test adding the current working directory.
cwd = os.getcwd()
os.chdir(TEMPDIR)
try:
open("foo", "w").close()
tar = tarfile.open(tmpname, self.mode)
tar.add(".")
tar.close()
tar = tarfile.open(tmpname, "r")
for t in tar:
self.assert_(t.name == "." or t.name.startswith("./"))
tar.close()
finally:
os.chdir(cwd)
class StreamWriteTest(WriteTestBase): class StreamWriteTest(WriteTestBase):

View File

@ -65,6 +65,8 @@ C-API
Library Library
------- -------
- Issue #6054: Do not normalize stored pathnames in tarfile.
- Issue #6794: Fix Decimal.compare_total and Decimal.compare_total_mag: NaN - Issue #6794: Fix Decimal.compare_total and Decimal.compare_total_mag: NaN
payloads are now ordered by integer value rather than lexicographically. payloads are now ordered by integer value rather than lexicographically.