From: Neal Norwitz Date: Tue, 20 Jul 2004 22:07:44 +0000 (+0000) Subject: SF #857297 and 916874, improve handling of hard links when extracting X-Git-Tag: v2.4a2~177 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a4f651a2ae9288df1006c6f5a1ca922a5120dde1;p=python SF #857297 and 916874, improve handling of hard links when extracting --- diff --git a/Lib/tarfile.py b/Lib/tarfile.py index d20107ecd2..077fbee709 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1294,6 +1294,10 @@ class TarFile(object): else: tarinfo = self.getmember(member) + # Prepare the link target for makelink(). + if tarinfo.islnk(): + tarinfo._link_target = os.path.join(path, tarinfo.linkname) + try: self._extract_member(tarinfo, os.path.join(path, tarinfo.name)) except EnvironmentError, e: @@ -1466,7 +1470,8 @@ class TarFile(object): if tarinfo.issym(): os.symlink(linkpath, targetpath) else: - os.link(linkpath, targetpath) + # See extract(). + os.link(tarinfo._link_target, targetpath) except AttributeError: if tarinfo.issym(): linkpath = os.path.join(os.path.dirname(tarinfo.name), diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index bd3fd8e535..52b62048da 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -293,6 +293,24 @@ class WriteGNULongTest(unittest.TestCase): self._test(("longnam/" * 127) + "longname_", ("longlnk/" * 127) + "longlink_") +class ExtractHardlinkTest(BaseTest): + + def test_hardlink(self): + """Test hardlink extraction (bug #857297) + """ + # Prevent errors from being caught + self.tar.errorlevel = 1 + + self.tar.extract("0-REGTYPE", dirname()) + try: + # Extract 1-LNKTYPE which is a hardlink to 0-REGTYPE + self.tar.extract("1-LNKTYPE", dirname()) + except EnvironmentError, e: + import errno + if e.errno == errno.ENOENT: + self.fail("hardlink not extracted properly") + + # Gzip TestCases class ReadTestGzip(ReadTest): comp = "gz" @@ -337,6 +355,9 @@ def test_main(): WriteGNULongTest, ] + if hasattr(os, "link"): + tests.append(ExtractHardlinkTest) + if gzip: tests.extend([ ReadTestGzip, ReadStreamTestGzip, diff --git a/Misc/NEWS b/Misc/NEWS index fd6d24456e..f6792396fd 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -39,6 +39,9 @@ Extension modules Library ------- +- Bug #857297/Patch #916874. Fix an error when extracting a hard link + from a tarfile. + - Patch #846659. Fix an error in tarfile.py when using GNU longname/longlink creation.