U
    -_gr                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ zd dlZW n ek
r   Y nX dZdZdZd	ZG d
d dZG dd deZG dd deZdS )    N)datetime)Lock)	find_spec)sos_get_command_outputfilelinkZnodedirc                   @   s   e Zd ZdZedd ZedZdZ	dZ
e Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zd-ddZd.ddZdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, ZdS )/Archivez!Abstract base class for archives.c                 C   s   | j S )z6Returns the archive class's name as a string.
        )__name__)cls r   -/usr/lib/python3/dist-packages/sos/archive.pyarchive_type+   s    zArchive.archive_typeZsosZunsetFc                 C   s   d|    d| S )Nz	[archive:z] )r   selfmsgr   r   r   _format_msg8   s    zArchive._format_msgc                 C   s
   || _ d S N)_debug)r   debugr   r   r   	set_debug;   s    zArchive.set_debugc                 C   s   | j | | d S r   )logerrorr   r   r   r   r   	log_error>   s    zArchive.log_errorc                 C   s   | j | | d S r   )r   Zwarningr   r   r   r   r   log_warnA   s    zArchive.log_warnc                 C   s   | j | | d S r   )r   infor   r   r   r   r   log_infoD   s    zArchive.log_infoc                 C   s    | j s
d S | j| | d S r   )r   r   r   r   r   r   r   r   	log_debugG   s    zArchive.log_debugc                 C   s   | j S r   )_namer   r   r   r   nameL   s    zArchive.nameNc                 C   s   t d S r   NotImplementedError)r   srcdestforcer   r   r   add_fileR   s    zArchive.add_filewc                 C   s   t d S r   r!   )r   contentr$   moder   r   r   
add_stringU   s    zArchive.add_stringc                 C   s   t d S r   r!   )r   r(   r$   r   r   r   
add_binaryX   s    zArchive.add_binaryc                 C   s   t d S r   r!   )r   source	link_namer   r   r   add_link[   s    zArchive.add_linkc                 C   s   t d S r   r!   r   pathr   r   r   add_dir^   s    zArchive.add_dirc                 C   s   t d S r   r!   )r   r0   r)   devicer   r   r   add_nodea   s    zArchive.add_nodec                 C   s   t dS )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.Nr!   r   r   r   r   get_tmp_dird   s    zArchive.get_tmp_dirc                 C   s   t dS )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.Nr!   r   r   r   r   name_maxj   s    zArchive.name_maxc                 C   s   t dS )aT  Return 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
        locatedNr!   r   r   r   r   get_archive_pathp   s    zArchive.get_archive_pathc                 C   s   t dS )z:Clean up any temporary resources used by an Archive class.Nr!   r   r   r   r   cleanupy   s    zArchive.cleanupc                 C   s   t dS )a  Finalize 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, gzipNr!   r   methodr   r   r   finalize}   s    zArchive.finalize)NF)r'   )r
   
__module____qualname____doc__classmethodr   loggingZ	getLoggerr   r   r   r   
_path_lockr   r   r   r   r   r   r    r&   r*   r+   r.   r1   r3   r4   r5   r6   r7   r:   r   r   r   r   r	   (   s2   



	r	   c                   @   s   e Zd ZdZdZdZdZd7ddZdd Zdd	 Z	d8ddZ
d9ddZdd Zd:ddZd;ddZdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd<d%d&Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 ZdS )=FileCacheArchivezd Abstract superclass for archive types that use a temporary cache
    directory in the file system.  Nc              	   C   s   || _ tj| j |g|kr,tj||| _ || _|| _|| _|| _|pJd| _	|| _
tj||| _| j t| jd W 5 Q R X | d| j d d S )N/  z'initialised empty FileCacheArchive at '')r   osr0   commonprefixrelpath_tmp_dirZ_policyZ_threadsenc_optssysrootmanifestjoin_archive_rootr@   makedirsr   r   r    ZtmpdirZpolicyZthreadsrJ   rK   rL   r   r   r   __init__   s    
zFileCacheArchive.__init__c                 C   s(   t j|r|t j}t j| j|S r   )rF   r0   isabslstripseprM   rN   )r   r    r   r   r   	dest_path   s    zFileCacheArchive.dest_pathc                 C   s@   | j r|| j r|S |d tjkr0|dd  }tj| j |S )Nr      )rK   
startswithrF   rT   r0   rM   r/   r   r   r   join_sysroot   s
    zFileCacheArchive.join_sysrootrD   c                    s  |  d|  | j |} fdd}|ds6|}n&tj| |rL|ntj|d }|}g }|dkrtj|\}}	||	 |}qd|	   }
d}|D ]}tj
|
|}
||
sqtj
||}tj|
s|  d|
  tj|rtj|rt|}tj|d }tj
