o
    Žõ±juR  ã                   @   sÚ   d dl mZmZmZ d dlmZ d dlT d dlmZ d dl	m
Z
 d dlmZ d dlZd dlZd dlZd dlmZ d d	lmZ d d
lmZ G dd„ dƒZeddedefdd„ƒZdd„ Zedkrkd dlZe eƒ ¡ dS dS )é    )ÚPureWindowsPathÚPurePathÚPath)ÚFileAccessMask)Ú*)ÚFileInfoClass)ÚSecurityInfo)ÚFileAttributesN)Úasynccontextmanager)ÚSMBConnection)ÚSECURITY_DESCRIPTORc                   @   s´  e Zd Zdd„ Zdd„ Zdd„ ZedJded	efd
d„ƒZed	efdd„ƒZ	ede
defdd„ƒZedefdd„ƒZede
defdd„ƒZede
defdd„ƒZede
defdd„ƒZdd„ Zde
fdd„Zd ed!efd"d#„ZdKd%ed&efd'd(„ZdLde
d+efd,d-„ZejejB fde
d.efd/d0„Zde
d+efd1d2„ZdKd!ed3efd4d5„ZdMd efd7d8„ZdNd ed9efd:d;„Zd%efd<d=„Zd>e j!fd?d@„Z"dAdB„ Z#dCdD„ Z$dEefdFdG„Z%dHdI„ Z&d*S )OÚSMBFilec                 C   s|   d | _ d | _d | _d | _d | _d | _d | _d | _d | _d | _	d | _
d | _d | _d | _d | _d | _d| _d| _d | _d| _d S )Nr   FÚ )Útree_idÚ
parent_dirÚfullpathÚunc_pathÚ
share_pathÚnameÚsizeÚcreation_timeÚlast_access_timeÚlast_write_timeÚchange_timeÚallocation_sizeÚ
attributesÚfile_idÚsecurity_descriptorÚ_SMBFile__connectionÚ_SMBFile__positionÚis_pipeÚmaxreadsizeÚmode©Úself© r%   ú‘/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/aiosmb/commons/interfaces/file.pyÚ__init__   s(   
zSMBFile.__init__c                 Ã   s   �| S ©Nr%   r#   r%   r%   r&   Ú
__aenter__(   s   €zSMBFile.__aenter__c                 Ã   s   �|   ¡ I d H  d S r(   )Úclose)r$   Úexc_typeÚexc_valÚexc_tbr%   r%   r&   Ú	__aexit__+   s   €zSMBFile.__aexit__TÚbasedirr   c                 C   s¤   |  d¡}td| ƒ}|jd  dd¡}|jd }| d¡dkr(|d d… d }|d	u r:||gt|jd
d… ƒ }n||gt|jd
d … ƒ }t| ƒj|Ž  ¡ }|S )Nú\z\\\\é   Ú.Ú_é   ú$éÿÿÿÿTé   )	Úlstripr   ÚpartsÚreplaceÚfindÚlistr   ÚjoinpathÚresolve)r/   r   Úpath_with_fileÚuncÚhostÚshareÚdirsÚpath_to_creater%   r%   r&   Úprepare_mirror_path.   s   

zSMBFile.prepare_mirror_pathc                 C   s8   t | ƒ}tƒ }|j|_|j|_d |jdd… ¡|_|S )zh
		Creates SMBFile object from the UNC path supplied.
		Example uncpath: \\127.0.0.1\C$\temp\test.exe
		r0   r1   N)r   r   Údriver   r   Újoinr9   r   )r   r@   Úfr%   r%   r&   Úfrom_uncpath=   s   zSMBFile.from_uncpathÚ
connectionÚ
remotepathc                 C   s0   d}|d dkr
d}|| j  ¡ |f }t |¡S )zv
		Creates SMBFile object from the connection and the remote path supplied.
		Example remotepath: \C$\temp\test.exe
		z\\%s\%sr   r0   ú\\%s%s)ÚtargetÚget_hostname_or_ipr   rI   )rJ   rK   Útempr@   r%   r%   r&   Úfrom_remotepathK   s
   
zSMBFile.from_remotepathrM   c                 C   sP   | j du rdS | j  dd¡}| d¡du rd| }d}||  ¡ |f }t |¡S )z3
		Creates SMBFile object from the SMBUrl object
		Nú/r0   FrL   )Úpathr:   Ú
startswithrN   r   rI   )rM   ÚfpathrO   r@   r%   r%   r&   Úfrom_smbtargetW   s   

