]> granicus.if.org Git - python/commitdiff
tarfile.open() with mode 'x' created files without an end of archive marker.
authorLars Gustäbel <lars@gustaebel.de>
Wed, 27 May 2015 10:53:44 +0000 (12:53 +0200)
committerLars Gustäbel <lars@gustaebel.de>
Wed, 27 May 2015 10:53:44 +0000 (12:53 +0200)
Lib/tarfile.py
Lib/test/test_tarfile.py

index 0f1c8253def610909014967b791f93fe254007b3..bf2234f63762db6756ad822b5359aa0a2c1785b4 100755 (executable)
@@ -1484,7 +1484,7 @@ class TarFile(object):
                     except HeaderError as e:
                         raise ReadError(str(e))
 
-            if self.mode in "aw":
+            if self.mode in ("a", "w", "x"):
                 self._loaded = True
 
                 if self.pax_headers:
@@ -1716,7 +1716,7 @@ class TarFile(object):
 
         self.closed = True
         try:
-            if self.mode in "aw":
+            if self.mode in ("a", "w", "x"):
                 self.fileobj.write(NUL * (BLOCKSIZE * 2))
                 self.offset += (BLOCKSIZE * 2)
                 # fill up the end with zero-blocks
index 45c30aba201bfb350384fa9e31e2e77fb0162245..66a0a61e0954fb370cfadbfafbedb5458e7daed5 100644 (file)
@@ -982,6 +982,19 @@ class WriteTestBase(TarTest):
         self.assertFalse(fobj.closed)
         self.assertEqual(data, fobj.getvalue())
 
+    def test_eof_marker(self):
+        # Make sure an end of archive marker is written (two zero blocks).
+        # tarfile insists on aligning archives to a 20 * 512 byte recordsize.
+        # So, we create an archive that has exactly 10240 bytes without the
+        # marker, and has 20480 bytes once the marker is written.
+        with tarfile.open(tmpname, self.mode) as tar:
+            t = tarfile.TarInfo("foo")
+            t.size = tarfile.RECORDSIZE - tarfile.BLOCKSIZE
+            tar.addfile(t, io.BytesIO(b"a" * t.size))
+
+        with self.open(tmpname, "rb") as fobj:
+            self.assertEqual(len(fobj.read()), tarfile.RECORDSIZE * 2)
+
 
 class WriteTest(WriteTestBase, unittest.TestCase):
 
@@ -1431,7 +1444,7 @@ class GNUWriteTest(unittest.TestCase):
                    ("longlnk/" * 127) + "longlink_")
 
 
-class CreateTest(TarTest, unittest.TestCase):
+class CreateTest(WriteTestBase, unittest.TestCase):
 
     prefix = "x:"