[Python-checkins] python/dist/src/Lib/test test_tarfile.py, 1.16.2.1, 1.16.2.2

nnorwitz@users.sourceforge.net nnorwitz at users.sourceforge.net
Thu Oct 20 06:56:12 CEST 2005


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25202/Lib/test

Modified Files:
      Tag: release24-maint
	test_tarfile.py 
Log Message:
Backport:
Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)

Problem: if two files are assigned the same inode
number by the filesystem, the second one will be added
as a hardlink to the first, which means that the
content will be lost.

The patched code checks if the file's st_nlink is
greater 1. So only for files that actually have several
links pointing to them hardlinks will be created, which
is what GNU tar does.


Index: test_tarfile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_tarfile.py,v
retrieving revision 1.16.2.1
retrieving revision 1.16.2.2
diff -u -d -r1.16.2.1 -r1.16.2.2
--- test_tarfile.py	27 Aug 2005 10:08:21 -0000	1.16.2.1
+++ test_tarfile.py	20 Oct 2005 04:56:09 -0000	1.16.2.2
@@ -350,6 +350,53 @@
             if e.errno == errno.ENOENT:
                 self.fail("hardlink not extracted properly")
 
+class CreateHardlinkTest(BaseTest):
+    """Test the creation of LNKTYPE (hardlink) members in an archive.
+       In this respect tarfile.py mimics the behaviour of GNU tar: If
+       a file has a st_nlink > 1, it will be added a REGTYPE member
+       only the first time.
+    """
+
+    def setUp(self):
+        self.tar = tarfile.open(tmpname(), "w")
+
+        self.foo = os.path.join(dirname(), "foo")
+        self.bar = os.path.join(dirname(), "bar")
+
+        if os.path.exists(self.foo):
+            os.remove(self.foo)
+        if os.path.exists(self.bar):
+            os.remove(self.bar)
+
+        file(self.foo, "w").write("foo")
+        self.tar.add(self.foo)
+
+    def test_add_twice(self):
+        # If st_nlink == 1 then the same file will be added as
+        # REGTYPE every time.
+        tarinfo = self.tar.gettarinfo(self.foo)
+        self.assertEqual(tarinfo.type, tarfile.REGTYPE,
+                "add file as regular failed")
+
+    def test_add_hardlink(self):
+        # If st_nlink > 1 then the same file will be added as
+        # LNKTYPE.
+        os.link(self.foo, self.bar)
+        tarinfo = self.tar.gettarinfo(self.foo)
+        self.assertEqual(tarinfo.type, tarfile.LNKTYPE,
+                "add file as hardlink failed")
+
+        tarinfo = self.tar.gettarinfo(self.bar)
+        self.assertEqual(tarinfo.type, tarfile.LNKTYPE,
+                "add file as hardlink failed")
+
+    def test_dereference_hardlink(self):
+        self.tar.dereference = True
+        os.link(self.foo, self.bar)
+        tarinfo = self.tar.gettarinfo(self.bar)
+        self.assertEqual(tarinfo.type, tarfile.REGTYPE,
+                "dereferencing hardlink failed")
+
 
 # Gzip TestCases
 class ReadTestGzip(ReadTest):
@@ -407,6 +454,7 @@
 
     if hasattr(os, "link"):
         tests.append(ExtractHardlinkTest)
+        tests.append(CreateHardlinkTest)
 
     if gzip:
         tests.extend([



More information about the Python-checkins mailing list