From 25f58f6b5acb98b58e45714384f1549b61212bb1 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 6 Dec 2006 22:21:23 +0000 Subject: [PATCH] Patch #1610437: fix a tarfile bug with long filename headers. (backport from rev. 52938) --- Lib/tarfile.py | 8 ++++++-- Lib/test/test_tarfile.py | 22 +++++++++++++--------- Misc/NEWS | 2 ++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Lib/tarfile.py b/Lib/tarfile.py index b5f9f303465..1b8f1408a79 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -857,7 +857,11 @@ class TarInfo(object): if self.name.endswith("/"): type = DIRTYPE - name = normpath(self.name) + if type in (GNUTYPE_LONGNAME, GNUTYPE_LONGLINK): + # Prevent "././@LongLink" from being normalized. + name = self.name + else: + name = normpath(self.name) if type == DIRTYPE: # directories should end with '/' @@ -913,7 +917,7 @@ class TarInfo(object): ] buf += struct.pack("%ds" % BLOCKSIZE, "".join(parts)) - chksum = calc_chksums(buf)[0] + chksum = calc_chksums(buf[-BLOCKSIZE:])[0] buf = buf[:-364] + "%06o\0" % chksum + buf[-357:] self.buf = buf return buf diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index ee83cbebd7e..2685d67819c 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -388,13 +388,6 @@ class WriteGNULongTest(unittest.TestCase): is tested as well. """ - def setUp(self): - self.tar = tarfile.open(tmpname(), "w") - self.tar.posix = False - - def tearDown(self): - self.tar.close() - def _length(self, s): blocks, remainder = divmod(len(s) + 1, 512) if remainder: @@ -423,12 +416,23 @@ class WriteGNULongTest(unittest.TestCase): tarinfo.linkname = link tarinfo.type = tarfile.LNKTYPE - self.tar.addfile(tarinfo) + tar = tarfile.open(tmpname(), "w") + tar.posix = False + tar.addfile(tarinfo) v1 = self._calc_size(name, link) - v2 = self.tar.offset + v2 = tar.offset self.assertEqual(v1, v2, "GNU longname/longlink creation failed") + tar.close() + + tar = tarfile.open(tmpname()) + member = tar.next() + self.failIf(member is None, "unable to read longname member") + self.assert_(tarinfo.name == member.name and \ + tarinfo.linkname == member.linkname, \ + "unable to read longname member") + def test_longname_1023(self): self._test(("longnam/" * 127) + "longnam") diff --git a/Misc/NEWS b/Misc/NEWS index 35960e01550..c9ca09567c1 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -110,6 +110,8 @@ Extension Modules Library ------- +- Patch #1610437: fix a tarfile bug with long filename headers. + - Patch #1472877: Fix Tix subwidget name resolution. - Patch #1594554: Always close a tkSimpleDialog on ok(), even