idlddlZddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZ ddlZdZdZdZd ZGd d eZGd d eZGddeZy#e$rY1wxYw)N)datetime)Lock) find_spec)sos_get_command_outputfilelinknodedirceZdZdZedZejdZdZ dZ e Z dZ dZdZd Zd Zd Zdd ZddZdZdZdZdZdZdZdZdZdZy )Archivez!Abstract base class for archives.c|jS)z6Returns the archive class's name as a string. )__name__)clss -/usr/lib/python3/dist-packages/sos/archive.py archive_typezArchive.archive_type(s||sosunsetFc.d|jd|S)Nz [archive:z] )rselfmsgs r _format_msgzArchive._format_msg5s$($5$5$7==rc||_yN)_debug)rdebugs r set_debugzArchive.set_debug8s  rcX|jj|j|yr)logerrorrrs r log_errorzArchive.log_error;s t'',-rcX|jj|j|yr)r warningrrs rlog_warnzArchive.log_warn>s ))#./rcX|jj|j|yr)r inforrs rlog_infozArchive.log_infoAs  d&&s+,rcr|jsy|jj|j|yr)rr rrrs r log_debugzArchive.log_debugDs'{{  t'',-rNctrNotImplementedError)rsrcdestforces radd_filezArchive.add_fileL!!rctrr,)rcontentr/modes r add_stringzArchive.add_stringOr2rctrr,)rr4r/s r add_binaryzArchive.add_binaryRr2rctrr,)rsource link_names radd_linkzArchive.add_linkUr2rctrr,rpaths radd_dirzArchive.add_dirXr2rctrr,)rr?r5devices radd_nodezArchive.add_node[r2rct)zReturn a temporary directory that clients of the archive may use to write content to. The content of the path is guaranteed to be included in the generated archive.r,rs r get_tmp_dirzArchive.get_tmp_dir^ "!rct)zReturn the maximum file name length this archive can support. This is the lesser of the name length limit of the archive format and any temporary file system based cache.r,rEs rname_maxzArchive.name_maxdrGrcy)aTReturn a string representing the path to the temporary archive. For archive classes that implement in-line handling this will be the archive file itself. Archives that use a directory based cache prior to packaging should return the path to the temporary directory where the report content is locatedNrEs rget_archive_pathzArchive.get_archive_pathjs rcy)z:Clean up any temporary resources used by an Archive class.NrKrEs rcleanupzArchive.cleanupss rc$|jy)aFinalize an archive object via method. This may involve creating An archive that is subsequently compressed or simply closing an archive that supports in-line handling. If method is automatic then the following methods are tried in order: xz, gzipN)close)rmethods rfinalizezArchive.finalizews rNFw)r __module__ __qualname____doc__ classmethodrlogging getLoggerr _namerr _path_lockrrr"r%r(r*r1r6r8r<r@rCrFrIrLrNrRrKrrr r %s+ '  E "C E FJ>.0-.""""""" "   rr ceZdZdZdZdZdZ ddZdZdZ ddZ ddZ d Z dd Z dd Zd Zd ZdZdZdZdZdZddZdZdZdZdZdZdZy)FileCacheArchivezd Abstract superclass for archive types that use a temporary cache directory in the file system. Nc||_tjj|j|g|k(r%tjj |||_||_||_||_||_|xsd|_ ||_ tjj|||_ |j5tj|jdddd|jd|jdy#1swY)xYw)N/z'initialised empty FileCacheArchive at '')r\osr? commonprefixrelpath_tmp_dir_policy_threadsenc_optssysrootmanifestjoin _archive_rootr]makedirsr()rnametmpdirpolicythreadsrkrlrms r__init__zFileCacheArchive.__init__s 77  V 4 5 ?v6DJ     ~#   WW\\&$7 __ 3 KK**E 2 3 )), - 3 3s :!DD ctjj|r|jtj}tjj |j |Sr)rer?isabslstripseprnro)rrqs r dest_pathzFileCacheArchive.dest_paths? 77== ;;rvv&D T//67rc|jr|j|jr|S|dtjk(r|dd}tjj |j|S)Nr)rl startswithreryr?rnr>s r join_sysrootzFileCacheArchive.join_sysrootsQ||tt||<K 7bff 8Dww||DLL$//rc|jd|z|j|}fd}|jds|}nRtjj |j |r|n!tjj|d}|}g}|dk7rE|dk7r@tjj|\}} |j| |}|dk7r|dk7r@|j} d} |D]} tjj| | } || s-tjj| | } tjj| rm|jd| ztjj| rtjj | rtj| } tjj| d}tjj|| }|j||}tjj|}tjj!| r tjj#| |} |jd| d | d tj$| | |jd | ztj&| || }|S) aCreate leading path components The standard python `os.makedirs` is insufficient for our needs: it will only create directories, and ignores the fact that some path components may be symbolic links. :param src: The source path in the host file system for which leading components should be created, or the path to an sos_* virtual directory inside the archive. Host paths must be absolute (initial '/'), and sos_* directory paths must be a path relative to the root of the archive. :param mode: An optional mode to be used when creating path components. :returns: A rewritten destination path in the case that one or more symbolic links in intermediate components of the path have altered the path destination. zMaking leading paths for %scb|jtjjdS)z>Test whether path ``path`` is inside the archive. r`)r}rer?rn)r?roots r in_archivez8FileCacheArchive._make_leading_paths..in_archives#??277<<b#9: :rrbrr`zMaking path %sr5zMaking symlink '' -> 'rdzMaking directory %s)r*ror}rer?isdirr~splitappendreversernexistsislinkreadlink_make_leading_pathsnormpathrwrgsymlinkmkdir)rr.r5r/rsrc_dirr? path_compsheadtailabs_pathsrc_pathcomptarget target_dir target_srcrs @rrz$FileCacheArchive._make_leading_pathssH* 4s:;!! ; ~~c"G!ggmmD,=,=c,BCsGGMM#.q1  ckdbjt,JD$   d #Dckdbj % $Dww||Hd3Hh'ww||Hd3H77>>(+/(:;77>>(+ h0G[[2F"$x!8!;J"$j&!AJ 33JT3JD77++D1Dww}}V,!#!DNN$,f$67JJvx0NN#88#CDHHXt,#DK% $N rc|xs|j|}|tk(r|}n"tjj |d}|s|Stjj |r-tjj |std|ztjj |s>|tk(r|n!tjj |d}|j|d}|r|Stjj |rtj|}d} |tk(r/tj|jst| |dfz|tk(r/tj|jst| |dfz|t k(r"||jst| |dfz|tk(r/tj"|jst| |dfzy |S) a4Check a new destination path in the archive. Since it is possible for multiple plugins to collect the same paths, and since plugins can now run concurrently, it is possible for two threads to race in archive methods: historically the archive class only needed to test for the actual presence of a path, since it was impossible for another `Archive` client to enter the class while another method invocation was being dispatched. Deal with this by implementing a locking scheme for operations that modify the path structure of the archive, and by testing explicitly for conflicts with any existing content at the specified destination path. It is not an error to attempt to create a path that already exists in the archive so long as the type of the object to be added matches the type of object already found at the path. It is an error to attempt to re-create an existing path with a different path type (for example, creating a symbolic link at a path already occupied by a regular file). :param src: the source path to be copied to the archive :param path_type: the type of object to be copied :param dest: an optional destination path :param force: force file creation even if the path exists :returns: An absolute destination path if the path should be copied now or `None` otherwise rz'path '%s' exists and is not a directorycttj|tj|tj|tj |gSr)anystatS_ISBLKS_ISCHRS_ISFIFOS_ISSOCKrs r is_specialz/FileCacheArchive.check_path..is_special1sD T" T" d# d#  rz path '%s' exists and is not a %sz regular filez symbolic linkz special file directoryN)rzP_DIRrer?rrr ValueErrorrlstatP_FILErS_ISREGst_modeP_LINKS_ISLNKP_NODES_ISDIR) rr. path_typer/r0dest_dirrrstve_msgs r check_pathzFileCacheArchive.check_paths>*t~~c*  Hww}}T*1-HK 77>>( #BGGMM(,CF%&' ')&%/cRWW]]35G5JG  $ $W -  K 77>>$ $B7FF"4<< +C 4*@!@AAF"4<< +C 4*A!ABBF":bjj+A 4*@!@AAE!$,,rzz*B 4*=!=>> rc tj|}|jds|jdrDtj||tj ||j |jfntj||tj||j|jy#t$r"}|jd|d|dYd}~yd}~wwxYw)N/sys//proc/)nszcaught 'z' setting attributes of 'rd)rerr}shutilcopymodeutime st_atime_ns st_mtime_nscopystatchownst_uidst_gid Exceptionr*)rr.r/res r_copy_attributesz!FileCacheArchive._copy_attributesMs (773.is_loopswwy1H77==())"'',,x*HI((3IF"  "   77b= s"B88 CCCCzLink 'z' - 'z' loops: skipping...z Adding link z -> z for link follow upz Adding dir %s for link follow upz!Adding file %s for link follow upzNo link follow up: source=)r*r]rrrer?lexistsrrorrrnrzrrrrgr<rr@isfiler1) rr:r;r/ source_dirhost_path_namedest_path_namerrs rr<zFileCacheArchive.add_links" 9fMN __ E??9f5D E E 77??4( 64("&0B0B DE E  41 2WW__Y/ ))"'',,z6*JK7 .ww~~n-ww~~n-* 77??95h N0KL:9f-NN$-v$78 )6 34 fi0~.AFJK ^,/BVKL n- &  34/.W E EsKAKK!cr|j5|j|tdddy#1swYyxYw)zmCreate a directory in the archive. :param path: the path in the host file system to add N)r]rrr>s rr@zFileCacheArchive.add_dirs. __ ) OOD% ( ) ) )s-6ch|j|t}|sytjj |s+ tj ||||j||yy#t $rC}|jtjk(rd}|jd|d|dYd}~y|d}~wwxYw)NzOperation not permittedz add_node: z - mknod 'rd) rrrer?rmknodrrEPERMr(r)rr?r5rBr/rrs rrCzFileCacheArchive.add_nodestV, ww~~d# tV,  ! !$ -$ 77ekk)3CMMd"KL  sA%% B1.7B,*B,,B1cdtjvr3tjd}tj|j|Sy)N PC_NAME_MAX)repathconf_namespathconfro)r pc_name_maxs rrIzFileCacheArchive.name_maxs: B-- -++M:K;;t11;? ?rc|jSrrorEs rrFzFileCacheArchive.get_tmp_dir!!!rc|jSrrrEs rrLz!FileCacheArchive.get_archive_pathrrctjtjj|j|||j d|d|jdy)zsCreate path, including leading components. Used by sos.sosreport to set up sos_* directories. rzcreated directory at 'z' in FileCacheArchive 'rdN)rerpr?rnror*)rr?r5s rrpzFileCacheArchive.makedirs sC BGGLL!3!3T:F  2 24 5rcV|j|}tj|dddS)Nrrr)rerrors)rzrrr>s r open_filezFileCacheArchive.open_files%~~d#{{4wxHHrctjj|jr t j |jyyr)rer?rrorrmtreerEs rrNzFileCacheArchive.cleanups/ 77==++ , MM$,, - -rctj}|jj}||z }|jj d||jj d||jj d||j |jj dtjjddy) zAdds component-agnostic data to the manifest so that individual SoSComponents do not need to redundantly add these manually end_timerun_time compression)indent sos_reportsz manifest.jsonN) rnowrm start_time add_fieldr6get_jsonrer?rn)rrQendstartrs radd_final_manifest_dataz(FileCacheArchive.add_final_manifest_dataslln ((;  C0  H5  v6  ..a.8 ]OD Frct|j|j|_tjj |j |j}tj |j|||_tjj |j |j|_ y)zeRename the archive to an obfuscated version using an initialized SoSCleaner instance N) obfuscate_stringr\rer?rnrhrenamerorq _archive_name)rcleaner _new_roots rrename_archive_rootz$FileCacheArchive.rename_archive_root(ss--djj9 GGLL ;  $$$i0&WW\\$--Erc2|jd|jd|d |j|}|j |jd|jtj|jjfz|jdr |j|S|S#t$r.}|j d|z|j cYd}~Sd}~wwxYw#t$r$}d}|j |d||cYd}~Sd}~wwxYw) Nzfinalizing archive 'z' using method 'rdz-An error occurred compressing the archive: %szbuilt archive at '%s' (size=%d)encryptz)An error occurred encrypting the archive: ) r(ro_build_archiverr"rqrNrrerst_sizerk_encrypt)rrQreserrrexp_msgs rrRzFileCacheArchive.finalize2s ++V5 6 %%f-C  74;M;Mggd00199;;; < == # }}S)) J#  NNJ ! "99;   E'156  s;B/C)/ C&8#C!C&!C&) D2D DDc|jdd}|dz }d|z}d}|jdr|d|jdzz }||z }|jdr5d |jdjd d z}d |i}|d z }d|z}||z }t|d|}|ddk(r|S|ddk(r)|jdr d}t|d}t|d|dz}t|)aEncrypts the compressed archive using GPG. If encryption fails for any reason, it should be logged by sos but not cause execution to stop. The assumption is that the unencrypted archive would still be of use to the user, and/or that the end user has another means of securing the archive. Returns the name of the encrypted archive, or raises an exception to signal that encryption failed and the unencrypted archive name should be used. z sosreport-zsecured-sosreport-z.gpgzgpg --batch -o %s Nkeyz--trust-model always -e -r %s passwordz%sz'"r`sos_gpgz-c --passphrase-fd 0 z!/bin/bash -c "echo $sos_gpg | %s"r)timeoutenvstatuszSpecified key not in keyringzCould not read passphrasezgpg exited with code %s)replacerkrr)rarchivearc_nameenc_cmdr passwdrrs rrzFileCacheArchive._encryptJs+??<1EFF&1 ==  7$--:NN NG w G == $DMM*5==eRHHFf%C . .G;gEG w G "7A3 ? X;! O x[A }}U#4n 2 n,ak9Cnrr)rcrSrT)rrVrWrXrhrorrurzr~rrrr1r6r8r<r@rCrIrFrLrprrNr rrRrrKrrr_r_s&HMM-$8 0ZxIV ("z9TarFileArchive.copy_permissions_filter..s;Trxxi(;s #zRHT.security.selinux) rqlenrer?rrorrr_with_selinux_contextget_selinux_contextr5r;)rtarinfoskipsr:contextrCs @rcopy_permissions_filterz&TarFileArchive.copy_permissions_filtersLLRWW]]43E3E%Fr%J!K!LM **IJ ;U; ; GGI&E  % %..y9G>E##$:; ""7E2 N s,C CCcV tj|\}}|S#t$rYywxYwr)selinux getfileconr)rr?rccs rrGz"TarFileArchive.get_selinux_contexts2 ((.GRH  s  ((c8|jd|jS)N.)ror-rEs rrqzTarFileArchive.names,,dll;;rc(tt| Sr)r,r)rI)rr.s rrIzTarFileArchive.name_maxs^T355rc|dk(rtddnd}|jd}|jd|zz|_|dk(rddi}nd d i}tj|jfd d |zi|}d D]}t j jt j j|j|sK|jt j j|j||jd||j|j|j|j|j|xjd|zz c_|jS)Nautolzmaxzgzipipz.%s compresslevelpresetr5zw:%s)z version.txtrsos_logsrb)arcname)r_filter)rstriprtarfilerrer?rrnroaddr\rKrPr-rq)rrQ _comp_modekwargsr+_contents rrzTarFileArchive._build_archivesO V &v.:TF\\$' !//%*2DD V %q)F]Fll4--%FZ4G%#%C H77>>"'',,t/A/A8"LM GG T//:::,az2    ""DJJ33  5   ** yy{rr)rrVrWrXrQrFrur;rKrGrqrIr __classcell__)r.s@rr)r)vs9D F!  $$<6 rr))rerbrrZrrrr?r threadingrimportlib.utilr sos.utilitiesrrM ImportErrorrrrrobjectr r_r)rKrrrms   $0     XfXvswslY%Yu  sA((A0/A0