1 """Unpacking archives of various formats."""
3 # Copyright (C) 2006, Thomas Leonard
4 # See the README file for details, or visit http://0install.net.
9 from tempfile
import mkdtemp
, mkstemp
11 from logging
import debug
, warn
12 from zeroinstall
import SafeException
13 from zeroinstall
.support
import find_in_path
, ro_rmtree
16 def _get_cpio_version():
18 if _cpio_version
is None:
19 _cpio_version
= os
.popen('cpio --version 2>&1').next()
20 debug("cpio version = %s", _cpio_version
)
24 gnu_cpio
= '(GNU cpio)' in _get_cpio_version()
25 debug("Is GNU cpio = %s", gnu_cpio
)
29 def _get_tar_version():
31 if _tar_version
is None:
32 _tar_version
= os
.popen('tar --version 2>&1').next().strip()
33 debug("tar version = %s", _tar_version
)
37 gnu_tar
= '(GNU tar)' in _get_tar_version()
38 debug("Is GNU tar = %s", gnu_tar
)
42 """@deprecated: should be private"""
43 recent_gnu_tar
= False
45 version
= re
.search(r
'\)\s*(\d+(\.\d+)*)', _get_tar_version())
47 version
= map(int, version
.group(1).split('.'))
48 recent_gnu_tar
= version
> [1, 13, 92]
50 warn("Failed to extract GNU tar version number")
51 debug("Recent GNU tar = %s", recent_gnu_tar
)
54 # Disabled, as Plash does not currently support fchmod(2).
56 #_pola_run = find_in_path('pola-run')
58 # info('Found pola-run: %s', _pola_run)
60 # info('pola-run not found; archive extraction will not be sandboxed')
62 def type_from_url(url
):
63 """Guess the MIME type for this resource based on its URL. Returns None if we don't know what it is."""
65 if url
.endswith('.rpm'): return 'application/x-rpm'
66 if url
.endswith('.deb'): return 'application/x-deb'
67 if url
.endswith('.tar.bz2'): return 'application/x-bzip-compressed-tar'
68 if url
.endswith('.tar.gz'): return 'application/x-compressed-tar'
69 if url
.endswith('.tar.lzma'): return 'application/x-lzma-compressed-tar' # XXX: No registered MIME type!
70 if url
.endswith('.tgz'): return 'application/x-compressed-tar'
71 if url
.endswith('.tar'): return 'application/x-tar'
72 if url
.endswith('.zip'): return 'application/zip'
73 if url
.endswith('.cab'): return 'application/vnd.ms-cab-compressed'
76 def check_type_ok(mime_type
):
77 """Check we have the needed software to extract from an archive of the given type.
78 @raise SafeException: if the needed software is not available"""
80 if mime_type
== 'application/x-rpm':
81 if not find_in_path('rpm2cpio'):
82 raise SafeException("This package looks like an RPM, but you don't have the rpm2cpio command "
83 "I need to extract it. Install the 'rpm' package first (this works even if "
84 "you're on a non-RPM-based distribution such as Debian).")
85 elif mime_type
== 'application/x-deb':
86 if not find_in_path('ar'):
87 raise SafeException("This package looks like a Debian package, but you don't have the 'ar' command "
88 "I need to extract it. Install the package containing it (sometimes called 'binutils') "
89 "first. This works even if you're on a non-Debian-based distribution such as Red Hat).")
90 elif mime_type
== 'application/x-bzip-compressed-tar':
91 if not find_in_path('bunzip2'):
92 raise SafeException("This package looks like a bzip2-compressed package, but you don't have the 'bunzip2' command "
93 "I need to extract it. Install the package containing it (it's probably called 'bzip2') "
95 elif mime_type
== 'application/zip':
96 if not find_in_path('unzip'):
97 raise SafeException("This package looks like a zip-compressed archive, but you don't have the 'unzip' command "
98 "I need to extract it. Install the package containing it first.")
99 elif mime_type
== 'application/vnd.ms-cab-compressed':
100 if not find_in_path('cabextract'):
101 raise SafeException("This package looks like a Microsoft Cabinet archive, but you don't have the 'cabextract' command "
102 "I need to extract it. Install the package containing it first.")
103 elif mime_type
== 'application/x-lzma-compressed-tar':
104 if not find_in_path('unlzma'):
105 raise SafeException("This package looks like an LZMA archive, but you don't have the 'unlzma' command "
106 "I need to extract it. Install the package containing it (it's probably called 'lzma') first.")
107 elif mime_type
in ('application/x-compressed-tar', 'application/x-tar'):
110 from zeroinstall
import version
111 raise SafeException("Unsupported archive type '%s' (for injector version %s)" % (mime_type
, version
))
113 def _exec_maybe_sandboxed(writable
, prog
, *args
):
114 """execlp prog, with (only) the 'writable' directory writable if sandboxing is available.
115 If no sandbox is available, run without a sandbox."""
116 prog_path
= find_in_path(prog
)
117 if not prog_path
: raise Exception("'%s' not found in $PATH" % prog
)
118 if _pola_run
is None:
119 os
.execlp(prog_path
, prog_path
, *args
)
120 # We have pola-shell :-)
121 pola_args
= ['--prog', prog_path
, '-f', '/']
123 pola_args
+= ['-a', a
]
125 pola_args
+= ['-fw', writable
]
126 os
.execl(_pola_run
, _pola_run
, *pola_args
)
128 def unpack_archive_over(url
, data
, destdir
, extract
= None, type = None, start_offset
= 0):
129 """Like unpack_archive, except that we unpack to a temporary directory first and
130 then move things over, checking that we're not following symlinks at each stage.
131 Use this when you want to unpack an unarchive into a directory which already has
135 tmpdir
= mkdtemp(dir = destdir
)
139 unpack_archive(url
, data
, tmpdir
, extract
, type, start_offset
)
141 stem_len
= len(tmpdir
)
142 for root
, dirs
, files
in os
.walk(tmpdir
):
143 relative_root
= root
[stem_len
+ 1:] or '.'
144 target_root
= os
.path
.join(destdir
, relative_root
)
146 info
= os
.lstat(target_root
)
149 raise # Some odd error.
151 os
.mkdir(target_root
)
153 if stat
.S_ISLNK(info
.st_mode
):
154 raise SafeException('Attempt to unpack dir over symlink "%s"!' % relative_root
)
155 elif not stat
.S_ISDIR(info
.st_mode
):
156 raise SafeException('Attempt to unpack dir over non-directory "%s"!' % relative_root
)
157 mtimes
.append((relative_root
, os
.lstat(os
.path
.join(tmpdir
, root
)).st_mtime
))
159 for s
in dirs
: # Symlinks are counted as directories
160 src
= os
.path
.join(tmpdir
, relative_root
, s
)
161 if os
.path
.islink(src
):
165 src
= os
.path
.join(tmpdir
, relative_root
, f
)
166 dest
= os
.path
.join(destdir
, relative_root
, f
)
167 if os
.path
.islink(dest
):
168 raise SafeException('Attempt to unpack file over symlink "%s"!' %
169 os
.path
.join(relative_root
, f
))
172 for path
, mtime
in mtimes
[1:]:
173 os
.utime(os
.path
.join(destdir
, path
), (mtime
, mtime
))
177 def unpack_archive(url
, data
, destdir
, extract
= None, type = None, start_offset
= 0):
178 """Unpack stream 'data' into directory 'destdir'. If extract is given, extract just
179 that sub-directory from the archive. Works out the format from the name."""
180 if type is None: type = type_from_url(url
)
181 if type is None: raise SafeException("Unknown extension (and no MIME type given) in '%s'" % url
)
182 if type == 'application/x-bzip-compressed-tar':
183 extract_tar(data
, destdir
, extract
, 'bzip2', start_offset
)
184 elif type == 'application/x-deb':
185 extract_deb(data
, destdir
, extract
, start_offset
)
186 elif type == 'application/x-rpm':
187 extract_rpm(data
, destdir
, extract
, start_offset
)
188 elif type == 'application/zip':
189 extract_zip(data
, destdir
, extract
, start_offset
)
190 elif type == 'application/x-tar':
191 extract_tar(data
, destdir
, extract
, None, start_offset
)
192 elif type == 'application/x-lzma-compressed-tar':
193 extract_tar(data
, destdir
, extract
, 'lzma', start_offset
)
194 elif type == 'application/x-compressed-tar':
195 extract_tar(data
, destdir
, extract
, 'gzip', start_offset
)
196 elif type == 'application/vnd.ms-cab-compressed':
197 extract_cab(data
, destdir
, extract
, start_offset
)
199 raise SafeException('Unknown MIME type "%s" for "%s"' % (type, url
))
201 def extract_deb(stream
, destdir
, extract
= None, start_offset
= 0):
203 raise SafeException('Sorry, but the "extract" attribute is not yet supported for Debs')
205 stream
.seek(start_offset
)
206 # ar can't read from stdin, so make a copy...
207 deb_copy_name
= os
.path
.join(destdir
, 'archive.deb')
208 deb_copy
= file(deb_copy_name
, 'w')
209 shutil
.copyfileobj(stream
, deb_copy
)
211 _extract(stream
, destdir
, ('ar', 'x', 'archive.deb', 'data.tar.gz'))
212 os
.unlink(deb_copy_name
)
213 data_name
= os
.path
.join(destdir
, 'data.tar.gz')
214 data_stream
= file(data_name
)
216 extract_tar(data_stream
, destdir
, None, 'gzip')
218 def extract_rpm(stream
, destdir
, extract
= None, start_offset
= 0):
220 raise SafeException('Sorry, but the "extract" attribute is not yet supported for RPMs')
221 fd
, cpiopath
= mkstemp('-rpm-tmp')
227 os
.dup2(stream
.fileno(), 0)
228 os
.lseek(0, start_offset
, 0)
230 _exec_maybe_sandboxed(None, 'rpm2cpio', '-')
232 traceback
.print_exc()
235 id, status
= os
.waitpid(child
, 0)
238 raise SafeException("rpm2cpio failed; can't unpack RPM archive; exit code %d" % status
)
242 args
= ['cpio', '-mid']
244 args
.append('--quiet')
246 _extract(file(cpiopath
), destdir
, args
)
247 # Set the mtime of every directory under 'tmp' to 0, since cpio doesn't
248 # preserve directory mtimes.
249 os
.path
.walk(destdir
, lambda arg
, dirname
, names
: os
.utime(dirname
, (0, 0)), None)
255 def extract_cab(stream
, destdir
, extract
, start_offset
= 0):
258 raise SafeException('Sorry, but the "extract" attribute is not yet supported for Cabinet files')
260 stream
.seek(start_offset
)
261 # cabextract can't read from stdin, so make a copy...
262 cab_copy_name
= os
.path
.join(destdir
, 'archive.cab')
263 cab_copy
= file(cab_copy_name
, 'w')
264 shutil
.copyfileobj(stream
, cab_copy
)
267 _extract(stream
, destdir
, ['cabextract', '-s', '-q', 'archive.cab'])
268 os
.unlink(cab_copy_name
)
270 def extract_zip(stream
, destdir
, extract
, start_offset
= 0):
272 # Limit the characters we accept, to avoid sending dodgy
274 if not re
.match('^[a-zA-Z0-9][- _a-zA-Z0-9.]*$', extract
):
275 raise SafeException('Illegal character in extract attribute')
277 stream
.seek(start_offset
)
278 # unzip can't read from stdin, so make a copy...
279 zip_copy_name
= os
.path
.join(destdir
, 'archive.zip')
280 zip_copy
= file(zip_copy_name
, 'w')
281 shutil
.copyfileobj(stream
, zip_copy
)
284 args
= ['unzip', '-q', '-o', 'archive.zip']
287 args
.append(extract
+ '/*')
289 _extract(stream
, destdir
, args
)
290 os
.unlink(zip_copy_name
)
293 # unzip uses extract just as a filter, so we still need to move things
294 extracted_dir
= os
.path
.join(destdir
, extract
)
295 for x
in os
.listdir(extracted_dir
):
296 os
.rename(os
.path
.join(extracted_dir
, x
), os
.path
.join(destdir
, x
))
297 os
.rmdir(extracted_dir
)
299 def extract_tar(stream
, destdir
, extract
, decompress
, start_offset
= 0):
301 # Limit the characters we accept, to avoid sending dodgy
303 if not re
.match('^[a-zA-Z0-9][- _a-zA-Z0-9.]*$', extract
):
304 raise SafeException('Illegal character in extract attribute')
306 assert decompress
in [None, 'bzip2', 'gzip', 'lzma']
311 if decompress
== 'bzip2':
312 ext_cmd
.append('--bzip2')
313 elif decompress
== 'gzip':
315 elif decompress
== 'lzma':
316 ext_cmd
.append('--use-compress-program=unlzma')
319 ext_cmd
.extend(('-x', '--no-same-owner', '--no-same-permissions'))
321 ext_cmd
.extend(('xf', '-'))
324 ext_cmd
.append(extract
)
326 _extract(stream
, destdir
, ext_cmd
, start_offset
)
328 # Since we don't have GNU tar, use python's tarfile module. This will probably
329 # be a lot slower and we do not support lzma; however, it is portable.
330 if decompress
is None:
332 elif decompress
== 'bzip2':
334 elif decompress
== 'gzip':
337 raise SafeException('GNU tar unavailable; unsupported compression format: ' + decompress
)
341 stream
.seek(start_offset
)
342 # Python 2.5.1 crashes if name is None; see Python bug #1706850
343 tar
= tarfile
.open(name
= '', mode
= rmode
, fileobj
= stream
)
345 current_umask
= os
.umask(0)
346 os
.umask(current_umask
)
353 debug("Can't get uid/gid")
355 def chmod_extract(tarinfo
):
356 # If any X bit is set, they all must be
357 if tarinfo
.mode
& 0111:
360 # Everyone gets read and write (subject to the umask)
361 # No special bits are allowed.
362 tarinfo
.mode
= ((tarinfo
.mode |
0666) & ~current_umask
) & 0777
364 # Don't change owner, even if run as root
369 tar
.extract(tarinfo
, destdir
)
371 extracted_anything
= False
375 if extract
is None or \
376 tarinfo
.name
.startswith(extract
+ '/') or \
377 tarinfo
.name
== extract
:
379 ext_dirs
.append(tarinfo
)
381 chmod_extract(tarinfo
)
382 extracted_anything
= True
384 # Due to a bug in tarfile (python versions < 2.5), we have to manually
385 # set the mtime of each directory that we extract after extracting everything.
387 for tarinfo
in ext_dirs
:
388 dirname
= os
.path
.join(destdir
, tarinfo
.name
)
389 os
.utime(dirname
, (tarinfo
.mtime
, tarinfo
.mtime
))
393 if extract
and not extracted_anything
:
394 raise SafeException('Unable to find specified file = %s in archive' % extract
)
396 def _extract(stream
, destdir
, command
, start_offset
= 0):
397 """Run execvp('command') inside destdir in a child process, with
398 stream seeked to 'start_offset' as stdin."""
403 # Some zip archives are missing timezone information; force consistent results
404 os
.environ
['TZ'] = 'GMT'
407 stream
.seek(start_offset
)
408 os
.dup2(stream
.fileno(), 0)
409 _exec_maybe_sandboxed(destdir
, *command
)
411 traceback
.print_exc()
414 id, status
= os
.waitpid(child
, 0)
417 raise SafeException('Failed to extract archive; exit code %d' % status
)