zSMBFile.from_smbtargetÚpipenamec                 C   s   t  d| j ¡ |f ¡S )Nz\\%s\IPC$\%s)r   rI   rM   rN   )rJ   rV   r%   r%   r&   Úfrom_pipenamef   s   zSMBFile.from_pipenamec              
   Ã   sì   �z^t  |¡}|  |j¡I d H \}}|d ur|‚|j}tjtjB }tj	}t
jt
jB }tj}	d}
| j||j||||	|
dd�I d H \}}|d urH|‚|d urU|  ||¡I d H  |  |¡I d H  W dS  tyu } z
d|fW  Y d }~S d }~ww ©Nr   F©Úreturn_reply©TN)r   rI   Útree_connectr   r   r   ÚDELETEÚFILE_READ_ATTRIBUTESÚShareAccessÚFILE_SHARE_DELETEÚCreateOptionsÚFILE_NON_DIRECTORY_FILEÚFILE_DELETE_ON_CLOSEÚCreateDispositionÚ	FILE_OPENÚcreater   r*   Útree_disconnectÚ	Exception©rJ   rK   ÚremfileÚ
tree_entryÚerrr   Údesired_accessÚ
share_modeÚcreate_optionsÚcreate_dispositionÚ
file_attrsr   Úer%   r%   r&   Ú
delete_uncj   s.   €
&€ÿzSMBFile.delete_uncc              
   Ã   sî   �z_t  | |¡}|  |j¡I d H \}}|d ur|‚|j}tjtjB }tj	}t
jt
jB }tj}	d}
| j||j||||	|
dd�I d H \}}|d urI|‚|d urV|  ||¡I d H  |  |¡I d H  W dS  tyv } z
d|fW  Y d }~S d }~ww rX   )r   rP   r\   r   r   r   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   r   r*   rg   rh   ri   r%   r%   r&   Údelete_rempath…   s.   €&€ÿzSMBFile.delete_rempathc              
   Ã   sø   �zd|   ¡ I dH  | j | j¡I dH \}}|dur|‚|j}tjtjB }tj	}t
jt
jB }tj}d}| jj|| j|||||dd�I dH \}	}|durL|‚|	durZ| j  ||	¡I dH  | j |¡I dH  W dS  ty{ }
 z
d|
fW  Y d}
~
S d}
~
ww )zcDeletes the file. If the file is open, a flush will be performed followed by a close then a delete.Nr   FrY   r[   )r*   r   r\   r   r   r   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   r   rg   rh   )r$   rk   rl   r   rm   rn   ro   rp   rq   r   rr   r%   r%   r&   Údelete    s@   €ø€ÿzSMBFile.deletec              
   Ã   sà  �| j du rëd}zÁzi| j}| jdu r&| | j¡I dH \}}|dur#|‚|j}tj}tj}tj	tj
B }d}	tj}
| || j||||
|	¡I dH \}}|durN|‚|j||tjtjtjtjB tjB tjB dd�I dH \| _ }|durq|‚W n7 ty© } z+d|fW  Y d}~W |dur‘| ||¡I dH  |dur£| jdu r¤| |¡I dH  S S S d}~ww W |dur¸| ||¡I dH  |durÉ| jdu rÉ| |¡I dH  n!|dur×| ||¡I dH  |duré| jdu rê| |¡I dH  w w w | j dfS )z,Fetches the security descriptor of the file.Nr   )Ú	info_typeÚinformation_classÚadditional_informationÚflags)r   r   r\   r   r   ÚREAD_CONTROLr_   ÚFILE_SHARE_READra   rb   ÚFILE_SYNCHRONOUS_IO_NONALERTrd   re   rf   r   Ú
query_infoÚQueryInfoTypeÚSECURITYr   ÚNONEr   ÚATTRIBUTE_SECURITY_INFORMATIONÚDACL_SECURITY_INFORMATIONÚOWNER_SECURITY_INFORMATIONÚGROUP_SECURITY_INFORMATIONrh   r*   rg   )r$   rJ   r   r   rk   rl   rm   rn   ro   rq   rp   rr   r%   r%   r&   Úget_security_descriptorÆ   sd   €

