From: Michael Tremer Date: Thu, 10 Mar 2011 14:52:10 +0000 (+0100) Subject: Fixing support for capabilities. X-Git-Tag: 0.9.3~76^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=114ac7ee3668c807c69311342d723a93377ba599;p=pakfire.git Fixing support for capabilities. This code should work with Python 2.6 as well. --- diff --git a/pakfire/packages/__init__.py b/pakfire/packages/__init__.py index af5711871..440e874bb 100644 --- a/pakfire/packages/__init__.py +++ b/pakfire/packages/__init__.py @@ -1,6 +1,7 @@ #!/usr/bin/python from binary import BinaryPackage +from file import InnerTarFile from installed import DatabasePackage, InstalledPackage from source import SourcePackage from virtual import VirtualPackage diff --git a/pakfire/packages/file.py b/pakfire/packages/file.py index 9b50aae15..81b816aa5 100644 --- a/pakfire/packages/file.py +++ b/pakfire/packages/file.py @@ -1,8 +1,10 @@ #!/usr/bin/python -import tarfile +import logging import os import re +import tarfile +import xattr import util @@ -10,6 +12,65 @@ from pakfire.errors import FileError from base import Package +class InnerTarFile(tarfile.TarFile): + SUPPORTED_XATTRS = ("security.capability",) + + def __init__(self, *args, **kwargs): + # Force the PAX format. + kwargs["format"] = tarfile.PAX_FORMAT + + tarfile.TarFile.__init__(self, *args, **kwargs) + + def add(self, name, arcname=None, recursive=None, exclude=None, filter=None): + """ + Emulate the add function with xattrs support. + """ + tarinfo = self.gettarinfo(name, arcname) + + if tarinfo.isreg(): + attrs = xattr.get_all(name) + + for attr, val in attrs: + # Skip all attrs that are not supported (e.g. selinux). + if not attr in self.SUPPORTED_XATTRS: + continue + + logging.debug("Saving xattr %s=%s from %s" % (attr, val, name)) + + tarinfo.pax_headers[attr] = val + + # Append the tar header and data to the archive. + f = tarfile.bltn_open(name, "rb") + self.addfile(tarinfo, f) + f.close() + + elif tarinfo.isdir(): + self.addfile(tarinfo) + if recursive: + for f in os.listdir(name): + self.add(os.path.join(name, f), os.path.join(arcname, f), + recursive, exclude, filter) + + else: + self.addfile(tarinfo) + + def extract(self, member, path=""): + # Extract file the normal way... + tarfile.TarFile.extract(self, member, path) + + # ...and then apply the extended attributes. + if member.pax_headers: + target = os.path.join(path, member.name) + + for attr, val in member.pax_headers.items(): + # Skip all attrs that are not supported (e.g. selinux). + if not attr in self.SUPPORTED_XATTRS: + continue + + logging.debug("Restoring xattr %s=%s to %s" % (attr, val, target)) + xattr.set(target, attr, val) + + class FilePackage(Package): """ This class is a wrapper that reads package data from the (outer) diff --git a/pakfire/packages/packager.py b/pakfire/packages/packager.py index 66f2872b7..12fb5b23c 100644 --- a/pakfire/packages/packager.py +++ b/pakfire/packages/packager.py @@ -18,50 +18,7 @@ import pakfire.compress from pakfire.constants import * from pakfire.i18n import _ - -# XXX disable because python 2.6 does not support tarfile with filter -# -#class InnerTarFile(tarfile.TarFile): -# def __init__(self, *args, **kwargs): -# # Force the pax format -# kwargs["format"] = tarfile.PAX_FORMAT -# -# if kwargs.has_key("env"): -# self.env = kwargs.pop("env") -# -# tarfile.TarFile.__init__(self, *args, **kwargs) -# -# def __filter_xattrs(self, tarinfo): -# logging.debug("Adding file: %s" % tarinfo.name) -# -# filename = self.env.chrootPath(self.env.buildroot, tarinfo.name) -# -# # xattrs do only exists for regular files. If we don't have one, -# # simply skip. -# if os.path.isfile(filename): -# for attr, value in xattr.get_all(filename): -# tarinfo.pax_headers[attr] = value -# -# logging.debug(" xattr: %s=%s" % (attr, value)) -# -# return tarinfo -# -# def add(self, *args, **kwargs): -# # Add filter for xattrs if no other filter is set. -# if not kwargs.has_key("filter") and len(args) < 5: -# kwargs["filter"] = self.__filter_xattrs -# -# tarfile.TarFile.add(self, *args, **kwargs) - -class InnerTarFile(tarfile.TarFile): - def __init__(self, *args, **kwargs): - kwargs["format"] = tarfile.PAX_FORMAT - - if kwargs.has_key("env"): - del kwargs["env"] - - tarfile.TarFile.__init__(self, *args, **kwargs) - +from file import InnerTarFile # XXX this is totally ugly and needs to be done right! @@ -130,7 +87,7 @@ class Packager(object): tar.close() def create_tarball(self, compress="xz"): - tar = InnerTarFile(self.archive_files["data.img"], mode="w", env=self.env) + tar = InnerTarFile(self.archive_files["data.img"], mode="w") includes = [] excludes = [] @@ -188,7 +145,7 @@ class Packager(object): file_tar = file_real[len(self.env.chrootPath(self.env.buildroot)) + 1:] file_tmp = os.path.join(self.tempdir, file_tar) - tar.add(file_real, arcname=file_tar, recursive=False) + tar.add(file_real, arcname=file_tar) # Record the packaged file to the filelist. filelist.write("/%s\n" % file_tar) diff --git a/pakfire/transaction.py b/pakfire/transaction.py index f77337428..b903ed72e 100644 --- a/pakfire/transaction.py +++ b/pakfire/transaction.py @@ -184,7 +184,7 @@ class ActionInstall(Action): payload = open(tempf) # Open the tarball in the package. - payload_archive = tarfile.open(fileobj=payload) + payload_archive = packages.InnerTarFile.open(fileobj=payload) members = payload_archive.getmembers() diff --git a/po/pakfire.pot b/po/pakfire.pot index dff4e14ad..aca1645a5 100644 --- a/po/pakfire.pot +++ b/po/pakfire.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2011-03-10 11:38+0100\n" +"POT-Creation-Date: 2011-03-10 15:19+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n"