||}| j||d}tj|}tj|rbtj||}|  d	|
 d
| d t||
 q|  d|
  t|
| |}q|S )a  Create 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.
        zMaking leading paths for c                    s   |  tj dS )z>Test whether path ``path`` is inside the archive.
            rB   )rW   rF   r0   rM   )r0   rootr   r   
in_archive   s    z8FileCacheArchive._make_leading_paths.<locals>.in_archiverC   r   )rC   rB   zMaking path r)   zMaking symlink '' -> 'rE   zMaking directory )r   rN   rW   rF   r0   isdirrX   splitappendreverserM   existsislinkreadlink_make_leading_pathsnormpathrR   rH   symlinkmkdir)r   r#   r)   r$   r[   src_dirr0   Z
path_compsheadtailZabs_pathZsrc_pathcomptargetZ
target_dirZ
target_srcr   rY   r   re      sN    



z$FileCacheArchive._make_leading_pathsFc           
      C   s\  |p|  |}|tkr|}ntj|d }|s4|S tj|r\tj|s\td| dtj|s|tkrt|ntj|d }| | dd }|r|S tj|rXt	|}d}	|t
krt|jst|	|df |tkrt|jst|	|df |tkr,||js,t|	|d	f |tkrTt|jsTt|	|d
f dS |S )a4  Check 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
        r   zpath 'z' exists and is not a directoryc                 S   s(   t t| t| t| t| gS r   )anystatS_ISBLKS_ISCHRS_ISFIFOS_ISSOCKr\   r   r   r   
is_special7  s    z/FileCacheArchive.check_path.<locals>.is_specialz path '%s' exists and is not a %szregular filezsymbolic linkzspecial fileZ	directoryN)rU   P_DIRrF   r0   r_   rb   r^   
ValueErrorre   lstatP_FILEro   S_ISREGst_modeP_LINKS_ISLNKP_NODES_ISDIR)
r   r#   	path_typer$   r%   dest_dirri   rt   stZve_msgr   r   r   
check_path  s6    

zFileCacheArchive.check_pathc              
   C   s   zdt |}|ds |drDt|| t j||j|jfd nt|| t 	||j
|j W n: tk
r } z| d| d| d W 5 d }~X Y nX d S )N/sys//proc/)nszcaught 'z' setting attributes of 'rE   )rF   ro   rW   shutilZcopymodeutimest_atime_nsst_mtime_nsZcopystatchownst_uidst_gid	Exceptionr   )r   r#   r$   _stater   r   r   _copy_attributesS  s    
z!FileCacheArchive._copy_attributesc                 C   s   | j  |s|}| j|t|d}|s4W 5 Q R  d S t|dd szt|| W nP tk
r } z2|ds|drxn| d| d| d W 5 d }~X Y nX | 	|| d| d}n<|
d t|d	d
d}|D ]}|| qW 5 Q R X d}| d| d| j d W 5 Q R X d S )Nr%   readr   r   zFile z not collected: 'rE   r   r'   utf-8encodingz	open filezadded z to FileCacheArchive ')r@   r   rx   getattrr   copyOSErrorrW   r   r   seekopenwriter   rN   )r   r#   r$   r%   r   	file_namefliner   r   r   r&   `  s*    
*
zFileCacheArchive.add_filer'   c              
   C   s   | j  |}| j|tdd}tj||ddX}t|trD|dd}|| t	j
|rf| || | d| d| j d	 W 5 Q R X W 5 Q R X d S )
NTr   r   r   utf8ignorezadded string at 'z' to FileCacheArchive 'rE   )r@   r   rx   codecsr   
isinstancebytesdecoder   rF   r0   rb   r   r   rN   )r   r(   r$   r)   r#   r   r   r   r   r*     s    

zFileCacheArchive.add_stringc              
   C   st   | j d | |t}|s&W 5 Q R  d S tj|dd d}|| W 5 Q R X | d| d| j d W 5 Q R X d S )Nwbr   zadded binary content at 'z' to archive 'rE   )r@   r   rx   r   r   r   r   rN   )r   r(   r$   r   r   r   r   r+     s    zFileCacheArchive.add_binaryc           	   
   C   s  |  d| d| d | j\ | |t}|s>W 5 Q R  d S tj|svt|| |  d| d| d| j d W 5 Q R X |  d| d| d	|  tj	|}tj
tj||}| |}d
d }tj|stj|rl|}tj	|}tj|t|}tj||}|||rF|  d| d| d d S |  d| d| d | || nntj|r|  d| d | | nBtj|r|  d| d | | n|  d| d|  d S )Nzadding symlink at 'r]   rE   zadded symlink at 'z' to 'z' in archive 'zLink follow up: source=z link_name=z dest=c              
   S   s   t j| }t j|s.t jt j||}t j| } | |krFdS zt |  W n8 tk