"úÿÿ€úþ€ýÿ
zSMBFile.get_security_descriptorr   Úoffsetc           	      Ã   s  �| j dkr| jj| j| j||d�I dH \}}}||fS d}|| jjkrc|| jj }t|d ƒD ]*}| jj| j| j|| jjd�I dH \}}}|durPd|f  S |t|ƒ7 }||7 }q0|d|… |fS | jj| j| j|| jjd�I dH \}}}|durd|fS ||7 }|d|… |fS )a!  
		This is the main function for reading.
		It does not do buffering, so if more data is returned it will just discard it
		If less data is returned than requested it will do more reads until the requested size is reached.
		Do not call this directly as it could go in an infinite loop 
		T)r†   ÚlengthNó    r1   )r    r   Úreadr   r   ÚMaxReadSizeÚrangeÚlen)	r$   r   r†   ÚdataÚ	remainingrl   ÚbufferÚir3   r%   r%   r&   Ú__readð   s&   €
$(
(zSMBFile.__readr   r�   Úposition_in_filec           	   
   Ã   s°   �z@t |ƒ}d}d}|dkr=| jj| j| j||t |ƒ… || d�I dH \}}|dur-|‚||7 }||8 }||7 }|dks|dfW S  tyW } z
d|fW  Y d}~S d}~ww )z
		Data must be bytes
		r   )r†   N)rŒ   r   Úwriter   r   rh   )	r$   r�   r’   rŽ   Útotal_bytes_writtenr†   Úbytes_writtenrl   rr   r%   r%   r&   Ú__write  s$   €2ú
€ÿzSMBFile.__writeÚrNr"   c              
   Ã   s´  �zÂ|| _ |j| _|| _d| jv rd| _d| _| js/| | j¡I d H \}}|d ur+|‚|j| _d|v r;d|v r;t	dƒ‚d|v r|t
jt
jB }|d u rLtjn|}tjtjB }d}tj}	|j| j| j||||	|dd�I d H \| _}
}|d uru|‚|
j| _W d	S d|v rÀt
jt
jB }|d u r�tjtjB n|}tjtjB }d}tj}	|j| j| j||||	|dd�I d H \| _}
}|d ur¹|‚|
j| _W d	S tdƒ‚ tyÙ } z
d
|fW  Y d }~S d }~ww )NÚpTr   r—   Úwz(must have exactly one of read/write moderY   z/ONLY read and write is supported at the moment!r[   F)r   rŠ   r!   r"   r    r   r   r\   r   Ú
ValueErrorr   ÚFILE_READ_DATAr^   r_   r{   ra   rb   r|   rd   re   rf   r   r   Ú	EndofFileÚGENERIC_READÚGENERIC_WRITEÚFILE_SHARE_WRITEÚFILE_OPEN_IFrh   )r$   rJ   r"   rn   rk   rl   rm   ro   rq   rp   Ú	smb_replyrr   r%   r%   r&   Úopen#  sT   €
,ñ,þ€ÿzSMBFile.openÚ
local_pathc           
   
   Ã   sÄ  �zÄz‘| j du r
dnd}| jrtdƒ‚| jdurtdƒ‚| j|d|d�I dH \}}|dur/|‚t|ƒ}| ¡ r=| | j¡}t|dƒ�%}|  	¡ 2 z3 dH W \}}|durU|‚|sY n| 
|¡ qG6 W d  ƒ n1 sjw   Y  t| ¡ ƒdfW W |  ¡ I dH  | j dur’|du r“| | j ¡I dH  S S S  tyÅ }	 z&d|	fW  Y d}	~	W |  ¡ I dH  | j dur¿|du rÀ| | j ¡I dH  S S S d}	~	ww |  ¡ I dH  | j durà|du rá| | j ¡I dH  w w w )	z<Downloads the file to the local path. File must not be open.NTFzCannot download a pipe!z,Cannot download a file that is already open!r—   )rn   Úwb)r   r    rh   r   r¢   r   Úis_dirr=   r   Úread_chunkedr“   ÚstrÚabsoluter*   rg   )
r$   rJ   r£   rn   Ú	closetreer3   rl   rH   r�   rr   r%   r%   r&   ÚdownloadV  sP   €
û€ÿÿüÿ€üÿzSMBFile.downloadc              
   Ã   sè   �z\|| _ d| _d| _d}d}d}tj}d}d|v r|tjO }d|v r(|tjO }| j  | j	¡I d H \}}	|	d ur:|	‚|j
| _
| j j| j
| j|||||dd�I d H \| _}
}	|	d ur[|	‚W dS  tys } z
d|fW  Y d }~S d }~ww )	NTr   é€   r—   r™   rY   r[   F)r   r    r   rd   re   r   r›   ÚFILE_WRITE_DATAr\   r   r   rf   r   r   rh   )r$   rJ   r"   rn   rq   ro   rp   rm   rk   rl   r¡   rr   r%   r%   r&   Ú	open_pipex  s4   €

