Python tarfile.DIRTYPE Examples

The following are 30 code examples of tarfile.DIRTYPE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tarfile , or try the search function .
Example #1
Source File: __init__.py    From virt-bootstrap with GNU General Public License v3.0 6 votes vote down vote up
def create_user_dirs(self, tar_handle):
        """
        Create root file system tree in tar archive.
        """
        tar_members = [
            ['dirs', tarfile.DIRTYPE],
            ['files', tarfile.REGTYPE],
        ]

        for user in self.rootfs_tree:
            for members, tar_type in tar_members:
                self.create_tar_members(
                    tar_handle,
                    self.rootfs_tree[user][members],
                    tar_type,
                    uid=self.rootfs_tree[user]['uid'],
                    gid=self.rootfs_tree[user]['gid']
                ) 
Example #2
Source File: export_util.py    From peniot with MIT License 6 votes vote down vote up
def export_files_with_tar(output_name, list_of_files, output_path="./"):
        if not output_name.endswith(".tar.gz"):
            output_name = output_name + ".tar.gz"
        tar = tarfile.open(output_path + output_name, "w:gz")
        for _file in list_of_files:
            try:
                logger.info("File {0} is added to {1}".format(_file, output_name))
                if len(_file) == 2:
                    # Create file
                    tar.add(_file[0], _file[1])
                else:
                    # Create directory
                    t = tarfile.TarInfo(_file[0])
                    t.type = tarfile.DIRTYPE
                    tar.addfile(t)
            except RuntimeError as _:
                logger.error("Error has occurred while compressing file {0}".format(_file[0]))
        tar.close() 
Example #3
Source File: test_tarfile.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def _make_test_archive(filename_1, dirname_1, filename_2):
        # the file contents to write
        fobj = io.BytesIO(b"content")

        # create a tar file with a file, a directory, and a file within that
        #  directory. Assign various .uid/.gid values to them
        items = [(filename_1, 99, 98, tarfile.REGTYPE, fobj),
                 (dirname_1,  77, 76, tarfile.DIRTYPE, None),
                 (filename_2, 88, 87, tarfile.REGTYPE, fobj),
                 ]
        with tarfile.open(tmpname, 'w') as tarfl:
            for name, uid, gid, typ, contents in items:
                t = tarfile.TarInfo(name)
                t.uid = uid
                t.gid = gid
                t.uname = 'root'
                t.gname = 'root'
                t.type = typ
                tarfl.addfile(t, contents)

        # return the full pathname to the tar file
        return tmpname 
Example #4
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def _make_test_archive(filename_1, dirname_1, filename_2):
        # the file contents to write
        fobj = io.BytesIO(b"content")

        # create a tar file with a file, a directory, and a file within that
        #  directory. Assign various .uid/.gid values to them
        items = [(filename_1, 99, 98, tarfile.REGTYPE, fobj),
                 (dirname_1,  77, 76, tarfile.DIRTYPE, None),
                 (filename_2, 88, 87, tarfile.REGTYPE, fobj),
                 ]
        with tarfile.open(tmpname, 'w') as tarfl:
            for name, uid, gid, typ, contents in items:
                t = tarfile.TarInfo(name)
                t.uid = uid
                t.gid = gid
                t.uname = 'root'
                t.gname = 'root'
                t.type = typ
                tarfl.addfile(t, contents)

        # return the full pathname to the tar file
        return tmpname 
Example #5
Source File: test_tarfile.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_extractall(self):
        # Test if extractall() correctly restores directory permissions
        # and times (see issue1735).
        if (sys.platform == "win32" or
            test_support.is_jython and os._name == 'nt'):
            # Win32 has no support for utime() on directories or
            # fine grained permissions.
            return

        fobj = StringIO.StringIO()
        tar = tarfile.open(fileobj=fobj, mode="w:")
        for name in ("foo", "foo/bar"):
            tarinfo = tarfile.TarInfo(name)
            tarinfo.type = tarfile.DIRTYPE
            tarinfo.mtime = 07606136617
            tarinfo.mode = 0755
            tar.addfile(tarinfo)
        tar.close()
        fobj.seek(0)

        TEMPDIR = os.path.join(dirname(), "extract-test")
        tar = tarfile.open(fileobj=fobj)
        tar.extractall(TEMPDIR)
        for tarinfo in tar.getmembers():
            path = os.path.join(TEMPDIR, tarinfo.name)
            self.assertEqual(tarinfo.mode, os.stat(path).st_mode & 0777)
            self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
        tar.close() 
