9Qc@sdZdddddgZddlZddlZddlZddlZddlZdZd Zgd Z d Z e e d Z e e d Z ee dZe e dZdS(s=This module contains helper functions for dealing with files.t issamefiletlinkfiletcopyfiletlinkorcopyfiletoverwrite_safelyiNicCs1tj|}tj|}tjj||S(N(toststattpathtsamestat(tpath1tpath2ts1ts2((s3/usr/lib/python2.7/site-packages/slip/util/files.pyt _issamefile#scCs?|tkrt}nyt||SWn|k r:tSXdS(sECheck whether two paths point to the same file (i.e. are hardlinked).N(tTruet ExceptionR tFalse(R R tcatch_stat_exceptions((s3/usr/lib/python2.7/site-packages/slip/util/files.pyR*s    cCst||dtrdStjj|}tjj|}tjj|}t}xtt j D]n}t j d|tj d|}ytj ||Wn+tk r}|jtjkrqqeXt}PqeW|rtj||ndS(sUHardlink srcpath to dstpath. Attempt to atomically replace dstpath if it exists.RNtprefixtdir(RtOSErrorRRtabspathtdirnametbasenameRtxrangettempfiletTMP_MAXtmktemptextseptlinkterrnotEEXISTRtrename(tsrcpathtdstpathtdstdnametdstbnamet hardlinkedtattemptt_dsttmpte((s3/usr/lib/python2.7/site-packages/slip/util/files.pyR6s$c Cst||dtrdStjj|}tjj|}tjj|}t|d}tj d|tjj d|dt }t }|ryt j ||jt}Wqt jtfk rqXn|st j ||jnd} xa| dkrT|jt} y|j| Wq|j|jtj|jqXqW|j|jtj|j||rtjdkrtj|ndS( sCopy srcpath to dstpath. Abort operation if e.g. not enough space is available. Attempt to atomically replace dstpath if it exists.RNtrbRRtdeleteti(RRRRRRRtopenRtNamedTemporaryFileRRtshutiltcopymodetnameRtErrortNonetreadt BLOCKSIZEtwritetclosetunlinkR tselinuxtis_selinux_enabledt restorecon( R!R"tcopy_mode_from_dsttrun_restoreconR#R$tsrcfilet dsttmpfilet mode_copiedtdata((s3/usr/lib/python2.7/site-packages/slip/util/files.pyRWs@      cCsiyt||dSWn:tk rQ}|jtjtjtjfkrRqRnXt||||dS(stFirst attempt to hardlink srcpath to dstpath, if hardlinking isn't possible, attempt copying srcpath to dstpath.N(RRRtEMLINKtEPERMtEXDEVR(R!R"R;R<R(((s3/usr/lib/python2.7/site-packages/slip/util/files.pyRs !c Cstjj|}tjj|}t}d}|rQtjdkrQt}nly8tj|\}}|dkrt d|nWn1t k r} | j t j krt }qnX|stj|||rtj|qnd} xttjD]q} tjd|tjd|} ytj|| Wn.t k rk} | j t jkreqnqX| } PqW| dkrtt jdn|r| rtj| |nytj| |Wntj| nX|rtj|ndS(spCreate a symlink, optionally replacing dstpath atomically, optionally setting or preserving SELinux context.isgetfilecon(%r) failedRRs/No suitable temporary symlink could be created.N(RRRRRR2R8R9t lgetfilecont RuntimeErrorRRtENOENTRtsymlinkR:RRRRRRtIOErrort lsetfileconR tremove( R!R"tforcetpreserve_contextR#R$R<tctxtretR(tdsttmpR&R'((s3/usr/lib/python2.7/site-packages/slip/util/files.pytsymlink_atomicallysR        c Cstjj|}tjj|}tjj|}d}d}d}tjj|} |r{tjdkr{t }nzt j d|tjj d|\}}| r|rt j||n| r|rtj|\} } | dkrtd|qntj|d}d}|j||jd}tj|||r|| rltj|| q|tj|nWd|r|jn|rtj|n|rtjj|rytj|WqqXnXdS(sSafely 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.iRRsgetfilecon(%r) failedtwN(RRtrealpathRRR2texistsR8R9RRtmkstempRR.R/t getfileconREtfdopenR5R6R t setfileconR:tisfileR7( Rtcontentt preserve_modeRLtdir_tbasetfdtfttmpnameRSRNRM((s3/usr/lib/python2.7/site-packages/slip/util/files.pyRsJ       (t__doc__t__all__RR8R.RRR4R RRRRRRRPR(((s3/usr/lib/python2.7/site-packages/slip/util/files.pyts          !6 ;