3 ˆuAc¯ ã @ s¢ d Z ddlmZ deeƒkr eZdddddgZdd lZdd l Z dd l Z dd lZdd lZd Z dd„ Zg fd d„Zdd„ Zddd„Zddd„Zddd„Zddd„Zd 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 jj||ƒS )N)ÚosÚstatÚpathÚsamestat)Úpath1Úpath2Ús1Ús2© r ú/usr/lib/python3.6/files.pyÚ_issamefile+ s r c C s0 |dkrt }y t| |ƒS |k r* dS X dS )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 | |td�rdS tjj|ƒ}tjj|ƒ}tjj|ƒ}d}xpttj ƒD ]b}tj |tj |d�}ytj| |ƒ W n2 tk r } z|j t jkrŽn‚ W Y dd}~X qFX d}P qFW |r¼tj||ƒ dS )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 | |td�rdS tjj|ƒ}tjj|ƒ}tjj|ƒ}t| dƒ}tj |tjj |dd�}tj| ƒ}|rŠytj|ƒ}W n tk rˆ Y nX tj|j ƒ tj|jƒƒ d} xP| dkrö|jtƒ} y|j| ƒ W q¨ |jƒ |jƒ tj|jƒ ‚ Y q¨X q¨W |jƒ |jƒ tj|j|ƒ |�r4tjƒ dk�r4tj|ƒ dS )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 ZNamedTemporaryFiler r ÚfchmodÚfilenoÚS_IMODEÚst_modeÚreadÚ BLOCKSIZEÚwriteÚcloseÚunlinkÚnamer$ ÚselinuxÚis_selinux_enabledÚ restorecon) r% r&