
    h                        S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	J
r
  S SKJrJr  S SKJrJrJr  S SKJr  S SKJr  S S	KJrJr   " S
 S\R2                  5      r " S S\5      r " S S\5      r\" \	5       " S S5      5       r " S S5      r " S S\5      r Sr!Sr"Sr#Sr$Sr%Sr&Sr'Sr(Sr)Sr*S r+S!r,S"r-S#r.S$r/S%r0S&r1S'r2S(r3S)r4S*r5S+r6S,r7S-r8S.r9S/r:S0r;Sr<Sr=\=r>Sr?Sr@S1rASrBSrCSrDSrESrFSrGSrHSrISrJS%rKS2rLS3rMS rNSrOSrPSrQSrRSrSSrTSrUSrVS rW\RrX\RrY\Z" 5       r[\\" \[R                  5       5       H2  r^\^R                  S45      (       d  M  \[\^   r`\^SS \R                  \`'   M4     C[C^C`g)5    N)Dict)implementer)	ISFTPFileISFTPServer)NSgetNS)defererrorprotocol)Logger)failure)nativeStringnetworkStringc                   r   ^  \ rS rSr% \" 5       rSr0 r\\	\
4   \S'   S rS rS rS rS rU 4S	 jrS
rU =r$ )FileTransferBase   )   packetTypesc                      SU l         S U l        g N    )bufotherVersionselfs    e/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/conch/ssh/filetransfer.py__init__FileTransferBase.__init__   s     r   c                     U R                   R                  [        R                  " S[	        U5      S-   U5      U-   5        g )Nz!LB   )	transportwritestructpacklen)r   kinddatas      r   
sendPacketFileTransferBase.sendPacket"   s.    V[[D	AtDtKLr   c                 <   U =R                   U-  sl         [        U R                   5      S:  GaA  U R                   S S n[        R                  " SU5      u  p4n[        U R                   5      SU-   :  a  g U R                   SSU-    U R                   SU-   S  sol         U R                  R                  US 5      nU(       d  U R                  R                  SUS9  M  [        U SU 3S 5      nU(       d;  U R                  R                  SUUSS  S	9  U R                  U[        S
U 35        GM  U R                  R                  SUUS9   U" U5        [        U R                   5      S:  a  GM@  g g ! [         a    U R                  R                  SUS9   GM  f = f)N	   z!LBL      zno packet type for {kind})r&   packet_z+not implemented: {packetType} data={data!r})
packetTyper'   zdon't understand z+dispatching: {packetType} requestId={reqId})r/   reqIdz,Failed to handle packet of type {packetType})r/   )r   r%   r#   unpackr   get_loginfogetattr_sendStatusFX_OP_UNSUPPORTED	Exceptionr   )r   r'   headerlengthr&   r0   r/   fs           r   dataReceivedFileTransferBase.dataReceived%   s   D $((mq XXbq\F"(--"?F% 488}q6z) "XXa!f*5txxF
7MND())--dD9J		:F
|4d;A		A)ab  
   ,0A*.N IINN=%  
$W $((mq  X  		!!B) "  s   E2 2$FFc                    [         R                  " SUS S 5      u  n0 nUSS  nU[        -  [        :X  a%  [         R                  " SUS S 5      u  nXCS'   USS  nU[        -  [        :X  a)  [         R                  " SUS S 5      u  pVXSS'   XcS'   USS  nU[        -  [        :X  a%  [         R                  " SUS S 5      u  nXsS	'   USS  nU[
        -  [
        :X  a)  [         R                  " SUS S 5      u  pXS
'   XS'   USS  nU[        -  [        :X  a]  [         R                  " SUS S 5      u  n
USS  n[        U
5       H-  n[        U5      u  p[        U5      u  pXS[        U5       3'   M/     X14$ )N!Lr,   !Q   size!2Luidgidpermissionsatimemtimeext_)
r#   r1   FILEXFER_ATTR_SIZEFILEXFER_ATTR_OWNERGROUPFILEXFER_ATTR_PERMISSIONSFILEXFER_ATTR_ACMODTIMEFILEXFER_ATTR_EXTENDEDranger   r   )r   r'   flagsattrsrB   rD   rE   permsrG   rH   extendedCountiextendedTypeextendedDatas                 r   _parseAttributes!FileTransferBase._parseAttributes`   s   ==tBQx0ABx%%);;mmD$r(3GT &M8D++/GG}}UD!H5HC%L%L8D,,0II}}T484HU#(- 8D**.EE!==Ra9LE"'N"'N8D))-CC%}}T48<]8D=)',T{$',T{$=I\,789: * {r   c                    SnSnSU;   a&  U[         R                  " SUS   5      -  nU[        -  nSU;   a0  SU;   a*  U[         R                  " SUS   US   5      -  nU[        -  nSU;   a&  U[         R                  " S	US   5      -  nU[        -  nS
U;   a0  SU;   a*  U[         R                  " SUS
   US   5      -  nU[
        -  n/ nU HR  nUR                  S5      (       d  M  [        [        USS  5      5      n[        X   5      nUR                  Xg-   5        MT     U(       a@  U[         R                  " S	[        U5      5      -  nUSR                  U5      -  nU[        -  n[         R                  " S	U5      U-   $ )Nr   r   rB   r@   rD   rE   rC   rF   r?   rG   rH   rI   r,   )r#   r$   rJ   rK   rL   rM   
startswithr   r   appendr%   joinrN   )r   rQ   rP   r'   extendedkextTypeextDatas           r   _packAttributes FileTransferBase._packAttributes   ss   U?FKKeFm44D''EE>eunFKKuU|U5\BBD--EE!FKKeM&:;;D..Ee5 0FKKuW~uW~FFD,,EA||F##]1QR512UX, 12	 
 FKKc(m44DCHHX&&D++E{{4'$..r   c                 2   > [         TU ]  U5        SU l        g)z:
Called when connection to the remote subsystem was lost.
FN)superconnectionLost	connected)r   reason	__class__s     r   re   FileTransferBase.connectionLost   s    
 	v&r   )r   rf   r   )__name__
__module____qualname____firstlineno__r   r3   versionsr   r   intstr__annotations__r   r(   r<   rW   ra   re   __static_attributes____classcell__)rh   s   @r   r   r      sF    8DH"$Kc3h$!M9v>/6 r   r   c                       \ rS rSrS'S jrS rS rS rS rS(S jr	S	 r
S
 rS rS rS rS rS rS rS rS rS rS rS rS)S jrS rS rS rS rS rS rS rS rS r S  r!S! r"S*S" jr#S+S# jr$S,S$ jr%S% r&S&r'g)-FileTransferServer   Nc                 j    [         R                  U 5        [        U5      U l        0 U l        0 U l        g N)r   r   r   client	openFilesopenDirs)r   r'   avatars      r   r   FileTransferServer.__init__   s*    !!$'!&)r   c                    [         R                  " SUS S 5      u  n[        [        U R                  5      U/-   5      U l        USS  n0 nU(       a'  [        U5      u  pA[        U5      u  pQXSU'   U(       a  M'  U R                  R                  X#5      nSnUR                  5        H  u  pU[        U5      [        U	5      -   -  nM!     U R                  [        [         R                  " SU R
                  5      U-   5        g Nr?   r,   r   )r#   r1   minlistrn   versionr   ry   
gotVersionitemsr   r(   FXP_VERSIONr$   )
r   r'   r   extextNamer`   ourExt
ourExtDatar^   vs
             r   packet_INITFileTransferServer.packet_INIT   s    ]]4bq2
4.':;ABx!$KMG!$KMG"L d ''5
LLNDA"Q%"Q%-'J #V[[t||%Dz%QRr   c                    US S nUSS  n[        U5      u  p1[        R                  " SUS S 5      u  nUSS  nU R                  U5      u  pQUS:X  d   SU< 35       e[        R
                  " U R                  R                  X4U5      nUR                  U R                  U5        UR                  U R                  US5        g )Nr,   r?   r   zstill have data in OPEN: s   open failed)r   r#   r1   rW   r	   maybeDeferredry   openFileaddCallback_cbOpenFile
addErrback	_ebStatus)r   r'   	requestIdfilenamerP   rQ   ds          r   packet_OPENFileTransferServer.packet_OPEN   s    !H	ABxt==tBQx0ABx++D1s{@7x@@{ 4 4huM	d&&	2	T^^Y?r   c                     [        [        [        U5      5      5      nX0R                  ;   a  [	        S5      eXR                  U'   U R                  [        U[        U5      -   5        g )Nzid already open)r   rp   hashrz   KeyErrorr(   
FXP_HANDLEr   )r   fileObjr   fileIds       r   r   FileTransferServer._cbOpenFile   sN    s4=12^^#,--!(v
I6
$:;r   c                     US S nUSS  n[        U5      u  p1U R                  R                  SUUS9  US:X  d   SU< 35       eX0R                  ;   ai  U R                  U   n[        R
                  " UR                  5      nUR                  U R                  X25        UR                  U R                  US5        g X0R                  ;   am  U R                  U   S   n[        R
                  " UR                  5      nUR                  U R                  X2S5        UR                  U R                  US5        g [        R                  n[        R                  " U5      n[!        Xx5      n	U R                  ["        R$                  " U	5      U5        g )	Nr,   z!closing: {requestId!r} {handle!r})r   handler   zstill have data in CLOSE: s   close failedr   r    )r   r3   r4   rz   r	   r   closer   _cbCloser   r   r{   errnoENOENTosstrerrorOSErrorr   Failure)
r   r'   r   r   r   r   dirObjcodetexterrs
             r   packet_CLOSEFileTransferServer.packet_CLOSE   s?   !H	ABxT{		/ 	 	

 s{A8AA{^^#nnV,G##GMM2AMM$--;LLOD}}$]]6*1-F##FLL1AMM$--A>LLOD<<D;;t$D$%CNN7??3/;r   c                 v    U(       a  U R                   U	 OU R                  U	 U R                  U[        S5        g )Ns   file closed)r{   rz   r6   FX_OK)r   resultr   r   isDirs        r   r   FileTransferServer._cbClose   s/    f%v&E>:r   c                    US S nUSS  n[        U5      u  p1[        R                  " SUS S 5      USS  su  pEnUS:X  d   SU< 35       eX0R                  ;  a/  U R	                  [
        R                  " [        5       5      U5        g U R                  U   n[        R                  " UR                  XE5      nUR                  U R                  U5        UR                  U R                  US5        g )Nr,   !QL   r   zstill have data in READ: s   read failed)r   r#   r1   rz   _ebReadr   r   r   r	   r   	readChunkr   _cbReadr   r   )r   r'   r   r   offsetr:   r   r   s           r   packet_READFileTransferServer.packet_READ   s    !H	ABxT{!'ud3Bi!@$rs)$s{@7x@@{'LL4i@nnV,G##G$5$5vFAMM$,,	2LLNCr   c                 h    US:X  a
  [        5       eU R                  [        U[        U5      -   5        g r   )EOFErrorr(   FXP_DATAr   r   r   r   s      r   r   FileTransferServer._cbRead   s(    S=*)bj"89r   c                     US S nUSS  n[        U5      u  p1[        R                  " SUS S 5      u  nUSS  n[        U5      u  pQUS:X  d   SU< 35       eX0R                  ;  a/  U R	                  [
        R                  " [        5       5      U5        g U R                  U   n[        R                  " UR                  XE5      nUR                  U R                  US5        UR                  U R                  US5        g )Nr,   r@   rA   r   zstill have data in WRITE: s   write succeededs   write failed)r   r#   r1   rz   _ebWriter   r   r   r	   r   
writeChunkr   	_cbStatusr   r   )r   r'   r   r   r   	writeDatar   r   s           r   packet_WRITEFileTransferServer.packet_WRITE   s    !H	ABxT{MM$Ra1	ABx+	s{A8AA{'MM'//(*5yAnnV,G##G$6$6JAMM$..)5GHLLODr   c                    US S nUSS  n[        U5      u  p1US:X  d   SU< 35       e[        R                  " U R                  R                  U5      nUR                  U R                  US5        UR                  U R                  US5        g )Nr,   r   zstill have data in REMOVE: s   remove succeededs   remove failed)	r   r	   r   ry   
removeFiler   r   r   r   )r   r'   r   r   r   s        r   packet_REMOVE FileTransferServer.packet_REMOVE  s    !H	ABxts{B9$BB{ 6 6A	dnni1DE	T^^Y0@Ar   c                 8   US S nUSS  n[        U5      u  p1[        U5      u  pAUS:X  d   SU< 35       e[        R                  " U R                  R                  X45      nUR                  U R                  US5        UR                  U R                  US5        g )Nr,   r   zstill have data in RENAME: s   rename succeededs   rename failed)	r   r	   r   ry   
renameFiler   r   r   r   )r   r'   r   oldPathnewPathr   s         r   packet_RENAME FileTransferServer.packet_RENAME  s    !H	ABxdds{B9$BB{ 6 6I	dnni1DE	T^^Y0@Ar   c                 D   US S nUSS  n[        U5      u  p1U R                  U5      u  pAUS:X  d   SU< 35       e[        R                  " U R                  R
                  X45      nUR                  U R                  US5        UR                  U R                  US5        g )Nr,   r   zstill have data in MKDIR: s   mkdir succeededs   mkdir failed)
r   rW   r	   r   ry   makeDirectoryr   r   r   r   r   r'   r   pathrQ   r   s         r   packet_MKDIRFileTransferServer.packet_MKDIR#  s    !H	ABx4[
++D1s{A8AA{ 9 94G	dnni1CD	T^^Y@r   c                    US S nUSS  n[        U5      u  p1US:X  d   SU< 35       e[        R                  " U R                  R                  U5      nUR                  U R                  US5        UR                  U R                  US5        g )Nr,   r   zstill have data in RMDIR: s   rmdir succeededs   rmdir failed)	r   r	   r   ry   removeDirectoryr   r   r   r   r   r'   r   r   r   s        r   packet_RMDIRFileTransferServer.packet_RMDIR-  s    !H	ABx4[
s{A8AA{ ; ;TB	dnni1CD	T^^Y@r   c                    US S nUSS  n[        U5      u  p1US:X  d   SU< 35       e[        R                  " U R                  R                  U5      nUR                  U R                  U5        UR                  U R                  US5        g )Nr,   r   zstill have data in OPENDIR: s   opendir failed)	r   r	   r   ry   openDirectoryr   _cbOpenDirectoryr   r   r   s        r   packet_OPENDIR!FileTransferServer.packet_OPENDIR6  s    !H	ABx4[
s{C:4(CC{ 9 94@	d++Y7	T^^Y0ABr   c                     [        [        [        U5      5      5      nX0R                  ;   a  [	        S5      eU[        U5      /U R                  U'   U R                  [        U[        U5      -   5        g )Nzalready opened this directory)	r   rp   r   r{   r   iterr(   r   r   )r   r   r   r   s       r   r   #FileTransferServer._cbOpenDirectory?  sY    s4<01]]":;;!'f 6f
I6
$:;r   c                    US S nUSS  n[        U5      u  p1US:X  d   SU< 35       eX0R                  ;  a/  U R                  [        R                  " [        5       5      U5        g U R                  U   u  pE[        R                  " U R                  U/ 5      nUR                  U R                  U5        UR                  U R                  US5        g )Nr,   r   zstill have data in READDIR: s   scan directory failed)r   r{   r   r   r   r   r	   r   _scanDirectoryr   _cbSendDirectoryr   )r   r'   r   r   r   dirIterr   s          r   packet_READDIR!FileTransferServer.packet_READDIRF  s    !H	ABxT{s{C:4(CC{&NN7??8:6	B"mmF3OF##D$7$7"EAMM$//;LL4LMr   c                 6   [        U5      S:  aj   [        U5      n[	        U[
        R                  5      (       a  UR                  U R                  X5        g UR                  U5        [        U5      S:  a  Mj  U$ ! [         a    U(       d  [        eUs $ f = f)N   )
r%   nextStopIterationr   
isinstancer	   Deferredr   _cbScanDirectoryr[   )r   r   r;   r4   s       r   r   !FileTransferServer._scanDirectoryS  s    !fslG}
 $//  !6!6C !fsl  ! "Ns   A< <BBc                 F    UR                  U5        U R                  X#5      $ rx   )r[   r   )r   r   r   r;   s       r   r   #FileTransferServer._cbScanDirectoryb  s    	""7..r   c           
          SnU H5  u  pEnU[        U5      -  nU[        U5      -  nX0R                  U5      -  nM7     U R                  [        U[        R
                  " S[        U5      5      -   U-   5        g )Nr   r?   )r   ra   r(   FXP_NAMEr#   r$   r%   )r   r   r   r'   r   longnamerQ   s          r   r   #FileTransferServer._cbSendDirectoryf  sn    )/%HBxL DBxL D((//D *0 	)fkk$F.L"Lt"STr   c                    US S nUSS  n[        U5      u  pAUS:X  d   SU< 35       e[        R                  " U R                  R                  XB5      nUR                  U R                  U5        UR                  U R                  US5        g )Nr,   r   zstill have data in STAT/LSTAT: s   stat/lstat failed)	r   r	   r   ry   getAttrsr   _cbStatr   r   )r   r'   followLinksr   r   r   s         r   packet_STATFileTransferServer.packet_STATn  s    !H	ABx4[
s{F=dXFF{ 4 4dH	dllI.	T^^Y0DEr   c                 (    U R                  US5        g Nr   )r   )r   r'   s     r   packet_LSTATFileTransferServer.packet_LSTATw  s    q!r   c                    US S nUSS  n[        U5      u  p1US:X  d   SU< 35       eX0R                  ;  a3  U R                  [        R                  " [        U S35      5      U5        g U R                  U   n[        R                  " UR                  5      nUR                  U R                  U5        UR                  U R                  US5        g )Nr,   r   zstill have data in FSTAT: z not in self.openFiless   fstat failed)r   rz   r   r   r   r   r	   r   r   r   r   r   )r   r'   r   r   r   r   s         r   packet_FSTATFileTransferServer.packet_FSTATz  s    !H	ABxT{s{A8AA{'NNF83I)J KL
 nnV,G##G$4$45AMM$,,	2LLODr   c                 V    X R                  U5      -   nU R                  [        U5        g rx   )ra   r(   	FXP_ATTRS)r   r   r   r'   s       r   r   FileTransferServer._cbStat  s#    //77	4(r   c                 b   US S nUSS  n[        U5      u  p1U R                  U5      u  pAUS:w  a  U R                  R                  SUS9  [        R
                  " U R                  R                  X45      nUR                  U R                  US5        UR                  U R                  US5        g )Nr,   r   z$Still have data in SETSTAT: {data!r})r'   s   setstat succeededs   setstat failed)r   rW   r3   warnr	   r   ry   setAttrsr   r   r   r   r   s         r   packet_SETSTAT!FileTransferServer.packet_SETSTAT  s    !H	ABx4[
++D13;IINNANM 4 4dB	dnni1EF	T^^Y0ABr   c                    US S nUSS  n[        U5      u  p1U R                  U5      u  pAUS:X  d   SU< 35       eX0R                  ;  a/  U R                  [        R
                  " [        5       5      U5        g U R                  U   n[        R                  " UR                  U5      nUR                  U R                  US5        UR                  U R                  US5        g )Nr,   r   zstill have data in FSETSTAT: s   fsetstat succeededs   fsetstat failed)r   rW   rz   r   r   r   r   r	   r   r  r   r   r   )r   r'   r   r   rQ   r   r   s          r   packet_FSETSTAT"FileTransferServer.packet_FSETSTAT  s    !H	ABxT{++D1s{D;D8DD{'NN7??8:6	BnnV,G##G$4$4e<AMM$..)5JKLL4FGr   c                    US S nUSS  n[        U5      u  p1US:X  d   SU< 35       e[        R                  " U R                  R                  U5      nUR                  U R                  U5        UR                  U R                  US5        g )Nr,   r   zstill have data in READLINK: s   readlink failed)	r   r	   r   ry   readLinkr   _cbReadLinkr   r   r   s        r   packet_READLINK"FileTransferServer.packet_READLINK      !H	ABx4[
s{D;D8DD{ 4 4d;	d&&	2	T^^Y0BCr   c                 0    U R                  US0 4/U5        g r   )r   r   s      r   r  FileTransferServer._cbReadLink  s    R019=r   c                    US S nUSS  n[        U5      u  p1[        U5      u  pA[        R                  " U R                  R                  X45      nUR                  U R                  US5        UR                  U R                  US5        g )Nr,   s   symlink succeededs   symlink failed)	r   r	   r   ry   makeLinkr   r   r   r   )r   r'   r   linkPath
targetPathr   s         r   packet_SYMLINK!FileTransferServer.packet_SYMLINK  su    !H	ABxt ;
 4 4hK	dnni1EF	T^^Y0ABr   c                    US S nUSS  n[        U5      u  p1US:X  d   SU< 35       e[        R                  " U R                  R                  U5      nUR                  U R                  U5        UR                  U R                  US5        g )Nr,   r   zstill have data in REALPATH: s   realpath failed)	r   r	   r   ry   realPathr   r  r   r   r   s        r   packet_REALPATH"FileTransferServer.packet_REALPATH  r  r   c                    US S nUSS  n[        U5      u  p4[        R                  " U R                  R                  X45      nUR                  U R                  U5        UR                  U R                  USU-   S-   5        g )Nr,   s	   extended s    failed)	r   r	   r   ry   extendedRequestr   _cbExtendedr   r   )r   r'   r   r   r`   r   s         r   packet_EXTENDED"FileTransferServer.packet_EXTENDED  sr    !H	ABx ; ; ;WN	d&&	2	T^^Yw0F0STr   c                 4    U R                  [        X!-   5        g rx   )r(   FXP_EXTENDED_REPLY)r   r'   r   s      r   r'  FileTransferServer._cbExtended  s    *I,<=r   c                 2    U R                  U[        U5        g rx   )r6   r   )r   r   r   msgs       r   r   FileTransferServer._cbStatus  s    E3/r   c                    [         nUn[        UR                  [        [        45      (       a  UR                  R
                  [
        R                  :X  a'  [        n[        UR                  R                  5      nGOUR                  R
                  [
        R                  :X  a'  [        n[        UR                  R                  5      nGOUR                  R
                  [
        R                  :X  a  [        nGOUU R                  R                  SUUUS9  GO7[        UR                  [         5      (       aD  ["        nUR                  R$                  (       a"  [        UR                  R$                  S   5      nO[        UR                  [&        5      (       aD  [(        nUR                  R$                  (       a"  [        UR                  R$                  S   5      nOq[        UR                  [*        5      (       a6  UR                  R,                  n[        UR                  R.                  5      nOU R                  R                  SUUUS9  U R1                  X$U5        g )Nz%Request {requestId} failed: {message})r   r   messager   z8Request {requestId} failed with unknown error: {message})
FX_FAILUREr   valueIOErrorr   r   r   FX_NO_SUCH_FILEr   r   EACCESFX_PERMISSION_DENIEDEEXISTFX_FILE_ALREADY_EXISTSr3   r   r   FX_EOFargsNotImplementedErrorr7   	SFTPErrorr   r1  r6   )r   rg   r   r.  r   r1  s         r   r   FileTransferServer._ebStatus  s   fllWg$677||!!U\\1&'(=(=>##u||3+'(=(=>##u||3-		!!;"'#	 "  h//D||  '(9(9!(<=&9::$D||  '(9(9!(<=i00<<$$D#FLL$8$89GIIJ#	   	'2r   c                     U[         R                  " SU5      -   nU[        U5      -  nU[        U5      -  nU R                  [        U5        g)z-
Helper method to send a FXP_STATUS message.
r?   N)r#   r$   r   r(   
FXP_STATUS)r   r   r   r1  langr'   s         r   r6   FileTransferServer._sendStatus  sC     6;;tT2274
D)r   c                    [         R                  X5        U R                  R                  5        H  nUR	                  5         M     0 U l        U R
                  R                  5        H  u  p4UR	                  5         M     0 U l        g)zc
Called when connection to the remote subsystem was lost.

Clean all opened files and directories.
N)r   re   rz   valuesr   r{   )r   rg   r   r   r   s        r   re   !FileTransferServer.connectionLost  sb     	''5~~,,.GMMO /#}}335OFLLN  6r   )ry   r{   rz   r   )NNr   )r    )s   request succeeded)s   request failed)r   )(rj   rk   rl   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r  r  r  r#  r(  r'  r   r   r6   re   rr    r   r   ru   ru      s    S
@<<4;D:
E BBAAC<N/UF"E )	CHD>CDU>0%3N*r   ru   c                       \ rS rSr0 4S jrS rS rS rS rS r	S r
S	 rS
 rS rS rS rSS jrS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg) FileTransferClienti  c                 X    [         R                  U 5        0 U l        SU l        0 U l        g)zY
@param extData: a dict of extended_name : extended_data items
to be sent to the server.
r   N)r   r   r`   counteropenRequests)r   r`   s     r   r   FileTransferClient.__init__  s(    
 	!!$'r   c                     [         R                  " S[        U R                  5      5      nU R                  R                  5        H  u  p#U[        U5      [        U5      -   -  nM!     U R                  [        U5        g )Nr?   )	r#   r$   maxrn   r`   rD  r   r(   FXP_INIT)r   r'   r^   r   s       r   connectionMade!FileTransferClient.connectionMade  sX    {{4T]]!34LL'')DABqEBqEM!D *$'r   c                 n   [         R                  X5        U R                  (       a  [        R                  " 5       nUR
                  Ul        [        R                  " U5      nU R                  (       aA  U R                  R                  5       u  pEUR                  U5        U R                  (       a  M@  ggg)z]
Called when connection to the remote subsystem was lost.

Any pending requests are aborted.
N)r   re   rL  r
   ConnectionLostr3  	__cause__r   r   popitemerrback)r   rg   requestErrorrequestFailure_deferreds         r   re   !FileTransferClient.connectionLost%  s     	''5  !//1L%+\\L"$__\:N##"//779  0 ### r   c                 h   U R                   (       d)  [        R                  " [        R                  " 5       5      $ [
        R                  " SU R                  5      U-   n[        R                  " 5       nX0R                  U R                  '   U =R                  S-  sl        U R                  X5        U$ )z
Send a request and return a deferred which waits for the result.

@type msg: L{int}
@param msg: The request type (e.g., C{FXP_READ}).

@type data: L{bytes}
@param data: The body of the request.
r?   r    )rf   r	   failr
   rT  r#   r$   rK  r   rL  r(   )r   r.  r'   r   s       r   _sendRequestFileTransferClient._sendRequest:  sy     ~~::e22455{{4.5NN*+$,,'"r   c                 ~    [         R                  " SUS S 5      u  nU R                  U   nU R                  U	 X1SS  4$ Nr?   r,   )r#   r1   rL  )r   r'   idr   s       r   _parseRequest FileTransferClient._parseRequestN  sF    dD!H-b!b!qr({r   c                     [        U5      [        R                  " SU5      -   U R                  U5      -   nU R	                  [
        U5      nUR                  U R                  [        U5        U$ )aC  
Open a file.

This method returns a L{Deferred} that is called back with an object
that provides the L{ISFTPFile} interface.

@type filename: L{bytes}
@param filename: a string representing the file to open.

@param flags: an integer of the flags to open the file with, ORed together.
The flags and their values are listed at the bottom of this file.

@param attrs: a list of attributes to open the file with.  It is a
dictionary, consisting of 0 or more keys.  The possible keys are::

    size: the size of the file in bytes
    uid: the user ID of the file as an integer
    gid: the group ID of the file as an integer
    permissions: the permissions of the file with as an integer.
    the bit representation of this field is defined by POSIX.
    atime: the access time of the file as seconds since the epoch.
    mtime: the modification time of the file as seconds since the epoch.
    ext_*: extended attributes.  The server is not required to
    understand this, but it may.

NOTE: there is no way to indicate text or binary files.  it is up
to the SFTP client to deal with this.
r?   )	r   r#   r$   ra   r_  FXP_OPENr   _cbOpenHandle
ClientFile)r   r   rP   rQ   r'   r   s         r   r   FileTransferClient.openFileT  sZ    : (|fkk$669M9Me9TTh-	d((*h?r   c                 "    U" X5      nX4l         U$ )ar  
Callback invoked when an OPEN or OPENDIR request succeeds.

@param handle: The handle returned by the server
@type handle: L{bytes}
@param handleClass: The class that will represent the
newly-opened file or directory to the user (either L{ClientFile} or
L{ClientDirectory}).
@param name: The name of the file or directory represented
by C{handle}.
@type name: L{bytes}
)name)r   r   handleClassrl  cbs        r   rh   FileTransferClient._cbOpenHandlev  s     &	r   c                 @    U R                  [        [        U5      5      $ )z
Remove the given file.

This method returns a Deferred that is called back when it succeeds.

@type filename: L{bytes}
@param filename: the name of the file as a string.
)r_  
FXP_REMOVEr   )r   r   s     r   r   FileTransferClient.removeFile  s       R\::r   c                 X    U R                  [        [        U5      [        U5      -   5      $ )z
Rename the given file.

This method returns a Deferred that is called back when it succeeds.

@type oldpath: L{bytes}
@param oldpath: the current location of the file.
@type newpath: L{bytes}
@param newpath: the new file name.
)r_  
FXP_RENAMEr   )r   oldpathnewpaths      r   r   FileTransferClient.renameFile  s$       R[2g;-FGGr   c                 d    U R                  [        [        U5      U R                  U5      -   5      $ )a5  
Make a directory.

This method returns a Deferred that is called back when it is
created.

@type path: L{bytes}
@param path: the name of the directory to create as a string.

@param attrs: a dictionary of attributes to create the directory
with.  Its meaning is the same as the attrs in the openFile method.
)r_  	FXP_MKDIRr   ra   )r   r   rQ   s      r   r    FileTransferClient.makeDirectory  s+       BtHt7K7KE7R,RSSr   c                 @    U R                  [        [        U5      5      $ )z
Remove a directory (non-recursively)

It is an error to remove a directory that has files or directories in
it.

This method returns a Deferred that is called back when it is removed.

@type path: L{bytes}
@param path: the directory to remove.
)r_  	FXP_RMDIRr   )r   r   s     r   r   "FileTransferClient.removeDirectory  s       BtH55r   c                     U R                  [        [        U5      5      nUR                  U R                  [
        U5        U$ )a  
Open a directory for scanning.

This method returns a Deferred that is called back with an iterable
object that has a close() method.

The close() method is called when the client is finished reading
from the directory.  At this point, the iterable will no longer
be used.

The iterable returns triples of the form (filename, longname, attrs)
or a Deferred that returns the same.  The sequence must support
__getitem__, but otherwise may be any 'sequence-like' object.

filename is the name of the file relative to the directory.
logname is an expanded format of the filename.  The recommended format
is:
-rwxr-xr-x   1 mjos     staff      348911 Mar 25 14:29 t-filexfer
1234567890 123 12345678 12345678 12345678 123456789012

The first line is sample output, the second is the length of the field.
The fields are: permissions, link count, user owner, group owner,
size in bytes, modification time.

attrs is a dictionary in the format of the attrs argument to openFile.

@type path: L{bytes}
@param path: the directory to open.
)r_  FXP_OPENDIRr   r   rh  ClientDirectoryr   r   r   s      r   r    FileTransferClient.openDirectory  s5    < k2d84	d((/4@r   c                 `    U(       a  [         nO[        nU R                  U[        U5      5      $ )a  
Return the attributes for the given path.

This method returns a dictionary in the same format as the attrs
argument to openFile or a Deferred that is called back with same.

@type path: L{bytes}
@param path: the path to return attributes for as a string.
@param followLinks: a boolean.  if it is True, follow symbolic links
and return attributes for the real path at the base.  if it is False,
return attributes for the specified path.
)FXP_STAT	FXP_LSTATr_  r   )r   r   r   ms       r   r   FileTransferClient.getAttrs  s(     AA  BtH--r   c                 h    [        U5      U R                  U5      -   nU R                  [        U5      $ )a$  
Set the attributes for the path.

This method returns when the attributes are set or a Deferred that is
called back when they are.

@type path: L{bytes}
@param path: the path to set attributes for as a string.
@param attrs: a dictionary in the same format as the attrs argument to
openFile.
)r   ra   r_  FXP_SETSTAT)r   r   rQ   r'   s       r   r  FileTransferClient.setAttrs  s0     $x$..u55  d33r   c                 v    U R                  [        [        U5      5      nUR                  U R                  5      $ )z
Find the root of a set of symbolic links.

This method returns the target of the link, or a Deferred that
returns the same.

@type path: L{bytes}
@param path: the path of the symlink to read.
)r_  FXP_READLINKr   r   _cbRealPathr  s      r   r  FileTransferClient.readLink   /     lBtH5}}T--..r   c                 X    U R                  [        [        U5      [        U5      -   5      $ )a  
Create a symbolic link.

This method returns when the link is made, or a Deferred that
returns the same.

@type linkPath: L{bytes}
@param linkPath: the pathname of the symlink as a string
@type targetPath: L{bytes}
@param targetPath: the path of the target of the link as a string.
)r_  FXP_SYMLINKr   )r   r  r  s      r   r  FileTransferClient.makeLink  s$       blR
^.KLLr   c                 v    U R                  [        [        U5      5      nUR                  U R                  5      $ )z
Convert any path to an absolute path.

This method returns the absolute path as a string, or a Deferred
that returns the same.

@type path: L{bytes}
@param path: the path to convert as a string.
)r_  FXP_REALPATHr   r   r  r  s      r   r"  FileTransferClient.realPath  r  r   c                 8    US   u  p#nUR                  S5      nU$ )Nr   zutf-8)decode)r   r   rl  r   rQ   s        r   r  FileTransferClient._cbRealPath(  s#     &q	{{7#r   c                 F    U R                  [        [        U5      U-   5      $ )a+  
Make an extended request of the server.

The method returns a Deferred that is called back with
the result of the extended request.

@type request: L{bytes}
@param request: the name of the extended request to make.
@type data: L{bytes}
@param data: any other data that goes along with the request.
)r_  FXP_EXTENDEDr   )r   requestr'   s      r   r&  "FileTransferClient.extendedRequest-  s        r'{T/ABBr   c                     [         R                  " SUS S 5      u  nUSS  n0 nU(       a'  [        U5      u  pA[        U5      u  pQXSU'   U(       a  M'  X l        U R	                  X#5        g rb  )r#   r1   r   r   gotServerVersion)r   r'   r   r   r^   r   s         r   packet_VERSION!FileTransferClient.packet_VERSION;  sf    ]]4bq2
ABxDkGADkGAaD d g)r   c                    U R                  U5      u  p![        R                  " SUS S 5      u  nUSS  n[        U5      S:  a-  [	        U5      u  pA[        U5      S:  a  [	        U5      u  pQOSnOSnSnU[
        :X  a  UR                  XE45        g U[        :X  a  UR                  [        U5      5        g U[        :X  a  UR                  [        U5      5        g UR                  [        U[        U5      U5      5        g r   )rd  r#   r1   r%   r   r   callbackr:  rW  r   r7   r<  r=  r   )r   r'   r   r   r.  rA  s         r   packet_STATUS FileTransferClient.packet_STATUSF  s    $$T*--d2Ah/ABxt9>dIC4yA~"4[
dCD5=JJ{#V^IIhsm$&&II)#./IIil3&7>?r   c                 f    U R                  U5      u  p![        U5      u  p4UR                  U5        g rx   )rd  r   r  )r   r'   r   r   rZ  s        r   packet_HANDLE FileTransferClient.packet_HANDLE\  s+    $$T*$K		

6r   c                 d    U R                  U5      u  p!UR                  [        U5      S   5        g r  )rd  r  r   r   r'   r   s      r   packet_DATAFileTransferClient.packet_DATAa  s(    $$T*	

5;q>"r   c                 6   U R                  U5      u  p![        R                  " SUS S 5      u  nUSS  n/ n[        U5       HC  n[	        U5      u  pa[	        U5      u  pqU R                  U5      u  pUR                  XgU45        ME     UR                  U5        g rb  )rd  r#   r1   rO   r   rW   r[   r  )	r   r'   r   countfilesrT   r   r   rQ   s	            r   packet_NAMEFileTransferClient.packet_NAMEe  s    $$T*==tBQx0ABxuA"4[NH"4[NH//5KELL(e45	 
 	


5r   c                 p    U R                  U5      u  p!UR                  U R                  U5      S   5        g r  )rd  r  rW   r  s      r   packet_ATTRSFileTransferClient.packet_ATTRSq  s0    $$T*	

4((.q12r   c                 L    U R                  U5      u  p!UR                  U5        g rx   )rd  r  r  s      r   packet_EXTENDED_REPLY(FileTransferClient.packet_EXTENDED_REPLYu  s     $$T*	

4r   c                     g)a  
Called when the client sends their version info.

@param serverVersion: an integer representing the version of the SFTP
protocol they are claiming.
@param extData: a dictionary of extended_name : extended_data items.
These items are sent by the client to indicate additional features.
NrG  )r   serverVersionr`   s      r   r  #FileTransferClient.gotServerVersiony  s    r   )rK  r`   rL  r   NrF  ) rj   rk   rl   rm   r   rQ  re   r_  rd  r   rh  r   r   r   r   r   r   r  r  r  r"  r  r&  r  r  r  r  r  r  r  r  rr   rG  r   r   rI  rI    s    ! (1*( D"	;HT6 D.&4/M/
C	*@,
#
3r   rI  c                   8    \ rS rSrS rS rS rS rS rS r	Sr
g	)
ri  i  c                 0    Xl         [        U5      U l        g rx   )parentr   r   r   r  r   s      r   r   ClientFile.__init__  s    jr   c                 V    U R                   R                  [        U R                  5      $ rx   )r  r_  	FXP_CLOSEr   r   s    r   r   ClientFile.close      {{''	4;;??r   c                     U R                   [        R                  " SX5      -   nU R                  R	                  [
        U5      $ )Nr   )r   r#   r$   r  r_  FXP_READ)r   r   r:   r'   s       r   r   ClientFile.readChunk  s2    {{V[[??{{''$77r   c                     U R                   [        R                  " SU5      -   [        U5      -   nU R                  R                  [        U5      $ )Nr@   )r   r#   r$   r   r  r_  	FXP_WRITE)r   r   chunkr'   s       r   r   ClientFile.writeChunk  s;    {{V[[v66EB{{''	488r   c                 V    U R                   R                  [        U R                  5      $ rx   )r  r_  	FXP_FSTATr   r   s    r   r   ClientFile.getAttrs  r  r   c                     U R                   U R                  R                  U5      -   nU R                  R                  [        U5      $ rx   )r   r  ra   r_  r  )r   rQ   r'   s      r   r  ClientFile.setAttrs  s6    {{T[[88??{{''	488r   )r   r  N)rj   rk   rl   rm   r   r   r   r   r   r  rr   rG  r   r   ri  ri    s"    !@89@9r   ri  c                   B    \ rS rSrS rS rS rS rS r\r	S r
S rS	rg
)r  i  c                 >    Xl         [        U5      U l        / U l        g rx   )r  r   r   
filesCacher  s      r   r   ClientDirectory.__init__  s    jr   c                 V    U R                   R                  [        U R                  5      $ rx   )r  r_  FXP_READDIRr   r   s    r   readClientDirectory.read  s    {{''T[[AAr   c                     U R                   c  [        R                  " S 5      $ U R                  R	                  [
        U R                   5      nS U l         U$ rx   )r   r	   succeedr  r_  r  r   r   s     r   r   ClientDirectory.close  sA    ;;==&&KK$$Y<r   c                     U $ rx   rG  r   s    r   __iter__ClientDirectory.__iter__  s    r   c                 ,   [         R                  " S[        SS9  U R                  (       a  U R                  R	                  S5      $ U R                  c
  [        5       eU R                  5       nUR                  U R                  U R                  5        U$ )NzeUsing twisted.conch.ssh.filetransfer.ClientDirectory as an iterator was deprecated in Twisted 18.9.0.   )category
stacklevelr   )
warningsr  DeprecationWarningr  popr   r  addCallbacks
_cbReadDir
_ebReadDirr  s     r   __next__ClientDirectory.__next__  sr    C (	
 ????&&q))??"/!IIK	t8r   c                      USS  U l         US   $ )Nr    r   )r  )r   namess     r   r  ClientDirectory._cbReadDir  s    )Qxr   c                 v    UR                  [        5        S U l        [        R                  " [        5       5      $ rx   )trapr   r  r   r   r   )r   rg   s     r   r  ClientDirectory._ebReadDir  s&    H}//r   )r  r   r  N)rj   rk   rl   rm   r   r  r   r  r  r   r  r  rr   rG  r   r   r  r    s-    
B" D0r   r  c                   <    \ rS rSrSS jr\S 5       rS\4S jrSr	g)	r=  i  c                 R    [         R                  U 5        Xl        X l        X0l        g rx   )r8   r   r   _messagerA  )r   	errorCodeerrorMessagerA  s       r   r   SFTPError.__init__  s    4 	$	r   c                     U R                   $ )zH
A string received over the network that explains the error to a human.
)r  r   s    r   r1  SFTPError.message  s     }}r   returnc                 :    SU R                    SU R                   3$ )Nz
SFTPError z: )r   r1  r   s    r   __str__SFTPError.__str__  s    DII;b77r   )r  r   rA  N) )
rj   rk   rl   rm   r   propertyr1  rp   r  rr   rG  r   r   r=  r=    s'     	 	8 8r   r=  r    r  r   r,   r-         rA   r+   
      r                           e   f   g   h   i         l            @   FXP_)br   r   r#   r  typingr   zope.interfacer   twisted.conch.interfacesr   r   twisted.conch.ssh.commonr   r   twisted.internetr	   r
   r   twisted.loggerr   twisted.pythonr   twisted.python.compatr   r   Protocolr   ru   rI  ri  r  r8   r=  rP  r   rg  r  r  r  r  r  r  FXP_FSETSTATr  r  rq  ry  r|  r  r  rt  r  r  r@  r   r   r   r	  r  r+  rJ   FILEXFER_ATTR_UIDGIDrK   rL   rM   rN   FILEXFER_TYPE_REGULARFILEXFER_TYPE_DIRECTORYFILEXFER_TYPE_SYMLINKFILEXFER_TYPE_SPECIALFILEXFER_TYPE_UNKNOWNFXF_READ	FXF_WRITE
FXF_APPEND	FXF_CREAT	FXF_TRUNCFXF_EXCLFXF_TEXTr   r:  r5  r7  r2  FX_BAD_MESSAGEFX_NO_CONNECTIONFX_CONNECTION_LOSTr7   r9  FX_NOT_A_DIRECTORYFX_FILE_IS_A_DIRECTORYglobalsgr   keysrl  rZ   r3  r   rG  r   r   <module>r3     s!    	    & ; . 3 3 ! " =Ix(( IXn) nbm) m` Y9 9 90-0 -0`8	 8. 				
		


	  ! / & $ #      	
				
 
    
   #  INDv$.212h$$U+  tUr   