509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
ˆuAc¯  ã               @   s¢   d Z ddlmZ deeƒkr eZdddddgZdd	lZdd	l	Z	dd	l
Z
dd	lZdd	lZd
Zdd„ Zg fdd„Zdd„ Zddd„Zddd„Zddd„Zddd„Zd	S )z=This module contains helper functions for dealing with files.é    )Úabsolute_importÚxrangeÚ
issamefileÚlinkfileÚcopyfileÚlinkorcopyfileÚoverwrite_safelyNi   c             C   s"   t j| ƒ}t j|ƒ}t jj||ƒS )N)ÚosÚstatÚpathÚsamestat)Úpath1Úpath2Ús1Ús2© r   ú/usr/lib/python3.6/files.pyÚ_issamefile+   s    

r   c             C   s0   |dkrt }y
t| |ƒS  |k
r*   dS X dS )zECheck whether two paths point to the same file (i.e. are hardlinked).TFN)Ú	Exceptionr   )r   r   Úcatch_stat_exceptionsr   r   r   r   2   s    
c             C   sÀ   t | |td�rdS tjj|ƒ}tjj|ƒ}tjj|ƒ}d}xpttj	ƒD ]b}tj
|tj |d�}ytj| |ƒ W n2 tk
r  } z|jtjkrŽn‚ W Y dd}~X qFX d}P qFW |r¼tj||ƒ dS )zUHardlink srcpath to dstpath.

    Attempt to atomically replace dstpath if it exists.)r   NF)ÚprefixÚdirT)r   ÚOSErrorr	   r   ÚabspathÚdirnameÚbasenameÚrangeÚtempfileÚTMP_MAXÚmktempÚextsepÚlinkÚerrnoÚEEXISTÚrename)ÚsrcpathÚdstpathÚdstdnameÚdstbnameZ
hardlinkedÚattemptÚ_dsttmpÚer   r   r   r   >   s$    Tc       
      C   s8  t | |td�rdS tjj|ƒ}tjj|ƒ}tjj|ƒ}t| dƒ}tj	|tjj
 |dd�}tj| ƒ}|rŠytj|ƒ}W n tk
rˆ   Y nX tj|jƒ tj|jƒƒ d}	xP|	dkrö|jtƒ}	y|j|	ƒ W q¨   |jƒ  |jƒ  tj|jƒ ‚ Y q¨X q¨W |jƒ  |jƒ  tj|j|ƒ |�r4tjƒ dk�r4tj|ƒ dS )z�Copy srcpath to dstpath.

    Abort operation if e.g. not enough space is available.  Attempt to
    atomically replace dstpath if it exists.)r   NÚrbF)r   r   ÚdeleteÚ r   )r   r   r	   r   r   r   r   Úopenr   ZNamedTemporaryFiler    r
   ÚfchmodÚfilenoÚS_IMODEÚst_modeÚreadÚ	BLOCKSIZEÚwriteÚcloseÚunlinkÚnamer$   ÚselinuxÚis_selinux_enabledÚ
restorecon)
r%   r&   Úcopy_mode_from_dstÚrun_restoreconr'   r(   ZsrcfileZ
dsttmpfileÚsÚdatar   r   r   r   _   s<    



c             C   s^   yt | |ƒ dS  tk
rJ } z |jtjtjtjfkr:‚ n W Y dd}~X nX t| |||ƒ dS )ztFirst attempt to hardlink srcpath to dstpath, if hardlinking isn't
    possible, attempt copying srcpath to dstpath.N)r   r   r"   ZEMLINKZEPERMZEXDEVr   )r%   r&   r=   r>   r+   r   r   r   r   “   s    
Fc             C   sœ  t jj|ƒ}t jj|ƒ}d}d}|r6tjƒ dkr6d}n^y&tj|ƒ\}}|dk rZtd| ƒ‚W n6 tk
r’ }	 z|	j	t	j
kr€d}n‚ W Y dd}	~	X nX |s´t j| |ƒ |r²tj|ƒ näd}
xtttjƒD ]f}tj|t j |d�}yt j| |ƒ W n6 tk
�r" }	 z|	j	t	jk�rwÄ‚ W Y dd}	~	X qÄX |}
P qÄW |
dk�rDtt	jdƒ‚|�r^| �r^tj|
|ƒ yt j|
|ƒ W n   t j|
ƒ ‚ Y nX |�r˜tj|ƒ dS )zpCreate a symlink, optionally replacing dstpath atomically, optionally
    setting or preserving SELinux context.FNr   zgetfilecon(%r) failedT)r   r   z/No suitable temporary symlink could be created.)r	   r   r   r   r:   r;   ZlgetfileconÚRuntimeErrorr   r"   ÚENOENTÚsymlinkr<   r   r   r   r   r    r#   ÚIOErrorZlsetfileconr$   Úremove)r%   r&   ÚforceÚpreserve_contextr'   r(   r>   ÚctxÚretr+   Zdsttmpr)   r*   r   r   r   Úsymlink_atomically«   sV    

rJ   c             C   s~  t jj| ƒ} t jj| ƒ}t jj| ƒ}d}d}d}	t jj| ƒ}
|rPtjƒ dkrPd}zÖtj	|t jj
 |d�\}}	|
rÎt j| ƒ}|r�t j||j|jƒ |r¨t j|tj|jƒƒ |rÎtj| ƒ\}}|dk rÎtd|  ƒ‚t j|dƒ}d}|j|ƒ |jƒ  d}t j|	| ƒ |�r$|
�rtj| |ƒ n
tj| ƒ W d|�r8|jƒ  n|�rHt j|ƒ |	�rxt jj|	ƒ�rxyt j|	ƒ W n   Y nX X dS )zÁSafely overwrite a file by creating a temporary file in the same
    directory, writing it, moving it over the original file, eventually
    preserving file mode, SELinux context and ownership.Nr   F)r   r   zgetfilecon(%r) failedÚw)r	   r   Úrealpathr   r   Úexistsr:   r;   r   Zmkstempr    r
   ÚfchownÚst_uidÚst_gidr0   r2   r3   Z
getfileconrA   Úfdopenr6   r7   r$   Z
setfileconr<   Úisfiler8   )r   ZcontentZpreserve_moderG   Zpreserve_ownershipZdir_ÚbaseÚfdÚfZtmpnamerM   r?   rI   rH   r   r   r   r   é   sR    



)TT)TT)FT)TTT)Ú__doc__Z
__future__r   r   Ú__builtins__r   r   Ú__all__r	   r:   r   r"   r
   r5   r   r   r   r   r   rJ   r   r   r   r   r   Ú<module>   s&   !
5

? 