
    q&f                         d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZmZ ddlmZ e	j                  Ze	j                   ZdZ ej                         Zd Zd	 Zd
 Zd Zej/                  ed      d        Zd Zy)    N)_)open)error	exthelperhttppeerutilwireprototypeswireprotov1peerwireprotov1server   )lfutilsa   
This repository uses the largefiles extension.

Please enable it in your Mercurial config file.
c                 *   |j                         5 }t        j                  | |      }t        j                  t
        j                  j                  |             t        j                  || j                  j                        }	 |j                         D ]  }|j                  |        |j                  j                  d       |t        j                  |j                        k7  rt!        dt#        d            |j%                          t        j&                  | |       	 |j5                          	 ddd       t/        j0                  dr|j3                               S d      S # t         $ r~}| j(                  j+                  t#        d      ||j,                  fz         t/        j0                  d|r|j3                         nd      cY d}~|j5                          cddd       S d}~ww xY w# |j5                          w xY w# 1 sw Y   xY w)zcServer command for putting a largefile into a repository's local store
    and into the user cache.)
createmoder   s$   largefile contents do not match hashs,   largefiles: failed to put %s into store: %s
r       N)mayberedirectstdior   	storepathr   makedirsospathdirnameatomictempfilestorer   
getpayloadwrite_fpseekhexsha1IOErrorr   closelinktousercacheuiwarnstrerrorr	   pushresgetvaluediscard)repoprotoshaoutputr   tmpfppes           8/usr/lib/python3/dist-packages/hgext/largefiles/proto.pyputlfiler/   #   s    
	!	!	# vc*bggood+,##DTZZ5J5JK	%%' AIINN1fnnUYY//a#J!KLLKKM""4- MMO-0 !!!&V__%6JJcJJ  	GGLLBC

#$ "))6??$C  MMO- 	 MMO- sP   A4H	BE*%H	*	G13AG,G1G4H	,G11G44HH		Hc                    t        j                  | |      }|s!t        j                  t	        d      |z        t        |d      t        j                  j                               d   fd}t        j                   |             S )z\Server command for retrieving a largefile from the repository-local
    cache or user cache.s+   requested largefile %s not present in caches   rb   c               3   X   K   dz   t        j                        D ]  } |   y w)Ns   %d
)r   filechunkiter)chunkflengths    r.   	generatorzgetlfile.<locals>.generatorQ   s1     ''* 	EK	s   '*)gen)r   findfiler   Abortr   r   r   fstatfilenor	   streamreslegacy)r'   r(   r)   filenamer7   r5   r6   s        @@r.   getlfiler?   A   sx     tS)Hkk<=C
 	
 	XuAXXahhj!!$F
 ))ik::r   c                     t        j                  | |      }|st        j                  d      S t        j                  d      S )a;  Server command for checking if a largefile is present - returns '2
' if
    the largefile is missing, '0
' if it seems to be in good condition.

    The value 1 is reserved for mismatched checksum, but that is too expensive
    to be verified on every stat and must be caught be running 'hg verify'
    server side.s   2
s   0
)r   r9   r	   bytesresponse)r'   r(   r)   r>   s       r.   	statlfilerB   Y   s9     tS)H++F33''//r   c                 Z    |j                    G fdd|j                        }||_        y )Nc                   l    e Zd Z fdZej
                  d        Zd Zd Zej
                  d        Z	y)+wirereposetup.<locals>.lfileswirerepositoryc                             }| j                  d      r*|j                   G fdd|j                        }||_        |S )Ns
   largefilesc                       e Zd Z fdZy)Wwirereposetup.<locals>.lfileswirerepository.commandexecutor.<locals>.lfscommandexecutorc                 $    |dk(  rd} ||      S )Ns   headss   lheads )selfcommandargsorig_callcommands      r.   callcommandzcwirereposetup.<locals>.lfileswirerepository.commandexecutor.<locals>.lfscommandexecutor.callcommandp   s    "h.&/G/>>r   N)__name__
