def sigtar2path_iter(sigtarobj): u""" Convert signature tar file object open for reading into path iter """ tf = util.make_tarfile(u"r", sigtarobj) tf.debug = 1 for tarinfo in tf: tiname = util.get_tarinfo_name(tarinfo) for prefix in [r"signature/", r"snapshot/", r"deleted/"]: if tiname.startswith(prefix): # strip prefix and '/' from name and set it to difftype name, difftype = tiname[len(prefix):], prefix[:-1] break else: raise DiffDirException(u"Bad tarinfo name %s" % (tiname, )) if sys.version_info.major >= 3: index = tuple(util.fsencode(name).split(b"/")) else: index = tuple(name.split(b"/")) if not index[-1]: index = index[:-1] # deal with trailing /, "" ropath = ROPath(index) ropath.difftype = difftype if difftype == u"signature" or difftype == u"snapshot": ropath.init_from_tarinfo(tarinfo) if ropath.isreg(): ropath.setfileobj(tf.extractfile(tarinfo)) yield ropath sigtarobj.close()
def sigtar2path_iter(sigtarobj): """ Convert signature tar file object open for reading into path iter """ tf = util.make_tarfile("r", sigtarobj) tf.debug = 1 for tarinfo in tf: tiname = util.get_tarinfo_name(tarinfo) for prefix in ["signature/", "snapshot/", "deleted/"]: if tiname.startswith(prefix): # strip prefix and '/' from name and set it to difftype name, difftype = tiname[len(prefix):], prefix[:-1] break else: raise DiffDirException("Bad tarinfo name %s" % (tiname,)) index = tuple(name.split("/")) if not index[-1]: index = index[:-1] # deal with trailing /, "" ropath = ROPath(index) ropath.difftype = difftype if difftype == "signature" or difftype == "snapshot": ropath.init_from_tarinfo(tarinfo) if ropath.isreg(): ropath.setfileobj(tf.extractfile(tarinfo)) yield ropath sigtarobj.close()
def test_diff(self): """Test making a diff""" sel1 = selection.Select(Path("testfiles/dir1")) diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()), "testfiles/output/dir1.sigtar") sigtar_fp = open("testfiles/output/dir1.sigtar", "rb") sel2 = selection.Select(Path("testfiles/dir2")) delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp) diffdir.write_block_iter(delta_tar, "testfiles/output/dir1dir2.difftar") changed_files = ["diff/changeable_permission", "diff/regular_file", "snapshot/symbolic_link/", "deleted/deleted_file", "snapshot/directory_to_file", "snapshot/file_to_directory/"] for tarinfo in tarfile.TarFile("testfiles/output/dir1dir2.difftar", "r"): tiname = util.get_tarinfo_name(tarinfo) if tiname in changed_files: changed_files.remove(tiname) assert not changed_files, ("Following files not found:\n" + "\n".join(changed_files))
def get_index_from_tarinfo(tarinfo): u"""Return (index, difftype, multivol) pair from tarinfo object""" for prefix in [ u"snapshot/", u"diff/", u"deleted/", u"multivol_diff/", u"multivol_snapshot/" ]: tiname = util.get_tarinfo_name(tarinfo) if sys.version_info.major == 2 and isinstance(prefix, unicode): prefix = prefix.encode() if tiname.startswith(prefix): name = tiname[len(prefix):] # strip prefix if prefix.startswith(u"multivol"): if prefix == u"multivol_diff/": difftype = u"diff" else: difftype = u"snapshot" multivol = 1 name, num_subs = \ re.subn(u"(?s)^multivol_(diff|snapshot)/?(.*)/[0-9]+$", u"\\2", tiname) if num_subs != 1: raise PatchDirException(u"Unrecognized diff entry %s" % tiname) else: difftype = prefix[:-1] # strip trailing / name = tiname[len(prefix):] if name.endswith(r"/"): name = name[:-1] # strip trailing /'s multivol = 0 break else: raise PatchDirException(u"Unrecognized diff entry %s" % tiname) if name == r"." or name == r"": index = () else: if sys.version_info.major >= 3: index = tuple(util.fsencode(name).split(b"/")) else: index = tuple(name.split(b"/")) if b'..' in index: raise PatchDirException(u"Tar entry %s contains '..'. Security " u"violation" % util.fsdecode(tiname)) return (index, difftype, multivol)
def get_index_from_tarinfo(tarinfo): """Return (index, difftype, multivol) pair from tarinfo object""" for prefix in [ "snapshot/", "diff/", "deleted/", "multivol_diff/", "multivol_snapshot/" ]: tiname = util.get_tarinfo_name(tarinfo) if tiname.startswith(prefix): name = tiname[len(prefix):] # strip prefix if prefix.startswith("multivol"): if prefix == "multivol_diff/": difftype = "diff" else: difftype = "snapshot" multivol = 1 name, num_subs = \ re.subn( "(?s)^multivol_(diff|snapshot)/?(.*)/[0-9]+$", "\\2", tiname ) if num_subs != 1: raise PatchDirException(u"Unrecognized diff entry %s" % util.ufn(tiname)) else: difftype = prefix[:-1] # strip trailing / name = tiname[len(prefix):] if name.endswith("/"): name = name[:-1] # strip trailing /'s multivol = 0 break else: raise PatchDirException(u"Unrecognized diff entry %s" % util.ufn(tiname)) if name == "." or name == "": index = () else: index = tuple(name.split("/")) if '..' in index: raise PatchDirException(u"Tar entry %s contains '..'. Security " "violation" % util.ufn(tiname)) return (index, difftype, multivol)
def get_index_from_tarinfo(tarinfo): """Return (index, difftype, multivol) pair from tarinfo object""" for prefix in ["snapshot/", "diff/", "deleted/", "multivol_diff/", "multivol_snapshot/"]: tiname = util.get_tarinfo_name(tarinfo) if tiname.startswith(prefix): name = tiname[len(prefix):] # strip prefix if prefix.startswith("multivol"): if prefix == "multivol_diff/": difftype = "diff" else: difftype = "snapshot" multivol = 1 name, num_subs = \ re.subn("(?s)^multivol_(diff|snapshot)/?(.*)/[0-9]+$", "\\2", tiname) if num_subs != 1: raise PatchDirException(u"Unrecognized diff entry %s" % util.fsdecode(tiname)) else: difftype = prefix[:-1] # strip trailing / name = tiname[len(prefix):] if name.endswith("/"): name = name[:-1] # strip trailing /'s multivol = 0 break else: raise PatchDirException(u"Unrecognized diff entry %s" % util.fsdecode(tiname)) if name == "." or name == "": index = () else: index = tuple(name.split("/")) if '..' in index: raise PatchDirException(u"Tar entry %s contains '..'. Security " "violation" % util.fsdecode(tiname)) return (index, difftype, multivol)