
    q&f=                         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 d dl	m
Z
 d dlmZmZmZ d dlmZ ddlmZmZ  G d	 d
      Z G d d      Zy)    N)_)binhex)open)errorpycompatutil)hashutil   )	constantsshallowutilc                   p    e Zd ZddZd ZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zy)	basestorec                    || _         |j                  | _        || _        || _        || _        t
        j                  st        j                         nd| _	        | j                  j                  dd      | _        | j                  j                  ddd      | _        | j                  dvrd| _        | j                  dk(  rd| _        |r!t        j                  | j                  |       yy)	a  Creates a remotefilelog store object for the given repo name.

        `path` - The file path where this store keeps its data
        `reponame` - The name of the repo. This is used to partition data from
        many repos.
        `shared` - True if this store is a shared cache of data from the central
        server, for many repos on this machine. False means this store is for
        the local data for one repo.
        N   remotefilelogs   validatecachelogs   validatecache   on)r      strict   offr   F)repoui_path	_reponame_sharedr   	iswindowsosgetuid_uidconfig_validatecachelog_validatecacher   mkstickygroupdir)selfr   pathreponameshareds        ?/usr/lib/python3/dist-packages/hgext/remotefilelog/basestore.py__init__zbasestore.__init__   s     	''
!'/'9'9BIIKt	!%1"
 #ggnn.
 &@@"'D&("'D(($7     c                     g }|D ]q  \  }}| j                  ||      }t        j                  j                  |      }|r#| j                  dk(  r| j                  |d      sd}|r_|j                  ||f       s |S )Nr   s   containsF)_getfilepathr   r#   existsr    _validatekeyappend)r"   keysmissingnamenodefilepathr+   s          r&   
getmissingzbasestore.getmissing5   s~     
	-JD$((t4HWW^^H-F''94))(K@d|,
	- r(   Nc                     |r |j                  t        j                        ry | j                  rF| j	                         D ]2  \  }}|D ](  }|j                  | ||       |j                  | ||       * 4 y y N)getr   OPTION_PACKSONLYr   	_getfilesmarkdataentrymarkhistoryentry)r"   ledgeroptionsfilenamenodesr1   s         r&   
markledgerzbasestore.markledgerG   sv    w{{9#=#=><<#'>>#3 B%! BD((x>++D(DABB r(   c                    | j                   }|j                  j                  | g       }d}|j                  t	        d      dt        |            }|D ]w  }|j                  s|j                  rX|j                  rL|j                  |       | j                  |j                  |j                        }t        j                  |       |dz  }y |j                          | j!                  | j#                                y )Nr   s   cleaning up   filesunittotalr   )r   sourcesr6   makeprogressr   lengceddatarepackedhistoryrepackedupdater*   r=   r1   r	   	tryunlinkcomplete_cleanupdirectory_getrepocachepath)r"   r;   r   entriescountprogressentryr#   s           r&   cleanupzbasestore.cleanupP   s    WW..$$T2.??nHCL # 
  	Ezze00U5J5J&((Dt$QJE	 	 	t5578r(   c                 r   t               }t               }t        j                  j                  |      D ]  \  }}t	        j
                  |      rHt        j                  j                  ||      }| j                  |       	 t        j                  |       ct	        j                  |      sy|j                  d      r|j                  |dd        |j                  |        ||z
  D ]:  }t        j                  j                  ||dz         }t        j                  |       < y# t        $ r Y w xY w)zRemoves the empty directories and unnecessary files within the root
        directory recursively. Note that this method does not remove the root
        directory itself.   _oldN)setr	   osutillistdirstatS_ISDIRr   r#   joinrN   rmdirOSErrorS_ISREGendswithaddrL   )	r"   rootdiroldfiles
otherfilesr0   modedirpathr=   r2   s	            r&   rN   zbasestore._cleanupdirectoryd   s    
 5U
 ++--g6 	)JD$||D!'',,w5&&w/HHW% d#==)LLcr+NN4(!	)* !:- 	%Hww||GX-?@HNN8$	%  s   D))	D65D6c              #     K   i }| j                         D ]&  \  }}|j                  |g       j                  |       ( | j                  |j	                               }|j                         D ]  \  }}|||   f  yw)a8  Return a list of (filename, [node,...]) for all the revisions that
        exist in the store.

        This is useful for obtaining a list of all the contents of the store
        when performing a repack to another store, since the store API requires
        name+node keys and not namehash+node keys.
        N)	_listkeys
setdefaultr-   _resolvefilenamesr.   items)r"   existingfilenamehashr1   filenamemapr=   shas          r&   r8   zbasestore._getfiles   s      "&.."2 	?L$b188>	? ,,X]]_=(..0 	,MHcXc]++	,s   A=A?c                 @   |si S i }t        |      }| j                  d   j                         D ]@  }t        j                  |      j                         }||v s+|||<   |j                  |       B | j                  j                         j                  }t        t        |      dz
  dd      D ]l  }|s |S |j                  |j                  |            }|D ]@  }t        j                  |      j                         }||v s+|||<   |j                  |       B n |S )zGiven a list of filename hashes that are present in the
        remotefilelog store, return a mapping from filename->hash.

        This is useful when converting remotefilelog blobs into other storage
        formats.
        s   tipr   )rX   r   manifestr
   sha1digestdiscard