.€ÿzSMBFile.open_pipeÚwhencec              
   Ã   sþ   �zg|dkr|dk rt dƒ‚|| jkrt dƒ‚|| _W dS |dkrBd| j|   k r/| jk r>n t dƒ‚|  j|7  _W dS t dƒ‚|dkrfd| j|   k rT| jk rbn t dƒ‚| j| | _W dS t dƒ‚W dS  t y~ } z
d|fW  Y d}~S d}~ww )zASets the current position of the file buffer to the given offset.r   z#Offset must be > 0 when whence is 0zSeeking outside of file size!r1   r4   r[   N)rh   r   r   )r$   r†   r®   rr   r%   r%   r&   Úseek”  s4   €
ôþ
øþþ€ÿzSMBFile.seekr6   c              
   Ã   sd  �zš| j du r|dkr| jj}|  |d¡I d H \}}||fW S || jkr&d}|dkr-W dS |dkr^| j| jkr:W dS |  | j| j | j¡I d H \}}|d urP|‚|  jt|ƒ7  _||fW S |dkr™| j| jkrkW dS || j | jkry| j| j }|  || j¡I d H \}}|d ur‹|‚|  jt|ƒ7  _||fW S W d S  ty± } z
d |fW  Y d }~S d }~ww )NTr6   r   )rˆ   N)r    r   rŠ   Ú_SMBFile__readr   r   rŒ   rh   )r$   r   r�   rl   rr   r%   r%   r&   r‰   ®  sD   €


 

÷€ÿzSMBFile.readÚ	chunksizec                 C  s\  �| j du r
tdƒ‚|dkr| jj}|dkrdV  dS |dkrX	 |}| j| j |k r9| j| j }|dkr9dV  t‚|  || j¡I dH \}}|du rR|  jt|ƒ7  _||fV  q |dkr¬| j| jkrgdV  t‚|| j | jkru| j| j }	 |}|| j |k r„|| j }|dkr�dV  t‚|  || j¡I dH \}}|du r¦|  jt|ƒ7  _||fV  qvdS )aH  
		Much like read, but yields chuks of chunksize untill the full size is read
		Use this when reading large files as a whole, as this method doesn't caches 
		the whole data read in memory, only chunskize
		chunksize -1 will use the maximum chunk allowed by the underlying connection layer, it is advised to not set manually!
		TzCant stream pipes!r6   r   )NNN)	r    rh   r   rŠ   r   r   ÚStopIterationr°   rŒ   )r$   r   r±   Úreq_sizer�   rl   r%   r%   r&   r¦   Ö  sN   €


ô

öùzSMBFile.read_chunkedc              
   Ã   s   �zht |ƒ| jjk r-|  || j¡I dH \}}|dur|‚| jdkr(|  j|7  _|dfW S d}|t |ƒkre|  |||| jj … | j¡I dH \}}|durO|‚||7 }| jdkr_|  j|7  _|t |ƒks5|dfW S  ty } z
d|fW  Y d}~S d}~ww )z=Writes the given bytes to the file from the current position.NFr   )rŒ   r   ÚMaxWriteSizeÚ_SMBFile__writer   r    rh   )r$   r�   Úcountrl   Útotal_writenrr   r%   r%   r&   r“     s.   €

(
ú
€ÿzSMBFile.writer�   c              
   Ã   s¸   �zD| j dkrtdƒ‚d}	 t d¡I dH  | | jj¡}t|ƒdkr(|dfW S |  || j	¡I dH \}}|dur:|‚|  j	|7  _	||7 }q ty[ } z
d|fW  Y d}~S d}~ww )zVWrites the contents of the buffer(or file handle) to the file. Doesnt work with pipes!TzDoesnt work with pipes!r   N)
r    rh   ÚasyncioÚsleepr‰   r   r´   rŒ   rµ   r   )r$   r�   r·   Úchunkr•   rl   rr   r%   r%   r&   Úwrite_buffer"  s(   €

ö€ÿzSMBFile.write_bufferc                 Ã   s,   �d| j vrdS | j | j| j¡I dH  dS )z*Issues a flush command on the SMB protocolr™   N)r"   r   Úflushr   r   r#   r%   r%   r&   r¼   8  s   €
zSMBFile.flushc                 Ã   sV   �| j du rdS | jdu rdS z	|  ¡ I dH  W n   Y | j | j| j ¡I dH  dS )z$Closes the file including the fileIdN)r   r   r¼   r   r*   r#   r%   r%   r&   r*   >  s   €

