o idl@sddlZddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZzddlZWn eyGYnwdZdZdZd ZGd d d eZGd d d eZGdddeZdS)N)datetime)Lock) find_spec)sos_get_command_outputfilelinknodedirc@seZdZdZeddZedZdZ dZ e Z ddZ d d Zd d Zd dZddZddZd+ddZd,ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*ZdS)-Archivez!Abstract base class for archives.cC|jS)z6Returns the archive class's name as a string. )__name__)clsr-/usr/lib/python3/dist-packages/sos/archive.py archive_type(szArchive.archive_typesosunsetFcCsd||fS)Nz[archive:%s] %s)rselfmsgrrr _format_msg5zArchive._format_msgcCs ||_dSN)_debug)rdebugrrr set_debug8s zArchive.set_debugcC|j||dSr)logerrorrrrrr log_error;zArchive.log_errorcCrr)rwarningrrrrrlog_warn>r zArchive.log_warncCrr)rinforrrrrlog_infoAr zArchive.log_infocCs |jsdS|j||dSr)rrrrrrrr log_debugDszArchive.log_debugNcCtrNotImplementedError)rsrcdestforcerrradd_fileLzArchive.add_filewcCr&rr')rcontentr*moderrr add_stringOr-zArchive.add_stringcCr&rr')rr/r*rrr add_binaryRr-zArchive.add_binarycCr&rr')rsource link_namerrradd_linkUr-zArchive.add_linkcCr&rr'rpathrrradd_dirXr-zArchive.add_dircCr&rr')rr7r0devicerrradd_node[r-zArchive.add_nodecCr&)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'rrrr get_tmp_dir^zArchive.get_tmp_dircCr&)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'r;rrrname_maxdr=zArchive.name_maxcCdS)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 locatedNrr;rrrget_archive_pathjszArchive.get_archive_pathcCr?)z:Clean up any temporary resources used by an Archive class.Nrr;rrrcleanupsszArchive.cleanupcCs |dS)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)rmethodrrrfinalizews zArchive.finalizeNFr.)r __module__ __qualname____doc__ classmethodrlogging getLoggerr_namerr _path_lockrrrr"r$r%r,r1r2r5r8r:r<r>r@rArDrrrrr %s2     r c@seZdZdZdZdZdZ d3ddZddZdd Z d4d d Z d5ddZ ddZ d5ddZ d6ddZddZddZddZddZdd Zd!d"Zd#d$Zd4d%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2ZdS)7FileCacheArchivezd Abstract superclass for archive types that use a temporary cache directory in the file system. NcCs||_tj|j|g|krtj|||_||_||_||_||_|p%d|_ ||_ tj |||_ |j t|j dWdn1sGwY|d|j fdS)N/z*initialised empty FileCacheArchive at '%s')rMosr7 commonprefixrelpath_tmp_dir_policy_threadsenc_optssysrootmanifestjoin _archive_rootrNmakedirsr$rnametmpdirpolicythreadsrYrZr[rrr__init__s   zFileCacheArchive.__init__cCs(tj|r |tj}tj|j|Sr)rSr7isabslstripsepr\r])rr`rrr dest_paths  zFileCacheArchive.dest_pathcCs@|jr ||jr |S|dtjkr|dd}tj|j|S)Nr)rZ startswithrSrgr7r\r6rrr join_sysroots  zFileCacheArchive.join_sysrootrRcs|d||j|}fdd}|ds|}ntj||r%|ntj|d}|}g}|dkrP|dkrPtj|\}} || |}|dkrP|dks9| } d} |D]{} tj | | } || shqZtj | | } tj | s|d| tj | rtj| rt | } tj| d}tj || }|j||d}tj|}tj| rtj| |} |d | | ft| | qZ|d | t| || }qZ|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 %scs|tjdS)z>Test whether path ``path`` is inside the archive. rP)rjrSr7r\)r7rootrr in_archivesz8FileCacheArchive._make_leading_paths..in_archiverQrrPzMaking path %sr0zMaking symlink '%s' -> '%s'zMaking directory %s)r%r]rjrSr7isdirrksplitappendreverser\existsislinkreadlink_make_leading_pathsnormpathrerUsymlinkmkdir)rr)r0r*rnsrc_dirr7 path_compsheadtailabs_pathsrc_pathcomptarget target_dir target_srcrrlrrwsV        z$FileCacheArchive._make_leading_pathsFc CsJ|p||}|tkr|}ntj|d}|s|Stj|r,tj|s,td|tj|sE|tkr8|ntj|d}||dd}|rM|Stj|rt |}d} |t krlt |j slt| |df|tkr~t |j s~t| |df|tkr||j st| |df|tkrt |j st| |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 rz'path '%s' exists and is not a directorycSs(tt|t|t|t|gSr)anystatS_ISBLKS_ISCHRS_ISFIFOS_ISSOCKrorrr is_special1s z/FileCacheArchive.check_path..is_specialz path '%s' exists and is not a %sz regular filez symbolic linkz special file directoryN)rhP_DIRrSr7rqrtrp ValueErrorrwlstatP_FILErS_ISREGst_modeP_LINKS_ISLNKP_NODES_ISDIR) rr) path_typer*r+dest_dirr{rstve_msgrrr check_paths:    zFileCacheArchive.check_pathc Csz3t|}|ds|dr"t||tj||j|jfdnt||t ||j |j WdSt yO}z| d||fWYd}~dSd}~ww)N/sys//proc/)nsz&caught '%s' setting attributes of '%s')rSrrjshutilcopymodeutime st_atime_ns st_mtime_nscopystatchownst_uidst_gid Exceptionr%)rr)r*rerrr_copy_attributesMs   z!FileCacheArchive._copy_attributesc Cs:|j|s|}|j|t|d}|s WddSt|dds[zt||Wn&tyO}z|ds;|dr '%s'z-added symlink at '%s' to '%s' in archive '%s'z.Link follow up: source=%s link_name=%s dest=%sc Sstj|}tj|stjtj||}tj|}||kr#dSzt|WdStyD}z |jdkr?WYd}~dSd}~ww)zReturn ``True`` if the symbolic link ``link_name`` is part of a file system loop, or ``False`` otherwise. T(NF) rSr7dirnamererealpathr\rrerrno)r4r3link_dirrrrris_loops     z*FileCacheArchive.add_link..is_loopz#Link '%s' - '%s' loops: skipping...z'Adding link %s -> %s for link follow upz Adding dir %s for link follow upz!Adding file %s for link follow upz)No link follow up: source=%s link_name=%s)r%rNrrrSr7lexistsryr]rrr\rhrtrurvrUr5rpr8isfiler,) rr3r4r* source_dirhost_path_namedest_path_namerrrrrr5sZ              zFileCacheArchive.add_linkcCs8|j||tWddS1swYdS)zmCreate a directory in the archive. :param path: the path in the host file system to add N)rNrrr6rrrr8s"zFileCacheArchive.add_dirc Cs||t}|s dStj|sHz t|||Wn&ty?}z|jtjkr9d}| d||fWYd}~dS|d}~ww| ||dSdS)NzOperation not permittedzadd_node: %s - mknod '%s') rrrSr7rtmknodrrEPERMr$r)rr7r0r9r*rrrrrr:s   zFileCacheArchive.add_nodecCs&dtjvrtjd}t|j|SdS)N PC_NAME_MAX)rSpathconf_namespathconfr])r pc_name_maxrrrr>s  zFileCacheArchive.name_maxcCr rr]r;rrrr<zFileCacheArchive.get_tmp_dircCr rrr;rrrr@rz!FileCacheArchive.get_archive_pathcCs2tjtj|j||d|d||jfdS)zsCreate path, including leading components. Used by sos.sosreport to set up sos_* directories. roz2created directory at '%s' in FileCacheArchive '%s'N)rSr^r7r\r]r%)rr7r0rrrr^ s zFileCacheArchive.makedirscCs||}tj|ddddS)Nrrr)rerrors)rhrrr6rrr open_files zFileCacheArchive.open_filecCs"tj|jrt|jdSdSr)rSr7rpr]rrmtreer;rrrrAszFileCacheArchive.cleanupcCsft}|jj}||}|jd||jd||jd|||jjddtj dddS) 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) rnowr[ start_time add_fieldr1get_jsonrSr7r\)rrCendstartrrrradd_final_manifest_datas z(FileCacheArchive.add_final_manifest_datacCsN||j|_tj|j|j}t|j|||_tj|j||_ dS)zeRename the archive to an obfuscated version using an initialized SoSCleaner instance N) obfuscate_stringrMrSr7r\rVrenamer]r` _archive_name)rcleaner _new_rootrrrrename_archive_root(s z$FileCacheArchive.rename_archive_rootc Cs|d|j|fz||}Wnty.}z|d||WYd}~Sd}~ww||d|jt |jj f|j drmz| |WStyl}zd}|d||f|WYd}~Sd}~ww|S)Nz)finalizing archive '%s' using method '%s'z-An error occurred compressing the archive: %szbuilt archive at '%s' (size=%d)encryptz)An error occurred encrypting the archive:z%s %s) r$r]_build_archiverrr`rArrSrst_sizerY_encrypt)rrCreserrrexp_msgrrrrD2s4    zFileCacheArchive.finalizecCs|dd}|d7}d|}d}|jdr"|d|jd7}||7}|jdrBd |jdd d }d |i}|d 7}d|}||7}t|d|d}|ddkrQ|S|ddkrh|jdrbd}t|d}t|d|d}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'"rPsos_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)replacerYrr)rarchivearc_nameenc_cmdrpasswdrrrrrrJs0       zFileCacheArchive._encryptr)rRrErF)r rGrHrIrVr]rrdrhrkrwrrr,r1r2r5r8r:r>r<r@r^rrArrrDrrrrrrOs8   \K  $ G     rOcsbeZdZdZdZdZ dfdd ZdddZdd Zd d Z d d Z fddZ ddZ Z S)TarFileArchivez: archive class using python TarFile to create tar archivesNFc s:tt||||||||d|_tj|||_dS)Ntar) superrrd_suffixrSr7r\r`rr_ __class__rrrd|s zTarFileArchive.__init__cCsP|j|_d|j|jd<d|j|jd<|r||_n|j|_|j|_|j |_ dS)Nz%.9fatimectime) st_mtimemtimest_atime pax_headersst_ctimer0rruidrgid)rtar_infofstatr0rrrset_tarinfo_from_stats z$TarFileArchive.set_tarinfo_from_statcs|jttj|jdds|jgd}tfdd|Dr&dSzt}Wn ty8|YSw|j rH| }|rH||j d<| |||S)N)z /version.txt$z/sos_logs(/.*)?z/sos_reports(/.*)?c3s|] }t|VqdSr)rematch).0skip orig_pathrr sz9TarFileArchive.copy_permissions_filter..zRHT.security.selinux) r`lenrSr7rqr]rrr_with_selinux_contextget_selinux_contextr r)rtarinfoskipsrcontextrrrcopy_permissions_filters"     z&TarFileArchive.copy_permissions_filtercCs*z t|\}}|WStyYdSwr)selinux getfileconr)rr7rccrrrrs  z"TarFileArchive.get_selinux_contextcCsd|j|jfS)Nz%s.%s)r]rr;rrrr`rzTarFileArchive.namecstt|Sr)rrr>r;rrrr>szTarFileArchive.name_maxcCs|dkrtddur dnd}|d}|jd||_|dkr$ddi}nd d i}tj|jfd d |i|}d D]"}tjtj|j |sGq8|j tj|j ||j d|dq8|j |j |j |j d| |jd|7_|S)Nautolzmaxzgzipipz.%s compresslevelpresetr0zw:%s)z version.txtrsos_logsrQ)arcname)r-filter)rstriprtarfilerrSr7rtr\r]addrMrrBrr`)rrC _comp_modekwargsr_contentrrrrs.   zTarFileArchive._build_archiver)r rGrHrIrCrrdrrrr`r>r __classcell__rrrrrvs   r)rSr0rrKrrrrr threadingrimportlib.utilr sos.utilitiesrr ImportErrorrrrrobjectr rOrrrrrs6       [y