
    	^c0                     `   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m	Z	m
Z
mZmZ ej        rd dlmZmZmZmZ  e
j        dd          Z e
j        d          Zd	 Z G d
 d          ZddZd Zd Z G d d          Z ej        ed          rej        ZeeZej        Zej        ZdS )    N   )_)encodingerrorpolicypycompatutil)AnyCallableIteratorOptionaldirstateDirsparsersc                 N    t          j        |                                           S N)r   hfsignorecleanlower)ss    4/usr/lib/python3/dist-packages/mercurial/pathutil.py_lowercleanr      s    "17799---    c                   N    e Zd ZdZd
dZddZd Zd Zej	        d	             Z
dS )pathauditora^  ensure that a filesystem path contains no banned components.
    the following properties of a path are checked:

    - ends with a directory separator
    - under top-level .hg
    - starts at the root of a windows drive
    - contains ".."

    More check are also done about the file system states:
    - traverses a symlink (e.g. a/symlink_here/b)
    - inside a nested repository (a callback can be used to approve
      some nested repositories, e.g., subrepositories)

    The file system checks are only done when 'realfs' is set to True (the
    default). They should be disable then we are auditing path for operation on
    stored history.

    If 'cached' is set to True, audited paths and sub-directories are cached.
    Be careful to not keep the cache of unmanaged directories for long because
    audited paths may be replaced with symlinks.
    NTFc                 &   t                      | _        t                      | _        || _        || _        || _        || _        t          j        	                    |          r't          j        |          st          j        | _        d S d | _        d S )Nc                     | S r    )xs    r   <lambda>z&pathauditor.__init__.<locals>.<lambda>C   s    a r   )setaudited
auditeddirroot_realfs_cachedcallbackospathlexistsr	   fscasesensitivenormcase)selfr#   r&   realfscacheds        r   __init__zpathauditor.__init__9   sv    uu%%	 7??4   	()=d)C)C 	( MDMMM'KDMMMr   c                 f   t          j        |          }|                     |          }|| j        v rdS t          j        |          r$t          j        t          d          |z            t          j        |          }t          j
                            |          d         s%t          |d                   dv st          j        |v r$t          j        t          d          |z            |D ]m}d|v rg|                    dd          \  }}|                                r:|                                dv r$t          j        t          d          |z            nd	t          |          v rd
 |D             }dD ]v}||dd         v rh|                    |          }	t          j
        j        |d|	          }
t          j        t          d          |t          j        |
          fz            wt          j        |          }t+          |          t+          |          k    sJ |                                 |                                 t/          t+          |                    D ]}t          j                            |d|dz                      }t          j                            |d|dz                      }|| j        v r`| j        r|                     ||           | j        r| j                            |           | j        r| j                            |           dS dS )zPCheck the relative path.
        path may contain a pattern (e.g. foodir/**.txt)Ns$   path ends in directory separator: %sr   )   .hg   .hg.r   s#   path contains illegal component: %s   ~r   )s   HGs   HG8B6Cr1   c                 ,    g | ]}t          |          S r   )r   ).0ps     r   
<listcomp>z(pathauditor.__call__.<locals>.<listcomp>e   s    444k!nn444r   )r1   r2   "   path '%s' is inside nested repo %r)r	   	localpathr+   r!   endswithsepr   
InputErrorr   	splitpathr'   r(   
splitdriver   r   ospardirsplitisdigitupperindexjoinbytestrlenpoprangeossepr"   r$   _checkfsr%   add)r,   r(   modenormpathpartsr6   firstlastlpartsposbase	normpartsiprefix
normprefixs                  r   __call__zpathauditor.__call__E   s8   
 ~d##==&&t|##FD!! 	"9::TA   t$$Gt$$Q'	58$$(>>> E))"899D@    	 	AqyyggdA..t<<>> ekkmm7I&I&I*@AADH   [&&&&44e444F$  qrr
?? ,,q//C7<tt5D*?@@!1$!7!789   # N8,,	5zzS^^++++		 s5zz"" 	0 	0A^((wQw88F!,,YwQw-?@@JT_,,| ,fd+++| 0##J///< 	'LX&&&&&	' 	'r   c                 *   t           j                            | j        |          }	 t          j        |          }t          j        |j                  rLt          d          t          j
        |          t          j
        |          fz  }t          j        |          t          j        |j                  rt           j                            t           j                            |d                    rX| j        r|                     |          s>t          d          }t          j        ||t          j
        |          fz            dS dS dS # t           $ r6}|j        t"          j        t"          j        t"          j        fvr Y d}~dS d}~ww xY w)z3raise exception if a file system backed check failss"   path %r traverses symbolic link %rr1   r8   N)r'   r(   rC   r#   lstatstatS_ISLNKst_moder   r   rD   r   AbortS_ISDIRisdirr&   OSErrorerrnoENOENTENOTDIREINVAL)r,   rU   r(   curpathstmsgerrs          r   rI   zpathauditor._checkfs   s    ',,ty&11	N'""B |BJ'' N=>>$T**$V,,B  k#&&&bj)) NbgmmWf--/ / N } NDMM',B,B NABBC+cT83CF3K3K,L&LMMMN N N NN N  	 	 	 yu}el KKK LKKKKK	s   E 
F+FFc                 X    	  | |           dS # t           t          j        f$ r Y dS w xY wNTF)r`   r   r]   )r,   r(   s     r   checkzpathauditor.check   sB    	DJJJ4% 	 	 	55	s    ))c              #   *  K   | j         rd V  d S 	 d| _         d V  | j                                         | j                                         d| _         d S # | j                                         | j                                         d| _         w xY wrj   )r%   r!   clearr"   r,   s    r   r.   zpathauditor.cached   s      < 		%EEEEE%#""$$$%%'''$ ""$$$%%'''$$$$$s   A ;Brj   r   )__name__
__module____qualname____doc__r/   rW   rI   rk   
contextlibcontextmanagerr.   r   r   r   r   r   "   s         ,
( 
( 
( 
(?' ?' ?' ?'BN N N2   
% 
% 
% 
% 
%r   r   c                    t          j        |           r| }n| t          j        z   }|}t          j                            |          s!t          j                            | ||          }t          j                            |          }|t          |           }||k    rK|
                    |          r6|t          |          d         } ||           t          j        |          S || k    rdS g }	 	 t          j        ||           }n# t          $ r d}Y nw xY w|rJ|sdS |                                 t	          j        j        | } ||           t          j        |          S t          j        |          \  }}	|                    |	           ||k    rn|}d}
	 || k    rct%          | | ||           t          j        | |d          }|                    t          j                  r
|dd         }t+          d          |z  }
n# t,          j        $ r Y nw xY wt-          j        t+          d          || fz  |
          )	a  return the canonical path of myname, given cwd and root

    >>> def check(root, cwd, myname):
    ...     a = pathauditor(root, realfs=False)
    ...     try:
    ...         return canonpath(root, cwd, myname, a)
    ...     except error.Abort:
    ...         return 'aborted'
    >>> def unixonly(root, cwd, myname, expected='aborted'):
    ...     if pycompat.iswindows:
    ...         return expected
    ...     return check(root, cwd, myname)
    >>> def winonly(root, cwd, myname, expected='aborted'):
    ...     if not pycompat.iswindows:
    ...         return expected
    ...     return check(root, cwd, myname)
    >>> winonly(b'd:\\repo', b'c:\\dir', b'filename')
    'aborted'
    >>> winonly(b'c:\\repo', b'c:\\dir', b'filename')
    'aborted'
    >>> winonly(b'c:\\repo', b'c:\\', b'filename')
    'aborted'
    >>> winonly(b'c:\\repo', b'c:\\', b'repo\\filename',
    ...         b'filename')
    'filename'
    >>> winonly(b'c:\\repo', b'c:\\repo', b'filename', b'filename')
    'filename'
    >>> winonly(b'c:\\repo', b'c:\\repo\\subdir', b'filename',
    ...         b'subdir/filename')
    'subdir/filename'
    >>> unixonly(b'/repo', b'/dir', b'filename')
    'aborted'
    >>> unixonly(b'/repo', b'/', b'filename')
    'aborted'
    >>> unixonly(b'/repo', b'/', b'repo/filename', b'filename')
    'filename'
    >>> unixonly(b'/repo', b'/repo', b'filename', b'filename')
    'filename'
    >>> unixonly(b'/repo', b'/repo/subdir', b'filename', b'subdir/filename')
    'subdir/filename'
    Nr   TFs   consider using '--cwd %s's   %s not under root '%s')hint)r	   r:   r   rH   r'   r(   isabsrC   rL   r   
startswithrE   pconvertsamefiler`   reverser?   append	canonpathpathtoendswithr   r   r]   )r#   cwdmynameauditorrootsepnamerelr   dirnamebasenamerw   relpaths               r   r~   r~      sv   V  ('D7== -w||D#t,,7D!!Dd##w4??733CLLNN#}T"""	s 	M$--    + 3w|S)}T*** $
4 0 0GXJJx   $D#	* 	d{{$fg666+dC55##HN33 +%crclG566@{ 	 	 	D	 k'((FD>9
 
 
 	
s%   6D DD#A)H HHc                     t           j                            |           \  }}t          |          t          t          j                  k    r| t          j        z   S | S )a  normalize the specified path as path prefix

    Returned value can be used safely for "p.startswith(prefix)",
    "p[len(prefix):]", and so on.

    For efficiency, this expects "path" argument to be already
    normalized by "os.path.normpath", "os.path.realpath", and so on.

    See also issue3033 for detail about need of this function.

    >>> normasprefix(b'/foo/bar').replace(pycompat.ossep, b'/')
    '/foo/bar/'
    >>> normasprefix(b'/').replace(pycompat.ossep, b'/')
    '/'
    )r'   r(   r=   rE   r   rH   )r(   dr6   s      r   normasprefixr     sJ    " 7d##DAq
1vvX^$$$$hn$$r   c              #      K   |                      d          }|dk    r)| d |         V  |                      dd|          }|dk    )dV  d S )N   /rv   r   r   )rfind)r(   rQ   s     r   finddirsr   4  sc      
**T

C
))4C4jjjq#&& )) IIIIIr   c                   2    e Zd ZdZd	dZd Zd Zd Zd ZdS )
dirsz6a multiset of directory names from a set of file pathsFc                    i | _         | j        }t          |t                    r4|r2|                                D ]\  }}|j        dk    r ||           dS |rd}t          j        |          |D ]} ||           dS )zS
        a dict map indicates a dirstate while a list indicates a manifest
           rs3   `only_tracked` is only supported with a dict sourceN)_dirsaddpath
isinstancedictitemsstater   ProgrammingError)r,   maponly_trackedr   fr   rg   s          r   r/   zdirs.__init__@  s     
,c4   		\ 				  17d??GAJJJ   	HC(---  



 r   c                     | j         }t          |          D ]E}|                    d          rt          d|z            ||v r||xx         dz  cc<    d S d||<   Fd S )Nr   z-found invalid consecutive slashes in path: %rr   )r   r   r   
ValueErrorr,   r(   r   rR   s       r   r   zdirs.addpathQ  s    zTNN 	 	D}}T""  CdJ   t||T


a


DJJ	 	r   c                 |    | j         }t          |          D ]$}||         dk    r||xx         dz  cc<    d S ||= %d S )Nr   )r   r   r   s       r   delpathzdirs.delpath^  sZ    zTNN 	 	DDzA~~T


a


T

		 	r   c                 *    t          | j                  S r   )iterr   rn   s    r   __iter__zdirs.__iter__g  s    DJr   c                     || j         v S r   )r   )r,   r   s     r   __contains__zdirs.__contains__j  s    DJr   N)F)	ro   rp   rq   rr   r/   r   r   r   r   r   r   r   r   r   =  sj        @@   "             r   r   r   ) rs   ra   r'   	posixpathrZ   i18nr    r   r   r   r   r	   TYPE_CHECKINGtypingr
   r   r   r   
importrustrustdirs	importmodr   r   r   r~   r   r   r   safehasattrrC   r   r   r   r   <module>r      s        				                                      6Z00
&
9
%
%. . .
P% P% P% P% P% P% P% P%fd
 d
 d
 d
N  0  / / / / / / / /d 4GV$$ <DD ~

r   