Example #6
Source File: test_tarfile.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_v7_dirtype(self):
        # Test old style dirtype member (bug #1336623):
        # Old V7 tars create directory members using an AREGTYPE
        # header with a "/" appended to the filename field.
        tarinfo = self.tar.getmember("misc/dirtype-old-v7")
        self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
                "v7 dirtype failed") 
Example #7
Source File: test_tarfile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_v7_dirtype(self):
        # Test old style dirtype member (bug #1336623):
        # Old V7 tars create directory members using an AREGTYPE
        # header with a "/" appended to the filename field.
        tarinfo = self.tar.getmember("misc/dirtype-old-v7")
        self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
                "v7 dirtype failed") 
Example #8
Source File: test_tarfile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_read_longname(self):
        # Test reading of longname (bug #1471427).
        longname = self.subdir + "/" + "123/" * 125 + "longname"
        try:
            tarinfo = self.tar.getmember(longname)
        except KeyError:
            self.fail("longname not found")
        self.assertTrue(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") 
Example #9
Source File: ratarmount.py    From ratarmount with MIT License 5 votes vote down vote up
def _getFileInfo( self, filePath ):
        """Wrapper for _getUnionMountFileInfo, which also resolves special file version specifications in the path."""
        result = self._getUnionMountFileInfo( filePath )
        if result:
            return result

        # If no file was found, check if a special .versions folder to an existing file/folder was queried.
        result = self._decodeVersionsPathAPI( filePath )
        if not result:
            raise fuse.FuseOSError( fuse.errno.ENOENT )
        filePath, pathIsVersions, fileVersion = result

        # 2.) Check if the request was for the special .versions folder and return its contents or stats
        # At this point, filePath is assured to actually exist!
        if pathIsVersions:
            parentFileInfo, mountSource = self._getUnionMountFileInfo( filePath )
            return SQLiteIndexedTar.FileInfo(
                offset       = None                ,
                offsetheader = None                ,
                size         = 0                   ,
                mtime        = parentFileInfo.mtime,
                mode         = 0o777 | stat.S_IFDIR,
                type         = tarfile.DIRTYPE     ,
                linkname     = ""                  ,
                uid          = parentFileInfo.uid  ,
                gid          = parentFileInfo.gid  ,
                istar        = False               ,
                issparse     = False
            ), mountSource

        # 3.) At this point the request is for an actual version of a file or folder
        result = self._getUnionMountFileInfo( filePath, fileVersion = fileVersion )
        if result:
            return result

        raise fuse.FuseOSError( fuse.errno.ENOENT ) 
Example #10
Source File: test_tarfile.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_v7_dirtype(self):
        # Test old style dirtype member (bug #1336623):
        # Old V7 tars create directory members using an AREGTYPE
        # header with a "/" appended to the filename field.
        tarinfo = self.tar.getmember("misc/dirtype-old-v7")
        self.assertEqual(tarinfo.type, tarfile.DIRTYPE,
                "v7 dirtype failed") 
Example #11
Source File: test_tarfile.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_read_longname(self):
        # Test reading of longname (bug #1471427).
        longname = self.subdir + "/" + "123/" * 125 + "longname"
        try:
            tarinfo = self.tar.getmember(longname)
        except KeyError:
            self.fail("longname not found")
        self.assertNotEqual(tarinfo.type, tarfile.DIRTYPE,
                "read longname as dirtype") 
Example #12
Source File: test_tarfile.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_old_dirtype(self):
        """Test old style dirtype member (bug #1336623).
        """
        # Old tars create directory members using a REGTYPE
        # header with a "/" appended to the filename field.

        # Create an old tar style directory entry.
        filename = tmpname()
        tarinfo = tarfile.TarInfo("directory/")
        tarinfo.type = tarfile.REGTYPE

        fobj = open(filename, "w")
        fobj.write(tarinfo.tobuf())
        fobj.close()

        try:
            # Test if it is still a directory entry when
            # read back.
            tar = tarfile.open(filename)
            tarinfo = tar.getmembers()[0]
            tar.close()

            self.assert_(tarinfo.type == tarfile.DIRTYPE)
            self.assert_(tarinfo.name.endswith("/"))
        finally:
            try:
                os.unlink(filename)
            except:
                pass 
Example #13
Source File: tar.py    From fwtool.py with MIT License 5 votes vote down vote up
def _convertFileType(type):
 return {
  tarfile.REGTYPE: S_IFREG,
  tarfile.LNKTYPE: S_IFLNK,
  tarfile.SYMTYPE: S_IFLNK,
  tarfile.CHRTYPE: S_IFCHR,
  tarfile.BLKTYPE: S_IFBLK,
  tarfile.DIRTYPE: S_IFDIR,
  tarfile.FIFOTYPE: S_IFIFO,
 }.get(type, S_IFREG) 
Example #14
Source File: test_tarfile.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_1471427(self):
        """Test reading of longname (bug #1471427).
        """
        name = "test/" * 20 + "0-REGTYPE"
        try:
            tarinfo = self.tar.getmember(name)
        except KeyError:
            tarinfo = None
        self.assert_(tarinfo is not None, "longname not found")
        self.assert_(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") 
Example #15
Source File: test_tarfile.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_v7_dirtype(self):
        # Test old style dirtype member (bug #1336623):
        # Old V7 tars create directory members using an AREGTYPE
        # header with a "/" appended to the filename field.
        tarinfo = self.tar.getmember("misc/dirtype-old-v7")
        self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
                "v7 dirtype failed") 
Example #16
Source File: test_tarfile.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_read_longname(self):
        # Test reading of longname (bug #1471427).
        longname = self.subdir + "/" + "123/" * 125 + "longname"
        try:
            tarinfo = self.tar.getmember(longname)
        except KeyError:
            self.fail("longname not found")
        self.assertTrue(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") 
Example #17
Source File: test_tarfile.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_v7_dirtype(self):
        # Test old style dirtype member (bug #1336623):
        # Old V7 tars create directory members using an AREGTYPE
        # header with a "/" appended to the filename field.
        tarinfo = self.tar.getmember("misc/dirtype-old-v7")
        self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
                "v7 dirtype failed") 
Example #18
Source File: test_tarfile.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_read_longname(self):
        # Test reading of longname (bug #1471427).
        longname = self.subdir + "/" + "123/" * 125 + "longname"
        try:
            tarinfo = self.tar.getmember(longname)
        except KeyError:
            self.fail("longname not found")
        self.assertTrue(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") 
Example #19
Source File: test_tarfs.py    From pyfilesystem2 with MIT License 5 votes vote down vote up
def setUp(self):
        self.tempfile = self.tmpfs.open("test.tar", "wb+")
        with tarfile.open(mode="w", fileobj=self.tempfile) as tf:
            tf.addfile(tarfile.TarInfo("foo/bar/baz/spam.txt"), io.StringIO())
            tf.addfile(tarfile.TarInfo("./foo/eggs.bin"), io.StringIO())
            tf.addfile(tarfile.TarInfo("./foo/yolk/beans.txt"), io.StringIO())
            info = tarfile.TarInfo("foo/yolk")
            info.type = tarfile.DIRTYPE
            tf.addfile(info, io.BytesIO())
        self.tempfile.seek(0)
        self.fs = tarfs.TarFS(self.tempfile) 
Example #20
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_read_longname(self):
        # Test reading of longname (bug #1471427).
        longname = self.subdir + "/" + "123/" * 125 + "longname"
        try:
            tarinfo = self.tar.getmember(longname)
        except KeyError:
            self.fail("longname not found")
        self.assertNotEqual(tarinfo.type, tarfile.DIRTYPE,
                "read longname as dirtype") 
Example #21
Source File: test_tarfile.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_read_longname(self):
        # Test reading of longname (bug #1471427).
        longname = self.subdir + "/" + "123/" * 125 + "longname"
        try:
            tarinfo = self.tar.getmember(longname)
        except KeyError:
            self.fail("longname not found")
        self.assertTrue(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") 
Example #22
Source File: test_tarfile.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_v7_dirtype(self):
        # Test old style dirtype member (bug #1336623):
        # Old V7 tars create directory members using an AREGTYPE
        # header with a "/" appended to the filename field.
        tarinfo = self.tar.getmember("misc/dirtype-old-v7")
        self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
                "v7 dirtype failed") 
Example #23
Source File: test_tarfile.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_read_longname(self):
        # Test reading of longname (bug #1471427).
        longname = self.subdir + "/" + "123/" * 125 + "longname"
        try:
            tarinfo = self.tar.getmember(longname)
        except KeyError:
            self.fail("longname not found")
        self.assertTrue(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") 
Example #24
Source File: file_finder_git.py    From setuptools_scm with MIT License 5 votes vote down vote up
def _git_interpret_archive(fd, toplevel):
    with tarfile.open(fileobj=fd, mode="r|*") as tf:
        git_files = set()
        git_dirs = {toplevel}
        for member in tf.getmembers():
            name = os.path.normcase(member.name).replace("/", os.path.sep)
            if member.type == tarfile.DIRTYPE:
                git_dirs.add(name)
            else:
                git_files.add(name)
        return git_files, git_dirs 
Example #25
Source File: test_tarfile.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_v7_dirtype(self):
        # Test old style dirtype member (bug #1336623):
        # Old V7 tars create directory members using an AREGTYPE
        # header with a "/" appended to the filename field.
        tarinfo = self.tar.getmember("misc/dirtype-old-v7")
        self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
                "v7 dirtype failed") 
Example #26
Source File: test_tarfile.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_read_longname(self):
        # Test reading of longname (bug #1471427).
        longname = self.subdir + "/" + "123/" * 125 + "longname"
        try:
            tarinfo = self.tar.getmember(longname)
        except KeyError:
            self.fail("longname not found")
        self.assertTrue(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") 
Example #27
Source File: file_finder_git.py    From safrs with GNU General Public License v3.0 5 votes vote down vote up
def _git_ls_files_and_dirs(toplevel):
    # use git archive instead of git ls-file to honor
    # export-ignore git attribute
    cmd = ['git', 'archive', '--prefix', toplevel + os.path.sep, 'HEAD']
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=toplevel)
    tf = tarfile.open(fileobj=proc.stdout, mode='r|*')
    git_files = set()
    git_dirs = set([toplevel])
    for member in tf.getmembers():
        name = os.path.normcase(member.name).replace('/', os.path.sep)
        if member.type == tarfile.DIRTYPE:
            git_dirs.add(name)
        else:
            git_files.add(name)
    return git_files, git_dirs 
Example #28
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_v7_dirtype(self):
        # Test old style dirtype member (bug #1336623):
        # Old V7 tars create directory members using an AREGTYPE
        # header with a "/" appended to the filename field.
        tarinfo = self.tar.getmember("misc/dirtype-old-v7")
        self.assertEqual(tarinfo.type, tarfile.DIRTYPE,
                "v7 dirtype failed") 
Example #29
Source File: common.py    From quay with Apache License 2.0 5 votes vote down vote up
def tar_folder(self, name, mtime=None):
        """
        Returns tar file header data for a folder with the given name.
        """
        info = tarfile.TarInfo(name=name)
        info.type = tarfile.DIRTYPE

        if mtime is not None:
            info.mtime = mtime

        # allow the directory to be readable by non-root users
        info.mode = 0o755
        return info.tobuf() 
Example #30
Source File: utils_test.py    From appstart with Apache License 2.0 5 votes vote down vote up
def test_tar_wrapper(self):
        temp = tempfile.NamedTemporaryFile()
        tar = tarfile.open(mode='w', fileobj=temp)

        tinfo1 = tar.gettarinfo(fileobj=self.tempfile1,
                                arcname='/root/baz/foo.txt')
        tar.addfile(tinfo1, self.tempfile1)

        tinfo2 = tar.gettarinfo(fileobj=self.tempfile2,
                                arcname='/root/bar.txt')
        tar.addfile(tinfo2, self.tempfile2)

        fake_root = tarfile.TarInfo('root')
        fake_root.type = tarfile.DIRTYPE
        tar.addfile(fake_root)

        fake_baz = tarfile.TarInfo('root/baz')
        fake_baz.type = tarfile.DIRTYPE
        tar.addfile(fake_baz)

        tar.close()
        temp.seek(0)

        wrapped_tar = utils.TarWrapper(tarfile.open(mode='r', fileobj=temp))
        self.assertEqual(wrapped_tar.get_file('root/bar.txt').read(), 'bar')
        self.assertEqual(wrapped_tar.get_file('root/baz/foo.txt').read(), 'foo')
        with self.assertRaises(ValueError):
            wrapped_tar.get_file('root')

        files, dirs = wrapped_tar.list('root')
        self.assertEqual(files, ['bar.txt'])
        self.assertEqual(dirs, ['baz'])
        with self.assertRaises(ValueError):
            wrapped_tar.list('root/bar.txt')