
    6h2                       % S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJrJ	r	J
r
Jr  SSKJr  SSKJrJrJrJrJr  SSKJr  \R.                  " SS	S
S9r\R2                  " S5      rSS jr " S S5      r S         SS jjrSS jrSS jrSS jr  " S S\RB                  5      r"\#" \S5      (       a  \RD                  r"\b  \r"\RH                  r$\RJ                  r%S\&S'   g)    )annotationsN)AnyCallableIteratorOptional   )_)encodingerrorpolicypycompatutil)miscdirstateDirsT)pyo3parsersc                J    [         R                  " U R                  5       5      $ N)r
   hfsignorecleanlower)ss    4/usr/lib/python3/dist-packages/mercurial/pathutil.py_lowercleanr      s    ""1779--    c                      \ rS rSrSrSS jrSSS jjr\ S     SS jj5       rSS jr	\
R                  S 5       rS	 rS
rg)pathauditor#   a  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.
Nc                *   [        5       U l        [        5       U l        Xl        X0l        X@l        X l        [        R                  R                  U5      (       a1  [        R                  " U5      (       d  [        R                  U l        g S U l        g )Nc                    U $ r    )xs    r   <lambda>&pathauditor.__init__.<locals>.<lambda>D   s    ar   )setauditeddict
auditeddirroot_realfs_cachedcallbackospathlexistsr   fscasesensitivenormcase)selfr)   r,   realfscacheds        r   __init__pathauditor.__init__:   s]    u&	 77??4  )=)=d)C)C MMDM'DMr   c                   [         R                  " U5      nXR                  ;   a  g[         R                  " U5      (       a"  [        R
                  " [        S5      U-  5      e[         R                  " U5      n[        R                  R                  U5      S   (       d&  [        US   5      S;   d  [        R                  U;   a"  [        R
                  " [        S5      U-  5      eSU;   ar  U Hl  nSU;   d  M  UR                  SS5      u  pVUR                  5       (       d  M6  UR!                  5       S;   d  ML  [        R
                  " [        S5      U-  5      e   S	[        U5      ;   a  U Vs/ s H  n[        U5      PM     nnS
 Ht  nXGSS ;   d  M  UR#                  U5      n[        R                  R$                  " USU 6 n	[        R
                  " [        S5      U[        R&                  " U	5      4-  5      e   U R(                  (       a  [+        U5       Hu  n
XR,                  ;   a  U R,                  U
   nOJ[.        R1                  U R2                  XU R4                  5      nU R6                  (       a  XR,                  U
'   U(       a  Mu    O   U R6                  (       a  U R                  R9                  U5        ggs  snf )zHCheck 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   HG8B6Cr8   )r8   r9   "   path '%s' is inside nested repo %r)r   	localpathr&   endswithsepr   
InputErrorr	   	splitpathr-   r.   
splitdriver   r   ospardirsplitisdigitupperindexjoinbytestrr*   finddirs_rev_norootr(   r   _checkfs_existsr)   r,   r+   add)r2   r.   modepartspfirstlastlpartsposbaseprefixress               r   __call__pathauditor.__call__F   sG    ~~d#<<D!!""9:TA  t$GGt$Q'58$(>>  E)""89D@  4<19"#''$"2KE||~~%++-;M*M#..DEL 	  [&&.34ek!neF4$qr
? ,,q/C77<<t5D**?@!1!1$!789 	 % << .d3__,//&1C%55		6C ||25/s 4 <<LLT" 9 5s   4Kc                   [         R                  R                  X5      n [         R                  " U5      n[        R
                  " UR                  5      (       aN  [        S5      [        R                  " U5      [        R                  " U5      4-  n[        R                  " U5      e[        R                  " UR                  5      (       a  [         R                  R                  [         R                  R                  US5      5      (       aM  U(       a  U" U5      (       d9  [        S5      n[        R                  " Xb[        R                  " U5      4-  5      eg! [         ak  nUR                  [        R                   :X  a   SnAgUR                  [        R                   [        R"                  [        R$                  4;  a  e  SnAgSnAff = f)zwraise exception if a file system backed check fails.

Return a bool that indicates that the directory (or file) exists.s"   path %r traverses symbolic link %rr8   r;   NFT)r-   r.   rF   lstatstatS_ISLNKst_moder	   r   rG   r   AbortS_ISDIRisdirOSErrorerrnoENOENTENOTDIREINVAL)r)   rS   r.   r,   curpathstmsgerrs           r   rI   pathauditor._checkfs_exists   sN    '',,t,	N'"B ||BJJ''=>$$T*$$V,B  kk#&&bjj))bggmmWf-/ /  x'8'8ABC++c83C3CF3K,L&LMM)  	yyELL( yyu}}ell KK L )	s   E 
G)G>GGc                V     U " U5        g! [         [        R                  4 a     gf = fNTF)r_   r   r\   )r2   r.   s     r   checkpathauditor.check   s,    	J% 		s    ((c              #  F  #    U R                   (       a  S v   g  SU l         S v   U R                  R                  5         U R                  R                  5         SU l         g ! U R                  R                  5         U R                  R                  5         SU l         f = f7frj   )r+   r&   clearr(   r2   s    r   r4   pathauditor.cached   so     <<%#""$%%'$ ""$%%'$s   B!A! <B!!=BB!c                l    U R                   R                  5         U R                  R                  5         g)zLreset all audit cache

intended for debug and performance benchmark purposesN)r&   rn   r(   ro   s    r   clear_audit_cachepathauditor.clear_audit_cache   s$     	r   )r+   r*   r&   r(   r,   r1   r)   rj   r   )r.   bytesrK   zOptional[Any]returnNone)rS   rt   r.   rt   r,   z!Optional[Callable[[bytes], bool]])r.   rt   ru   bool)__name__
__module____qualname____firstlineno____doc__r5   rU   staticmethodrI   rk   
contextlibcontextmanagerr4   rr   __static_attributes__r!   r   r   r   r   #   sl    ,
(<#| 
 7;	     4	   D 
% 
% r   r   c                   [         R                  " U 5      (       a  U nOU [        R                  -   nUn[        R
                  R                  U5      (       d   [        R
                  R                  XU5      n[        R
                  R                  U5      nUc  [        U 5      nXT:w  aB  UR                  U5      (       a,  U[        U5      S nU" U5        [         R                  " U5      $ XP:X  a  g/ n  [         R                  " XP5      nU(       aS  U(       d  gUR                  5         [        R
                  R                  " U6 nU" U5        [         R                  " U5      $ [         R                   " U5      u  pUR#                  U	5        X:X  a  OUnM  Sn
 X:w  aZ  [%        X X#5        [         R&                  " XS5      nUR)                  [        R                  5      (       a  USS n[+        S5      U-  n
[,        R.                  " [+        S5      X 4-  U
S9e! [         a    Sn GN f = f! [,        R.                   a     NKf = f)aP  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   Fs   consider using '--cwd %s's   %s not under root '%s')hint)r   r=   r   ossepr-   r.   isabsrF   normpathr   
startswithlenpconvertsamefiler_   reverserB   append	canonpathpathtoendswithr	   r   r\   )r)   cwdmynameauditorrootsepnamerelr   dirnamebasenamer   relpaths               r   r   r      s   ^ 'D77==ww||Dt,77D!Dd#4??733CLN#}}T""	 MM$- ww||S)}}T** $

4 0GJJx D# * 	{$f6++d5##HNN33%crlG56@ kk'(F>9
 	
;  4 {{ 		s%   6H AH0 H-,H-0IIc                    [         R                  R                  U 5      u  p[        U5      [        [        R
                  5      :w  a  U [        R
                  -   $ U $ )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@   r   r   r   )r.   drM   s      r   normasprefixr   .  sC      77d#DA
1vX^^$$hnn$$r   c              #     #    U R                  S5      nUS:w  a"  U S U v   U R                  SSU5      nUS:w  a  M"  Sv   g 7f)N   /r   r   r   )rfindr.   rQ   s     r   finddirsr   E  sH     
**T
C
)4Cjjjq#& ) Is
   8AAc              #     #    U R                  [        R                  5      nUS:w  a3  U S U v   U R                  [        R                  US-   5      nUS:w  a  M2  g g 7f)Nr   r   )findr   r   r   s     r   rH   rH   M  sK     
))HNN
#C
)4Cjiia0 )s   AAAc                  F    \ rS rSrSrS
S jrSS jrSS jrS rSS jr	Sr
g	)dirsiT  z6a multiset of directory names from a set of file pathsc                *   0 U l         U R                  n[        U[        5      (       a;  U(       a4  UR	                  5        H  u  pEUR
                  S:w  d  M  U" U5        M!     gU(       a  Sn[        R                  " U5      eU H  nU" U5        M     g)zC
a dict map indicates a dirstate while a list indicates a manifest
   rs3   `only_tracked` is only supported with a dict sourceN)_dirsaddpath
isinstancer'   itemsstater   ProgrammingError)r2   maponly_trackedr   fr   rf   s          r   r5   dirs.__init__W  ss     
,,c4  \		77d?AJ $ HC((--
 r   c                    U R                   n[        U5       H>  nUR                  S5      (       a  [        SU-  5      eX2;   a  X#==   S-  ss'     g SX#'   M@     g )Nr   z-found invalid consecutive slashes in path: %rr   )r   r   r   
ValueErrorr2   r.   r   rR   s       r   r   dirs.addpathh  sY    zzTND}}T"" CdJ  |
a
DJ #r   c                p    U R                   n[        U5       H  nX#   S:  a  X#==   S-  ss'     g X#	 M     g )Nr   )r   r   r   s       r   delpathdirs.delpatht  s5    zzTNDzA~
a

	 #r   c                ,    [        U R                  5      $ r   )iterr   ro   s    r   __iter__dirs.__iter__|  s    DJJr   c                    XR                   ;   $ r   r   )r2   r   s     r   __contains__dirs.__contains__  s    JJr   r   N)F)r.   rt   ru   rv   )r   rt   ru   rw   )rx   ry   rz   r{   r|   r5   r   r   r   r   r   r!   r   r   r   r   T  s    @"
 r   r   zCallable[[bytes], bytes]r   )r   rt   ru   rt   r   )
r)   rt   r   rt   r   rt   r   zOptional[pathauditor]ru   rt   )r.   rt   ru   rt   )r.   rt   ru   zIterator[bytes])'
__future__r   r~   r`   r-   	posixpathrY   typingr   r   r   r   i18nr	    r
   r   r   r   r   
interfacesr   int_misc
importrustrustdirs	importmodr   r   r   r   r   r   rH   IDirsr   hasattrrF   r   __annotations__r!   r   r   <module>r      s	   "   	      )Zd;


9
%.]  ] H &*	h

h
	h
 h
 #	h

 h
V.1,8>> ,^ 7F<<DD ~~$-$5$5	! 5r   