unfiltered	changelograngerG   	readfilesr1   )	r"   hashes	filenamesmissingfilenamer=   rp   clrevfiless	            r&   rk   zbasestore._resolvefilenames   s"    I	f+ 		&)224 	-H--)002Co%&)	(#'',		- YY!!#--R1b"- 	1C"  LL.E! 1mmH-446/)*-Ih'#++C0	1		1 r(   c                     | j                   r4t        j                  j                  | j                  | j
                        S | j                  S r5   )r   r   r#   r]   r   r   )r"   s    r&   rO   zbasestore._getrepocachepath   s:     || GGLLT^^4	
 	
r(   c              #      K   t        j                  | j                               D ]S  \  }}}|D ]H  }t        |      dk7  r|}| j                  r|dd |dd z   }n|dd }t        |      t        |      f J U yw)zList all the remotefilelog keys that exist in the store.

        Returns a iterator of (filename hash, filecontent hash) tuples.
        (   iiiNi)r   walkrO   rG   r   r   )r"   rootdirsr   r=   r1   rn   s          r&   ri   zbasestore._listkeys   s      "$)?)?)A!B 
	5D$! 	5x=B&<<#'C=4:#=L#':L<(#d)44	5
	5s   A;A=c                     t        |      }| j                  r"t        j                  | j                  ||      }nt        j
                  ||      }t        j                  j                  | j                  |      S r5   )
r   r   r   getcachekeyr   getlocalkeyr   r#   r]   r   )r"   r0   r1   keys       r&   r*   zbasestore._getfilepath   sU    4y<<))$..$EC))$5Cww||DJJ,,r(   c           	         | j                  ||      }	 t        j                  |      }| j                  rx| j	                  ||      sf| j
                  r3t        | j
                  d      5 }|j                  d|z         d d d        t        j                  ||dz          t        d|z        |S # 1 sw Y   2xY w# t        $ r t        d||t        |      fz        w xY w)N   ab+s   corrupt %s during read
   .corrupts   corrupt local cache file %ss   no file found at %s for %s:%s)r*   r   readfiler    _validatedatar   r   writer   renameKeyErrorIOErrorr   )r"   r0   r1   r2   datafs         r&   _getdatazbasestore._getdata   s    $$T40	''1D""4+=+=dH+M))d44f= H ;h FGH		(H{$:;=HII H H  	0HdCI3NN 	s$   AB; )B/>/B; /B84B; ;$Cc                    | j                  ||      }t        j                  d      }	 t        j                  j	                  |      rO|dz   }t        j                  j	                  |      rt        j                  |       t        j                  ||       t        j                  | j                  t        j                  j                  |             t        j                  ||d       | j                  r3| j                  |d      s!t        j                   t#        d      |z        t        j                  |       y # t        j                  |       w xY w)N   rV   T)readonlys   writes"   local cache write was corrupted %s)r*   r   umaskr#   r+   r   
unlinkfileshutilcopyr!   r   dirname	writefiler    r,   r   Abortr   )r"   r0   r1   r   r2   oldumasknewfilenames          r&   addremotefilelognodezbasestore.addremotefilelognode   s    $$T4088E?	 ww~~h'&0 77>>+.**;7Hk2(("''//(2KL!!(D4@""((8<++?@8K  HHXBHHXs   DE Ec                    t         j                  j                  | j                  d      }t	        |d      5 }|j                  t         j                  j                  |      dz          ddd       t        j                  |      }|j                  | j                  k(  rt        j                  |d       yy# 1 sw Y   OxY w)a  Call this to add the given repo path to the store's list of
        repositories that are using it. This is useful later when doing garbage
        collection, since it allows us to insecpt the repos to see what nodes
        they want to be kept alive in the store.
           reposs   ab   
Ni  )r   r#   r]   r   r   r   r   r[   st_uidr   chmod)r"   r#   	repospath	reposfilerepospathstats        r&   markrepozbasestore.markrepo  s     GGLLX6	)U# 	;yOOBGGOOD1E9:	; 	*499,HHY' -		; 	;s   2B77C c                 X   t        |d      5 }|j                         }d d d        | j                  |      ry| j                  r5t        | j                  d      5 }|j	                  d||fz         d d d        t        j                  ||dz          y# 1 sw Y   wxY w# 1 sw Y   /xY w)Ns   rbTr   s   corrupt %s during %s
r   F)r   readr   r   r   r   r   )r"   r#   actionr   r   s        r&   r,   zbasestore._validatekey  s    $ 	!668D	 dD)!!d,,f5 D1T6NBCD 			${*+	 	D Ds   BB B B)c                 &   	 t        |      dkD  rbt        j                  |      \  }}}t        |      |k  ry||z  }|||dz    }t        j                  j                  |      t        |      k(  ryy	 y# t        t        j                  f$ r Y yw xY w)Nr   F   T)	rG   r   parsesizeflagsr   r#   basenamer   
ValueErrorBadRemotefilelogHeader)r"   r   r#   offsetsizeflagsdatanodes          r&   r   zbasestore._validatedata"  s    	4y1}&1&@&@&F#et9$  $"5 77##D)S]: &  K>>? 		s   5A4 8A4 4BBc           	      ^   | j                   }| j                  }t        j                  j	                         }d}d}d}d}t        j
                         dz
  }	|j                  t        d      d      }