zSMBFile.closeÚreturnc                 C   s   | j S )z Returns current position in file)r   r#   r%   r%   r&   ÚtellK  s   zSMBFile.tellc                 C   s6   d}| j D ]}| d¡rq|d|| j | f 7 }q|S )Nz===== FILE =====
Úparent_z	%s : %s
)Ú__dict__rS   )r$   ÚtÚkr%   r%   r&   Ú__str__O  s   

zSMBFile.__str__)T)r   ©r—   N)r6   )r6   r6   )'Ú__name__Ú
__module__Ú__qualname__r'   r)   r.   Ústaticmethodr§   rE   rI   r   rP   rU   rW   rs   rt   ru   r…   Úintr°   Úbytesrµ   r¢   r_   r{   rŸ   rª   r­   r¯   r‰   r¦   r“   ÚioÚBytesIOr»   r¼   r*   r¾   rÃ   r%   r%   r%   r&   r      sD    &*3"(2r   r—   rR   r½   c              	   C  s¾  �zÐ|   ¡  d¡r˜ddlm} | | ¡}|du rp| ¡ }|4 I dH šB | ¡ I dH \}}|dur2|‚| ¡ }| ||¡I dH \}}|durG|‚|V  	 W d  ƒI dH  W |durb| 	¡ I dH  dS dS 1 I dH sjw   Y  nR| ¡ }| ||¡I dH \}}|dur…|‚|V  W |dur–| 	¡ I dH  dS dS t
 || ¡}| ||¡I dH \}}|dur¯|‚|V  W |durÀ| 	¡ I dH  dS dS W |durÐ| 	¡ I dH  dS dS |durÞ| 	¡ I dH  w w )zj
	Opens a file on the remote server
	path can be a smb:// url or a path like \\server\share\path\to\file
	Úsmbr   ©ÚSMBConnectionFactoryN)ÚlowerrS   Ú!aiosmb.commons.connection.factoryrÏ   Úfrom_urlÚget_connectionÚloginÚget_filer¢   r*   r   rP   )rR   r"   rJ   rÏ   Úfactoryr3   rl   Úfiler%   r%   r&   Úsmb_openX  s\   €
÷ÿç	ÿùÿçÿÿrØ   c               	   Ã   s"  �d} t | dƒ4 I d H š}| d¡I d H \}}t|ƒ tt|ƒƒ W d   ƒI d H  n1 I d H s1w   Y  ddlm} | | ¡ ¡ }| ¡ I d H \}}|d urR|‚t dd|d�4 I d H š&}	 | d¡I d H \}}t|ƒ tt|ƒƒ |d	krxnq_W d   ƒI d H  n1 I d H s‰w   Y  t d
d|d�4 I d H š}| 	d¡I d H \}}|d ur¬t|ƒ W d   ƒI d H  n1 I d H s¼w   Y  t d
d|d�4 I d H š3}| d¡I d H \}}|d urët|ƒ 	 W d   ƒI d H  d S t|ƒ tt|ƒƒ W d   ƒI d H  n1 I d H �sw   Y  tdƒ d S )NzSsmb2+ntlm-password://TEST\Administrator:Passw0rd!1@10.10.10.2/C$/temp/repodata.jsonÚrbi   r   rÎ   zC$/temp/repodata.json)rJ   Trˆ   zC$/temp/repodata1.jsonr¤   s   HELLOzDONE!)
rØ   r‰   ÚprintrŒ   rÑ   rÏ   rÒ   rÓ   rÔ   r“   )ÚurlrH   r�   rl   rÏ   rJ   r3   r%   r%   r&   Úamain�  sJ   €(ýû(ÿ€(ýü*úrÜ   Ú__main__rÄ   )Úpathlibr   r   r   Úaiosmb.wintypes.access_maskr   Úaiosmb.protocol.smb2.commandsÚ-aiosmb.wintypes.fscc.structures.fileinfoclassr   Ú(aiosmb.protocol.smb2.commands.query_infor   Ú#aiosmb.wintypes.fscc.FileAttributesr	   rË   Údatetimer¸   Ú
contextlibr
   Úaiosmb.connectionr   Úwinacl.dtyp.security_descriptorr   r   r§   rØ   rÜ   rÅ   Úrunr%   r%   r%   r&   Ú<module>   s0        M($þ