[Python-checkins] cpython (3.5): tarfile.open() with mode 'x' created files without an end of archive marker.

lars.gustaebel python-checkins at python.org
Wed May 27 13:08:50 CEST 2015


https://hg.python.org/cpython/rev/d91367e63ebd
changeset:   96308:d91367e63ebd
branch:      3.5
parent:      96306:a77214dbf1f3
user:        Lars Gustäbel <lars at gustaebel.de>
date:        Wed May 27 12:53:44 2015 +0200
summary:
  tarfile.open() with mode 'x' created files without an end of archive marker.

files:
  Lib/tarfile.py           |   4 ++--
  Lib/test/test_tarfile.py |  15 ++++++++++++++-
  2 files changed, 16 insertions(+), 3 deletions(-)


diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1484,7 +1484,7 @@
                     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 @@
 
         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
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -982,6 +982,19 @@
         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 @@
                    ("longlnk/" * 127) + "longlink_")
 
 
-class CreateTest(TarTest, unittest.TestCase):
+class CreateTest(WriteTestBase, unittest.TestCase):
 
     prefix = "x:"
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list