dbfdg 3 ([Qhq @sddlZddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZy ddlZWnek rYnXdZdZdZd ZGd d d ZGd d d eZGdddeZdS)N)datetime)Lock) find_spec)sos_get_command_outputfilelinkZnodedirc@seZdZdZeddZejdZdZ dZ e Z ddZ d d Zd d Zd dZddZddZddZd-ddZd.ddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,ZdS)/Archivez!Abstract base class for archives.cCs|jS)z6Returns the archive class's name as a string. )__name__)clsr /usr/lib/python3.6/archive.py archive_type+szArchive.archive_typeZsosZunsetFcCsd|jd|S)Nz [archive:z] )r)selfmsgr r r _format_msg8szArchive._format_msgcCs ||_dS)N)_debug)rdebugr r r set_debug;szArchive.set_debugcCs|jj|j|dS)N)logerrorr)rrr r r log_error>szArchive.log_errorcCs|jj|j|dS)N)rZwarningr)rrr r r log_warnAszArchive.log_warncCs|jj|j|dS)N)rinfor)rrr r r log_infoDszArchive.log_infocCs |js dS|jj|j|dS)N)rrrr)rrr r r log_debugGszArchive.log_debugcCs|jS)N)_name)rr r r nameLsz Archive.nameNcCstdS)N)NotImplementedError)rsrcdestforcer r r add_fileRszArchive.add_filewcCstdS)N)r)rcontentr moder r r add_stringUszArchive.add_stringcCstdS)N)r)rr$r r r r add_binaryXszArchive.add_binarycCstdS)N)r)rsource link_namer r r add_link[szArchive.add_linkcCstdS)N)r)rpathr r r add_dir^szArchive.add_dircCstdS)N)r)rr+r%devicer r r add_nodeaszArchive.add_nodecCstdS)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.N)r)rr r r get_tmp_dirdszArchive.get_tmp_dircCstdS)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.N)r)rr r r name_maxjszArchive.name_maxcCstdS)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 locatedN)r)rr r r get_archive_pathpszArchive.get_archive_pathcCstdS)z:Clean up any temporary resources used by an Archive class.N)r)rr r r cleanupyszArchive.cleanupcCstdS)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)r)rmethodr r r finalize}szArchive.finalize)NF)r#)r __module__ __qualname____doc__ classmethodrloggingZ getLoggerrrrr _path_lockrrrrrrrr"r&r'r*r,r.r/r0r1r2r4r r r r r (s0     r c@seZdZdZdZdZdZd7ddZddZdd Z d8d d Z d9ddZ ddZ d:ddZ d;ddZddZddZddZddZdd Zd!d"Zd#d$ZdTest whether path ``path`` is inside the archive. r<)rRr?r+rF)r+)rootr r in_archivesz8FileCacheArchive._make_leading_paths..in_archiver=rr<z Making path )r%zMaking symlink 'z' -> 'r>zMaking directory )r=r<)rrGrRr?r+isdirrSsplitappendreverserFexistsislinkreadlink_make_leading_pathsnormpathrMrAsymlinkmkdir)rrr%r rVsrc_dirr+Z path_compsheadtailZabs_pathZsrc_pathcomptargetZ target_dirZ target_srcr )rUr r^sL        z$FileCacheArchive._make_leading_pathsFc Csd|p |j|}|tkr|}ntjj|d}|s4|Stjj|r^tjj| r^td|dtjj|s|tkrv|ntjj|d}|j|dd}|r|Stjj|r`tj |}d} |t krt j |j  rt| |df|tkot j|j  r t| |df|tkr2||j  r2t| |d f|tkr\t j|j  r\t| |d fd S|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 rzpath 'z' exists and is not a directorycSs(ttj|tj|tj|tj|gS)N)anystatS_ISBLKS_ISCHRS_ISFIFOS_ISSOCK)r%r r r is_special7s z/FileCacheArchive.check_path..is_specialz path '%s' exists and is not a %sz regular filez symbolic linkz special fileZ directoryN)rPP_DIRr?r+rXr[rW ValueErrorr^lstatP_FILErhS_ISREGst_modeP_LINKS_ISLNKP_NODES_ISDIR) rr path_typer r!dest_dirrbrmstZve_msgr r r check_paths6   zFileCacheArchive.check_pathcCsydtj|}|jds |jdrDtj||tj||j|jfdn tj||tj ||j |j Wn:t k r}z|j d|d|dWYdd}~XnXdS)Nz/sys/z/proc/)nszcaught 'z' setting attributes of 'r>)r?rhrRshutilZcopymodeutime st_atime_ns st_mtime_nsZcopystatchownst_uidst_gid Exceptionr)rrr _stater r r _copy_attributesSs   z!FileCacheArchive._copy_attributescCs|j|s|}|j|t|d}|s*dSt|ddsytj||WnPtk r}z4|jds|jdrnn|jd|d|dWYdd}~XnX|j ||d|d}n@|j dt |d d d }x|D]}|j |qWWdQRXd }|j d |d|jdWdQRXdS)N)r!readz/sys/z/proc/zFile z not collected: 'r>rr#zutf-8)encodingz open filezadded z to FileCacheArchive ')r:r{rqgetattrr}copyOSErrorrRrrseekopenwriterrG)rrr r!r file_namefliner r r r"`s*  *   zFileCacheArchive.add_filer#cCs|j|}|j|tdd}tj||ddX}t|trD|jdd}|j|t j j |rf|j |||j d|d|jd WdQRXWdQRXdS) NT)r!zutf-8)rutf8ignorezadded string at 'z' to FileCacheArchive 'r>)r:r{rqcodecsr isinstancebytesdecoderr?r+r[rrrG)rr$r r%rrr r r r&s     zFileCacheArchive.add_stringcCsj|jZ|j|t}|sdStj|ddd}|j|WdQRX|jd|d|jdWdQRXdS)Nwb)rzadded binary content at 'z' to archive 'r>)r:r{rqrrrrrG)rr$r rr r r r's zFileCacheArchive.add_binaryc Cs|jd|d|d|jR|j|t}|s4dStjj|sltj|||jd|d|d|jdWdQRX|jd|d|d |tjj |}tjj tjj ||}|j |}d d }tjj |stjj|rb|}tjj |}tjj |tj|}tjj||}|||r<|jd |d |ddS|jd|d|d|j||nntjj|r|jd|d|j|nBtjj|r|jd|d|j|n|jd|d|dS)Nzadding symlink at 'z' -> 'r>zadded symlink at 'z' to 'z' in archive 'zLink follow up: source=z link_name=z dest=cSstjj|}tjj|s.tjjtjj||}tjj|}||krFdSytj|Wn2tk r}z|jdkrtdSWYdd}~XnXdS)zReturn ``True`` if the symbolic link ``link_name`` is part of a file system loop, or ``False`` otherwise. T(NF) r?r+dirnamerMrealpathrFrhrerrno)r)r(Zlink_dirrr r r is_loops    z*FileCacheArchive.add_link..is_loopzLink 'z' - 'z' loops: skipping...z Adding link z -> z for link follow upz Adding dir z Adding file zNo link follow up: source=)rr:r{rtr?r+lexistsr`rGrrrFrPr[r\r]rAr*rWr,isfiler") rr(r)r Z source_dirZhost_path_nameZdest_path_namerryr r r r*s>   *      zFileCacheArchive.add_linkc Cs"|j|j|tWdQRXdS)zmCreate a directory in the archive. :param path: the path in the host file system to add N)r:r{rn)rr+r r r r,szFileCacheArchive.add_dircCs|j|t}|sdStjj|sytj|||WnRtk r}z6|jtjkrpd}|j d|d|ddS|WYdd}~XnX|j ||dS)NzOperation not permittedz add_node: z - mknod 'r>) r{rvr?r+r[mknodrrZEPERMrr)rr+r%r-r rrr r r r.s   zFileCacheArchive.add_nodecCs&dtjkr"tjd}tj|j|SdS)N PC_NAME_MAX)r?pathconf_namespathconfrG)rZ pc_name_maxr r r r0s  zFileCacheArchive.name_maxcCs|jS)N)rG)rr r r r/ szFileCacheArchive.get_tmp_dircCs|jS)N)rG)rr r r r1 sz!FileCacheArchive.get_archive_pathcCs8tjtjj|j||d|jd|d|jddS)zsCreate path, including leading components. Used by sos.sosreport to set up sos_* directories. )r%zcreated directory at 'z' in FileCacheArchive 'r>N)r?rHr+rFrGr)rr+r%r r r rHszFileCacheArchive.makedirscCs|j|}tj|ddddS)Nrzutf-8r)rerrors)rPrr)rr+r r r open_files zFileCacheArchive.open_filecCstjj|jrtj|jdS)N)r?r+rWrGr}Zrmtree)rr r r r2szFileCacheArchive.cleanupcCsftj}|jj}||}|jjd||jjd||jjd||j|jjddtjj dddS) zAdds component-agnostic data to the manifest so that individual SoSComponents do not need to redundantly add these manually Zend_timerun_timeZ compression)indent sos_reportsz manifest.jsonN) rZnowrEZ start_timeZ add_fieldr&Zget_jsonr?r+rF)rr3endstartrr r r add_final_manifest_data sz(FileCacheArchive.add_final_manifest_datacCsN|j|j|_tjj|j|j}tj|j|||_tjj|j|j|_ dS)zeRename the archive to an obfuscated version using an initialized SoSCleaner instance N) Zobfuscate_stringrr?r+rFrBrenamerGr _archive_name)rZcleanerZ _new_rootr r r rename_archive_root-s z$FileCacheArchive.rename_archive_rootc CstjtjB}t|dr(|j}|j|B}n|}|}d}|j|}|j}WdQRXt|t sh|j dd}tj ||||d\} } | r|j | |nd} | S)aApply a regexp substitution to a file in the archive. :param path: Path in the archive where the file can be found :type path: ``str`` :param regexp: A regex to match the contents of the file :type regexp: ``str`` or compiled ``re`` object :param subst: The substitution string to be used to replace matches within the file :type subst: ``str`` :returns: Number of replacements made :rtype: ``int`` patternr<Nrr)flagsr) re IGNORECASE MULTILINEhasattrrrrrrstrrsubnr&) rr+ZregexpZsubstZ common_flagsrrr$readableresultZ replacementsr r r do_file_sub7s"        zFileCacheArchive.do_file_subcCs|jd|jd|dy|j|}Wn4tk r\}z|jd||jSd}~XnX|j|jd|jdtj |jj d|j dry |j |Stk r}zd }|j|d ||Sd}~XqXn|SdS) Nzfinalizing archive 'z' using method 'r>z+An error occurred compressing the archive: zbuilt archive at 'z' (size=)Zencryptz)An error occurred encrypting the archive: ) rrG_build_archiverrrr2rr?rhst_sizerC_encrypt)rr3reserrrZexp_msgr r r r4\s $  zFileCacheArchive.finalizecCs|jdd}|d7}d|d}d}|jdrL|d|jdd7}||7}|jd r|jd jd d }d |i}|d 7}d|d}||7}t|d|dd}|ddkr|S|ddkr|jdrd}qd}nd|dd|d}t|dS)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 rNkeyz--trust-model always -e -r Zpasswordz'"r<Zsos_gpgz-c --passphrase-fd 0 z/bin/bash -c "echo $sos_gpg | "rT)ZtimeoutenvstderrZstatuszSpecified key not in keyringzCould not read passphrasezgpg exited with code z and error output)replacerCrr)rarchiveZarc_nameZenc_cmdrZpasswdrrr r r rss,        zFileCacheArchive._encryptcCs|jS)N)r)rr3r r r rszFileCacheArchive._build_archive)N)rT)NF)NF)r#)rT)r r5r6r7rBrGrrLrPrSr^r{rr"r&r'r*r,r.r0r/r1rHrr2rrrr4rrr r r r r;s8  ] K $  G    %)r;csTeZdZdZdZdZdfdd ZdddZdd Zd d Z d d Z ddZ Z S)TarFileArchivez: archive class using python TarFile to create tar archivesNFcs6tj|||||||d|_tjj||j|_dS)Ntar)superrL_suffixr?r+rFrr)rrrIrJrKrCrDrE) __class__r r rLs  zTarFileArchive.__init__cCsP|j|_|jd|jd<|jd|jd<|r4||_n|j|_|j|_|j |_ dS)Nz.9fZatimeZctime) st_mtimemtimest_atime pax_headersst_ctimer%rsrZuidrgid)rZtar_infofstatr%r r r set_tarinfo_from_statsz$TarFileArchive.set_tarinfo_from_statc s|jttjj|jdds*|jdddg}tfdd|DrNdSytj}Wntk rp|SX|j r|j }|r||j d<|j |||S) NrQz /version.txt$z/sos_logs(/.*)?z/sos_reports(/.*)?c3s|]}tj|VqdS)N)rmatch).0skip) orig_pathr r sz9TarFileArchive.copy_permissions_filter..zRHT.security.selinux) rlenr?r+rXrGrgrhr_with_selinux_contextget_selinux_contextrr)rZtarinfoZskipsrcontextr )rr copy_permissions_filters      z&TarFileArchive.copy_permissions_filterc Cs,ytj|\}}|Stk r&dSXdS)N)selinuxZ getfileconr)rr+_cr r r rs z"TarFileArchive.get_selinux_contextcCs|jd|jS)N.)rGr)rr r r rszTarFileArchive.namec Cs|dkrtddk rdnd}|jd}|jd||_|dkrJddi}nd d i}tj|jfd d |i|h}xJdD]B}tjjtjj|j |rx|j tjj|j ||j d|dqxW|j |j |j |j dWdQRX|j d|7_ |jS)NautoZlzmaZxzZgzipZiprZ compresslevelZpresetr%zw: version.txtrsos_logsr=)arcname)rfilter)rrr)rstriprtarfilerr?r+r[rFrGaddrrrr)rr3Z _comp_modekwargsrZ_contentr r r rs$     zTarFileArchive._build_archive)N)N) r r5r6r7r3rrLrrrrr __classcell__r r )rr rs r)r?rr}r9rrrhrrZ threadingrimportlib.utilrZ sos.utilitiesrr ImportErrorrqrtrvrnr r;rr r r r  s4     ]