
9Qc           @   s   d  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	   Z g  d
  Z	 d   Z
 e e d  Z e e d  Z e e d  Z e e d  Z d S(   s=   This module contains helper functions for dealing with files.t
   issamefilet   linkfilet   copyfilet   linkorcopyfilet   overwrite_safelyiNi   c         C   s1   t  j |   } t  j |  } t  j j | |  S(   N(   t   ost   statt   patht   samestat(   t   path1t   path2t   s1t   s2(    (    s3   /usr/lib/python2.7/site-packages/slip/util/files.pyt   _issamefile#   s    c         C   s?   | t  k r t } n  y t |  |  SWn | k
 r: t SXd S(   sE   Check whether two paths point to the same file (i.e. are hardlinked).N(   t   Truet	   ExceptionR   t   False(   R	   R
   t   catch_stat_exceptions(    (    s3   /usr/lib/python2.7/site-packages/slip/util/files.pyR    *   s    	c         C   s   t  |  | d t r d St j j |  } t j j |  } t j j |  } t } x t t	 j
  D]n } t	 j d | t j d |  } y t j |  |  Wn+ t k
 r } | j t j k r q   qe Xt } Pqe W| r t j | |  n  d S(   sU   Hardlink srcpath to dstpath.

    Attempt to atomically replace dstpath if it exists.R   Nt   prefixt   dir(   R    t   OSErrorR   R   t   abspatht   dirnamet   basenameR   t   xranget   tempfilet   TMP_MAXt   mktempt   extsept   linkt   errnot   EEXISTR   t   rename(   t   srcpatht   dstpatht   dstdnamet   dstbnamet
   hardlinkedt   attemptt   _dsttmpt   e(    (    s3   /usr/lib/python2.7/site-packages/slip/util/files.pyR   6   s$    c   
      C   s  t  |  | d t r d St j j |  } t j j |  } t j j |  } t |  d  } t j	 d | t j j
 d | d t  } t } | r y t j | | j  t } Wq t j t f k
 r q Xn  | s t j |  | j  n  d }	 xa |	 d k rT| j t  }	 y | j |	  Wq | j   | j   t j | j    q Xq W| j   | j   t j | j |  | rt j   d k rt j |  n  d S(	   s   Copy srcpath to dstpath.

    Abort operation if e.g. not enough space is available.  Attempt to
    atomically replace dstpath if it exists.R   Nt   rbR   R   t   deletet    i    (   R    R   R   R   R   R   R   t   openR   t   NamedTemporaryFileR   R   t   shutilt   copymodet   nameR   t   Errort   Nonet   readt	   BLOCKSIZEt   writet   closet   unlinkR    t   selinuxt   is_selinux_enabledt
   restorecon(
   R!   R"   t   copy_mode_from_dstt   run_restoreconR#   R$   t   srcfilet
   dsttmpfilet   mode_copiedt   data(    (    s3   /usr/lib/python2.7/site-packages/slip/util/files.pyR   W   s@    




c         C   si   y t  |  |  d SWn: t k
 rQ } | j t j t j t j f k rR   qR n Xt |  | | |  d S(   st   First attempt to hardlink srcpath to dstpath, if hardlinking isn't
    possible, attempt copying srcpath to dstpath.N(   R   R   R   t   EMLINKt   EPERMt   EXDEVR   (   R!   R"   R;   R<   R(   (    (    s3   /usr/lib/python2.7/site-packages/slip/util/files.pyR      s    !c         C   s   t  j j |  } t  j j |  } t } d } | rQ t j   d k rQ t } nl y8 t j |  \ } } | d k  r t	 d |   n  Wn1 t
 k
 r }	 |	 j t j k r t } q   n X| s t  j |  |  | rt j |  qnd }
 x t t j  D]q } t j d | t  j d |  } y t  j |  |  Wn. t
 k
 rk}	 |	 j t j k reqn    qX| }
 PqW|
 d k rt t j d   n  | r| rt j |
 |  n  y t  j |
 |  Wn t  j |
    n X| rt j |  n  d S(   sp   Create a symlink, optionally replacing dstpath atomically, optionally
    setting or preserving SELinux context.i    s   getfilecon(%r) failedR   R   s/   No suitable temporary symlink could be created.N(   R   R   R   R   R   R2   R8   R9   t   lgetfilecont   RuntimeErrorR   R   t   ENOENTR   t   symlinkR:   R   R   R   R   R   R   t   IOErrort   lsetfileconR    t   remove(   R!   R"   t   forcet   preserve_contextR#   R$   R<   t   ctxt   retR(   t   dsttmpR&   R'   (    (    s3   /usr/lib/python2.7/site-packages/slip/util/files.pyt   symlink_atomically   sR    			c         C   s  t  j j |   }  t  j j |   } t  j j |   } d } d } d } t  j j |   }	 | r{ t j   d k r{ t	 } n  zt
 j d | t  j j d |  \ } } |	 r | r t j |  |  n  |	 r| rt j |   \ }
 } |
 d k  rt d |    qn  t  j | d  } d } | j |  | j   d } t  j | |   | r||	 rlt j |  |  q|t j |   n  Wd | r| j   n | rt  j |  n  | rt  j j |  ry t  j |  WqqXn  Xd S(   s   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 and SELinux context.i    R   R   s   getfilecon(%r) failedt   wN(   R   R   t   realpathR   R   R2   t   existsR8   R9   R   R   t   mkstempR   R.   R/   t
   getfileconRE   t   fdopenR5   R6   R    t
   setfileconR:   t   isfileR7   (   R   t   contentt   preserve_modeRL   t   dir_t   baset   fdt   ft   tmpnameRS   RN   RM   (    (    s3   /usr/lib/python2.7/site-packages/slip/util/files.pyR      sJ    	
(   t   __doc__t   __all__R   R8   R.   R   R   R4   R   R    R   R   R   R   R   RP   R   (    (    (    s3   /usr/lib/python2.7/site-packages/slip/util/files.pyt   <module>   s    			!6;