Bug 1347582 - Use File object to obtain normalized file mode; r?dustin, glandium, gps draft
authorSwapnesh Kumar Sahoo <swapneshks@gmail.com>
Wed, 26 Jul 2017 11:51:37 +0530
changeset 615720 fd74e219f216fbe6cf639b59884f28fc5e2e0d0b
parent 615011 131e19a573e901fb4d01b471b11b7916420b9fee
child 639246 51754d07da98603805ef0a73e52636eed1fbbd82
push id70438
push userswapneshks@gmail.com
push dateWed, 26 Jul 2017 08:33:05 +0000
reviewersdustin, glandium, gps
bugs1347582
milestone56.0a1
Bug 1347582 - Use File object to obtain normalized file mode; r?dustin, glandium, gps MozReview-Commit-ID: HeDq4EwAfVS
python/mozbuild/mozpack/archive.py
--- a/python/mozbuild/mozpack/archive.py
+++ b/python/mozbuild/mozpack/archive.py
@@ -6,17 +6,17 @@ from __future__ import absolute_import
 
 import bz2
 import gzip
 import io
 import stat
 import tarfile
 
 from .files import (
-    BaseFile,
+    BaseFile, File,
 )
 
 # 2016-01-01T00:00:00+0000
 DEFAULT_MTIME = 1451606400
 
 
 def create_tar_from_files(fp, files):
     """Create a tar file deterministically.
@@ -28,22 +28,22 @@ def create_tar_from_files(fp, files):
     for writing.
 
     Only regular files can be written.
 
     FUTURE accept a filename argument (or create APIs to write files)
     """
     with tarfile.open(name='', mode='w', fileobj=fp, dereference=True) as tf:
         for archive_path, f in sorted(files.items()):
-            if isinstance(f, BaseFile):
-                ti = tarfile.TarInfo(archive_path)
-                ti.mode = f.mode or 0644
-                ti.type = tarfile.REGTYPE
-            else:
-                ti = tf.gettarinfo(f, archive_path)
+            if not isinstance(f, BaseFile):
+                f = File(f)
+
+            ti = tarfile.TarInfo(archive_path)
+            ti.mode = f.mode or 0644
+            ti.type = tarfile.REGTYPE
 
             if not ti.isreg():
                 raise ValueError('not a regular file: %s' % f)
 
             # Disallow setuid and setgid bits. This is an arbitrary restriction.
             # However, since we set uid/gid to root:root, setuid and setgid
             # would be a glaring security hole if the archive were
             # uncompressed as root.
@@ -55,24 +55,20 @@ def create_tar_from_files(fp, files):
             ti.uid = 0
             ti.gid = 0
             ti.uname = ''
             ti.gname = ''
 
             # Set mtime to a constant value.
             ti.mtime = DEFAULT_MTIME
 
-            if isinstance(f, BaseFile):
-                ti.size = f.size()
-                # tarfile wants to pass a size argument to read(). So just
-                # wrap/buffer in a proper file object interface.
-                tf.addfile(ti, f.open())
-            else:
-                with open(f, 'rb') as fh:
-                    tf.addfile(ti, fh)
+            ti.size = f.size()
+            # tarfile wants to pass a size argument to read(). So just
+            # wrap/buffer in a proper file object interface.
+            tf.addfile(ti, f.open())
 
 
 def create_tar_gz_from_files(fp, files, filename=None, compresslevel=9):
     """Create a tar.gz file deterministically from files.
 
     This is a glorified wrapper around ``create_tar_from_files`` that
     adds gzip compression.