|
j                  d       t        j                  |      D ]  \  }}}|D ]  }|dk(  r	d|v r|
j                  |       t        j                  j                  ||      }t        j                  j                  ||      }|dz  }	 t        j                  |      }||j"                  z  }||v s|j$                  |	kD  r.|j'                  |j$                  ||f       ||j"                  z  }	 t)        j*                  |       |dz  }  |
j-                          |j/                  d
d      }	||	kD  r||	z
  }|j                  t        d      d|      }
d}|ru||	kD  rp|dkD  rk|
j                  |       |j1                         \  }}}	 t)        j*                  |       ||j"                  z  }|dz  }||j"                  z  }|r||	kD  r|dkD  rk|
j-                          |j3                  t        d      ||t5        |      dz  dz  dz  t5        |      dz  dz  dz  fz         y # t        $ r# t        d	      }|j!                  ||z         Y ,w xY w# t        $ r# t        d	      }|j!                  ||z         Y [w xY w# t        $ r# t        d	      }|j!                  ||z         Y w xY w)Nr   iQ s   removing unnecessary filesrA   )rC   r   s   /packs/r   s0   warning: file %s was removed by another process
r   s
   cachelimits   enforcing cache limits   bytesrB   s8   finished: removed %d of %d files (%0.2f GB to %0.2f GB)
g      @)r   r   r   queuePriorityQueuetimerF   r   rK   r   r   r#   r]   relpathr[   FileNotFoundErrorwarnst_sizest_atimeputr   r   rM   configbytesr6   statusfloat)r"   keepkeysr   	cachepathr   originalsizer   rQ   removedlimitrR   r   r   r   filer#   r   pathstatmsgexcessremovedexcessatimeoldpatholdpathstats                           r&   gczbasestore.gc9  sI   WWJJ	 ,,. 		|,??+,8 # 
 	!#!3 %	!D$ $!8# %&ww||D$/ggoodI6
!wwt}H  0 00(?h&7&7%&?IIx00$ABH,,,D!#..t4 qLGI$!%	!L 	 /?%<E\F*+(& ' H MD5LTAX..3iik+w+**73 +++1!4!44 D5LTAX 
		JKl#f,v5>df$v-6		
a ) LC GGC$J' - !) d
+ !. ) +LC GGC'M*	+s6   J",KL  "(KK(K=<K= (L,+L,)Fr5   )__name__
__module____qualname__r'   r3   r?   rT   rN   r8   rk   rO   ri   r*   r   r   r   r,   r   r    r(   r&   r   r      sV    8>$B9( %D," D
5$-"4(.Y
r(   r   c                   (    e Zd Zd Zd Zed        Zy)baseunionstorec                 f    |j                  dd      dz   | _        |j                  dd       | _        y )N
numretriesr   r   retrylog)r6   numattemptsr   )r"   argskwargss      r&   r'   zbaseunionstore.__init__  s0    
 "::lA6: 

:t4r(   c                 `    | j                   D ]  }t        |d      s|j                          ! y )Nmarkforrefresh)storeshasattrr   )r"   stores     r&   r   zbaseunionstore.markforrefresh  s,    [[ 	'Eu./$$&	'r(   c                      d  fd}|S )Nc                       y r5   r   )r   s    r&   noopz&baseunionstore.retriable.<locals>.noop  s    r(   c                    | j                   xs }j                  }d}|| j                  k  rH|dkD  r0 |d|t        j                  |      fz         | j                          |dz  }	  | g|i |S y y # t        $ r1 || j                  k(  r |dt        j                  |      z          Y nw xY w|| j                  k  rP)Nr   s   re-attempting (n=%d) %s
r   s*   retries exhausted in %s, raising KeyError
)r   r   r   r   sysbytesr   r   )r"   r   r   r   funcnameifnr   s         r&   wrappedz)baseunionstore.retriable.<locals>.wrapped  s    }},H{{HAd&&&q54h//9:; '')Q	d4T4V44 '   D,,, J&//9:  - d&&&s   *A8 87B21B2r   )r   r   r   s   ` @r&   	retriablezbaseunionstore.retriable  s    		. r(   N)r   r   r   r'   r   staticmethodr   r   r(   r&   r   r     s     5'
  r(   r   )r   r   r[   r   mercurial.i18nr   mercurial.noder   r   mercurial.pycompatr   	mercurialr   r   r	   mercurial.utilsr
    r   r   r   r   r   r(   r&   <module>r      sE    	     # # 
 %}
 }
@, ,r(   