r } z|jdkrzW Y 
dS  W 5 d}~X Y nX dS )zReturn ``True`` if the symbolic link ``link_name`` is part
                of a file system loop, or ``False`` otherwise.
            T(   NF)	rF   r0   dirnamerR   realpathrM   ro   r   errno)r-   r,   Zlink_dirr   r   r   r   is_loop  s    

z*FileCacheArchive.add_link.<locals>.is_loopzLink 'z' - 'z' loops: skipping...zAdding link z -> z for link follow upzAdding dir zAdding file zNo link follow up: source=)r   r@   r   r{   rF   r0   lexistsrg   rN   r   r   rM   rU   rb   rc   rd   rH   r.   r^   r1   isfiler&   )	r   r,   r-   r$   Z
source_dirZhost_path_nameZdest_path_namer   r   r   r   r   r.     s>    *
zFileCacheArchive.add_linkc              	   C   s"   | j  | |t W 5 Q R X dS )zmCreate a directory in the archive.

            :param path: the path in the host file system to add
        N)r@   r   ru   r/   r   r   r   r1     s    zFileCacheArchive.add_dirc              
   C   s   |  |t}|sd S tj|szt||| W nX tk
r } z:|jtjkrvd}| 	d| d| d W Y d S |W 5 d }~X Y nX | 
|| d S )NzOperation not permittedz
add_node: z
 - mknod 'rE   )r   r}   rF   r0   rb   mknodr   r   ZEPERMr   r   )r   r0   r)   r2   r$   r   r   r   r   r   r3     s    
zFileCacheArchive.add_nodec                 C   s&   dt jkr"t jd }t | j|S dS )NPC_NAME_MAX   )rF   pathconf_namespathconfrN   )r   Zpc_name_maxr   r   r   r5     s    

zFileCacheArchive.name_maxc                 C   s   | j S r   rN   r   r   r   r   r4   	  s    zFileCacheArchive.get_tmp_dirc                 C   s   | j S r   r   r   r   r   r   r6     s    z!FileCacheArchive.get_archive_pathc                 C   s8   t jt j| j||d | d| d| j d dS )zsCreate path, including leading components.

            Used by sos.sosreport to set up sos_* directories.
        r\   zcreated directory at 'z' in FileCacheArchive 'rE   N)rF   rO   r0   rM   rN   r   )r   r0   r)   r   r   r   rO     s    zFileCacheArchive.makedirsc                 C   s   |  |}tj|ddddS )Nrr   r   )r   errors)rU   r   r   r/   r   r   r   	open_file  s    
zFileCacheArchive.open_filec                 C   s   t j| jrt| j d S r   )rF   r0   r^   rN   r   Zrmtreer   r   r   r   r7     s    zFileCacheArchive.cleanupc                 C   sf   t  }| jj}|| }| jd| | jd| | jd| | | jjddtj	dd dS )	zAdds component-agnostic data to the manifest so that individual
        SoSComponents do not need to redundantly add these manually
        Zend_timerun_timeZcompression   )indentsos_reportszmanifest.jsonN)
r   ZnowrL   Z
start_timeZ	add_fieldr*   Zget_jsonrF   r0   rM   )r   r9   endstartr   r   r   r   add_final_manifest_data   s    z(FileCacheArchive.add_final_manifest_datac                 C   sN   | | j| _tj| j| j}t| j| || _tj| j|  | _	dS )zeRename the archive to an obfuscated version using an initialized
        SoSCleaner instance
        N)
Zobfuscate_stringr   rF   r0   rM   rI   renamerN   r    _archive_name)r   ZcleanerZ	_new_rootr   r   r   rename_archive_root-  s
    z$FileCacheArchive.rename_archive_rootc              	   C   s   t jt jB }t|dr(|j}|j|B }n|}|}d}| |}| }W 5 Q R X t|t	sh|
dd}t j||||d\}	}
|
r| |	| nd}
|
S )a  Apply 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``
        patternrB   r   r   )flagsr   )re
IGNORECASE	MULTILINEhasattrr   r   r   r   r   strr   subnr*   )r   r0   ZregexpZsubstZcommon_flagsr   r   r(   readableresultZreplacementsr   r   r   do_file_sub7  s$    



zFileCacheArchive.do_file_subc              
   C   s   |  d| j d| d z| |}W n> tk
rf } z | d|  |   W Y S d }~X Y nX |   |  d| j dt	| jj
 d | jd rz| |W S  tk
