]>
git.ipfire.org Git - people/stevee/pakfire.git/blob - python/pakfire/packages/tar.py
2 ###############################################################################
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2012 Pakfire development team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
26 log
= logging
.getLogger("pakfire")
28 import pakfire
.lzma
as lzma
29 import pakfire
.util
as util
30 from pakfire
.constants
import *
31 from pakfire
.i18n
import _
33 class InnerTarFile(tarfile
.TarFile
):
34 def __init__(self
, *args
, **kwargs
):
35 # Force the PAX format.
36 kwargs
["format"] = tarfile
.PAX_FORMAT
38 tarfile
.TarFile
.__init
__(self
, *args
, **kwargs
)
40 def add(self
, name
, arcname
=None, recursive
=None, exclude
=None, filter=None):
42 Emulate the add function with capability support.
44 tarinfo
= self
.gettarinfo(name
, arcname
)
50 caps
= util
.get_capabilities(name
)
52 log
.debug("Saving capabilities for %s: %s" % (name
, caps
))
53 tarinfo
.pax_headers
["PAKFIRE.capabilities"] = caps
55 # Append the tar header and data to the archive.
56 f
= tarfile
.bltn_open(name
, "rb")
57 self
.addfile(tarinfo
, f
)
63 for f
in os
.listdir(name
):
64 self
.add(os
.path
.join(name
, f
), os
.path
.join(arcname
, f
),
65 recursive
, exclude
, filter)
70 # Return the tar information about the file
73 def extract(self
, member
, path
=""):
74 target
= os
.path
.join(path
, member
.name
)
76 # Remove symlink targets, because tarfile cannot replace them.
83 # Extract file the normal way...
85 tarfile
.TarFile
.extract(self
, member
, path
)
87 log
.warning(_("Could not extract file: /%(src)s - %(dst)s") \
88 % { "src" : member
.name
, "dst" : e
, })
91 target
= os
.path
.join(path
, member
.name
)
93 target
= "/%s" % member
.name
95 # ...and then apply the capabilities.
96 caps
= member
.pax_headers
.get("PAKFIRE.capabilities", None)
98 log
.debug("Restoring capabilities for /%s: %s" % (member
.name
, caps
))
99 util
.set_capabilities(target
, caps
)
102 class InnerTarFileXz(InnerTarFile
):
104 def open(cls
, name
=None, mode
="r", fileobj
=None, **kwargs
):
105 fileobj
= lzma
.LZMAFile(name
, mode
, fileobj
=fileobj
)
108 t
= cls
.taropen(name
, mode
, fileobj
, **kwargs
)
109 except lzma
.LZMAError
:
111 raise tarfile
.ReadError("not an lzma file")
113 t
._extfileobj
= False