1"""Unpacking archives of various formats.""" 2 3# Copyright (C) 2009, Thomas Leonard 4# See the README file for details, or visit http://0install.net. 5 6fromzeroinstallimport_,logger 7importos,subprocess 8importshutil 9importglob 10importtraceback 11fromtempfileimportmkdtemp,mkstemp 12importre 13importerrno 14fromzeroinstallimportSafeException 15fromzeroinstall.supportimportfind_in_path,ro_rmtree 16 17_cpio_version=None
68 69# Disabled, as Plash does not currently support fchmod(2). 70_pola_run=None 71#_pola_run = find_in_path('pola-run') 72#if _pola_run: 73# info('Found pola-run: %s', _pola_run) 74#else: 75# info('pola-run not found; archive extraction will not be sandboxed') 76
78"""Guess the MIME type for this resource based on its URL. Returns None if we don't know what it is. 79 @type url: str 80 @rtype: str | None""" 81url=url.lower() 82ifurl.endswith('.rpm'):return'application/x-rpm' 83ifurl.endswith('.deb'):return'application/x-deb' 84ifurl.endswith('.tar.bz2'):return'application/x-bzip-compressed-tar' 85ifurl.endswith('.tar.gz'):return'application/x-compressed-tar' 86ifurl.endswith('.tar.lzma'):return'application/x-lzma-compressed-tar' 87ifurl.endswith('.tar.xz'):return'application/x-xz-compressed-tar' 88ifurl.endswith('.tbz'):return'application/x-bzip-compressed-tar' 89ifurl.endswith('.tgz'):return'application/x-compressed-tar' 90ifurl.endswith('.tlz'):return'application/x-lzma-compressed-tar' 91ifurl.endswith('.txz'):return'application/x-xz-compressed-tar' 92ifurl.endswith('.tar'):return'application/x-tar' 93ifurl.endswith('.zip'):return'application/zip' 94ifurl.endswith('.cab'):return'application/vnd.ms-cab-compressed' 95ifurl.endswith('.dmg'):return'application/x-apple-diskimage' 96ifurl.endswith('.gem'):return'application/x-ruby-gem' 97returnNone
100"""Check we have the needed software to extract from an archive of the given type.101 @type mime_type: str102 @raise SafeException: if the needed software is not available"""103assertmime_type104ifmime_type=='application/x-rpm':105ifnotfind_in_path('rpm2cpio'):106raiseSafeException(_("This package looks like an RPM, but you don't have the rpm2cpio command "107"I need to extract it. Install the 'rpm' package first (this works even if "108"you're on a non-RPM-based distribution such as Debian)."))109elifmime_type=='application/x-deb':110ifnotfind_in_path('ar'):111raiseSafeException(_("This package looks like a Debian package, but you don't have the 'ar' command "112"I need to extract it. Install the package containing it (sometimes called 'binutils') "113"first. This works even if you're on a non-Debian-based distribution such as Red Hat)."))114elifmime_type=='application/x-bzip-compressed-tar':115pass# We'll fall back to Python's built-in tar.bz2 support116elifmime_type=='application/zip':117ifnotfind_in_path('unzip'):118raiseSafeException(_("This package looks like a zip-compressed archive, but you don't have the 'unzip' command "119"I need to extract it. Install the package containing it first."))120elifmime_type=='application/vnd.ms-cab-compressed':121ifnotfind_in_path('cabextract'):122raiseSafeException(_("This package looks like a Microsoft Cabinet archive, but you don't have the 'cabextract' command "123"I need to extract it. Install the package containing it first."))124elifmime_type=='application/x-apple-diskimage':125ifnotfind_in_path('hdiutil'):126raiseSafeException(_("This package looks like a Apple Disk Image, but you don't have the 'hdiutil' command "127"I need to extract it."))128elifmime_type=='application/x-lzma-compressed-tar':129pass# We can get it through Zero Install130elifmime_type=='application/x-xz-compressed-tar':131ifnotfind_in_path('unxz'):132raiseSafeException(_("This package looks like a xz-compressed package, but you don't have the 'unxz' command "133"I need to extract it. Install the package containing it (it's probably called 'xz-utils') "134"first."))135elifmime_typein('application/x-compressed-tar','application/x-tar','application/x-ruby-gem'):136pass137else:138fromzeroinstallimportversion139raiseSafeException(_("Unsupported archive type '%(type)s' (for injector version %(version)s)")%{'type':mime_type,'version':version})
142"""execlp prog, with (only) the 'writable' directory writable if sandboxing is available.143 If no sandbox is available, run without a sandbox."""144prog_path=find_in_path(prog)145ifnotprog_path:raiseException(_("'%s' not found in $PATH")%prog)146if_pola_runisNone:147os.execlp(prog_path,prog_path,*args)148# We have pola-shell :-)149pola_args=['--prog',prog_path,'-f','/']150forainargs:151pola_args+=['-a',a]152ifwritable:153pola_args+=['-fw',writable]154os.execl(_pola_run,_pola_run,*pola_args)
157"""Like unpack_archive, except that we unpack to a temporary directory first and158 then move things over, checking that we're not following symlinks at each stage.159 Use this when you want to unpack an unarchive into a directory which already has160 stuff in it.161 @type url: str162 @type data: file163 @type destdir: str164 @type extract: str | None165 @type type: str | None166 @type start_offset: int167 @note: Since 0.49, the leading "extract" component is removed (unlike unpack_archive).168 @since: 0.28"""169importstat170tmpdir=mkdtemp(dir=destdir)171assertextractisNoneoros.sepnotinextract,extract172try:173mtimes=[]174175unpack_archive(url,data,tmpdir,extract,type,start_offset)176177ifextractisNone:178srcdir=tmpdir179else:180srcdir=os.path.join(tmpdir,extract)181assertnotos.path.islink(srcdir)182183stem_len=len(srcdir)184forroot,dirs,filesinos.walk(srcdir):185relative_root=root[stem_len+1:]or'.'186target_root=os.path.join(destdir,relative_root)187try:188info=os.lstat(target_root)189exceptOSErrorasex:190ifex.errno!=errno.ENOENT:191raise# Some odd error.192# Doesn't exist. OK.193os.mkdir(target_root)194else:195ifstat.S_ISLNK(info.st_mode):196raiseSafeException(_('Attempt to unpack dir over symlink "%s"!')%relative_root)197elifnotstat.S_ISDIR(info.st_mode):198raiseSafeException(_('Attempt to unpack dir over non-directory "%s"!')%relative_root)199mtimes.append((relative_root,os.lstat(os.path.join(srcdir,root)).st_mtime))200201forsindirs:# Symlinks are counted as directories202src=os.path.join(srcdir,relative_root,s)203ifos.path.islink(src):204files.append(s)205206forfinfiles:207src=os.path.join(srcdir,relative_root,f)208dest=os.path.join(destdir,relative_root,f)209ifos.path.islink(dest):210raiseSafeException(_('Attempt to unpack file over symlink "%s"!')%211os.path.join(relative_root,f))212os.rename(src,dest)213214forpath,mtimeinmtimes[1:]:215os.utime(os.path.join(destdir,path),(mtime,mtime))216finally:217ro_rmtree(tmpdir)
220"""Unpack stream 'data' into directory 'destdir'. If extract is given, extract just221 that sub-directory from the archive (i.e. destdir/extract will exist afterwards).222 Works out the format from the name.223 @type url: str224 @type data: file225 @type destdir: str226 @type extract: str | None227 @type type: str | None228 @type start_offset: int"""229iftypeisNone:type=type_from_url(url)230iftypeisNone:raiseSafeException(_("Unknown extension (and no MIME type given) in '%s'")%url)231iftype=='application/x-bzip-compressed-tar':232extract_tar(data,destdir,extract,'bzip2',start_offset)233eliftype=='application/x-deb':234extract_deb(data,destdir,extract,start_offset)235eliftype=='application/x-rpm':236extract_rpm(data,destdir,extract,start_offset)237eliftype=='application/zip':238extract_zip(data,destdir,extract,start_offset)239eliftype=='application/x-tar':240extract_tar(data,destdir,extract,None,start_offset)241eliftype=='application/x-lzma-compressed-tar':242extract_tar(data,destdir,extract,'lzma',start_offset)243eliftype=='application/x-xz-compressed-tar':244extract_tar(data,destdir,extract,'xz',start_offset)245eliftype=='application/x-compressed-tar':246extract_tar(data,destdir,extract,'gzip',start_offset)247eliftype=='application/vnd.ms-cab-compressed':248extract_cab(data,destdir,extract,start_offset)249eliftype=='application/x-apple-diskimage':250extract_dmg(data,destdir,extract,start_offset)251eliftype=='application/x-ruby-gem':252extract_gem(data,destdir,extract,start_offset)253else:254raiseSafeException(_('Unknown MIME type "%(type)s" for "%(url)s"')%{'type':type,'url':url})
257"""@type stream: file258 @type destdir: str259 @type start_offset: int"""260ifextract:261raiseSafeException(_('Sorry, but the "extract" attribute is not yet supported for Debs'))262263stream.seek(start_offset)264# ar can't read from stdin, so make a copy...265deb_copy_name=os.path.join(destdir,'archive.deb')266withopen(deb_copy_name,'wb')asdeb_copy:267shutil.copyfileobj(stream,deb_copy)268269data_tar=None270p=subprocess.Popen(('ar','t','archive.deb'),stdout=subprocess.PIPE,cwd=destdir,universal_newlines=True)271o=p.communicate()[0]272forlineino.split('\n'):273ifline=='data.tar':274data_compression=None275elifline=='data.tar.gz':276data_compression='gzip'277elifline=='data.tar.bz2':278data_compression='bzip2'279elifline=='data.tar.lzma':280data_compression='lzma'281elifline=='data.tar.xz':282data_compression='xz'283else:284continue285data_tar=line286break287else:288raiseSafeException(_("File is not a Debian package."))289290_extract(stream,destdir,('ar','x','archive.deb',data_tar))291os.unlink(deb_copy_name)292data_name=os.path.join(destdir,data_tar)293withopen(data_name,'rb')asdata_stream:294os.unlink(data_name)295extract_tar(data_stream,destdir,None,data_compression)
298ifextract:299raiseSafeException(_('Sorry, but the "extract" attribute is not yet supported for RPMs'))300fd,cpiopath=mkstemp('-rpm-tmp')301try:302child=os.fork()303ifchild==0:304try:305try:306os.dup2(stream.fileno(),0)307os.lseek(0,start_offset,0)308os.dup2(fd,1)309_exec_maybe_sandboxed(None,'rpm2cpio','-')310except:311traceback.print_exc()312finally:313os._exit(1)314id,status=os.waitpid(child,0)315assertid==child316ifstatus!=0:317raiseSafeException(_("rpm2cpio failed; can't unpack RPM archive; exit code %d")%status)318os.close(fd)319fd=None320321args=['cpio','-mid']322if_gnu_cpio():323args.append('--quiet')324325withopen(cpiopath,'rb')ascpio_stream:326_extract(cpio_stream,destdir,args)327# Set the mtime of every directory under 'tmp' to 0, since cpio doesn't328# preserve directory mtimes.329forroot,dirs,filesinos.walk(destdir):330os.utime(root,(0,0))331finally:332iffdisnotNone:333os.close(fd)334os.unlink(cpiopath)
355"""@since: 0.24"""356ifextract:357raiseSafeException(_('Sorry, but the "extract" attribute is not yet supported for Cabinet files'))358359stream.seek(start_offset)360# cabextract can't read from stdin, so make a copy...361cab_copy_name=os.path.join(destdir,'archive.cab')362cab_copy=open(cab_copy_name,'wb')363shutil.copyfileobj(stream,cab_copy)364cab_copy.close()365366_extract(stream,destdir,['cabextract','-s','-q','archive.cab'])367os.unlink(cab_copy_name)
370"""@since: 0.46"""371ifextract:372raiseSafeException(_('Sorry, but the "extract" attribute is not yet supported for DMGs'))373374stream.seek(start_offset)375# hdiutil can't read from stdin, so make a copy...376dmg_copy_name=os.path.join(destdir,'archive.dmg')377dmg_copy=open(dmg_copy_name,'wb')378shutil.copyfileobj(stream,dmg_copy)379dmg_copy.close()380381mountpoint=mkdtemp(prefix='archive')382subprocess.check_call(["hdiutil","attach","-quiet","-mountpoint",mountpoint,"-nobrowse",dmg_copy_name])383subprocess.check_call(["cp","-pR"]+glob.glob("%s/*"%mountpoint)+[destdir])384subprocess.check_call(["hdiutil","detach","-quiet",mountpoint])385os.rmdir(mountpoint)386os.unlink(dmg_copy_name)
389"""@type stream: file390 @type destdir: str391 @type extract: str392 @type start_offset: int"""393ifextract:394# Limit the characters we accept, to avoid sending dodgy395# strings to zip396ifnotre.match('^[a-zA-Z0-9][- _a-zA-Z0-9.]*$',extract):397raiseSafeException(_('Illegal character in extract attribute'))398399stream.seek(start_offset)400# unzip can't read from stdin, so make a copy...401zip_copy_name=os.path.join(destdir,'archive.zip')402withopen(zip_copy_name,'wb')aszip_copy:403shutil.copyfileobj(stream,zip_copy)404405args=['unzip','-q','-o','archive.zip']406407ifextract:408args.append(extract+'/*')409410_extract(stream,destdir,args)411os.unlink(zip_copy_name)
414"""@type stream: file415 @type destdir: str416 @type extract: str417 @type decompress: str418 @type start_offset: int"""419ifextract:420# Limit the characters we accept, to avoid sending dodgy421# strings to tar422ifnotre.match('^[a-zA-Z0-9][- _a-zA-Z0-9.]*$',extract):423raiseSafeException(_('Illegal character in extract attribute'))424425assertdecompressin[None,'bzip2','gzip','lzma','xz']426427if_gnu_tar():428ext_cmd=['tar']429ifdecompress:430ifdecompress=='bzip2':431ext_cmd.append('--bzip2')432elifdecompress=='gzip':433ext_cmd.append('-z')434elifdecompress=='lzma':435unlzma=find_in_path('unlzma')436ifnotunlzma:437unlzma=os.path.abspath(os.path.join(os.path.dirname(__file__),'_unlzma'))438ext_cmd.append('--use-compress-program='+unlzma)439elifdecompress=='xz':440unxz=find_in_path('unxz')441ifnotunxz:442unxz=os.path.abspath(os.path.join(os.path.dirname(__file__),'_unxz'))443ext_cmd.append('--use-compress-program='+unxz)444445ifrecent_gnu_tar():446ext_cmd.extend(('-x','--no-same-owner','--no-same-permissions'))447else:448ext_cmd.extend(('xf','-'))449450ifextract:451ext_cmd.append(extract)452453_extract(stream,destdir,ext_cmd,start_offset)454else:455importtempfile456457# Since we don't have GNU tar, use python's tarfile module. This will probably458# be a lot slower and we do not support lzma and xz; however, it is portable.459# (lzma and xz are handled by first uncompressing stream to a temporary file.460# this is simple to do, but less efficient than piping through the program)461ifdecompressisNone:462rmode='r|'463elifdecompress=='bzip2':464rmode='r|bz2'465elifdecompress=='gzip':466rmode='r|gz'467elifdecompress=='lzma':468unlzma=find_in_path('unlzma')469ifnotunlzma:470unlzma=os.path.abspath(os.path.join(os.path.dirname(__file__),'_unlzma'))471temp=tempfile.NamedTemporaryFile(suffix='.tar',mode='w+b')472subprocess.check_call(unlzma,stdin=stream,stdout=temp)473rmode='r|'474stream=temp475elifdecompress=='xz':476unxz=find_in_path('unxz')477ifnotunxz:478unxz=os.path.abspath(os.path.join(os.path.dirname(__file__),'_unxz'))479temp=tempfile.NamedTemporaryFile(suffix='.tar',mode='w+b')480subprocess.check_call(unxz,stdin=stream,stdout=temp)481rmode='r|'482stream=temp483else:484raiseSafeException(_('GNU tar unavailable; unsupported compression format: %s')%decompress)485486importtarfile487488stream.seek(start_offset)489# Python 2.5.1 crashes if name is None; see Python bug #1706850490tar=tarfile.open(name='',mode=rmode,fileobj=stream)491492current_umask=os.umask(0)493os.umask(current_umask)494495uid=gid=None496try:497uid=os.geteuid()498gid=os.getegid()499except:500logger.debug(_("Can't get uid/gid"))501502defchmod_extract(tarinfo):503# If any X bit is set, they all must be504iftarinfo.mode&0o111:505tarinfo.mode|=0o111506507# Everyone gets read and write (subject to the umask)508# No special bits are allowed.509tarinfo.mode=((tarinfo.mode|0o666)&~current_umask)&0o777510511# Don't change owner, even if run as root512ifuid:513tarinfo.uid=uid514ifgid:515tarinfo.gid=gid516tar.extract(tarinfo,destdir)
517518extracted_anything=False519ext_dirs=[]520521fortarinfointar:522ifextractisNoneor \ 523tarinfo.name.startswith(extract+'/')or \ 524tarinfo.name==extract:525iftarinfo.isdir():526ext_dirs.append(tarinfo)527528chmod_extract(tarinfo)529extracted_anything=True530531# Due to a bug in tarfile (python versions < 2.5), we have to manually532# set the mtime of each directory that we extract after extracting everything.533534fortarinfoinext_dirs:535dirname=os.path.join(destdir,tarinfo.name)536os.utime(dirname,(tarinfo.mtime,tarinfo.mtime))537538tar.close()539540ifextractandnotextracted_anything:541raiseSafeException(_('Unable to find specified file = %s in archive')%extract)542
544"""Run execvp('command') inside destdir in a child process, with545 stream seeked to 'start_offset' as stdin.546 @type stream: file547 @type destdir: str548 @type command: [str]549 @type start_offset: int"""550551# Some zip archives are missing timezone information; force consistent results552child_env=os.environ.copy()553child_env['TZ']='GMT'554555stream.seek(start_offset)556557# TODO: use pola-run if available, once it supports fchmod558child=subprocess.Popen(command,cwd=destdir,stdin=stream,stderr=subprocess.PIPE,env=child_env)559560unused,cerr=child.communicate()561562status=child.wait()563ifstatus!=0:564raiseSafeException(_('Failed to extract archive (using %(command)s); exit code %(status)d:\n%(err)s')%{'command':command,'status':status,'err':cerr.strip()})