r } z$d	}| | d
|  | W Y S d }~X Y nX n|S d S )Nzfinalizing archive 'z' using method 'rE   z+An error occurred compressing the archive: zbuilt archive at 'z' (size=)Zencryptz)An error occurred encrypting the archive: )r   rN   _build_archiver   r   r    r7   r   rF   ro   st_sizerJ   _encrypt)r   r9   reserrr   Zexp_msgr   r   r   r:   \  s     $
zFileCacheArchive.finalizec                 C   s   | dd}|d7 }d| d}d}| jd rL|d| jd  d7 }||7 }| jd	 r| jd	  d
d}d|i}|d7 }d| d}||7 }t|d|d}|d dkr|S |d dkr| jd rd}qd}nd|d  }t|dS )a  Encrypts 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-zsecured-sosreport-z.gpgzgpg --batch -o r   Nkeyz--trust-model always -e -r Zpasswordz'"rB   Zsos_gpgz-c --passphrase-fd 0 z/bin/bash -c "echo $sos_gpg | "r   )ZtimeoutenvZstatus   zSpecified key not in keyringzCould not read passphrasezgpg exited with code )replacerJ   r   r   )r   archiveZarc_nameZenc_cmdr   Zpasswdr   r   r   r   r   r   s  s,    


zFileCacheArchive._encryptc                 C   s   |   S r   )r    r8   r   r   r   r     s    zFileCacheArchive._build_archive)N)rD   )NF)NF)r'   )rD   )r
   r;   r<   r=   rI   rN   r   rQ   rU   rX   re   r   r   r&   r*   r+   r.   r1   r3   r5   r4   r6   rO   r   r7   r   r   r   r:   r   r   r   r   r   r   rA      s:    

]
K
$
G	
	
%+rA   c                       sT   e Zd ZdZdZdZd fdd	ZdddZdd	 Zd
d Z	dd Z
dd Z  ZS )TarFileArchivez: archive class using python TarFile to create tar archivesNFc              	      s6   t  ||||||| d| _tj||  | _d S )Ntar)superrQ   _suffixrF   r0   rM   r    r   rP   	__class__r   r   rQ     s       zTarFileArchive.__init__c                 C   sP   |j |_|jd|jd< |jd|jd< |r4||_n|j|_|j|_|j	|_
d S )Nz.9fZatimectime)st_mtimemtimest_atimepax_headersst_ctimer)   rz   r   Zuidr   gid)r   Ztar_infofstatr)   r   r   r   set_tarinfo_from_stat  s    z$TarFileArchive.set_tarinfo_from_statc                    s   |j ttj| jd d    s*| j dddg}t fdd|D rNd S zt }W n tk
rt   | Y S X | j	r| 
 }|r||jd< | || |S )Nz/version.txt$z/sos_logs(/.*)?z/sos_reports(/.*)?c                 3   s   | ]}t | V  qd S r   )r   match).0skip	orig_pathr   r   	<genexpr>  s     z9TarFileArchive.copy_permissions_filter.<locals>.<genexpr>zRHT.security.selinux)r    lenrF   r0   r_   rN   rn   ro   r   _with_selinux_contextget_selinux_contextr   r   )r   ZtarinfoZskipsr   contextr   r   r   copy_permissions_filter  s      



z&TarFileArchive.copy_permissions_filterc                 C   s0   zt |\}}|W S  tk
r*   Y d S X d S r   )selinuxZ
getfileconr   )r   r0   _cr   r   r   r     s
    z"TarFileArchive.get_selinux_contextc                 C   s   | j  d| j S )N.)rN   r   r   r   r   r   r      s    zTarFileArchive.namec              	   C   s   |dkrt dd k	rdnd}|d}| j d| | _|dkrJddi}nd	d
i}tj| jfdd| i|d}dD ]B}tjtj| j	|rv|j
tj| j	|| j d| d qv|j
| j	| j| jd W 5 Q R X |  jd| 7  _|  S )NautoZlzmaZxzZgzipZipr   Zcompresslevel   Zpreset   r)   zw:)zversion.txtr   Zsos_logsrC   )arcname)r   filter)r   stripr   tarfiler   rF   r0   rb   rM   rN   addr   r   r   r    )r   r9   Z
_comp_modekwargsr   Z_contentr   r   r   r     s,    

zTarFileArchive._build_archive)N)N)r
   r;   r<   r=   r9   r   rQ   r   r   r   r    r   __classcell__r   r   r   r   r     s    	
r   )rF   r   r   r?   r   r   ro   r   r   Z	threadingr   importlib.utilr   Zsos.utilitiesr   r   ImportErrorrx   r{   r}   ru   r	   rA   r   r   r   r   r   <module>   s4   ]    !