__module____qualname__rO   )rN   s   r.   lfscommandexecutorrH   o   s    ?r   rS   )capablerO   	__class__)rK   executorrS   rN   orig_commandexecutors      @r.   commandexecutorz;wirereposetup.<locals>.lfileswirerepository.commandexecutorj   sD    +-H||M*#+#7#7 ?);); ? &8"Or   c                 8    | j                   j                  |       S )N)heads	batchable)rK   s    r.   lheadsz2wirereposetup.<locals>.lfileswirerepository.lheadsx   s    ::''--r   c                    t        | j                  t        j                        rs| j                  d||ddi      }	 |j	                  dd      \  }}|j                  d      D ]'  }| j                  j                  t        d      |       ) t        |      S 	 | j                  d||
      \  }}|dk(  rt        j                  t        d      |      t        |      S # t        $ r* | j                  j                  t        d	      |z         Y yw xY w# t        $ r Y yt        $ r  t        j                  t        d            w xY w)Ns   putlfilezcontent-typezapplication/mercurial-0.1)datar)   headers   
r   Ts   remote: s!   unexpected putlfile response: %r
r)   r   s   putlfile failed:s&   putlfile failed (unexpected response):)
issubclassrU   r   _callsplit
splitlinesr!   r"   r   int
ValueError	_callpushr   ResponseErrorr   )rK   r)   fdresdr*   lrets           r.   r/   z4wirereposetup.<locals>.lfileswirerepository.putlfile|   sD    $..(*;*;<jj+-HI	 !  #		% 3IAv#..t4 8Q{^Q78q6M"&..bc."JKCcz#1112F  s8O " GGLL#H!IC!OP  ! --CDc s%   AC AD 0DD	E (Ec              3     K   | j                  d|      }|j                         }	 t        |      }t        j                  ||      D ]  }|  t        | j                  t        j                        rC|j                  d      }|r/| j	                  t        j                  t        d      |             yyy# t        $ r1 | j	                  t        j                  t        d      |             Y w xY ww)z<returns an iterable with the chunks of the file with sha shas   getlfilera   s   unexpected response:)limitr   N)_callstreamreadlinerf   rg   _abortr   ri   r   r   r3   rb   rU   r   read)rK   r)   streamr6   r4   s        r.   r?   z4wirereposetup.<locals>.lfileswirerepository.getlfile   s     %%ks%;F__&FV ++F&A  $..(*;*;<AKK++A.E,FN  =  ''*A(BFKs(   $C7B: BC7:7C41C73C44C7c                     d }d|i}||fS )Nc                 X    	 t        |       S # t        t        j                  f$ r Y yw xY w)N   )rf   rg   urlerr	httperror)rl   s    r.   decodezEwirereposetup.<locals>.lfileswirerepository.statlfile.<locals>.decode   s/    q6M"F$4$45 
 s   
 ))s   sharJ   )rK   r)   r{   results       r.   rB   z5wirereposetup.<locals>.lfileswirerepository.statlfile   s     c]F6>!r   N)
rP   rQ   rR   rX   r
   r[   r\   r/   r?   rB   )rW   s   r.   lfileswirerepositoryrE   i   sG    	 
	"	"	. 
#	.#	J	. 
	"	"	" 
#	"r   r}   )rX   rU   )r!   r'   r}   rW   s      @r.   wirereposetupr~   f   s+    //\"t~~ \"| *DNr   _capabilitiesc                 :     | ||      }|j                  d       |S )z$announce largefile server capabilitys   largefiles=serve)append)origr'   r(   capss       r.   r   r      s"     eDKK#$Kr   c                 p    t        j                  |      rt        j                  t              S  | ||      S )zTWrap server command - largefile capable clients will know to call
    lheads instead)r   islfilesrepor	   ooberrorLARGEFILES_REQUIRED_MSG)r   r'   r(   s      r.   rZ   rZ      s1     4 &&'>??er   )r   mercurial.i18nr   mercurial.pycompatr   	mercurialr   r   r   r   r	   r
   r    r   ry   urlreqr   ehr/   r?   rB   r~   wrapfunctionr   rZ   rJ   r   r.   <module>r      s    
  #   		  YK<;0
0a*J "O4 5r   