HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/local/CyberCP/lib/python3.10/site-packages/asyncssh/__pycache__/sftp.cpython-310.pyc
o

�h�y�@s�UdZddlZddlZddlmZddlZddlZddlmZmZmZddl	m
Z
ddlZddlZddl
Z
ddlZddlmZddlmZmZmZmZmZddlmZmZmZmZmZmZmZdd	lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%dd
l&m'Z'm(Z(m)Z)ddl*m+Z+dd
l+m,Z,ddl+m-Z-m.Z.m/Z/m0Z0m1Z1ddl+m2Z2m3Z3m4Z4m5Z5ddl+m6Z6m7Z7m8Z8m9Z9ddl+m:Z:m;Z;m<Z<m=Z=m>Z>ddl+m?Z?m@Z@mAZAmBZBddl+mCZCmDZDmEZEmFZFddl+mGZGmHZHmIZImJZJddl+mKZKddl+mLZLmMZMmNZNddl+mOZOmPZPmQZQddl+mRZRmSZSmTZTddl+mUZUmVZVddl+mWZWmXZXddl+mYZYmZZZddl+m[Z[ddl+m\Z\m]Z]m^Z^ddl+m_Z_m`Z`ddl+maZambZbdd l+mcZcmdZddd!l+meZemfZfdd"l+mgZgmhZhdd#l+miZimjZjdd$l+mkZkmlZldd%l+mmZmdd&l+mnZnmoZodd'l+mpZpmqZqdd(l+mrZrmsZsdd)l+mtZtmuZudd*l+mvZvdd+l+mwZwmxZxmyZymzZzdd,l+m{Z{m|Z|m}Z}dd-l+m~Z~mZm�Z�dd.l+m�Z�m�Z�dd/l+m�Z�m�Z�m�Z�dd0l+m�Z�m�Z�m�Z�dd1l+m�Z�m�Z�m�Z�dd2l+m�Z�m�Z�dd3l+m�Z�m�Z�m�Z�dd4l+m�Z�m�Z�dd5l+m�Z�m�Z�dd6l+m�Z�m�Z�m�Z�dd7l+m�Z�m�Z�dd8l+m�Z�dd9l+m�Z�m�Z�dd:l+m�Z�m�Z�dd;l+m�Z�m�Z�dd<l+m�Z�m�Z�dd=l+m�Z�dd>l�m�Z�dd?l�m�Z�m�Z�m�Z�m�Z�m�Z�m�Z�dd@l�m�Z�ddAl�m�Z�m�Z�m�Z�ddBl�m�Z�m�Z�ddCl�m�Z�m�Z�m�Z�m�Z�m�Z�m�Z�ddDl�m�Z�m�Z�m�Z�ddEl�m�Z�m�Z�dFZ�e
j�dGk�r�zddl�Z�ddl�Z�ddl�Z�ddl�Z�ddl�Z�dHZ�Wn
eǐy�Ynwe�r�ddIl�m�Z�ddJl�m�Z�m�Z�ddKl�m�Z�m�Z�e�r�ej�e e�e�fZ�nej�Z�e
j�dGk�r�e�Z�ne�Z�ee�Z�e#e�e�fZ�e#e�ee�fZ�ee#e�ee�fZ�ee�gedLfZ�e#dMe�fZ�e edNe�fZ�e#ej�dLfZ�e#ej�dOfZ�eeee�e�gdfZ�eedPe�gee�fZ�e#de'dFee�gdffZ�eee�e�e�e�gdfZ�e"dQ�Z�dRZ�dSZ�dTZ�dTZ�dUZ�dUZ�e�dVZ�dWZ�dXZ�dXZ�dXZ�dYZ�e+j�Z�ee�e�fe�dZ<efemeoevd[�Z�eOePeRBeSBePeRBeQBePeRBeTBeOePBeOePBeRBeSBeOePBeRBeQBeOePBeRBeTBd\�Z�d]d^�e�e�d_d`����D�Z�Gdadb�dbe(�Z�Gdcdd�dde(�Z�Gdedf�dfe(�Z�Gdgdh�dhe(�Z�die�dje�fdkdl��Zdie�dje e�e�e�e�e�ee�ffdmdn��Zdie�dje e�e�e�e�e�e�e�ee�ee�f	fdodp��Zdie�dje e�e�e�e�ffdqdr��Zdse�dje�fdtdu��Zdve�dje e�e�ffdwdx��Zdy�edje e�e�ffdzd{��Zdye�dvee�dj�efd|d}��Zdye�dvee�dje�fd~d��Z			�dd�ee �e�efd�ee e�e�fdjdLfd�d���Z
d�ee�djee�fd�d���Zd�ee�djee�fd�d���Zd�ee�dje�fd�d���Z
d�ee�dje�fd�d���Zdse�dje e�e�ffd�d���Zd�e�dje e�e�ffd�d���Zd�e�dje�fd�d���Zd�e�dje�fd�d���ZdHd��d�e#e�e�fd�dLd�e�djdfd�d���Ze
j�dGk�r e��r d�e�d�e�d�e�djee e�e�ffd�d���Zn0�eed���r<d�e�d�e�d�e�djee e�e�ffd�d���Znd�e�d�e�d�e�djee e�e�ffd�d���ZGd�d��d�ee���ZGd�d��d��ee���ZGd�d��d��ee���ZGd�d��d��ee���ZGd�d��d�e���ZGd�d��d��e��ZGd�d��d��e��ZGd�d��d��e��ZGd�d��d��e��ZGd�d��d��e��ZGd�d��d��e��Z Gd�d��d��e��Z!Gd�d��d��e��Z"Gd�d„de��Z#Gd�dĄdĐe��Z$Gd�dƄdƐe��Z%Gd�dȄdȐe��Z&Gd�dʄdʐe��Z'Gd�d̄d̐e��Z(Gd�d΄dΐe��Z)Gd�dЄdАe��Z*Gd�d҄dҐe��Z+Gd�dԄdԐe��Z,Gd�dքd֐e��Z-Gd�d؄dؐe��Z.Gd�dڄdڐe��Z/Gd�d܄dܐe��Z0Gd�dބdސe��Z1Gd�d�d�e��Z2Gd�d�d�e��Z3Gd�d�d�e��Z4Gd�d�d�e��Z5Gd�d�d�e��Z6Gd�d�d�e��Z7Gd�d�d�e��Z8Gd�d�d�e��Z9iex�e�ey�e�ez�e�e{�e�e|�e�e}�e �e~�e!�e�e"�e��e#�e��e$�e��e%�e��e&�e��e'�e��e(�e��e)�e��e*�e��e+�e��e,e��e-e��e.e��e/e��e0e��e1e��e2e��e3e��e4e��e5e��e6e��e7e��e8e��e9i��Z:ee�ee�e�g�effe�d�<Gd�dL�dLe���Z;Gd�dO�dOe���Z<Gd�dN�dNe���Z=Gd�d�d�e���Z>Gd�d��d�e���Z?Gd�d��d���Z@Gd�d��d�e���ZAGd�d��d��eA��ZBGd�dM�dM��ZCGd�d��d���ZDG�ddP�dP�eA��ZEG�d�d��d��ZFG�d�d��d��ZGG�d�d��d��ZH�eH��ZIG�d�d��d��ZJG�d	�d
��d
��ZK�d�d�d
e�jL�d�d�d�d�dee��deӐde�dj�eDf�d�d��ZM�de��eF�d�d�d�d�de�djdf
�d�d��ZN�de��eF�d�d�d�d�de�djedf
�d�d��ZOdS(z
SFTP handlers�N)�fnmatch)�SEEK_SET�SEEK_CUR�SEEK_END)�PurePath)�
TracebackType)�
TYPE_CHECKING�AnyStr�
AsyncIterator�	Awaitable�Callable)�Dict�Generic�IO�Iterable�List�Mapping�Optional)�Sequence�Set�Tuple�Type�TypeVar�Union�cast�overload)�Literal�Protocol�Self�)�	constants)�DEFAULT_LANG)�FXP_INIT�FXP_VERSION�FXP_OPEN�	FXP_CLOSE�FXP_READ)�	FXP_WRITE�	FXP_LSTAT�	FXP_FSTAT�FXP_SETSTAT)�FXP_FSETSTAT�FXP_OPENDIR�FXP_READDIR�
FXP_REMOVE)�	FXP_MKDIR�	FXP_RMDIR�FXP_REALPATH�FXP_STAT�
FXP_RENAME)�FXP_READLINK�FXP_SYMLINK�FXP_LINK�	FXP_BLOCK)�FXP_UNBLOCK�
FXP_STATUS�
FXP_HANDLE�FXP_DATA)�FXP_NAME�	FXP_ATTRS�FXP_EXTENDED�FXP_EXTENDED_REPLY)�
FXR_OVERWRITE)�
FXRP_NO_CHECK�FXRP_STAT_IF_EXISTS�FXRP_STAT_ALWAYS)�FXF_READ�	FXF_WRITE�
FXF_APPEND)�	FXF_CREAT�	FXF_TRUNC�FXF_EXCL)�FXF_ACCESS_DISPOSITION�FXF_CREATE_NEW)�FXF_CREATE_TRUNCATE�FXF_OPEN_EXISTING)�FXF_OPEN_OR_CREATE�FXF_TRUNCATE_EXISTING)�FXF_APPEND_DATA)�ACE4_READ_DATA�ACE4_WRITE_DATA�ACE4_APPEND_DATA)�ACE4_READ_ATTRIBUTES�ACE4_WRITE_ATTRIBUTES)�FILEXFER_ATTR_SIZE�FILEXFER_ATTR_UIDGID)�FILEXFER_ATTR_PERMISSIONS�FILEXFER_ATTR_ACMODTIME)�FILEXFER_ATTR_EXTENDED�FILEXFER_ATTR_DEFINED_V3)�FILEXFER_ATTR_ACCESSTIME�FILEXFER_ATTR_CREATETIME)�FILEXFER_ATTR_MODIFYTIME�FILEXFER_ATTR_ACL)�FILEXFER_ATTR_OWNERGROUP�FILEXFER_ATTR_SUBSECOND_TIMES)�FILEXFER_ATTR_DEFINED_V4)�FILEXFER_ATTR_BITS�FILEXFER_ATTR_DEFINED_V5)�FILEXFER_ATTR_ALLOCATION_SIZE�FILEXFER_ATTR_TEXT_HINT)�FILEXFER_ATTR_MIME_TYPE�FILEXFER_ATTR_LINK_COUNT)�FILEXFER_ATTR_UNTRANSLATED_NAME�FILEXFER_ATTR_CTIME)�FILEXFER_ATTR_DEFINED_V6)�FX_OK�FX_EOF�FX_NO_SUCH_FILE�FX_PERMISSION_DENIED)�
FX_FAILURE�FX_BAD_MESSAGE�FX_NO_CONNECTION)�FX_CONNECTION_LOST�FX_OP_UNSUPPORTED�	FX_V3_END)�FX_INVALID_HANDLE�FX_NO_SUCH_PATH)�FX_FILE_ALREADY_EXISTS�FX_WRITE_PROTECT�FX_NO_MEDIA)�	FX_V4_END�FX_NO_SPACE_ON_FILESYSTEM�FX_QUOTA_EXCEEDED)�FX_UNKNOWN_PRINCIPAL�FX_LOCK_CONFLICT�	FX_V5_END)�FX_DIR_NOT_EMPTY�FX_NOT_A_DIRECTORY)�FX_INVALID_FILENAME�FX_LINK_LOOP�FX_CANNOT_DELETE)�FX_INVALID_PARAMETER�FX_FILE_IS_A_DIRECTORY)�FX_BYTE_RANGE_LOCK_CONFLICT�FX_BYTE_RANGE_LOCK_REFUSED)�FX_DELETE_PENDING�FX_FILE_CORRUPT�FX_OWNER_INVALID)�FX_GROUP_INVALID�FX_NO_MATCHING_BYTE_RANGE_LOCK)�	FX_V6_END)�FILEXFER_TYPE_REGULAR�FILEXFER_TYPE_DIRECTORY)�FILEXFER_TYPE_SYMLINK�FILEXFER_TYPE_SPECIAL)�FILEXFER_TYPE_UNKNOWN�FILEXFER_TYPE_SOCKET)�FILEXFER_TYPE_CHAR_DEVICE�FILEXFER_TYPE_BLOCK_DEVICE)�FILEXFER_TYPE_FIFO)�	SSHLogger)�
BytesOrStr�Error�FilePath�
MaybeAwait�
OptExcInfo�Record)�ConnectionLost)�async_context_manager�get_symbol_names�
hide_empty)�make_sparse_file�plural)�Boolean�Byte�String�UInt16�UInt32�UInt64)�PacketDecodeError�	SSHPacket�SSHPacketLogger)�
__author__�__version__F�win32T)�SSHServerChannel)�SSHClientConnection�SSHServerConnection)�	SSHReader�	SSHWriter�	SFTPAttrs�SFTPClientFile�SFTPName�SFTPVFSAttrs�SFTPServerHandler�_T��i@i@ii�iʚ;�_const_dict)r���r�)�r�w�a�xzr+zw+za+zx+cCsi|]	\}}||���qS�)�lower)�.0�k�vr�r��@/usr/local/CyberCP/lib/python3.10/site-packages/asyncssh/sftp.py�
<dictcomp>��r��FILEXFER_TYPE_�c@s"eZdZdZdedefdd�ZdS)�_SupportsEncodez,Protocol for applying encoding to path names�sftp_version�returncC�dS)z'Encode result as bytes in an SSH packetNr���selfr�r�r�r��encode��z_SupportsEncode.encodeN)�__name__�
__module__�__qualname__�__doc__�int�bytesr�r�r�r�r�r��sr�c@s8eZdZdZdeddfdd�Zdededfdd	�Zd
S)�_SFTPGlobProtocolz;Protocol for getting files to perform glob matching against�pathr�r�c���dS)zGet attributes of a fileNr��r�r�r�r�r��stat��z_SFTPGlobProtocol.statr�cCr���7Return names and attributes of the files in a directoryNr�r�r�r�r��scandir�r�z_SFTPGlobProtocol.scandirN)r�r�r�r�r�r�r
r�r�r�r�r�r��sr�c	@s�eZdZdZdefdd�Zdeeedeedee	de
fdd	�Zd
edede
eeeffdd
�Zded
edefdd�Zded
edefdd�Zddd�ZdS)�SFTPFileProtocolz0Protocol for accessing a file via an SFTP serverr�c�r�)z=Allow SFTPFileProtocol to be used as an async context managerNr��r�r�r�r��
__aenter__�r�zSFTPFileProtocol.__aenter__�	_exc_type�
_exc_value�
_tracebackc�r�)�9Wait for file close when used as an async context managerNr��r�r�r�r�r�r�r��	__aexit__�r�zSFTPFileProtocol.__aexit__�offset�lengthcCr��z"Return file ranges containing dataNr��r�r�r�r�r�r��request_ranges�r�zSFTPFileProtocol.request_ranges�sizec�r�)�Read data from the local fileNr��r�r�r�r�r�r��read�r�zSFTPFileProtocol.read�datac�r�)�Write data to the local fileNr��r�r�r�r�r�r��write�r�zSFTPFileProtocol.writeNc�r��zClose the local fileNr�r�r�r�r��close�r�zSFTPFileProtocol.close�r�N)r�r�r�r�rr�rr�
BaseExceptionr�boolr�r�r
rr�r�r�r�r�r�r�r�r�r��s ��
�

�r�c
@s$eZdZdZed*dd��Zededefdd��Zde	defd	d
�Z
	d+dedeedefd
d�Zdd�dede
ddfdd�Zdd�deddde
ddfdd�Zdede
fdd�Zdededfdd�Zdeddfdd�Zdedefdd �Zd!ed"eddfd#d$�Ze	%d,ded&ed'edefd(d)��ZdS)-�_SFTPFSProtocolz6Protocol for accessing a filesystem via an SFTP serverr��
SFTPLimitscCr�)z4SFTP server limits associated with this SFTP sessionNr�r�r�r�r��limitsr�z_SFTPFSProtocol.limitsr�cCr�)�0Return the final component of a POSIX-style pathNr��r�r�r�r��basenamer�z_SFTPFSProtocol.basenamecCr�)z/Encode path name using configured path encodingNr�r�r�r�r�r�r�z_SFTPFSProtocol.encodeN�parentcCr�)zCompose a pathNr��r�r�r	r�r�r��compose_pathr�z_SFTPFSProtocol.compose_pathT��follow_symlinksr
r�c�r�)z/Get attributes of a file, directory, or symlinkNr��r�r�r
r�r�r�r�r�z_SFTPFSProtocol.stat�attrsc�r�)z/Set attributes of a file, directory, or symlinkNr��r�r�rr
r�r�r��setstatr�z_SFTPFSProtocol.setstatc�r��z(Return if the path refers to a directoryNr�r�r�r�r��isdirr�z_SFTPFSProtocol.isdirr�cCr�r�r�r�r�r�r�r�r�z_SFTPFSProtocol.scandirc�r��zCreate a directoryNr�r�r�r�r��mkdir r�z_SFTPFSProtocol.mkdirc�r�)z$Return the target of a symbolic linkNr�r�r�r�r��readlink#r�z_SFTPFSProtocol.readlink�oldpath�newpathc�r�)zCreate a symbolic linkNr��r�rrr�r�r��symlink&r�z_SFTPFSProtocol.symlink����mode�
block_sizec�r��zOpen a fileNr�)r�r�rrr�r�r��open)r�z_SFTPFSProtocol.open)r�r�N�r)r�r�r�r��propertyr�staticmethodr�r�	_SFTPPathr�rrrr�rrr
r�rrrr��strr�r�rr�r�r�r�rsJ��
�
��
���
����rr�r�cCst|�}|��}|��|S)z)Parse an SFTPv6 "acl-supported" extension)r��
get_uint32�	check_end)r��packet�capabilitiesr�r�r��_parse_acl_supported/sr*c	Cs^t|�}|��}|��}|��}|��}|��}g}|r'|��}|�|�|s||||||fS)z%Parse an SFTPv5 "supported" extension)r�r&�
get_string�append)	r�r(�	attr_mask�attrib_mask�
open_flags�access_mask�
max_read_size�	ext_names�namer�r�r��_parse_supported9s
��r4c	Cs�t|�}|��}|��}|��}|��}|��}|��}|��}|��}	g}
t|	�D]	}|
�|���q*|��}g}
t|�D]	}|
�|���q>|��||||||||
|
f	S)z&Parse an SFTPv6 "supported2" extension)r�r&�
get_uint16�ranger,r+r')r�r(r-r.r/r0r1�open_block_vector�block_vector�attrib_ext_count�attrib_ext_names�_�	ext_countr2r�r�r��_parse_supported2Ns*�r=cCsLt|�}|���dd�}|���dd�}|���dd�}|��}||||fS)zParse a "vendor-id" extension�utf-8�backslashreplace)r�r+�decode�
get_uint64)r�r(�vendor_name�product_name�product_version�
product_buildr�r�r��_parse_vendor_idnsrFrcCs�t�|�r	t}|St�|�rt}|St�|�rt}|St�|�r$t}|St�	|�r-t
}|St�|�r6t}|St�
|�r?t}|St�|�dkrJt}|St}|S)z*Convert stat mode/permissions to file typer)r��S_ISREGr��S_ISDIRr��S_ISLNKr��S_ISSOCKr��S_ISCHRr��S_ISBLKr��S_ISFIFOr��S_IFMTr�r�)r�filetyper�r�r��_stat_mode_to_filetype{s4

�
�
�
�

�
���rP�nseccCs
t|t�S)z6Convert nanoseconds since epoch to seconds & remainder)�divmod�
_NSECS_IN_SEC)rQr�r�r��_nsec_to_tuple�s
rT�seccCst|�t|dt�fS)z8Convert float seconds since epoch to seconds & remainderr)r�rS)rUr�r�r��_float_sec_to_tuple�srVcCs|t|pd�tS)z:Convert seconds and remainder to float seconds since epochr)�floatrS�rUrQr�r�r��_tuple_to_float_sec�srYcCs|t|pdS)z8Convert seconds and remainder to nanoseconds since epochr)rSrXr�r�r��_tuple_to_nsec�srZ�times�nscCs�|rt|d�\}}t|d�\}}n.|r&t|d�\}}t|d�\}}nttd�r4tt���\}}ntt���\}}||}}t||||d�S)z$Convert utime arguments to SFTPAttrsrr�time_ns)�atime�atime_ns�mtime�mtime_ns)rTrV�hasattr�timer]r�)r[r\r^r_r`rar�r�r��_utime_to_attrs�s

�rd�usercC�n|dur3z
ddl}|�|�j}W|Sttfy2zt|�}WY|Sty1td|���d�wwd}|S)z*Return the uid associated with a user nameNrzInvalid owner: )�pwd�getpwnam�pw_uid�ImportError�KeyErrorr��
ValueError�SFTPOwnerInvalid)rerg�uidr�r�r��_lookup_uid��	����ro�groupcCrf)z+Return the gid associated with a group nameNrzInvalid group: )�grp�getgrnam�gr_gidrjrkr�rl�SFTPGroupInvalid)rqrr�gidr�r�r��_lookup_gid�rprwrnc	C�L|dur"z
ddl}|�|�j}W|Sttfy!t|�}Y|Swd}|S)z*Return the user name associated with a uidNr�)rg�getpwuid�pw_namerjrkr%)rnrgrer�r�r��_lookup_user���
�r|rvc	Crx)z+Return the group name associated with a gidNrry)rr�getgrgid�gr_namerjrkr%)rvrrrqr�r�r��
_lookup_group�r}r�cCsBd|vr
|�dd�}d}nd}t�|�}|std|����||fS)z$Convert open mode to SFTP open flags�bryTFzInvalid mode: )�replace�_open_modes�getrl)r�binary�pflagsr�r�r��_mode_to_pflagss
r�r�cCs�d}d}|ttB@ttBkrt}n|ttB@ttBkrt}n|t@r%t}n	|t@r,t}nt}|t@r8|t	t
BO}|t@rB|tt
BO}|t@rN|tO}|tO}||fS)z8Convert SFTPv3 pflags to SFTPv5 desired-access and flagsr)rGrIrKrHrLrNrOrMrDrQrTrErRrUrFrSrP)r��desired_access�flagsr�r�r��_pflags_to_flagss&r�r�cCsLt�|�}tjdkr$|�dd�}|dd�dkr$|dd�dkr$d|}|S)zConvert local path to SFTP pathr��\�/Nr��:)�os�fsencode�sys�platformr�rr�r�r��_from_local_path7s

 r�cCs^tjdkr(t�|�}|dd�dkr |dd�dkr |dd�}|�dd�}|St�|�}|S)	zConvert SFTP path to local pathr�Nr�/r�r��:�\)r�r�r��fsdecoder�r�rr�r�r��_to_local_pathEs

 
�r�rrr
cCs�|jdurt�||j�|jdurt|j�n|j}|jdur#t|j�n|j}|j	dur2t
|j	|j�nd}|jdur@t
|j|j
�nd}|durJ|dusR|duro|durotj||d�}|durd|durd|j}|duro|duro|j}|dur�|dur�ztj||||d�Wnty�Yn
ty�td�w|jdur�ztj|t�|j�|d�Wn	ty�Ynw|dur�|dur�z
tj|||f|d�WdSty�YdSwdSdS)z'Utility function to set file attributesNr)r\r
)r�r��truncaternro�ownerrvrwrqr^rZr_r`rar��st_atime_ns�st_mtime_ns�chown�NotImplementedError�AttributeError�permissions�chmod�S_IMODE�utime)r�rr
rnrvr_ra�stat_resultr�r�r��_setstatUsX

��
���

�����r��file_objr�r�c	
Cs��t�|���}td}	z|�dd�|�dd�}t�|tj||d�}Wn!t	j
yF}z|jdtj
kr;|d9}n�WYd}~nd}~wwnq
tdt|�d�D]!}t�|||d�d�}t�||d|d�d�}||fVqQdS)z-Return file ranges containing data on Windows�T��littleNrr�)�msvcrt�
get_osfhandle�fileno�_MAX_SPARSE_RANGES�to_bytes�	win32file�DeviceIoControl�winioctlcon�FSCTL_QUERY_ALLOCATED_RANGES�
pywintypes�error�args�winerror�ERROR_MORE_DATAr6�lenr��
from_bytes)	r�r�r��handle�bufsize�query_range�ranges�exc�posr�r�r��_request_ranges�s4�

��
�����r��	SEEK_DATAc
Cs��|}||}z&||kr+|�|tj�}t|�|tj�|�}|||fV||ksWdSWdStyG}z|jtjkr<�WYd}~dSd}~wwr�)�seekr�r��min�	SEEK_HOLE�OSError�errno�ENXIO)r�r�r��end�limit�startr�r�r�r�r��s�����cCs�|r
||fVdSdS)z;Sparse files aren't supported - return the full input rangeNr�)r�r�r�r�r�r�r��s��c
@s�eZdZdZdedededefdd�Zdededeeeeeffd	d
�Zddd
�Z	dededeeeffdd�Z
deeeeffdd�ZdS)�_SFTPParallelIOzlParallelize I/O requests on files

       This class issues parallel read and write requests on files.

    r�max_requestsr�r�cCs$||_||_||_||_t�|_dSr )�_block_size�
_max_requests�_offset�_bytes_left�set�_pending)r�rr�r�r�r�r�r��__init__�s
z_SFTPParallelIO.__init__r�c�s$�|�||�IdH\}}||||fS)z;Start a task to perform file I/O on a particular byte rangeN)�run_task)r�r�r��count�resultr�r�r��_start_task�s�z_SFTPParallelIO._start_taskNcCs�|jr=t|j�|jkrAt|j|j�}t�|�|j	|��}|j�
|�|j	|7_	|j|8_|jr?t|j�|jksdSdSdSdS)zCreate parallel file I/O tasksN)r�r�r�r�r�r��asyncio�
ensure_futurer�r��add)r�r��taskr�r�r��_start_tasks�s&�z_SFTPParallelIO._start_tasksc���t�)z+Perform file I/O on a particular byte range�r�)r�r�r�r�r�r�r����z_SFTPParallelIO.run_taskc	Cs�|��|jr�tj|jtjd�IdH\}|_g}|D]L}z&|��\}}}}||fV|rB||krB|j�t�|�||||���Wqt	yOd|_
Yqttfyg}z|�
|�WYd}~qd}~ww|rx|jD]}|��qm|d�|��|jsdSdS)z5Perform file I/O and return async iterator of results)�return_whenNr)r�r�r��wait�FIRST_COMPLETEDr�r�r�r��SFTPEOFErrorr�r��	SFTPErrorr,�cancel)	r��done�
exceptionsr�r�r�r�r�r�r�r�r��iter�s8��

��
��

�z_SFTPParallelIO.iterr)
r�r�r�r�r�r�rr�r�r�r�r
r�r�r�r�r�r��s
�
�

�
r�csfeZdZdZdededddededef�fd	d
�Zdededeeeffdd
�Zdefdd�Z	�Z
S)�_SFTPFileReaderzParallelized SFTP file readerrr��handler�SFTPClientHandlerr�r�r�cs(t��||||�||_||_||_dSr )�superr��_handler�_handle�_start)r�rr�r�r�r�r���	__class__r�r�r�s
z_SFTPFileReader.__init__r�c�s*�|j�|j||�IdH\}}t|�|fS)zRead a block of the fileN)r�r�r�r�)r�r�r�r�r;r�r�r�r�s�z_SFTPFileReader.run_taskc�sj�t�}|��2z'3dHW\}}||j}|t|�}|dkr%||d7}||||t|��<q6t|�S)z.Reassemble and return data from parallel readsNr�)�	bytearrayr�r�r�r�)r�r�r�r�r��padr�r�r��runs�
�	z_SFTPFileReader.run�r�r�r�r�r�r�r�rr�r��
__classcell__r�r�r�r�r�s
����	r�cs`eZdZdZdededddededef�fd	d
�Zdededeeeffd
d�Zdd�Z	�Z
S)�_SFTPFileWriterzParallelized SFTP file writerrr�r�r�r�r�r�cs2t��|||t|��||_||_||_||_dSr )r�r�r�r�r�r��_data)r�rr�r�r�r�r�r�r�r�r�,s

z_SFTPFileWriter.__init__r�r�c�s:�||j}|j�|j||j|||��IdH||fS)zWrite a block to the fileN)r�r�r�r�r�)r�r�r�r�r�r�r�r�6s�

�z_SFTPFileWriter.run_taskc�s�|��2z3dHW}q6dS)zPerform parallel writesN)r�)r�r;r�r�r�r�>s��z_SFTPFileWriter.runr�r�r�r�r�r�)s
����
r�csneZdZdZdededededededed	ed
ef�fdd�Z	d
edede
eeffdd�Zddd�Z�Z
S)�_SFTPFileCopierz�SFTP file copier

       This class parforms an SFTP file copy, initiating multiple
       read and write requests to copy chunks of the file in parallel.

    rr��total_bytes�sparse�srcfs�dstfs�srcpath�dstpath�progress_handlerc

sRt��||dd�||_||_||_||_||_d|_d|_d|_	||_
|	|_dS)Nr)r�r��_sparse�_srcfs�_dstfs�_srcpath�_dstpath�_src�_dst�
_bytes_copied�_total_bytes�_progress_handler)
r�rr�r�r�r�r�r�rrr�r�r�r�Ls
z_SFTPFileCopier.__init__r�r�r�c�sV�|jdusJ�|jdusJ�|j�||�IdH}|j�||�IdHt|�}||fS)zCopy a block of the source fileN)rrr�r�r�)r�r�r�r��datalenr�r�r�r�as�z_SFTPFileCopier.run_taskNc	�s��dtdtdttttffdd�}�z|jj|jddd�Id	H|_|jj|jd
dd�Id	H|_	|j
r]|jdkr]|�
|j|jdd�W|jrN|j��Id	H|j	r[|j	��Id	Hd	Sd	S|j
ri|j�d|j�}n|d|j�}|j|jkr�t|jt�r�|jjr�|2z33d	HW\}}|j�tt|j�tt|j	�|||�Id	H|j|7_|j
r�|�
|j|j|j|j�q�6nU|2z23d	HW\|_|_|��2z"3d	HW\}}|j|7_|j
r�|dkr�|�
|j|j|j|j�q�6q�6|j|jk�r|j
�std�}t|d|j�t|d|j�|�W|j�r|j��Id	H|j	�r'|j	��Id	Hd	Sd	S|j�r5|j��Id	H|j	�rB|j	��Id	Hww)
zPerform parallel file copyr�r�r�cSs�||fVdS)z+Return the entire file as the range to copyNr�)r�r�r�r�r��_request_nonsparse_rangeps�z5_SFTPFileCopier.run.<locals>._request_nonsparse_range�rbr�rN�wbzUnexpected EOF during file copy�filename)r�r
rrrrrrrrrr
r�rr��
isinstance�
SFTPClient�supports_remote_copy�remote_copyrr�r	r�r�r��SFTPFailure�setattr)r�r
r�r�r�r;rr�r�r�r�r�ms��

���(��
��


����
����	����z_SFTPFileCopier.runr)r�r�r�r�r�rrr��SFTPProgressHandlerr�rr�r�r�r�r�r�r�r�Ds"������r�c@sNeZdZdZedededfdd��Zdede	fdd�Z
dedd	fd
d�Zd	S)r�a"SFTP error

       This exception is raised when an error occurs while processing
       an SFTP request. Exception codes should be taken from
       :ref:`SFTP error codes <SFTPErrorCodes>`.

       :param code:
           Disconnect reason, taken from :ref:`disconnect reason
           codes <DisconnectReasons>`
       :param reason:
           A human-readable reason for the disconnect
       :param lang: (optional)
           The language the reason is in
       :type code: `int`
       :type reason: `str`
       :type lang: `str`

    r(r�c	Cs�|��}|r#z|���d�}|���d�}Wnty"td�d�wd}d}|tkr-dSz	t|||�}WntyKt||�d|�d�|�}Ynw|�|�|S)z2Construct an SFTPError from an FXP_STATUS responser>�ascii�Invalid status messageNryz (error �))	r&r+r@�UnicodeDecodeError�SFTPBadMessagerl�_sftp_error_maprkr�)r(�code�reason�langr�r�r�r��	construct�s&
��
zSFTPError.construct�versioncCs�|jtkr|dkrt}n&|jtkr/|jtkr|dks,|jtkr#|dks,|jtkr/|dkr/t}n|j}t|�t	|j
�t	|j�S)z,Encode an SFTPError as bytes in an SSHPacketr�r�r�r�)rr�rnr�rur{r�rpr�r�r r!)r�r#rr�r�r�r��s
zSFTPError.encodeNcCr�)�Decode error-specific dataNr��r�r(r�r�r�r@�r�zSFTPError.decode)r�r�r�r�r#r�rr"r�r�r�r@r�r�r�r�r��sr�cs0eZdZdZdefdedef�fdd�
Z�ZS)r�a9SFTP EOF error

       This exception is raised when end of file is reached when
       reading a file or directory.

       :param reason: (optional)
           Details about the EOF
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    ryr r!c�t��t||�dSr )r�r�rm�r�r r!r�r�r�r��zSFTPEOFError.__init__�r�r�r�r�r!r%r�r�r�r�r�r�r��s$r�c�.eZdZdZefdedef�fdd�
Z�ZS)�SFTPNoSuchFileaSFTP no such file

       This exception is raised when the requested file is not found.

       :param reason:
           Details about the missing file
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rnr'r�r�r�r�r(zSFTPNoSuchFile.__init__r)r�r�r�r�r+
�"
r+cr*)�SFTPPermissionDeniedaSSFTP permission denied

       This exception is raised when the permissions are not available
       to perform the requested operation.

       :param reason:
           Details about the invalid permissions
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�ror'r�r�r�r�+r(zSFTPPermissionDenied.__init__r)r�r�r�r�r-�"r-cr*)raSFTP failure

       This exception is raised when an unexpected SFTP failure occurs.

       :param reason:
           Details about the failure
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rpr'r�r�r�r�=r(zSFTPFailure.__init__r)r�r�r�r�r/r,rcr*)ra(SFTP bad message

       This exception is raised when an invalid SFTP message is
       received.

       :param reason:
           Details about the invalid message
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rqr'r�r�r�r�Pr(zSFTPBadMessage.__init__r)r�r�r�r�rAr.rcr*)�SFTPNoConnectiona;SFTP no connection

       This exception is raised when an SFTP request is made on a
       closed SSH connection.

       :param reason:
           Details about the closed connection
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rrr'r�r�r�r�cr(zSFTPNoConnection.__init__r)r�r�r�r�r/Tr.r/cr*)�SFTPConnectionLostaMSFTP connection lost

       This exception is raised when the SSH connection is lost or
       closed while making an SFTP request.

       :param reason:
           Details about the connection failure
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rsr'r�r�r�r�vr(zSFTPConnectionLost.__init__r)r�r�r�r�r0gr.r0cr*)�SFTPOpUnsupportedaBSFTP operation unsupported

       This exception is raised when the requested SFTP operation
       is not supported.

       :param reason:
           Details about the unsupported operation
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rtr'r�r�r�r��r(zSFTPOpUnsupported.__init__r)r�r�r�r�r1zr.r1cr*)�SFTPInvalidHandlea(SFTP invalid handle (SFTPv4+)

       This exception is raised when the handle provided is invalid.

       :param reason:
           Details about the invalid handle
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rvr'r�r�r�r��r(zSFTPInvalidHandle.__init__r)r�r�r�r�r2�r,r2cr*)�SFTPNoSuchPatha%SFTP no such path (SFTPv4+)

       This exception is raised when the requested path is not found.

       :param reason:
           Details about the missing path
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rwr'r�r�r�r��r(zSFTPNoSuchPath.__init__r)r�r�r�r�r3�r,r3cr*)�SFTPFileAlreadyExistsa/SFTP file already exists (SFTPv4+)

       This exception is raised when the requested file already exists.

       :param reason:
           Details about the existing file
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rxr'r�r�r�r��r(zSFTPFileAlreadyExists.__init__r)r�r�r�r�r4�r,r4cr*)�SFTPWriteProtectaTSFTP write protect (SFTPv4+)

       This exception is raised when a write is attempted to a file
       on read-only or write protected media.

       :param reason:
           Details about the requested file
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�ryr'r�r�r�r��r(zSFTPWriteProtect.__init__r)r�r�r�r�r5�r.r5cr*)�SFTPNoMediaa4SFTP no media (SFTPv4+)

       This exception is raised when there is no media in the
       requested drive.

       :param reason:
           Details about the requested drive
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rzr'r�r�r�r��r(zSFTPNoMedia.__init__r)r�r�r�r�r6�r.r6cr*)�SFTPNoSpaceOnFilesystemaqSFTP no space on filesystem (SFTPv5+)

       This exception is raised when there is no space available
       on the filesystem a file is being written to.

       :param reason:
           Details about the filesystem which has filled up
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r|r'r�r�r�r��r(z SFTPNoSpaceOnFilesystem.__init__r)r�r�r�r�r7�r.r7cr*)�SFTPQuotaExceededa5SFTP quota exceeded (SFTPv5+)

       This exception is raised when the user's storage quota
       is exceeded.

       :param reason:
           Details about the exceeded quota
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r}r'r�r�r�r�r(zSFTPQuotaExceeded.__init__r)r�r�r�r�r8�r.r8cs`eZdZdZedfdededeef�fdd�
Zded	e	f�fd
d�Z
ded	d
fdd�Z�Z
S)�SFTPUnknownPrincipala�SFTP unknown principal (SFTPv5+)

       This exception is raised when a file owner or group is
       not reocgnized.

       :param reason:
           Details about the unknown principal
       :param lang: (optional)
           The language the reason is in
       :param unknown_names: (optional)
           A list of unknown principal names
       :type reason: `str`
       :type lang: `str`
       :type unknown_names: list of `str`

    r�r r!�
unknown_namescst��t||�||_dSr )r�r�r~r:)r�r r!r:r�r�r�r�!s
zSFTPUnknownPrincipal.__init__r#r�cs"t��|�d�dd�|jD��S)z7Encode an SFTPUnknownPrincipal as bytes in an SSHPacket�cs��|]}t|�VqdSr �r��r�r3r�r�r��	<genexpr>*��z.SFTPUnknownPrincipal.encode.<locals>.<genexpr>)r�r��joinr:)r�r#r�r�r�r�&s
�zSFTPUnknownPrincipal.encoder(NcCsJg|_z|r|j�|���d��|sWdSWdSty$td�d�w)r$r>rN)r:r,r+r@rrr%r�r�r�r@,s��
�zSFTPUnknownPrincipal.decode)r�r�r�r�r!r%rr�r�r�r�r�r@r�r�r�r�r�r9s��r9cr*)�SFTPLockConflicta=SFTP lock conflict (SFTPv5+)

       This exception is raised when a requested lock is held by
       another process.

       :param reason:
           Details about the conflicting lock
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�rr'r�r�r�r�Hr(zSFTPLockConflict.__init__r)r�r�r�r�rB9r.rBcr*)�SFTPDirNotEmptya,SFTP directory not empty (SFTPv6+)

       This exception is raised when a directory is not empty.

       :param reason:
           Details about the non-empty directory
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r�Zr(zSFTPDirNotEmpty.__init__r)r�r�r�r�rCLr,rCcr*)�SFTPNotADirectorya]SFTP not a directory (SFTPv6+)

       This exception is raised when a specified file is
       not a directory where one was expected.

       :param reason:
           Details about the file expected to be a directory
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r�mr(zSFTPNotADirectory.__init__r)r�r�r�r�rD^r.rDcr*)�SFTPInvalidFilenamea%SFTP invalid filename (SFTPv6+)

       This exception is raised when a filename is not valid.

       :param reason:
           Details about the invalid filename
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r�r(zSFTPInvalidFilename.__init__r)r�r�r�r�rEqr,rEcr*)�SFTPLinkLoopa SFTP link loop (SFTPv6+)

       This exception is raised when a symbolic link loop is detected.

       :param reason:
           Details about the link loop
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r��r(zSFTPLinkLoop.__init__r)r�r�r�r�rF�r,rFcr*)�SFTPCannotDeletea#SFTP cannot delete (SFTPv6+)

       This exception is raised when a file cannot be deleted.

       :param reason:
           Details about the undeletable file
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r��r(zSFTPCannotDelete.__init__r)r�r�r�r�rG�r,rGcr*)�SFTPInvalidParametera`SFTP invalid parameter (SFTPv6+)

       This exception is raised when parameters in a request are
       out of range or incompatible with one another.

       :param reason:
           Details about the invalid parameter
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r��r(zSFTPInvalidParameter.__init__r)r�r�r�r�rH�r.rHcr*)�SFTPFileIsADirectoryaSSFTP file is a directory (SFTPv6+)

       This exception is raised when a specified file is a
       directory where one isn't allowed.

       :param reason:
           Details about the unexpected directory
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r��r(zSFTPFileIsADirectory.__init__r)r�r�r�r�rI�r.rIcr*)�SFTPByteRangeLockConflictarSFTP byte range lock conflict (SFTPv6+)

       This exception is raised when a read or write request overlaps
       a byte range lock held by another process.

       :param reason:
           Details about the conflicting byte range lock
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r��r(z"SFTPByteRangeLockConflict.__init__r)r�r�r�r�rJ�r.rJcr*)�SFTPByteRangeLockRefusedaNSFTP byte range lock refused (SFTPv6+)

       This exception is raised when a request for a byte range
       lock was refused.

       :param reason:
           Details about the refused byte range lock
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r��r(z!SFTPByteRangeLockRefused.__init__r)r�r�r�r�rK�r.rKcr*)�SFTPDeletePendingaySFTP delete pending (SFTPv6+)

       This exception is raised when an operation was attempted
       on a file for which a delete operation is pending.
       another process.

       :param reason:
           Details about the file being deleted
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r�r(zSFTPDeletePending.__init__r)r�r�r�r�rL�s"rLcr*)�SFTPFileCorrupta/SFTP file corrupt (SFTPv6+)

       This exception is raised when filesystem corruption is detected.

       :param reason:
           Details about the corrupted filesystem
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r�r(zSFTPFileCorrupt.__init__r)r�r�r�r�rMr,rMcr*)rma\SFTP owner invalid (SFTPv6+)

       This exception is raised when a principal cannot be assigned
       as the owner of a file.

       :param reason:
           Details about the principal being set as a file's owner
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r�(r(zSFTPOwnerInvalid.__init__r)r�r�r�r�rmr.rmcr*)ruadSFTP group invalid (SFTPv6+)

       This exception is raised when a principal cannot be assigned
       as the primary group of a file.

       :param reason:
           Details about the principal being set as a file's group
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r�;r(zSFTPGroupInvalid.__init__r)r�r�r�r�ru,r.rucr*)�SFTPNoMatchingByteRangeLockavSFTP no matching byte range lock (SFTPv6+)

       This exception is raised when an unlock is requested for a
       byte range lock which is not currently held.

       :param reason:
           Details about the byte range lock being released
       :param lang: (optional)
           The language the reason is in
       :type reason: `str`
       :type lang: `str`

    r r!cr&r )r�r�r�r'r�r�r�r�Nr(z$SFTPNoMatchingByteRangeLock.__init__r)r�r�r�r�rN?r.rNrc@s�eZdZUdZeZeed<eeed<eeed<eeed<eeed<ee	ed<ee	ed<eeed	<eeed
<eeed<eeed<eeed
<eeed<eeed<eeed<eeed<ee
ed<eeed<eeed<eeed<ee	ed<eeed<ee
ed<dZee
e
e
fed<de	fdd�Zde	dedee	fd d!�Zd"ede
fd#d$�Zed%ed"eddfd&d'��Zed(ejddfd)d*��Zd+S),r�a�	SFTP file attributes

       SFTPAttrs is a simple record class with the following fields:

         ============ ================================================= ======
         Field        Description                                       Type
         ============ ================================================= ======
         type         File type (SFTPv4+)                               byte
         size         File size in bytes                                uint64
         alloc_size   Allocation file size in bytes (SFTPv6+)           uint64
         uid          User id of file owner                             uint32
         gid          Group id of file owner                            uint32
         owner        User name of file owner (SFTPv4+)                 string
         group        Group name of file owner (SFTPv4+)                string
         permissions  Bit mask of POSIX file permissions                uint32
         atime        Last access time, UNIX epoch seconds              uint64
         atime_ns     Last access time, nanoseconds (SFTPv4+)           uint32
         crtime       Creation time, UNIX epoch seconds (SFTPv4+)       uint64
         crtime_ns    Creation time, nanoseconds (SFTPv4+)              uint32
         mtime        Last modify time, UNIX epoch seconds              uint64
         mtime_ns     Last modify time, nanoseconds (SFTPv4+)           uint32
         ctime        Last change time, UNIX epoch seconds (SFTPv6+)    uint64
         ctime_ns     Last change time, nanoseconds (SFTPv6+)           uint32
         acl          Access control list for file (SFTPv4+)            bytes
         attrib_bits  Attribute bits set for file (SFTPv5+)             uint32
         attrib_valid Valid attribute bits for file (SFTPv5+)           uint32
         text_hint    Text/binary hint for file (SFTPv6+)               byte
         mime_type    MIME type for file (SFTPv6+)                      string
         nlink        Link count for file (SFTPv6+)                     uint32
         untrans_name Untranslated name for file (SFTPv6+)              bytes
         ============ ================================================= ======

       Extended attributes can also be added via a field named
       `extended` which is a list of bytes name/value pairs.

       When setting attributes using an :class:`SFTPAttrs`, only fields
       which have been initialized will be changed on the selected file.

    �typer��
alloc_sizernrvr�rqr�r^r_�crtime�	crtime_nsr`ra�ctime�ctime_ns�acl�attrib_bits�attrib_valid�	text_hint�	mime_type�nlink�untrans_namer��extendedr�cCsNt�t||��}t||d�}|r%|r%|dd�d|d��|dd�}|S)z;Convert epoch seconds & nanoseconds to a string date & time�_nsN��.�09d)rcrS�getattr)r�r�r�rQr�r�r��
_format_ns�s
$zSFTPAttrs._format_nsr�r�cCs�|dus
|dkr|sdS|dkr!|tkrt�tt|�t|��SdS|dkr,tt|�d�S|dvr5|�|�S|dvr;dSt|�p@dS)z*Convert attributes to more readable valuesNr\rOr��04o)r^rQr`rS)r_rRrarT)r��_file_typesr�rr�r%rb�r�r�r�r�r�r��_format�s��
zSFTPAttrs._formatr�cCsdd}g}|dkr|dkr|jtkrt}n|j}|�t|��|jdur/|tO}|�t|j��|jdur@|t	O}|�t|j��|dkro|j
dur`|jdur`|tO}|�t
|j
�t
|j��nJ|jdurn|jdurntd��n;|jdur�|jdur�|tO}|�t|j�t|j��n|j
dur�|jdur�|tO}|�tt|j
��tt|j���|jdur�|tO}|�t
|j��|dkr�|jdur�|jdur�|tO}|�t
t|j��t
t|j���n�|jdup�|jdup�|jdup�|jdu}|r�|tO}|jdu�r|tO}|�tt|j���|�r|�t
|j�pd��|j du�r=|t!O}|�tt|j ���|�r=|�t
|j�p:d��|jdu�r_|t"O}|�tt|j���|�r_|�t
|j�p\d��|dk�r�|j#du�r�|t$O}|�tt|j#���|�r�|�t
|j�p�d��|dk�r�|j%du�r�|t&O}|�t|j%��|dk�r�|j'du�r�|j(du�r�|t)O}|�t
|j'�t
|j(��|dk�r|j*du�r�|t+O}|�t|j*��|j,du�r�|t-O}|�t|j,��|j.du�r�|t/O}|�t
|j.��|j0du�r|t1O}|�t|j0��|j2�r)|t3O}|�t
t4|j2���|�5dd	�|j2D��t
|�d
�6|�S)z0Encode SFTP attributes as bytes in an SSH packetrr�r�Nr�z0Setting owner and group requires SFTPv4 or laterr�cs�$�|]
\}}t|�t|�VqdSr r=)r�rOr�r�r�r�r?D���z#SFTPAttrs.encode.<locals>.<genexpr>r;)7rOr�r�r,r�r�rVr�rPrernrvrWr�r�rqrlr`r�r%r�rXr^r`rYr�r_rRrarTrar\rQr]r^rSrjrUr_rVrWrcrXrfrYrgrZrhr[rir\rZr��extendrA)r�r�r�rrO�	subsecondr�r�r�r��s�

�"
"�
���


�zSFTPAttrs.encoder(c
	Cs|��}|�}|dkr|ttB@r|tM}|t|@}|r'td|d����|dkr0|��|_|t@r9|��|_	|t
@rB|��|_|dkrU|t@rT|��|_
|��|_nB|t@r�|��}z|�d�|_Wntywtd|�dd��d�w|��}z|�d�|_Wnty�td	|�dd��d�w|t@r�|��}|dkr�t|�|_|d
@|_n|d@|_|dkr�|t@r�|��|_|��|_nJ|t@r�|��|_|t@r�|��|_|t@r�|��|_ |t@r�|��|_!|t@r�|��|_|t@r�|��|_"|t#@�r|��|_$|t@�r|��|_%|t&@�r|��|_'|t(@�r)|��|_)|��|_*|t+@�r3|��|_,|t-@�rPz
|���d�|_.Wn
t�yOtd�d�w|t/@�rZ|��|_0|t1@�rd|��|_2|t3@�r�|��}	g|_4t5|	�D]}
|��}|��}|j4�6||f��qt|S)
z0Decode bytes in an SSH packet as SFTP attributesr�zUnsupported attribute flags: 0x�08xr�r>zInvalid owner name: r?NzInvalid group name: i��i�zInvalid MIME type)7r&rYr^�_valid_attr_flagsr�get_byterOrVrAr�rerPrWrnrvr`r+r@r�rrmrqrurXrPr�r^r`r\rar_r]rQrRrarjrSrTr_rUrcrVrWrfrXrgrYrhrZrir[rZr\r6r,)
�clsr(r�r�r�unsupported_attrsr�rqrr�r;�attrr�r�r�r�r@Is��

�




�
���
���



�


















�




zSFTPAttrs.decoder�cCs�|j}t|�}tjdkrd}d}d}d}n|j}|j}t|�}t|�}t|j	�\}}	t|j
�\}
}t|j�\}}
tjdkrC||
}}nt|d�rPt
|j�\}}n|
|}}|||jd|||||||	|||
|||
ddddd|jd�S)z"Convert from local stat attributesr�rry�st_birthtimeN)�st_moderPr�r��st_uid�st_gidr|r�rTr�r��st_ctime_nsrbrVrq�st_size�st_nlink)rnr�rrOrnrvr�rqr^r_r`rarSrTrQrRr�r�r��
from_local�s0



�zSFTPAttrs.from_localN)r�r�r�r�r�rOr��__annotations__rr%r�r\rrrb�objectrfr��classmethodr�r@r�r�rxr�r�r�r�r�usB
(uzc@s�eZdZUdZdZeed<dZeed<dZeed<dZ	eed<dZ
eed<dZeed<dZeed	<dZ
eed
<dZeed<dZeed<dZeed
<dedefdd�Zedededdfdd��Zedejddfdd��ZdS)r�a�SFTP file system attributes

       SFTPVFSAttrs is a simple record class with the following fields:

         ============ =========================================== ======
         Field        Description                                 Type
         ============ =========================================== ======
         bsize        File system block size (I/O size)           uint64
         frsize       Fundamental block size (allocation size)    uint64
         blocks       Total data blocks (in frsize units)         uint64
         bfree        Free data blocks                            uint64
         bavail       Available data blocks (for non-root)        uint64
         files        Total file inodes                           uint64
         ffree        Free file inodes                            uint64
         favail       Available file inodes (for non-root)        uint64
         fsid         File system id                              uint64
         flags        File system flags (read-only, no-setuid)    uint64
         namemax      Maximum filename length                     uint64
         ============ =========================================== ======

    r�bsize�frsize�blocks�bfree�bavail�files�ffree�favail�fsidr��namemaxr�r�cCsbd�t|j�t|j�t|j�t|j�t|j�t|j�t|j�t|j	�t|j
�t|j�t|j�f�S)z8Encode SFTP statvfs attributes as bytes in an SSH packetr;)
rAr�r|r}r~rr�r�r�r�r�r�r�r�r�r�r�r�
s�zSFTPVFSAttrs.encoder(cCsx|�}|��|_|��|_|��|_|��|_|��|_|��|_|��|_|��|_|��|_	|��|_
|��|_|S)z8Decode bytes in an SSH packet as SFTP statvfs attributes)rAr|r}r~rr�r�r�r�r�r�r�)rnr(r��vfsattrsr�r�r�r@s










zSFTPVFSAttrs.decoder�cCs0||j|j|j|j|j|j|j|jd|j|j	�S)z%Convert from local statvfs attributesr)
�f_bsize�f_frsize�f_blocks�f_bfree�f_bavail�f_files�f_ffree�f_favail�f_flag�	f_namemax)rnr�r�r�r�rx,s
�zSFTPVFSAttrs.from_localN)r�r�r�r�r|r�ryr}r~rr�r�r�r�r�r�r�r�r�r{r�r@r��statvfs_resultrxr�r�r�r�r��s$
c@s~eZdZUdZdZeed<dZeed<e�Z	eed<de
dedee
fd	d
�Z
dedefdd
�Zedededdfdd��ZdS)r�a�SFTP file name and attributes

       SFTPName is a simple record class with the following fields:

         ========= ================================== ==================
         Field     Description                        Type
         ========= ================================== ==================
         filename  Filename                           `str` or `bytes`
         longname  Expanded form of filename & attrs  `str` or `bytes`
         attrs     File attributes                    :class:`SFTPAttrs`
         ========= ================================== ==================

       A list of these is returned by :meth:`readdir() <SFTPClient.readdir>`
       in :class:`SFTPClient` when retrieving the contents of a directory.

    ryr�longnamerr�r�r�cCs2|dkr|sdSt|t�r|�dd�}t|�pdS)z+Convert name fields to more readable valuesr�Nr>r?)rr�r@r%rer�r�r�rfLs

zSFTPName._formatr�cCs0|dkr	t|j�nd}t|j�||j�|�S)z-Encode an SFTP name as bytes in an SSH packetr�r;)r�r�rrr�)r�r�r�r�r�r�r�Ws
�zSFTPName.encoder(cCs4|��}|dkr|��nd}t�||�}||||�S)z-Decode bytes in an SSH packet as an SFTP namer�N)r+r�r@)rnr(r�rr�rr�r�r�r@_szSFTPName.decodeN)r�r�r�r�rr�ryr�r�rr%rzrrfr�r�r�r{r�r@r�r�r�r�r�6s
c@sleZdZUdZeed<eed<eed<eed<defdd�Zed	e	de
fd
d��Zded
e
ddfdd�ZdS)ra�SFTP server limits

       SFTPLimits is a simple record class with the following fields:

         ================= ========================================= ======
         Field             Description                               Type
         ================= ========================================= ======
         max_packet_len    Max allowed size of an SFTP packet        uint64
         max_read_len      Max allowed size of an SFTP read request  uint64
         max_write_len     Max allowed size of an SFTP write request uint64
         max_open_handles  Max allowed number of open file handles   uint64
         ================= ========================================= ======

    �max_packet_len�max_read_len�
max_write_len�max_open_handlesr�cCs(t|j�t|j�t|j�t|j�S)z*Encode SFTP server limits in an SSH packet)r�r�r�r�r�r�r�r�r�r�s
��zSFTPLimits.encoder(cCs.|��}|��}|��}|��}|||||�S)z3Decode bytes in an SSH packet as SFTP server limits�rA)rnr(r�r�r�r�r�r�r�r@�s�zSFTPLimits.decode�logger�labelNcCsH|�d|�|�d|j�|�d|j�|�d|j�|�d|j�dS)z$Log sending or receiving SFTP limitsz%s erver limits:z  Max packet len: %dz  Max read len: %dz  Max write len: %dz  Max open handles: %dN)�debug1r�r�r�r�)r�r�r�r�r�r��log�s
zSFTPLimits.log)r�r�r�r�r�ryr�r�r{r�rr@r�r%r�r�r�r�r�rjs
rc@sheZdZUdZeeeefed<eed<de	fdd�Z
edede
fdd	��Zd
ededdfd
d�ZdS)�
SFTPRangeszSFTP sparse file rangesr��at_endr�cCs.tt|j��d�dd�|jD��t|j�S)z*Encode sparse file ranges in an SSH packetr;csrgr )r�)r�r�r�r�r�r�r?�rhz$SFTPRanges.encode.<locals>.<genexpr>)r�r�r�rAr�r�r�r�r�r�r��s
���zSFTPRanges.encoder(cs0���}�fdd�t|�D�}���}|||�S)z3Decode bytes in an SSH packet as sparse file rangescsg|]
}������f�qSr�r��r�r;�r(r�r��
<listcomp>�s�z%SFTPRanges.decode.<locals>.<listcomp>)r&r6�get_boolean)rnr(r�r�r�r�r�r�r@�s
�
zSFTPRanges.decoder�r�NcCsH|�d|tt|j�d�|jrdnd�|jD]\}}|�d||�qdS)z+Log sending or receiving sparse file rangesz%s %s%szsparse file range�	 (at end)ryz  offset %d, length %dN)r�r�r�r�r�)r�r�r�r�r�r�r�r�r��s��zSFTPRanges.log)r�r�r�r�rrr�ryrr�r�r{r�rr@r�r%r�r�r�r�r�r��s

r�c
@s�eZdZdZddefdd�Zdedeeeffdd	�Z	d
d�Z
deefdd
�Z
deefdd�Zdedeededdfdd�Zdededededdf
dd�Zdedededdfdd�Zdefdededeefdd�ZdS)�SFTPGlobzSFTP glob matcherF�fscCs0||_||_t�|_g|_d|_i|_i|_dS)NF)�_fs�	_multipler��
_prev_matches�_new_matches�_matched�_stat_cache�_scandir_cache)r�r��multipler�r�r�r��s
zSFTPGlob.__init__�patternr�cs�g}t�fdd�dD��rMd}g}��d�D]+�t�fdd�dD��r<|r6|r-|�|�nd�|�p3d}g}|���q|���q|rI|�|�||fS�}||fS)z'Split out exact parts of a glob patternc3��|]}|�vVqdSr r��r��c)r�r�r�r?�r@z"SFTPGlob._split.<locals>.<genexpr>s*?[]r;r�c3r�r r�r�)�currentr�r�r?�r@)�any�splitr,rA)r�r��patlistr��plainr�)r�r�r��_split�s$
�zSFTPGlob._splitcCs@d|_|jr||jvr|j�|�ndS|j�t||d��dS)zReport a matching nameTN�r)r�r�r�r�r�r,r��r�r�rr�r�r��
_report_match�s
zSFTPGlob._report_matchc
�sd�z|j|WStyYnwz|j�|�IdH}Wntttfy*d}Ynw||j|<|S)zCache results of calls to statN)r�rkr�r�r+r-r3r�r�r�r��_stat�s���
zSFTPGlob._statc
Cs��z|j|D]}|VqWdStyYnwg}z|j�|�2z3dHW}|�|�|Vq"6Wntttfy?Ynw||j|<dS)z!Cache results of calls to scandirN)r�rkr�r�r,r+r-r3)r�r��entry�entriesr�r�r��_scandir	s$��
��zSFTPGlob._scandirr�r�Nc�sv�tj|g|�R�}|dd�}|�|�IdH}|durdS|r3|jtkr1|�|||�IdHdSdS|�||�dS)z#Match on an exact portion of a pathrN)�	posixpathrAr�rOr��_matchr�)r�r�r�r�r�
newpatlistrr�r�r��_match_exact	s�
�zSFTPGlob._match_exactrc	�s��|dd�}|dkr|r|�|||�IdHn|�||�|�|p#d�2zM3dHW}tt|j�}|dvr6q%|r=t||�rrt�||�}|j	}|dkrZ|j
tkrZ|�|||�IdHq%|rl|j
tkrk|�|||�IdHq%|�||�q%6dS)z$Match on a pattern portion of a pathrNs**�.�r�s..)r�r�r�rr�rrr�rArrOr�)	r�r�rr�r�r�r�rrr�r�r��_match_pattern.	s,�
���zSFTPGlob._match_patternc�sF�|d}t|t�r|�|||�IdHdS|�||||�IdHdS)z(Recursively match against a glob patternrN)r�listr�r�)r�r�rr�r�r�r�r�r�L	s
�
zSFTPGlob._match�
error_handlerc
�s��g|_d|_|�|�\}}z?|�|pd�IdH}|r5|r-|jtkr,|�|||�IdHn|r5|�||�|rF|jsJ|dkr@tnt	}|d��W|jSW|jSt
tfyq}zt|d|�|rd||�n�WYd}~|jSd}~ww)zMatch against a glob patternFr�Nr�zNo matches foundr�)
r�r�r�r�rOr�r�r�r3r+r�r�r)r�r�r�r�r�r�rr�r�r�r��matchW	s:�
�
���

���zSFTPGlob.match�F)r�r�r�r�r�r�r�r�_SFTPPatListr�r�rr�r�r
r�r�rr�r�r��MIN_SFTP_VERSION�SFTPErrorHandlerr�r�r�r�r�r��s>	
�
�
��
�
�
����r�c@s
eZdZdZeehZeed�Z	eedd�Z
ddgZee
eeeeeeee
eeeeeeeededed	ed
ei
Zd'dd�Zedefdd��Zdeeddfdd�Z de!e"e#e#ffdd�Z$de%de%de&ddfdd�Z'de%dee%d e#ddfd!d"�Z(de&fd#d$�Z)d(d%d&�Z*dS))�SFTPHandlerzSFTP session handler�FXP_�FXRP_r��OpenSSH�paramiko�statvfs@openssh.com�fstatvfs@openssh.com�limits@openssh.com�ranges@asyncssh.com�reader�SSHReader[bytes]�writer�SSHWriter[bytes]cCs.||_||_|j�d�|_tdttd�|_dS)N�sftpr)	�_reader�_writerr��	get_child�_loggerr�SAFE_SFTP_READ_LEN�SAFE_SFTP_WRITE_LENr)r�r�r�r�r�r�r��	szSFTPHandler.__init__r�cC�|jS)z*A logger associated with this SFTP handler)r�r�r�r�r�r��	�zSFTPHandler.loggerr�Nc�s&�|jr|j��d|_d|_dSdS)zClean up this SFTP sessionN)r�r�r�)r�r�r�r�r��_cleanup�	s�

�zSFTPHandler._cleanup�
extensionsc
Cs6|D�]\}}|dkrt|�}|j�d�|j�d|�q|dkrit|�\}}}}}	}
|j�d�|j�d|�|j�d|�|j�d|�|j�d	|�|j�d
|	�|
rh|j�d�|
D]	}|j�d|�q^q|d
kr�t|�\	}}}}}	}}
}}
|j�d�|j�d|�|j�d|�|j�d|�|j�d	|�|j�d
|	�|j�d|�|j�d|
�|r�|j�d�|D]	}|j�d|�q�|
r�|j�d�|
D]	}|j�d|�q�q|dk�r
t|�\}}}}|j�d�|j�d|�|j�d|�|j�d|�|j�d|�q|j�d||�rdnd|�qdS)z4Dump a formatted list of extensions to the debug log�
acl-supportedz  acl-supported:z    capabilities: 0x%08x�	supportedz  supported:z    attr_mask: 0x%08xz    attrib_mask: 0x%08xz    open_flags: 0x%08xz    access_mask: 0x%08xz    max_read_size: %dz    extensions:z      %s�
supported2z
  supported2:z    open_block_vector: 0x%04xz    block_vector: 0x%04xz    attrib_extensions:�	vendor-idz  vendor-id:z    vendor_name: %sz    product_name: %sz    product_version: %sz    product_build: %dz  %s%s%s�: ryN)r*r�r�r4r=rF)r�r�r3r�r)r-r.r/r0r1r2�ext_namer7r8r:�attrib_ext_namerBrCrDrEr�r�r��_log_extensions�	sp
��
���

�
��zSFTPHandler._log_extensions�pkttype�pktidr(c�r�)z+Abstract method for processing SFTP packetsr�)r�r�r�r(r�r�r��_process_packet�	s�zSFTPHandler._process_packetr�c
Gsv|jstd��t|�d�|�}z|j�tt|��|�Wnty1}ztt	|��d�d}~ww|�
|||�dS)zSend an SFTP packetzConnection not openr;N)r�r/r�rAr�r�r��ConnectionErrorr0r%�log_sent_packet)r�r�r�r��payloadr�r�r�r��send_packet�	s��zSFTPHandler.send_packetc�sH�|jdusJ�|j�d�IdH}t�|d�}|j�|�IdH}t|�S)zReceive an SFTP packetNr��big)r��readexactlyr�r�r�)r��pktlenr(r�r�r��recv_packet
s�zSFTPHandler.recv_packetc
�s��z,|jr+|��IdH}|��}|��}|�|||�|�|||�IdH|jsWdSWdStyL}z|�tt	|���IdHWYd}~dSd}~wt
y]|�d�IdHYdSttfyy}z|�|�IdHWYd}~dSd}~ww)z Receive and process SFTP packetsN)
r�r�rmr&�log_received_packetr�r�r�rr%�EOFErrorr�r�)r�r(r�r�r�r�r�r��recv_packets
s$��	&���zSFTPHandler.recv_packets)r�r�r�r�r)+r�r�r�r�r'r;�_data_pkttypesr�r��_handler_names�_realpath_check_names�_nonstandard_symlink_implsr$r:r&r(r=r)r,r-r<r1r2r4r?�
_return_typesr�r"r�r�r�	Exceptionr�rrr�r�r�r�r�r�r�r�r�r�r�r�r�y	sJ
�


@�
��
�r�cs&eZdZdZdejdddddef�fdd	�Zed
efdd��Z	ed
e
fd
d��Zdee
d
df�fdd�Zdededed
dfdd�Zdeeefdeeded
dfdd�Zdeeefded
efdd�Zded
dfdd�Zded
efd d!�Zded
eee
ffd"d#�Zded
efd$d%�Zded
efd&d'�Zded
efd(d)�Z e!ee"ee#ee$ee%ee&e iZ'd�d*d+�Z(d�d,d-�Z)d.ed/ed0ed
efd1d2�Z*d.ed3ed4ed0ed
ef
d5d6�Z+d7ed
dfd8d9�Z,d7ed:ed;ed
eee
ffd<d=�Z-d7ed:ed>ed
efd?d@�Z.dAdB�dCed4edDe
d
efdEdF�Z/dCed4ed
efdGdH�Z0d7ed4ed
efdIdJ�Z1dAdB�dCed0edDe
d
dfdKdL�Z2d7ed0ed
dfdMdN�Z3dCed
e4fdOdP�Z5d7ed
e4fdQdR�Z6dCed
dfdSdT�Z7dUedVed4ed
dfdWdX�Z8dUedVed
dfdYdZ�Z9dCed
efd[d\�Z:d7ed
efd]d^�Z;dCed0ed
dfd_d`�Z<dCed
dfdadb�Z=e>dc�dCeddedeed
efdfdg�Z?dCed
efdhdi�Z@dUedVed
dfdjdk�ZAdUedVed
dfdldm�ZBd7ed:ed;ed4ed
df
dndo�ZCd7ed:ed;ed
dfdpdq�ZDd7ed
dfdrds�ZEdteduedvedwedxed
dfdydz�ZFd7ed:ed;ed
eGfd{d|�ZHd�d}d~�ZId�dd��ZJ�ZKS)�r�zAn SFTP client session handler�loopr�r�r�r�r�csft��||�||_||_d|_i|_d|_d|_d|_d|_	d|_
d|_d|_d|_
d|_d|_dS)NrF)r�r��_loop�_version�_next_pktid�	_requests�_nonstandard_symlink�_supports_posix_rename�_supports_statvfs�_supports_fstatvfs�_supports_hardlink�_supports_fsync�_supports_lsetstat�_supports_limits�_supports_copy_data�_supports_ranges)r�rr�r�r�r�r�r�r�&
s
zSFTPClientHandler.__init__r�cCr��z.SFTP version associated with this SFTP session)rr�r�r�r�r#:
r�zSFTPClientHandler.versioncCr��z3Return whether or not SFTP remote copy is supported)r
r�r�r�r��supports_copy_data@
r�z$SFTPClientHandler.supports_copy_datar�Nc�sn�|ptd�}t|j���D]}|��s|�|�qi|_|j�d|r)dt|�nd�t	��
|�IdHdS)z!Clean up this SFTP client sessionzConnection closedzSFTP client exited%sr�ryN)r0r�r�values�	cancelled�
set_exceptionr��infor%r�r�)r�r��req_exc�waiterr�r�r�r�F
s�
�zSFTPClientHandler._cleanupr�r�r(c�sZ�z|j�|�}Wnty|�td��IdHYdSw|��s+|�||f�dSdS)zProcess incoming SFTP responseszInvalid response idN)r�poprkr�rr�
set_result)r�r�r�r(rr�r�r�r�U
s���z!SFTPClientHandler._process_packetr�rcCsb|j}|jdd@|_||j|<t|t�r t|�t|�}t}nt|�}|j|||g|�R�dS)zSend an SFTP requestr���N)rrrr�r�r�r>r�)r�r�r�rr��hdrr�r�r��
_send_requesta
s

zSFTPClientHandler._send_requestc�sz�|j��}|�|||�|IdH\}}|j�|�}|t|fvr'td|����|j|||�}|dus7|dur9|Std��)z,Make an SFTP request and wait for a responseNzUnexpected response type: zUnexpected FX_OK response)r�
create_futurerr�r�r9r�_packet_handlers)r�r�r�r�resptype�resp�return_typer�r�r�r��
_make_requestr
s�
zSFTPClientHandler._make_requestcCs4t�|�}|jdkr|��|r|�|j�d�dS)z(Process an incoming SFTP status responser�zReceived OKN)r�r"rr'r�r�)r�r(r�r�r�r��_process_status�
s

z!SFTPClientHandler._process_statuscCs0|��}|jdkr
|��|j�d|���|S)z(Process an incoming SFTP handle responser�zReceived handle %s)r+rr'r�r��hex)r�r(r�r�r�r��_process_handle�
s

z!SFTPClientHandler._process_handlecCs^|��}|r|jdkr|��nd}|jdkr|��|j�dtt|�d�|r(dnd�||fS)z&Process an incoming SFTP data responser�F�
Received %s%s�	data byter�ry)r+rr�r'r�r�r�r�)r�r(r�r�r�r�r��
_process_data�
s�

�zSFTPClientHandler._process_datacs����}��fdd�t|�D�}�r�jdkr���nd}�jdkr&����j�dt|d�|r2dnd�|D]	}�j�d	|�q7||fS)
z&Process an incoming SFTP name responsecsg|]	}t���j��qSr�)r�r@rr��r(r�r�r�r��
r�z3SFTPClientHandler._process_name.<locals>.<listcomp>r�Fr&r3r�ry�  %s)r&r6rr�r'r�r�r�)r�r(r��namesr�r3r�r)r��
_process_name�
s�

�zSFTPClientHandler._process_namecCs4t��||j�}|jdkr|��|j�d|�|S)z,Process an incoming SFTP attributes responser��Received %s)r�r@rr'r�r�)r�r(rr�r�r��_process_attrs�
s

z SFTPClientHandler._process_attrscCs|S)z0Process an incoming SFTP extended reply responser�r%r�r�r��_process_extended_reply�
sz)SFTPClientHandler._process_extended_replyc
�s��|jdusJ�|j�d|j�|�tdt|j��zG|��IdH}|��}|�	|d|�|t
kr5td��|��}t
|krCtksKntd|����g}|r`|��}|��}|�||f�|sOWnAtyt}ztt|��d�d}~wty{�ty�}ztt|��d�d}~wtjtfy�}ztt|��d�d}~ww|j�d||r�dnd�|�|�||_|D]w\}}|dkr�|d	kr�d
|_q�|dkr�|dkr�d
|_q�|d
kr�|dkr�d
|_q�|dkr�|d	kr�d
|_q�|dkr�|d	kr�d
|_q�|dk�r|d	k�rd
|_q�|dk�r|d	k�rd
|_ q�|dk�r#|d	k�r#d
|_!q�|dk�r0|d	k�r0d
|_"q�|dk�rXt#t|j�$dd���t%�fdd�|j&D���rZ|j�d�d
|_'dSdSdS)�Start an SFTP clientNzSending init, version=%dzExpected version messagezUnsupported version: zReceived version=%d%s�
, extensions:ry�posix-rename@openssh.com�1Tr��2r��hardlink@openssh.com�fsync@openssh.com�lsetstat@openssh.comr��	copy-datar�r��server_versionc3r�r r�r>�r9r�r�r?&���z*SFTPClientHandler.start.<locals>.<genexpr>�1Adjusting for non-standard symlink implementation)(r�r�r�rr�r"r�r�rmr�r#rr&r��MAX_SFTP_VERSIONr+r,r�r%r�r�r0r��IncompleteReadErrorr�r�rrrr	r
rrr
rr�get_extra_infor�r�r)r�r rr#�rcvd_extensionsr3r�r�r�r:r�r��
s��������

�
�
�
�
�zSFTPClientHandler.startc�sj�|jr1tt|�d�IdH�}t�|�}|��|�|jd�|j	r'|j	|j
_	|jr3|j|j
_dSdSdS)zRequest SFTP server limitsr�N�Received)rrr�r"rr@r'r�r�r�rr�)r�r(rr�r�r��request_limits,s��

�z SFTPClientHandler.request_limitsrr�rc�s��|jdkr1t|�\}}|j�d|||t|��tt|�tt	|�t
|�t
|�|�|j��IdH�S|j�d||t|��tt|�tt	|�t
|�|�|j��IdH�S)zMake an SFTP open requestr��:Sending open for %s, desired_access=0x%08x, flags=0x%08x%sNz"Sending open for %s, mode 0x%02x%s)rr�r�r�r�rr�r"r$r�r�r�)r�rr�rr�r�r�r�r�r>s$�
��
�
�zSFTPClientHandler.openr�r�c�s^�|j�d|||t|��|jdkr+tt|�tt|�t	|�t	|�|�
|j��IdH�Std��)zMake an SFTPv5/v6 open requestrCr�Nz&SFTPv5/v6 open not supported by server)r�r�r�rrr�r"r$r�r�r�r1)r�rr�r�rr�r�r��open56Ts��
�zSFTPClientHandler.open56r�c�s8�|j�d|���|jr|�tt|��IdHdSdS)zMake an SFTP close requestzSending close for handle %sN)r�r�r$r�r"r%r��r�r�r�r�r�r�cs
��zSFTPClientHandler.closer�r�c	�sN�|j�dt|d�||���ttttf|�t	t
|�t|�t|��IdH�S)zMake an SFTP read requestz-Sending read for %s at offset %d in handle %s�byteN)
r�r�r�r$rrr�rr"r&r�r�r��r�r�r�r�r�r�r�r�ks���zSFTPClientHandler.readr�c	�sJ�|j�dtt|�d�||���tt|�tt	|�t
|�t	|��IdH�S)zMake an SFTP write requestz.Sending write for %s at offset %d in handle %srFN)r�r�r�r�r$rr�r"r'r�r�)r�r�r�r�r�r�r�r�us���zSFTPClientHandler.writeTrr�r
c�s��|jdkrt|�}d|d��}nd}d}|r.|j�d||�tt|�tt|�|�IdH�S|j�d||�tt|�t	t|�|�IdH�S)	z"Make an SFTP stat or lstat requestr��
, flags 0xrkr;ryzSending stat for %s%sN�Sending lstat for %s%s)
rr�r�r�rr�r"r2r�r()r�r�r�r
�
flag_bytes�	flag_textr�r�r�r�~s�

�
�zSFTPClientHandler.statc�sX�|jdkrt|�}d|d��}nd}d}|j�d||�tt|�tt|�|�IdH�S)zMake an SFTP lstat requestr�rHrkr;ryrIN)	rr�r�r�rr�r"r(r�)r�r�r�rJrKr�r�r��lstat�s�

�zSFTPClientHandler.lstatc�s\�|jdkrt|�}d|d��}nd}d}|j�d|��|�tt|�tt	|�|�IdH�S)zMake an SFTP fstat requestr�rHrkr;ryzSending fstat for handle %s%sN)
rr�r�r�r$rr�r"r)r�)r�r�r�rJrKr�r�r��fstat�s�
�
�zSFTPClientHandler.fstatc�s��|r|j�d|t|��|�tt|�|�|j��IdHdS|jr>|j�d|t|��|�dt|�|�|j��IdHdSt	d��)z(Make an SFTP setstat or lsetstat requestzSending setstat for %s%sNzSending lsetstat for %s%sr7z lsetstat not supported by server)
r�r�r�r"r*r�r�rrr1rr�r�r�r�s ��
��
�zSFTPClientHandler.setstatc�s>�|j�d|��t|��|�tt|�|�|j��IdHdS)zMake an SFTP fsetstat requestz Sending fsetstat for handle %s%sN)	r�r�r$r�r"r+r�r�r)r�r�rr�r�r��fsetstat�s��
�zSFTPClientHandler.fsetstatc�sb�|jr-|j�d|�tt|�dt|��IdH�}t�||j	�}|�
�|j�d|�|Std��)zMake an SFTP statvfs requestzSending statvfs for %sr�Nr-�statvfs not supported)rr�r�rr�r"r�r�r@rr'r1)r�r�r(r�r�r�r��statvfs�s��zSFTPClientHandler.statvfsc�sf�|jr/|j�d|���tt|�dt|��IdH�}t�	||j
�}|��|j�d|�|Std��)zMake an SFTP fstatvfs requestzSending fstatvfs for handle %sr�Nr-�fstatvfs not supported)
rr�r�r$rr�r"r�r�r@rr'r1)r�r�r(r�r�r�r��fstatvfs�s��zSFTPClientHandler.fstatvfsc��*�|j�d|�|�tt|��IdHdS)zMake an SFTP remove requestzSending remove for %sN)r�r�r"r.r�r�r�r�r��remove���zSFTPClientHandler.removerrc�s��|jdkr*|j�d|||rd|d��nd�|�tt|�t|�t|��IdHdS|rG|jrG|j�d||�|�dt|�t|��IdHdS|sa|j�d	||�|�tt|�t|��IdHdStd
��)zMake an SFTP rename requestr�z&Sending rename request from %s to %s%s�
, flags=0xr�ryN�2Sending OpenSSH POSIX rename request from %s to %sr2z$Sending rename request from %s to %sz#Rename with overwrite not supported)	rr�r�r"r3r�r�rr1�r�rrr�r�r�r��rename�s4�
���
����zSFTPClientHandler.renamec�s��|jr|j�d||�|�dt|�t|��IdHdS|jdkr<|j�d||�|�tt|�t|�tt��IdHdSt	d��)z!Make an SFTP POSIX rename requestrWr2Nr�z3Sending rename request from %s to %s with overwritezPOSIX rename not supported)
rr�r�r"r�rr3r�r@r1rr�r�r��posix_renames ���
��zSFTPClientHandler.posix_renamec��,�|j�d|�tt|�tt|��IdH�S)zMake an SFTP opendir requestzSending opendir for %sN)r�r�rr�r"r,r�r�r�r�r��opendir%�
��zSFTPClientHandler.opendirc�s0�|j�d|���tt|�tt|��IdH�S)zMake an SFTP readdir requestzSending readdir for handle %sN)r�r�r$r�
_SFTPNamesr"r-r�rEr�r�r��readdir-s
��zSFTPClientHandler.readdirc�s4�|j�d|�|�tt|�|�|j��IdHdS)zMake an SFTP mkdir requestzSending mkdir for %sN)r�r�r"r/r�r�rr�r�r�r�r5s
�
�zSFTPClientHandler.mkdirc�rS)zMake an SFTP rmdir requestzSending rmdir for %sN)r�r�r"r0r�r�r�r�r��rmdir=rUzSFTPClientHandler.rmdir��check�
compose_pathsrbc�s��|tkrd}nz
d|j|��}Wnty d|��}Ynw|j�d||r/dd�|�nd|�|jdkrPtt|j	t
t|�t|�gt
t|��R�IdH�Stt|�	t
t|��IdH�S)	zMake an SFTP realpath requestry�, check=zSending realpath of %s%s%s�, compose_path: �, r;r�N)rAr�rkr�r�rArrr^r"r1r�r��map)r�r�rbrc�checkmsgr�r�r��realpathDs,��
��
���zSFTPClientHandler.realpathc�r[)zMake an SFTP readlink requestzSending readlink for %sN)r�r�rr^r"r4r�r�r�r�r�r\r]zSFTPClientHandler.readlinkc�s��|j�d||�|jdkr!|�tt|�t|�td��IdHdS|jr-t|�t|�}nt|�t|�}|�t|�IdHdS)zMake an SFTP symlink requestz%Sending symlink request from %s to %sr�TN)	r�r�rr"r6r�r�rr5)r�rrr�r�r�r�rds��
�zSFTPClientHandler.symlinkc�sz�|jdks	|jr9|j�d||�|jdkr)|�tt|�t|�td��IdHdS|�dt|�t|��IdHdStd��)zMake an SFTP hard link requestr�z&Sending hardlink request from %s to %sFNr5zlink not supported)	rr	r�r�r"r6r�r�r1rr�r�r��linkus��
��zSFTPClientHandler.linkc�sX�|jdkr(|j�d|��|||�|�tt|�t|�t|�t|��IdHdSt	d��)z$Make an SFTP byte range lock requestr�zQSending byte range lock request for handle %s, offset %d, length %d, flags 0x%04xN�Byte range locks not supported)
rr�r�r$r"r7r�r�r�r1)r�r�r�r�r�r�r�r��lock�s�
��zSFTPClientHandler.lockc�sP�|jdkr$|j�d|��||�|�tt|�t|�t|��IdHdStd��)z&Make an SFTP byte range unlock requestr�zESending byte range unlock request for handle %s, offset %d, length %dNrk)	rr�r�r$r"r8r�r�r1rGr�r�r��unlock�s�

��zSFTPClientHandler.unlockc�s<�|jr|j�d|���|�dt|��IdHdStd��)zMake an SFTP fsync requestzSending fsync for handle %sr6Nzfsync not supported)r
r�r�r$r"r�r1rEr�r�r��fsync�s
�zSFTPClientHandler.fsync�read_from_handle�read_from_offset�read_from_length�write_to_handle�write_to_offsetc	�s`�|jr,|j�d|��|||��|�|�dt|�t|�t|�t|�t|��IdHdStd��)zMake an SFTP copy data requestzNSending copy-data from handle %s, offset %d, length %d to handle %s, offset %dr8Nzcopy-data not supported)r
r�r�r$r"r�r�r1)r�rorprqrrrsr�r�r��	copy_data�s���zSFTPClientHandler.copy_datac	�sz�|jr5|j�d|��||�tt|�dt|�t|�t|��IdH�}t	�
|�}|��|�|jd�|St	||fgd�S)z4Request file ranges containing data in a remote filez:Sending ranges request for handle %s, offset %d, length %dr�NrAT)
rr�r�r$rr�r"r�r�r�r@r'r�)r�r�r�r�r(r�r�r�r�r��s���
z SFTPClientHandler.request_rangescCs|jr
|j��dSdS)z*Handle a request to close the SFTP sessionN)r��	write_eofr�r�r�r��exit�s�zSFTPClientHandler.exitc�s"�|jr|jj��IdHdSdS)z#Wait for this SFTP session to closeN)r��channel�wait_closedr�r�r�r�rx�s��zSFTPClientHandler.wait_closedr)Lr�r�r�r�r��AbstractEventLoopr�r�r"r#rrrr�r�r�r�rr�r�_RequestWaiterrrzr"r#r%rr(r^r,r�r.r/r9r:r;r<r=r?rr�rBrrDr�r�r�r�rLrMrrNr�rPrRrTrYrZr\r_rr`rArirrrjrlrmrnrtr�r�rvrxr�r�r�r�r�r�#
s����
�
��
��
�
	�
	
N
�
�
��
�
�

�

��
���
�	��
��
�

���
�
�
�
r�c@sZeZdZdZdedededeedede	de	fd	d
�Z
defdd
�Zdee
edeedeedefdd�Zedefdd��Zde	fdd�Zde	de	deee	e	ffdd�Z		dKde	dee	defdd�Z		dKde	dee	deee	effd d!�ZdLd"edee	de	fd#d$�Zefde	d%e	de	fd&d'�Zde	fd(d)�Zefdefd*d+�Z d,eddfd-d.�Z!de"fd/d0�Z#dLdee	ddfd1d2�Z$e%d3e	d4e	ddfd5d6��Z&e%d7ed8eddfd9d6��Z&		dMd:d6�Z&d;e	ddfd<d=�Z'		dNd>eee(e(fd?eee	e	fddfd@dA�Z)de	de	dBe	ddfdCdD�Z*de	de	ddfdEdF�Z+dOdGdH�Z,dOdIdJ�Z-dS)Pr�a5SFTP client remote file object

       This class represents an open file on a remote SFTP server. It
       is opened with the :meth:`open() <SFTPClient.open>` method on the
       :class:`SFTPClient` class and provides methods to read and write
       data and get and set attributes on the open file.

    r�r��	appending�encoding�errorsrr�cCs�||_||_||_||_||_|rdnd|_|dkr|jjn||_|dkr)|jj	n||_
|dkrA|jr?tdtt
|jd��}nd}||_dS)Nrrr�r�r)r�r��
_appending�	_encoding�_errorsr�rr��read_lenr��	write_len�maxr��MAX_SFTP_READ_LENr�)r�r�r�r{r|r}rr�r�r�r�r��s&����
zSFTPClientFile.__init__r�c���|S)z;Allow SFTPClientFile to be used as an async context managerr�r�r�r�r�r�
r�zSFTPClientFile.__aenter__r�r�r�c���|��IdHdS�r�NF�r�r�r�r�r�r�

s�zSFTPClientFile.__aexit__cCs|jdur	td��|jS)z)Return handle or raise an error if clsoedN�I/O operation on closed file)r�rlr�r�r�r�r�
s
zSFTPClientFile.handlec�s�|��IdH}|jpdS)z(Return the offset of the end of the fileNr�r�r��r�rr�r�r��_end
s�
zSFTPClientFile._endr�r�c
Cs��|}|}||}d}z5|s>|j�|j||�IdH}|jr3|jD]	\}}	||	fVq ||	}||}nWdS|j}|rWdSWdStyJYdSw)z3Return file ranges containing data in a remote fileFN)r�r�r�r�r�r�)
r�r�r��next_offset�next_lengthr�r�r��range_offset�range_lengthr�r�r�r�!
s*�
�
��zSFTPClientFile.request_rangesrNr�c�s��|jdur
td��|dur|j}d}|durj|dus|dkr(|��IdH|}z8|jrJ|t|j|jjj�krJt	|j|j
|j|j||���IdH}n|j�|j||�IdH\}}|t
|�|_Wn	tyiYnw|jrxtt|�|j|j��Stt|�S)a-Read data from the remote file

           This method reads and returns up to `size` bytes of data
           from the remote file. If size is negative, all data up to
           the end of the file is returned.

           If offset is specified, the read will be performed starting
           at that offset rather than the current file position. This
           argument should be provided if you want to issue parallel
           reads on the same file, since the file position is not
           predictable in that case.

           Data will be returned as a string if an encoding was set when
           the file was opened. Otherwise, data is returned as bytes.

           An empty `str` or `bytes` object is returned when at EOF.

           :param size:
               The number of bytes to read
           :param offset: (optional)
               The offset from the beginning of the file to begin reading
           :type size: `int`
           :type offset: `int`

           :returns: data read from the file, as a `str` or `bytes`

           :raises: | :exc:`ValueError` if the file has been closed
                    | :exc:`UnicodeDecodeError` if the data can't be
                      decoded using the requested encoding
                    | :exc:`SFTPError` if the server returns an error

        Nr�r;r)r�rlr�r�r�r�r�rr�r�r�r�r�r�r�rrr	r@r�)r�r�r�r�r;r�r�r�r�>
s:�
#��
�
��
zSFTPClientFile.readc�st�|jdur
td��|dur|j}|dur'|dus|dkr&|��IdH|}nd}d}t|j|j|j|j||���S)a�Read parallel blocks of data from the remote file

           This method reads and returns up to `size` bytes of data
           from the remote file. If size is negative, all data up to
           the end of the file is returned.

           If offset is specified, the read will be performed starting
           at that offset rather than the current file position.

           Data is returned as a series of tuples delivered by an
           async iterator, where each tuple contains an offset and
           data bytes. Encoding is ignored here, since multi-byte
           characters may be split across block boundaries.

           To maximize performance, multiple reads are issued in
           parallel, and data blocks may be returned out of order.
           The size of the blocks and the maximum number of
           outstanding read requests can be controlled using
           the `block_size` and `max_requests` arguments passed
           in the call to the :meth:`open() <SFTPClient.open>`
           method on the :class:`SFTPClient` class.

           :param size:
               The number of bytes to read
           :param offset: (optional)
               The offset from the beginning of the file to begin reading
           :type size: `int`
           :type offset: `int`

           :returns: an async iterator of tuples of offset and data bytes

           :raises: | :exc:`ValueError` if the file has been closed
                    | :exc:`SFTPError` if the server returns an error

        Nr�r)	r�rlr�r�r�r�r�r�r�r�r�r�r��
read_parallel�
s"�
'�

��zSFTPClientFile.read_parallelr�c�s��|jdur
td��|dur|jpd}|jr"tt|��|j|j�}ntt|�}t	|�}|j
rF||j
krFt|j
|j|j
|j||���IdHn|j
�|j||�IdH|jrZd|_|S|||_|S)aWrite data to the remote file

           This method writes the specified data at the current
           position in the remote file.

           :param data:
               The data to write to the file
           :param offset: (optional)
               The offset from the beginning of the file to begin writing
           :type data: `str` or `bytes`
           :type offset: `int`

           If offset is specified, the write will be performed starting
           at that offset rather than the current file position. This
           argument should be provided if you want to issue parallel
           writes on the same file, since the file position is not
           predictable in that case.

           :returns: number of bytes written

           :raises: | :exc:`ValueError` if the file has been closed
                    | :exc:`UnicodeEncodeError` if the data can't be
                      encoded using the requested encoding
                    | :exc:`SFTPError` if the server returns an error

        Nr�r)r�rlr�rrr%r�r�r�r�r�r�r�r�r�r�r~)r�r�r��
data_bytesrr�r�r�r��
s*�


�
�
�zSFTPClientFile.write�	from_whatc�s��|jdur
td��|tkr||_|jS|tkr4|jdur*|��IdH||_|jS|j|7_|jS|tkrE|��IdH||_|jStd��)a�Seek to a new position in the remote file

           This method changes the position in the remote file. The
           `offset` passed in is treated as relative to the beginning
           of the file if `from_what` is set to `SEEK_SET` (the
           default), relative to the current file position if it is
           set to `SEEK_CUR`, or relative to the end of the file
           if it is set to `SEEK_END`.

           :param offset:
               The amount to seek
           :param from_what: (optional)
               The reference point to use
           :type offset: `int`
           :type from_what: `SEEK_SET`, `SEEK_CUR`, or `SEEK_END`

           :returns: The new byte offset from the beginning of the file

        Nr�zInvalid reference point)r�rlrr�rr�r)r�r�r�r�r�r�r��
s �
�
���zSFTPClientFile.seekc�s4�|jdur
td��|jdur|��IdH|_|jS)z�Return the current position in the remote file

           This method returns the current position in the remote file.

           :returns: The current byte offset from the beginning of the file

        Nr�)r�rlr�r�r�r�r�r��tells�
	
zSFTPClientFile.tellc�s*�|jdur
td��|j�|j|�IdHS)a�Return file attributes of the remote file

           This method queries file attributes of the currently open file.

           :param flags: (optional)
               Flags indicating attributes of interest (SFTPv4 or later)
           :type flags: `int`

           :returns: An :class:`SFTPAttrs` containing the file attributes

           :raises: :exc:`SFTPError` if the server returns an error

        Nr�)r�rlr�rM)r�r�r�r�r�r�,s�
zSFTPClientFile.statrc�s.�|jdur
td��|j�|j|�IdHdS)a0Set attributes of the remote file

           This method sets file attributes of the currently open file.

           :param attrs:
               File attributes to set on the file
           :type attrs: :class:`SFTPAttrs`

           :raises: :exc:`SFTPError` if the server returns an error

        Nr�)r�rlr�rNr�r�r�r�r@s�

zSFTPClientFile.setstatc�s(�|jdur
td��|j�|j�IdHS)a�Return file system attributes of the remote file

           This method queries attributes of the file system containing
           the currently open file.

           :returns: An :class:`SFTPVFSAttrs` containing the file system
                     attributes

           :raises: :exc:`SFTPError` if the server doesn't support this
                    extension or returns an error

        Nr�)r�rlr�rRr�r�r�r�rPRs�
zSFTPClientFile.statvfsc�s*�|dur|j}|�t|d��IdHdS)a�Truncate the remote file to the specified size

           This method changes the remote file's size to the specified
           value. If a size is not provided, the current file position
           is used.

           :param size: (optional)
               The desired size of the file, in bytes
           :type size: `int`

           :raises: :exc:`SFTPError` if the server returns an error

        N�r�)r�rr�)r�r�r�r�r�r�es�zSFTPClientFile.truncaternrvc���dSr r�)r�rnrvr�r�r�r�y��zSFTPClientFile.chownr�rqc�r�r r�)r�r�rqr�r�r�r�|��c�s^�t|t�r	|}nt|t�r|}t|t�r|}nt|t�r|}|�t||||d��IdHdS)a�Change the owner user and group of the remote file

           This method changes the user and group of the currently open file.

           :param uid:
               The new user id to assign to the file
           :param gid:
               The new group id to assign to the file
           :param owner:
               The new owner to assign to the file (SFTPv4 only)
           :param group:
               The new group to assign to the file (SFTPv4 only)
           :type uid: `int`
           :type gid: `int`
           :type owner: `str`
           :type group: `str`

           :raises: :exc:`SFTPError` if the server returns an error

        �rnrvr�rqN�rr�r%rr�)r��uid_or_owner�gid_or_grouprnrvr�rqr�r�r�r��s�




�rc�s�|�t|d��IdHdS)aGChange the file permissions of the remote file

           This method changes the permissions of the currently
           open file.

           :param mode:
               The new file permissions, expressed as an int
           :type mode: `int`

           :raises: :exc:`SFTPError` if the server returns an error

        �r�N�rr�)r�rr�r�r�r��s�zSFTPClientFile.chmodr[r\c�s�|�t||��IdHdS)a�Change the access and modify times of the remote file

           This method changes the access and modify times of the
           currently open file. If `times` is not provided,
           the times will be changed to the current time.

           :param times: (optional)
               The new access and modify times, as seconds relative to
               the UNIX epoch
           :param ns: (optional)
               The new access and modify times, as nanoseconds relative to
               the UNIX epoch
           :type times: tuple of two `int` or `float` values
           :type ns: tuple of two `int` values

           :raises: :exc:`SFTPError` if the server returns an error

        N�rrd)r�r[r\r�r�r�r��s�zSFTPClientFile.utimer�c�s2�|jdur
td��|j�|j|||�IdHdS)z,Acquire a byte range lock on the remote fileNr�)r�rlr�rl)r�r�r�r�r�r�r�rl�s�
zSFTPClientFile.lockc�s0�|jdur
td��|j�|j||�IdHdS)z,Release a byte range lock on the remote fileNr�)r�rlr�rmr�r�r�r�rm�s�
zSFTPClientFile.unlockc�s,�|jdur
td��|j�|j�IdHdS)z0Force the remote file data to be written to diskNr�)r�rlr�rnr�r�r�r�rn�s�
zSFTPClientFile.fsyncc�s*�|jr|j�|j�IdHd|_dSdS)zClose the remote fileN)r�r�r�r�r�r�r�r��s
�
�zSFTPClientFile.close)rNr �NNNNNN�NNr).r�r�r�r�r�r�rrr%r�r�rr�rrrr�r"r�r�r
rr�r	r�r�r�rr�r�rbr�r�rr�rPr�rr�r�rWr�rlrmrnr�r�r�r�r�r��s�
	����
���
�

���
�F��
�<5&��
�$��
�
c!@sd	eZdZdZdedeedefdd�Zdefdd	�Z	d
ee
edeedeede
fd
d�Zedefdd��Zedefdd��Zedefdd��Zede
fdd��Zededefdd��Zdedefdd�Zd�dede
defdd�Z	 d�ded!eedefd"d#�Z	 d�ded$eedefd%d&�Z d'e!d(e!d)ed*ed+e"d,e
d-e
d.e
d/e
d0ed1ed2e#d3e$d4e
dd fd5d6�Z%	7d�d'e!d(e!d8e&d*eed9ed:e
d,e
d-e
d.e
d/e
d0ed1ed2e#d3e$d4e
dd f d;d<�Z'	 d�d7d7d7dd=d=d d d>�d?e&d@eed,e
d-e
d.e
d/e
d0ed1ed2e#d3e$dd fdAdB�Z(	 d�d7d7d7dd=d=d d d>�dCe&dDeed,e
d-e
d.e
d/e
d0ed1ed2e#d3e$dd fdEdF�Z)	 d�d7d7d7dd=d=d d d7dG�	d8e&d*eed,e
d-e
d.e
d/e
d0ed1ed2e#d3e$d4e
dd fdHdI�Z*	 d�d7d7d7dd=d=d d d>�d?e&d@eed,e
d-e
d.e
d/e
d0ed1ed2e#d3e$dd fdJdK�Z+	 d�d7d7d7dd=d=d d d>�dCe&dDeed,e
d-e
d.e
d/e
d0ed1ed2e#d3e$dd fdLdM�Z,	 d�d7d7d7dd=d=d d d7dG�	d8e&d*eed,e
d-e
d.e
d/e
d0ed1ed2e#d3e$d4e
dd fdNdO�Z-	P	Pd�dQe.dRe.dSedTedUedd fdVdW�Z/	 d�dXe&d3e$de0efdYdZ�Z1	 d�dXe&d3e$de0e2fd[d\�Z3e"�d7fded]e"d^e
dd fd_d`�Z4	7	 d�dedae
dbe5dd fdcdd�Z6e7e8e"�dedfd=d=fdedge9eefd]e"dheedied0ed1ede:fdjdk��Z;e7e<e=Be>e"�dedfd=d=fdedledmed]e"dheedied0ed1ede:fdndo��Z?e@fddp�ded.e
de"fdqdr�ZAe@fdede"fdsdt�ZBddp�ded]e"d.e
dd fdudv�ZCdedeDfdwdx�ZEdedyedd fdzd{�ZFeGddp�ded|ed}ed.e
dd f
d~d��ZHeGddp�ded�ed�ed.e
dd f
d�d��ZH	 	 d�ddp�d�d�ZHddp�ded�ed.e
dd fd�d��ZI	 	 d�ddp�ded�eeJeKeKfd�eeJeefd.e
dd f
d�d��ZLdede
fd�d��ZMdede
fd�d��ZNdedeeKfd�d��ZOdedeefd�d��ZPdedeeKfd�d��ZQdedeefd�d��ZRdedeeKfd�d��ZSdedeefd�d��ZTdedeefd�d��ZUdede
fd�d��ZVdede
fd�d��ZWdede
fd�d��ZXdedd fd�d��ZYdedd fd�d��ZZ	Pd�d�ed�edmedd fd�d��Z[d�ed�edd fd�d��Z\d�dede]e2fd�d��Z^d�dede0e2fd�d��Z_eGdede0efd�d���Z`eGd�deade0efd�d���Z`d�dede0efd�d��Z`e"�fded]e"dd fd�d��Zbdedd fd�d��ZceGded�edefd�d���ZdeGdead�eadefd�d���ZdeGded�ed�ede2fd�d���ZdeGdead�ead�ede2fd�d���Zdeedœded�ed�ede9ee2ffd�d��Zddefd�dńZfdedd fd�dDŽZgeGdedefd�dɄ�ZheGdeadefd�dɄ�Zhdedefd�dɄZhd�ed�edd fd�d̈́Zid�ed�edd fd�dτZjd�d�dфZkd�d�dӄZld S)�rz�SFTP client

       This class represents the client side of an SFTP session. It is
       started by calling the :meth:`start_sftp_client()
       <SSHClientConnection.start_sftp_client>` method on the
       :class:`SSHClientConnection` class.

    r��
path_encoding�path_errorscCs||_||_||_d|_dSr )r��_path_encoding�_path_errors�_cwd)r�r�r�r�r�r�r�r��s
zSFTPClient.__init__r�c�r�)z7Allow SFTPClient to be used as an async context managerr�r�r�r�r�r��r�zSFTPClient.__aenter__r�r�r�c�s�|��|��IdHdS�z;Wait for client close when used as an async context managerNF)rvrxr�r�r�r�r�s�zSFTPClient.__aexit__cC�|jjS)z)A logger associated with this SFTP client)r�r�r�r�r�r�r�
�zSFTPClient.loggercCr�r)r�r#r�r�r�r�r#r�zSFTPClient.versioncCr�)z5:class:`SFTPLimits` associated with this SFTP session)r�rr�r�r�r�rr�zSFTPClient.limitscCr�r)r�rr�r�r�r�rr�zSFTPClient.supports_remote_copyr�cC�
t�|�S�r�r�rrr�r�r�r"�
zSFTPClient.basenamecCsBt|t�r	t|�}t|t�r|jr|�|j|j�}|Std��|S)z}Encode path name using configured path encoding

           This method has no effect if the path is already bytes.

        z+Path must be bytes when encoding is not set)rrr%r�r�r�rr�r�r�r�r�(s

�zSFTPClient.encodeT�want_stringcCs:|r|jrz	|�|j|j�WStytd�d�w|S)z�Decode path name using configured path encoding

           This method has no effect if want_string is set to `False`.

        zUnable to decode nameN)r�r@r�rr)r�r�r�r�r�r�r@;s

�zSFTPClient.decodeNr	cCs,|dur|j}|�|�}|rt�||�S|S)z�Compose a path

           If parent is not specified, return a path relative to the
           current remote working directory.

        N)r�r�r�rAr
r�r�r�rJs	
zSFTPClient.compose_path�statfuncc
�s@�|dur|j}z	||�IdHjWStttfytYSw)zbReturn the file type of a remote path, or FILEXFER_TYPE_UNKNOWN
           if it can't be accessedN)r�rOr+r3r-r�)r�r�r�r�r�r��_typeZs��zSFTPClient._typer�r�r�r�srcattrs�preserve�recurser
r�rr�rr��remote_onlyc�s���z|j}|r|tkr|�|�IdH}|j}|tkr�|s0|jdkr$tnt}||�dd�d��|j�	d||�|�
|�IdHsH|�|�IdH|�|�2z33dHW}t
t|j�}|dvr^qMt�||�}t�||�}|�|||||j||||	|
|||
|�IdHqM6|j�	d||�nJ|tkr�|�|�IdH}|j�	d	||�|j�	d
|�|�||�IdHn%|j�	d||�|r�|js�td��t|
||jp�d
|	|||||�	��IdH|�r|j||d�IdH}t|j|j|j|j|j d�}z|j!|||p�|tkd�IdH|j�	d|�WWdSt�y|j�	d�YWdSwWdSt"t#f�yJ}zt$|d|�t$|d|�|
�r>|
|�n�WYd}~dSd}~ww)z(Copy a file, directory, or symbolic linkNr�r>r?z is a directoryz%  Starting copy of directory %s to %sr�z%  Finished copy of directory %s to %sz  Copying symlink %s to %sz    Target path: %sz  Copying file %s to %szRemote copy not supportedrr)r�r^r_r`raz    Preserved attrs: %sz(    Preserving symlink attrs unsupportedr�r)%rOr�r�r�r#rIrr@r�rrrr�rr�rr�rA�_copyrrrrr1r�r�r�r�r�r^r_r`rarr�r�r)r�r�r�r�rr�r�r�r
r�rr�rr�r�rOr��srcnamer�srcfile�dstfile�
targetpathrr�r�r�r�gs��	������

�������
���zSFTPClient._copyF�srcpaths�	copy_type�expand_globc�s��|dkrt|jj|jj�}|dkrtdtt|d��}t|ttt	f�r(|g}n	t|t
�s1t
|�}|j�d|||�g}|r^t
|t|�dk�}|D]}|�|�|�|�||j�IdH�qIn|D]}|�|�}|j||	d�IdH}|�t||d��q`|r�|�|�}|dup�|�|�IdH}t|�dkr�|s�|dus�J�|jd	kr�tnt}||�d
d�d��|D]6}tt|j�}|�|�}|dur�|}n|r�|j||d
�}n|}|�|||||j|||	|
|||
||�IdHq�dS)z*Begin a new file upload, download, or copyrr�r�zStarting SFTP %s of %s to %srNrr�r�r>r?z must be a directory)r	) r�rr�r�r�r�rr�r%rr�r�rr�r�rir�r�r#r�r,r�rrDrr@rrrrr�r)r�r�r�r�rr�r�r�r�r
r�rr�rr�r��srcnames�globr�r��	dst_isdirr�r�r�rr�r�r�r��_begin_copy�sf�
�
���
�
�
��zSFTPClient._begin_copyr)r�r�r
r�rr�rr��remotepaths�	localpathc��0�|�|t||dd|||||||	|
�IdHdS)a�Download remote files

           This method downloads one or more files or directories from
           the remote system. Either a single remote path or a sequence
           of remote paths to download can be provided.

           When downloading a single file or directory, the local path can
           be either the full path to download data into or the path to an
           existing directory where the data should be placed. In the
           latter case, the base file name from the remote path will be
           used as the local name.

           When downloading multiple files, the local path must refer to
           an existing directory.

           If no local path is provided, the file is downloaded
           into the current local working directory.

           If preserve is `True`, the access and modification times
           and permissions of the original file are set on the
           downloaded file.

           If recurse is `True` and the remote path points at a
           directory, the entire subtree under that directory is
           downloaded.

           If follow_symlinks is set to `True`, symbolic links found
           on the remote system will have the contents of their target
           downloaded rather than creating a local symbolic link. When
           using this option during a recursive download, one needs to
           watch out for links that result in loops.

           The block_size argument specifies the size of read and write
           requests issued when downloading the files, defaulting to
           the maximum allowed by the server, or 16 KB if the server
           doesn't advertise limits.

           The max_requests argument specifies the maximum number of
           parallel read or write requests issued, defaulting to a
           value between 16 and 128 depending on the selected block
           size to avoid excessive memory usage.

           If progress_handler is specified, it will be called after
           each block of a file is successfully downloaded. The arguments
           passed to this handler will be the source path, destination
           path, bytes downloaded so far, and total bytes in the file
           being downloaded. If multiple source paths are provided or
           recurse is set to `True`, the progress_handler will be
           called consecutively on each file being downloaded.

           If error_handler is specified and an error occurs during
           the download, this handler will be called with the exception
           instead of it being raised. This is intended to primarily be
           used when multiple remote paths are provided or when recurse
           is set to `True`, to allow error information to be collected
           without aborting the download of the remaining files. The
           error handler can raise an exception if it wants the download
           to completely stop. Otherwise, after an error, the download
           will continue starting with the next file.

           :param remotepaths:
               The paths of the remote files or directories to download
           :param localpath: (optional)
               The path of the local file or directory to download into
           :param preserve: (optional)
               Whether or not to preserve the original file attributes
           :param recurse: (optional)
               Whether or not to recursively copy directories
           :param follow_symlinks: (optional)
               Whether or not to follow symbolic links
           :param sparse: (optional)
               Whether or not to do a sparse file copy where it is supported
           :param block_size: (optional)
               The block size to use for file reads and writes
           :param max_requests: (optional)
               The maximum number of parallel read or write requests
           :param progress_handler: (optional)
               The function to call to report download progress
           :param error_handler: (optional)
               The function to call when an error occurs
           :type remotepaths:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`,
               or a sequence of these
           :type localpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type preserve: `bool`
           :type recurse: `bool`
           :type follow_symlinks: `bool`
           :type sparse: `bool`
           :type block_size: `int`
           :type max_requests: `int`
           :type progress_handler: `callable`
           :type error_handler: `callable`

           :raises: | :exc:`OSError` if a local file I/O error occurs
                    | :exc:`SFTPError` if the server returns an error

        r�FN�r��local_fs�r�r�r�r�r�r
r�rr�rr�r�r�r�r�	��j�zSFTPClient.get�
localpaths�
remotepathc��0�|�t|||dd|||||||	|
�IdHdS)a�Upload local files

           This method uploads one or more files or directories to the
           remote system. Either a single local path or a sequence of
           local paths to upload can be provided.

           When uploading a single file or directory, the remote path can
           be either the full path to upload data into or the path to an
           existing directory where the data should be placed. In the
           latter case, the base file name from the local path will be
           used as the remote name.

           When uploading multiple files, the remote path must refer to
           an existing directory.

           If no remote path is provided, the file is uploaded into the
           current remote working directory.

           If preserve is `True`, the access and modification times
           and permissions of the original file are set on the
           uploaded file.

           If recurse is `True` and the local path points at a
           directory, the entire subtree under that directory is
           uploaded.

           If follow_symlinks is set to `True`, symbolic links found
           on the local system will have the contents of their target
           uploaded rather than creating a remote symbolic link. When
           using this option during a recursive upload, one needs to
           watch out for links that result in loops.

           The block_size argument specifies the size of read and write
           requests issued when uploading the files, defaulting to
           the maximum allowed by the server, or 16 KB if the server
           doesn't advertise limits.

           The max_requests argument specifies the maximum number of
           parallel read or write requests issued, defaulting to a
           value between 16 and 128 depending on the selected block
           size to avoid excessive memory usage.

           If progress_handler is specified, it will be called after
           each block of a file is successfully uploaded. The arguments
           passed to this handler will be the source path, destination
           path, bytes uploaded so far, and total bytes in the file
           being uploaded. If multiple source paths are provided or
           recurse is set to `True`, the progress_handler will be
           called consecutively on each file being uploaded.

           If error_handler is specified and an error occurs during
           the upload, this handler will be called with the exception
           instead of it being raised. This is intended to primarily be
           used when multiple local paths are provided or when recurse
           is set to `True`, to allow error information to be collected
           without aborting the upload of the remaining files. The
           error handler can raise an exception if it wants the upload
           to completely stop. Otherwise, after an error, the upload
           will continue starting with the next file.

           :param localpaths:
               The paths of the local files or directories to upload
           :param remotepath: (optional)
               The path of the remote file or directory to upload into
           :param preserve: (optional)
               Whether or not to preserve the original file attributes
           :param recurse: (optional)
               Whether or not to recursively copy directories
           :param follow_symlinks: (optional)
               Whether or not to follow symbolic links
           :param sparse: (optional)
               Whether or not to do a sparse file copy where it is supported
           :param block_size: (optional)
               The block size to use for file reads and writes
           :param max_requests: (optional)
               The maximum number of parallel read or write requests
           :param progress_handler: (optional)
               The function to call to report upload progress
           :param error_handler: (optional)
               The function to call when an error occurs
           :type localpaths:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`,
               or a sequence of these
           :type remotepath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type preserve: `bool`
           :type recurse: `bool`
           :type follow_symlinks: `bool`
           :type sparse: `bool`
           :type block_size: `int`
           :type max_requests: `int`
           :type progress_handler: `callable`
           :type error_handler: `callable`

           :raises: | :exc:`OSError` if a local file I/O error occurs
                    | :exc:`SFTPError` if the server returns an error

        �putFNr��r�r�r�r�r�r
r�rr�rr�r�r�r�r�xr�zSFTPClient.put)	r�r�r
r�rr�rr�r�c	��2�|�||||dd|||||||	|
|�IdHdS)a+Copy remote files to a new location

           This method copies one or more files or directories on the
           remote system to a new location. Either a single source path
           or a sequence of source paths to copy can be provided.

           When copying a single file or directory, the destination path
           can be either the full path to copy data into or the path to
           an existing directory where the data should be placed. In the
           latter case, the base file name from the source path will be
           used as the destination name.

           When copying multiple files, the destination path must refer
           to an existing remote directory.

           If no destination path is provided, the file is copied into
           the current remote working directory.

           If preserve is `True`, the access and modification times
           and permissions of the original file are set on the
           copied file.

           If recurse is `True` and the source path points at a
           directory, the entire subtree under that directory is
           copied.

           If follow_symlinks is set to `True`, symbolic links found
           in the source will have the contents of their target copied
           rather than creating a copy of the symbolic link. When
           using this option during a recursive copy, one needs to
           watch out for links that result in loops.

           The block_size argument specifies the size of read and write
           requests issued when copying the files, defaulting to the
           maximum allowed by the server, or 16 KB if the server
           doesn't advertise limits.

           The max_requests argument specifies the maximum number of
           parallel read or write requests issued, defaulting to a
           value between 16 and 128 depending on the selected block
           size to avoid excessive memory usage.

           If progress_handler is specified, it will be called after
           each block of a file is successfully copied. The arguments
           passed to this handler will be the source path, destination
           path, bytes copied so far, and total bytes in the file
           being copied. If multiple source paths are provided or
           recurse is set to `True`, the progress_handler will be
           called consecutively on each file being copied.

           If error_handler is specified and an error occurs during
           the copy, this handler will be called with the exception
           instead of it being raised. This is intended to primarily be
           used when multiple source paths are provided or when recurse
           is set to `True`, to allow error information to be collected
           without aborting the copy of the remaining files. The error
           handler can raise an exception if it wants the copy to
           completely stop. Otherwise, after an error, the copy will
           continue starting with the next file.

           :param srcpaths:
               The paths of the remote files or directories to copy
           :param dstpath: (optional)
               The path of the remote file or directory to copy into
           :param preserve: (optional)
               Whether or not to preserve the original file attributes
           :param recurse: (optional)
               Whether or not to recursively copy directories
           :param follow_symlinks: (optional)
               Whether or not to follow symbolic links
           :param sparse: (optional)
               Whether or not to do a sparse file copy where it is supported
           :param block_size: (optional)
               The block size to use for file reads and writes
           :param max_requests: (optional)
               The maximum number of parallel read or write requests
           :param progress_handler: (optional)
               The function to call to report copy progress
           :param error_handler: (optional)
               The function to call when an error occurs
           :param remote_only: (optional)
               Whether or not to only allow this to be a remote copy
           :type srcpaths:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`,
               or a sequence of these
           :type dstpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type preserve: `bool`
           :type recurse: `bool`
           :type follow_symlinks: `bool`
           :type sparse: `bool`
           :type block_size: `int`
           :type max_requests: `int`
           :type progress_handler: `callable`
           :type error_handler: `callable`
           :type remote_only: `bool`

           :raises: | :exc:`OSError` if a local file I/O error occurs
                    | :exc:`SFTPError` if the server returns an error

        zremote copyFN�r��r�r�rr�r�r
r�rr�rr�r�r�r�r��copy�s�n�zSFTPClient.copyc�r�)acDownload remote files with glob pattern match

           This method downloads files and directories from the remote
           system matching one or more glob patterns.

           The arguments to this method are identical to the :meth:`get`
           method, except that the remote paths specified can contain
           wildcard patterns.

        �mgetTNr�r�r�r�r�r�Z���zSFTPClient.mgetc�r�)a[Upload local files with glob pattern match

           This method uploads files and directories to the remote
           system matching one or more glob patterns.

           The arguments to this method are identical to the :meth:`put`
           method, except that the local paths specified can contain
           wildcard patterns.

        �mputTNr�r�r�r�r�r�qr�zSFTPClient.mputc	�r�)a[Copy remote files with glob pattern match

           This method copies files and directories on the remote
           system matching one or more glob patterns.

           The arguments to this method are identical to the :meth:`copy`
           method, except that the source paths specified can contain
           wildcard patterns.

        zremote mcopyTNr�r�r�r�r��mcopy�s��zSFTPClient.mcopyr�src�dst�
src_offset�
src_length�
dst_offsetc�sp�t|tttf�r|j|ddd�IdH}t|tttf�r'|j|ddd�IdH}|j�|j|||j|�IdHdS)a�Copy data between remote files

           :param src:
               The remote file object to read data from
           :param dst:
               The remote file object to write data to
           :param src_offset: (optional)
               The offset to begin reading data from
           :param src_length: (optional)
               The number of bytes to attempt to copy
           :param dst_offset: (optional)
               The offset to begin writing data to
           :type src:
               :class:`SFTPClientFile`, :class:`PurePath <pathlib.PurePath>`,
               `str`, or `bytes`
           :type dst:
               :class:`SFTPClientFile`, :class:`PurePath <pathlib.PurePath>`,
               `str`, or `bytes`
           :type src_offset: `int`
           :type src_length: `int`
           :type dst_offset: `int`

           :raises: :exc:`SFTPError` if the server doesn't support this
                    extension or returns an error

        rrrNr)rr�r%rrr�rtr�)r�r�r�r�r�r�r�r�r�r�s��zSFTPClient.remote_copy�patternsc�s�dd�|�||�IdHD�S)aUMatch remote files against glob patterns

           This method matches remote files against one or more glob
           patterns. Either a single pattern or a sequence of patterns
           can be provided to match against.

           Supported wildcard characters include '*', '?', and
           character ranges in square brackets. In addition, '**'
           can be used to trigger a recursive directory search at
           that point in the pattern, and a trailing slash can be
           used to request that only directories get returned.

           If error_handler is specified and an error occurs during
           the match, this handler will be called with the exception
           instead of it being raised. This is intended to primarily be
           used when multiple patterns are provided to allow error
           information to be collected without aborting the match
           against the remaining patterns. The error handler can raise
           an exception if it wants to completely abort the match.
           Otherwise, after an error, the match will continue starting
           with the next pattern.

           An error will be raised if any of the patterns completely
           fail to match, and this can either stop the match against
           the remaining patterns or be handled by the error_handler
           just like other errors.

           :param patterns:
               Glob patterns to try and match remote files against
           :param error_handler: (optional)
               The function to call when an error occurs
           :type patterns:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`,
               or a sequence of these
           :type error_handler: `callable`

           :raises: :exc:`SFTPError` if the server returns an error
                    or no match is found

        cS�g|]}|j�qSr��rr>r�r�r�r���z#SFTPClient.glob.<locals>.<listcomp>N)�
glob_sftpname)r�r�r�r�r�r�r��s�,�zSFTPClient.globc�s��t|tttf�r|g}t|t|�dk�}g}|D]+}|�|�|�||j�IdH}t|ttf�r?|D]}|�	t
t|j��|_q2|�|�q|S)z�Match glob patterns and return SFTPNames

           This method is similar to :meth:`glob`, but it returns matching
           file names and attributes as :class:`SFTPName` objects.

        rN)
rr�r%rr�r�r�r�r#r@rrri)r�r�r�r��matchesr��new_matchesr3r�r�r�r��s�

�zSFTPClient.glob_sftpnamer�exist_okc

�s"�|�|�}t�|�r
dn|jpd}d}|�d�}t|�d}t|�D]R\}}	t�||	�}z
|�||�IdHd}Wq#t	t
fyh|�|�IdH}
|
tkrf|�
dd�}|jd	kr\tnt	}||�d
��d�Yq#tyu||krs�Yq#w|r�|s�|jd	kr�t
nt	}||�
dd�d��dSdS)a�Create a remote directory with the specified attributes

           This method creates a remote directory at the specified path
           similar to :meth:`mkdir`, but it will also create any
           intermediate directories which don't yet exist.

           If the target directory already exists and exist_ok is set
           to `False`, this method will raise an error.

           :param path:
               The path of where the new remote directory should be created
           :param attrs: (optional)
               The file attributes to use when creating the directory or
               any intermediate directories
           :param exist_ok: (optional)
               Whether or not to raise an error if thet target directory
               already exists
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type attrs: :class:`SFTPAttrs`
           :type exist_ok: `bool`

           :raises: :exc:`SFTPError` if the server returns an error

        r�r;TrNFr>r?r�z is not a directoryz already exists)r�r��isabsr�r�r��	enumeraterArrr4r�r�r@r#rDr-)
r�r�rr��curpath�exists�parts�last�i�partrO�curpath_strr�r�r�r��makedirss>�

������zSFTPClient.makedirs�
ignore_errors�onerrorc�s��dtddf��fdd��dtddf�����fdd��|r(dtddfd	d
��n
�dur5dtddfdd
���dus;J���|�}t�t��z��|�IdHrXt|�dd
�d��Wnt	yl��j|t
���YdSw�|�IdHdS)a<Recursively delete a directory tree

           This method removes all the files in a directory tree.

           If ignore_errors is set, errors are ignored. Otherwise,
           if onerror is set, it will be called with arguments of
           the function which failed, the path it failed on, and
           exception information returns by :func:`sys.exc_info()`.

           If follow_symlinks is set, files or directories pointed at by
           symlinks (and their subdirectories, if any) will be removed
           in addition to the links pointing at them.

           :param path:
               The path of the parent directory to remove
           :param ignore_errors: (optional)
               Whether or not to ignore errors during the remove
           :param onerror: (optional)
               A function to call when errors occur
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type ignore_errors: `bool`
           :type onerror: `callable`

           :raises: :exc:`SFTPError` if the server returns an error

        r�r�Nc�sL��dusJ�z��|�IdHWdSty%��j|t���YdSw)z-Internal helper for unlinking non-directoriesN)�unlinkr�r��exc_infor)r�r�r�r��_unlinkls��z"SFTPClient.rmtree.<locals>._unlinkc	�sN��dusJ�g}zS�4IdH�@��|�2z.3dHW}tt|j�}|dvr'qt�||�}|jjtkr8�|�}n�|�}|�	t
�|��q6Wd�IdHn1IdHsVwYWntyn��j|t
���Ynwt
j|ddi�IdH}tdd�|D�d�}|r�|�z��|�IdHWdSty���j|t
���YdSw)z$Internal helper for rmtree recursionNr��return_exceptionsTcss�|]
}t|t�r|VqdSr )rr�)r�r�r�r�r�r?�s��z5SFTPClient.rmtree.<locals>._rmtree.<locals>.<genexpr>)r�rr�rr�rArrOr�r,r�r�r�r�r��gather�nextr`)r��tasksr�rr��resultsr���_rmtreer�r�r��semr�r�r�vs>�
�(�����z"SFTPClient.rmtree.<locals>._rmtree�_argscWsdSr r��r�r�r�r�r��sz"SFTPClient.rmtree.<locals>.onerrorcWs�r r�r�r�r�r�r��sr>r?z must not be a symlink)r�rzr�r��	Semaphore�_MAX_SFTP_REQUESTS�islinkr+r@r�r�r�)r�r�r�r�r�r�r��rmtreeOs*�
'

���zSFTPClient.rmtreer>�strict�pflags_or_moder|r}c�sd�t|t�rt|�\}}	|	rd}n|}|�|�}|j�|||�IdH}
t|j|
t|t@�||||�S)anOpen a remote file

           This method opens a remote file and returns an
           :class:`SFTPClientFile` object which can be used to read and
           write data and get and set file attributes.

           The path can be either a `str` or `bytes` value. If it is a
           str, it will be encoded using the file encoding specified
           when the :class:`SFTPClient` was started.

           The following open mode flags are supported:

             ========== ======================================================
             Mode       Description
             ========== ======================================================
             FXF_READ   Open the file for reading.
             FXF_WRITE  Open the file for writing. If both this and FXF_READ
                        are set, open the file for both reading and writing.
             FXF_APPEND Force writes to append data to the end of the file
                        regardless of seek position.
             FXF_CREAT  Create the file if it doesn't exist. Without this,
                        attempts to open a non-existent file will fail.
             FXF_TRUNC  Truncate the file to zero length if it already exists.
             FXF_EXCL   Return an error when trying to open a file which
                        already exists.
             ========== ======================================================

           Instead of these flags, a Python open mode string can also be
           provided. Python open modes map to the above flags as follows:

             ==== =============================================
             Mode Flags
             ==== =============================================
             r    FXF_READ
             w    FXF_WRITE | FXF_CREAT | FXF_TRUNC
             a    FXF_WRITE | FXF_CREAT | FXF_APPEND
             x    FXF_WRITE | FXF_CREAT | FXF_EXCL

             r+   FXF_READ | FXF_WRITE
             w+   FXF_READ | FXF_WRITE | FXF_CREAT | FXF_TRUNC
             a+   FXF_READ | FXF_WRITE | FXF_CREAT | FXF_APPEND
             x+   FXF_READ | FXF_WRITE | FXF_CREAT | FXF_EXCL
             ==== =============================================

           Including a 'b' in the mode causes the `encoding` to be set
           to `None`, forcing all data to be read and written as bytes
           in binary format.

           Most applications should be able to use this method regardless
           of the version of the SFTP protocol negotiated with the SFTP
           server. A conversion from the pflags_or_mode values to the
           SFTPv5/v6 flag values will happen automatically. However, if
           an application wishes to set flags only available in SFTPv5/v6,
           the :meth:`open56` method may be used to specify these flags
           explicitly.

           The attrs argument is used to set initial attributes of the
           file if it needs to be created. Otherwise, this argument is
           ignored.

           The block_size argument specifies the size of parallel read and
           write requests issued on the file. If set to `None`, each read
           or write call will become a single request to the SFTP server.
           Otherwise, read or write calls larger than this size will be
           turned into parallel requests to the server of the requested
           size, defaulting to the maximum allowed by the server, or 16 KB
           if the server doesn't advertise limits.

               .. note:: The OpenSSH SFTP server will close the connection
                         if it receives a message larger than 256 KB. So,
                         when connecting to an OpenSSH SFTP server, it is
                         recommended that the block_size be left at its
                         default of using the server-advertised limits.

           The max_requests argument specifies the maximum number of
           parallel read or write requests issued, defaulting to a
           value between 16 and 128 depending on the selected block
           size to avoid excessive memory usage.

           :param path:
               The name of the remote file to open
           :param pflags_or_mode: (optional)
               The access mode to use for the remote file (see above)
           :param attrs: (optional)
               File attributes to use if the file needs to be created
           :param encoding: (optional)
               The Unicode encoding to use for data read and written
               to the remote file
           :param errors: (optional)
               The error-handling mode if an invalid Unicode byte
               sequence is detected, defaulting to 'strict' which
               raises an exception
           :param block_size: (optional)
               The block size to use for read and write requests
           :param max_requests: (optional)
               The maximum number of parallel read or write requests
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type pflags_or_mode: `int` or `str`
           :type attrs: :class:`SFTPAttrs`
           :type encoding: `str`
           :type errors: `str`
           :type block_size: `int` or `None`
           :type max_requests: `int`

           :returns: An :class:`SFTPClientFile` to use to access the file

           :raises: | :exc:`ValueError` if the mode is not valid
                    | :exc:`SFTPError` if the server returns an error

        N)	rr%r�rr�rr�rrF)r�r�rrr|r}rr�r�r�r�r�r�r�r�s�
v�
�zSFTPClient.openr�r�c	
�sJ�|�|�}|j�||||�IdH}	t|j|	t|t@p|t@�||||�S)a

Open a remote file using SFTP v5/v6 flags

           This method is very similar to :meth:`open`, but the pflags_or_mode
           argument is replaced with SFTPv5/v6 desired_access and flags
           arguments. Most applications can continue to use :meth:`open`
           even when talking to an SFTPv5/v6 server and the translation of
           the flags will happen automatically. However, if an application
           wishes to set flags only available in SFTPv5/v6, this method
           provides that capability.

           The following desired_access flags can be specified:

               | ACE4_READ_DATA
               | ACE4_WRITE_DATA
               | ACE4_APPEND_DATA
               | ACE4_READ_ATTRIBUTES
               | ACE4_WRITE_ATTRIBUTES

           The following flags can be specified:

               | FXF_CREATE_NEW
               | FXF_CREATE_TRUNCATE
               | FXF_OPEN_EXISTING
               | FXF_OPEN_OR_CREATE
               | FXF_TRUNCATE_EXISTING
               | FXF_APPEND_DATA
               | FXF_APPEND_DATA_ATOMIC
               | FXF_BLOCK_READ
               | FXF_BLOCK_WRITE
               | FXF_BLOCK_DELETE
               | FXF_BLOCK_ADVISORY (SFTPv6)
               | FXF_NOFOLLOW (SFTPv6)
               | FXF_DELETE_ON_CLOSE (SFTPv6)
               | FXF_ACCESS_AUDIT_ALARM_INFO (SFTPv6)
               | FXF_ACCESS_BACKUP (SFTPv6)
               | FXF_BACKUP_STREAM (SFTPv6)
               | FXF_OVERRIDE_OWNER (SFTPv6)

           At this time, FXF_TEXT_MODE is not supported. Also, servers
           may support only a subset of these flags. For example,
           the AsyncSSH SFTP server doesn't currently support ACLs,
           file locking, or most of the SFTPv6 open flags, but
           support for some of these may be added over time.

           :param path:
               The name of the remote file to open
           :param desired_access: (optional)
               The access mode to use for the remote file (see above)
           :param flags: (optional)
               The access flags to use for the remote file (see above)
           :param attrs: (optional)
               File attributes to use if the file needs to be created
           :param encoding: (optional)
               The Unicode encoding to use for data read and written
               to the remote file
           :param errors: (optional)
               The error-handling mode if an invalid Unicode byte
               sequence is detected, defaulting to 'strict' which
               raises an exception
           :param block_size: (optional)
               The block size to use for read and write requests
           :param max_requests: (optional)
               The maximum number of parallel read or write requests
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type desired_access: int
           :type flags: int
           :type attrs: :class:`SFTPAttrs`
           :type encoding: `str`
           :type errors: `str`
           :type block_size: `int` or `None`
           :type max_requests: `int`

           :returns: An :class:`SFTPClientFile` to use to access the file

           :raises: | :exc:`ValueError` if the mode is not valid
                    | :exc:`SFTPError` if the server returns an error

        N)rr�rDr�rrSrP)
r�r�r�r�rr|r}rr�r�r�r�r�rD8s�
X
��zSFTPClient.open56rc�s$�|�|�}|jj|||d�IdHS)a�Get attributes of a remote file, directory, or symlink

           This method queries the attributes of a remote file, directory,
           or symlink. If the path provided is a symlink and follow_symlinks
           is `True`, the returned attributes will correspond to the target
           of the link.

           :param path:
               The path of the remote file or directory to get attributes for
           :param flags: (optional)
               Flags indicating attributes of interest (SFTPv4 only)
           :param follow_symlinks: (optional)
               Whether or not to follow symbolic links
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type flags: `int`
           :type follow_symlinks: `bool`

           :returns: An :class:`SFTPAttrs` containing the file attributes

           :raises: :exc:`SFTPError` if the server returns an error

        rN)rr�r�)r�r�r�r
r�r�r�r��s
�

�zSFTPClient.statc�s �|�|�}|j�||�IdHS)a
Get attributes of a remote file, directory, or symlink

           This method queries the attributes of a remote file,
           directory, or symlink. Unlike :meth:`stat`, this method
           returns the attributes of a symlink itself rather than
           the target of that link.

           :param path:
               The path of the remote file, directory, or link to get
               attributes for
           :param flags: (optional)
               Flags indicating attributes of interest (SFTPv4 only)
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type flags: `int`

           :returns: An :class:`SFTPAttrs` containing the file attributes

           :raises: :exc:`SFTPError` if the server returns an error

        N)rr�rL)r�r�r�r�r�r�rL�s�
zSFTPClient.lstatc�s(�|�|�}|jj|||d�IdHdS)a�Set attributes of a remote file, directory, or symlink

           This method sets attributes of a remote file, directory, or
           symlink. If the path provided is a symlink and follow_symlinks
           is `True`, the attributes will be set on the target of the link.
           A subset of the fields in `attrs` can be initialized and only
           those attributes will be changed.

           :param path:
               The path of the remote file or directory to set attributes for
           :param attrs:
               File attributes to set
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type attrs: :class:`SFTPAttrs`

           :raises: :exc:`SFTPError` if the server returns an error

        rN)rr�rrr�r�r�r�s
�

�zSFTPClient.setstatc�s�|�|�}|j�|�IdHS)a1Get attributes of a remote file system

           This method queries the attributes of the file system containing
           the specified path.

           :param path:
               The path of the remote file system to get attributes for
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :returns: An :class:`SFTPVFSAttrs` containing the file system
                     attributes

           :raises: :exc:`SFTPError` if the server doesn't support this
                    extension or returns an error

        N)rr�rPr�r�r�r�rP�s�
zSFTPClient.statvfsr�c�s�|�|t|d��IdHdS)a9Truncate a remote file to the specified size

           This method truncates a remote file to the specified size.
           If the path provided is a symbolic link, the target of
           the link will be truncated.

           :param path:
               The path of the remote file to be truncated
           :param size:
               The desired size of the file, in bytes
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type size: `int`

           :raises: :exc:`SFTPError` if the server returns an error

        r�Nr�)r�r�r�r�r�r�r��s�zSFTPClient.truncaternrvc�r�r r�)r�r�rnrvr
r�r�r�r�r�zSFTPClient.chownr�rqc�r�r r�)r�r�r�rqr
r�r�r�r�r�c	�sd�t|t�r	|}nt|t�r|}t|t�r|}nt|t�r|}|j|t||||d�|d�IdHdS)aQChange the owner of a remote file, directory, or symlink

           This method changes the user and group id of a remote file,
           directory, or symlink. If the path provided is a symlink and
           follow_symlinks is `True`, the target of the link will be changed.

           :param path:
               The path of the remote file to change
           :param uid:
               The new user id to assign to the file
           :param gid:
               The new group id to assign to the file
           :param owner:
               The new owner to assign to the file (SFTPv4 only)
           :param group:
               The new group to assign to the file (SFTPv4 only)
           :param follow_symlinks: (optional)
               Whether or not to follow symbolic links
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type uid: `int`
           :type gid: `int`
           :type owner: `str`
           :type group: `str`
           :type follow_symlinks: `bool`

           :raises: :exc:`SFTPError` if the server returns an error

        r�rNr�)	r�r�r�r�rnrvr�rqr
r�r�r�r�s�
 


��rc�s"�|j|t|d�|d�IdHdS)aChange the permissions of a remote file, directory, or symlink

           This method changes the permissions of a remote file, directory,
           or symlink. If the path provided is a symlink and follow_symlinks
           is `True`, the target of the link will be changed.

           :param path:
               The path of the remote file to change
           :param mode:
               The new file permissions, expressed as an int
           :param follow_symlinks: (optional)
               Whether or not to follow symbolic links
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type mode: `int`
           :type follow_symlinks: `bool`

           :raises: :exc:`SFTPError` if the server returns an error

        r�rNr�)r�r�rr
r�r�r�r�Js��zSFTPClient.chmodr[r\c�s"�|j|t||�|d�IdHdS)a�Change the timestamps of a remote file, directory, or symlink

           This method changes the access and modify times of a remote file,
           directory, or symlink. If neither `times` nor '`ns` is provided,
           the times will be changed to the current time.

           If the path provided is a symlink and follow_symlinks is `True`,
           the target of the link will be changed.

           :param path:
               The path of the remote file to change
           :param times: (optional)
               The new access and modify times, as seconds relative to
               the UNIX epoch
           :param ns: (optional)
               The new access and modify times, as nanoseconds relative to
               the UNIX epoch
           :param follow_symlinks: (optional)
               Whether or not to follow symbolic links
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type times: tuple of two `int` or `float` values
           :type ns: tuple of two `int` values
           :type follow_symlinks: `bool`

           :raises: :exc:`SFTPError` if the server returns an error

        rNr�)r�r�r[r\r
r�r�r�r�cs� �zSFTPClient.utimec���|�|�IdHtkS)a Return if the remote path exists and isn't a broken symbolic link

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N�r�r�r�r�r�r�r����zSFTPClient.existsc�s�|j||jd�IdHtkS)a!Return if the remote path exists, without following symbolic links

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        �r�N)r�rLr�r�r�r�r��lexists����zSFTPClient.lexistsc��.�|�|�IdH}|jdurt|j|j�SdS)aReturn the last access time of a remote file or directory

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)r�r^rYr_r�r�r�r��getatime���
��zSFTPClient.getatimec�r)aVReturn the last access time of a remote file or directory

           The time returned is nanoseconds since the epoch.

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)r�r^rZr_r�r�r�r��getatime_ns���
��zSFTPClient.getatime_nsc�r)a#Return the creation time of a remote file or directory (SFTPv4 only)

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)r�rQrYrRr�r�r�r��	getcrtime�r	zSFTPClient.getcrtimec�r)aSReturn the creation time of a remote file or directory

           The time returned is nanoseconds since the epoch.

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)r�rQrZrRr�r�r�r��getcrtime_ns�rzSFTPClient.getcrtime_nsc�r)aReturn the last modification time of a remote file or directory

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)r�r`rYrar�r�r�r��getmtime�r	zSFTPClient.getmtimec�r)a\Return the last modification time of a remote file or directory

           The time returned is nanoseconds since the epoch.

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)r�r`rZrar�r�r�r��getmtime_ns�rzSFTPClient.getmtime_nsc�s�|�|�IdHjS)aReturn the size of a remote file or directory

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        Nr�r�r�r�r��getsizes�zSFTPClient.getsizec���|�|�IdHtkS)aReturn if the remote path refers to a directory

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N�r�r�r�r�r�r�rrzSFTPClient.isdirc�r)aReturn if the remote path refers to a regular file

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)r�r�r�r�r�r��isfile!rzSFTPClient.isfilec�s�|j||jd�IdHtkS)aReturn if the remote path refers to a symbolic link

           :param path:
               The remote path to check
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        rN)r�rLr�r�r�r�r�r�.rzSFTPClient.islinkc��"�|�|�}|j�|�IdHdS)aHRemove a remote file

           This method removes a remote file or symbolic link.

           :param path:
               The path of the remote file or link to remove
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)rr�rTr�r�r�r�rT<s�

zSFTPClient.removec�s�|�|�IdHdS)z)Remove a remote file (see :meth:`remove`)N)rTr�r�r�r�r�L��zSFTPClient.unlinkrrc�s0�|�|�}|�|�}|j�|||�IdHdS)a�Rename a remote file, directory, or link

           This method renames a remote file, directory, or link.

           .. note:: By default, this version of rename will not overwrite
                     the new path if it already exists. However, this
                     can be controlled using the `flags` argument,
                     available in SFTPv5 and later. When a connection
                     is negotiated to use an earliler version of SFTP
                     and `flags` is set, this method will attempt to
                     fall back to the OpenSSH "posix-rename" extension
                     if it is available. That can also be invoked
                     directly by calling :meth:`posix_rename`.

           :param oldpath:
               The path of the remote file, directory, or link to rename
           :param newpath:
               The new name for this file, directory, or link
           :param flags: (optional)
               A combination of the `FXR_OVERWRITE`, `FXR_ATOMIC`, and
               `FXR_NATIVE` flags to specify what happens when `newpath`
               already exists, defaulting to not allowing the overwrite
               (SFTPv5 and later)
           :type oldpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type newpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type flags: `int`

           :raises: :exc:`SFTPError` if the server returns an error

        N)rr�rYrXr�r�r�rYQs�
#
zSFTPClient.renamec��.�|�|�}|�|�}|j�||�IdHdS)a�Rename a remote file, directory, or link with POSIX semantics

           This method renames a remote file, directory, or link,
           removing the prior instance of new path if it previously
           existed.

           This method may not be supported by all SFTP servers. If it
           is not available but the server supports SFTPv5 or later,
           this method will attempt to send the standard SFTP rename
           request with the `FXR_OVERWRITE` flag set.

           :param oldpath:
               The path of the remote file, directory, or link to rename
           :param newpath:
               The new name for this file, directory, or link
           :type oldpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type newpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server doesn't support this
                    extension or returns an error

        N)rr�rZrr�r�r�rZxs�

zSFTPClient.posix_renamer_c	Cs��|�|�}|j�|�IdH}d}z[z9|sJ|j�|�IdH\}}|D]%}t|ttf�rD|�tt	|j
��|_
|jdurD|�tt	|j��|_|Vq"|rWn	tyTYn
wW|j�
|�IdHdSW|j�
|�IdHdS|j�
|�IdHw)a�Return names and attributes of the files in a remote directory

           This method reads the contents of a directory, returning
           the names and attributes of what is contained there as an
           async iterator. If no path is provided, it defaults to the
           current remote working directory.

           :param path: (optional)
               The path of the remote directory to read
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :returns: An async iterator of :class:`SFTPName` entries, with
                     path names matching the type used to pass in the path

           :raises: :exc:`SFTPError` if the server returns an error

        NF)rr�r\r_rr%rr@rr�rr�r�r�)r�r��dirpathr�r�r+r�r�r�r�r��s0�
�
���
���*zSFTPClient.scandirc�s�dd�|�|�2�IdHS)a�Read the contents of a remote directory

           This method reads the contents of a directory, returning
           the names and attributes of what is contained there. If no
           path is provided, it defaults to the current remote working
           directory.

           :param path: (optional)
               The path of the remote directory to read
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :returns: A list of :class:`SFTPName` entries, with path
                     names matching the type used to pass in the path

           :raises: :exc:`SFTPError` if the server returns an error

        c�s�g|z3dHW}|�q6Sr r�)r�r�r�r�r�r����z&SFTPClient.readdir.<locals>.<listcomp>N)r�r�r�r�r�r_�s�zSFTPClient.readdirc�r�r r�r�r�r�r��listdir�r�zSFTPClient.listdir.c�r�r r�r�r�r�r�r�r�c�s �|�|�IdH}dd�|D�S)axRead the names of the files in a remote directory

           This method reads the names of files and subdirectories
           in a remote directory. If no path is provided, it defaults
           to the current remote working directory.

           :param path: (optional)
               The path of the remote directory to read
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :returns: A list of file/subdirectory names, as a `str` or `bytes`
                     matching the type used to pass in the path

           :raises: :exc:`SFTPError` if the server returns an error

        NcSr�r�r�r>r�r�r�r��r�z&SFTPClient.listdir.<locals>.<listcomp>)r_)r�r�r+r�r�r�r�s�c�s$�|�|�}|j�||�IdHdS)aFCreate a remote directory with the specified attributes

           This method creates a new remote directory at the
           specified path with the requested attributes.

           :param path:
               The path of where the new remote directory should be created
           :param attrs: (optional)
               The file attributes to use when creating the directory
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type attrs: :class:`SFTPAttrs`

           :raises: :exc:`SFTPError` if the server returns an error

        N)rr�rr�r�r�r�r�s�
zSFTPClient.mkdirc�r)a�Remove a remote directory

           This method removes a remote directory. The directory
           must be empty for the removal to succeed.

           :param path:
               The path of the remote directory to remove
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)rr�r`r�r�r�r�r`s�
zSFTPClient.rmdirrcc�r�r r��r�r�rcr�r�r�rir�zSFTPClient.realpathc�r�r r�rr�r�r�rir�rbc�r�r r��r�r�rbrcr�r�r�ri!r�c�r�r r�rr�r�r�ri%r�rac�sR�|rt|dt�r|d}|dd�}|�|�}|jdkr5|jj|gt|j|��Rd|i�IdH\}}n|D]}|�||�}q7|j�|�IdH\}}t|�dkrUt	d��|t
kr�|jdkr�z|j�|�|dj�t
|j�IdH|d_W|dSty�|tkr�ttd�|d_n�Y|dSw|dS|�tt|dj�t|ttf��S)	a�Return the canonical version of a remote path

           This method returns a canonical version of the requested path.

           :param path: (optional)
               The path of the remote directory to canonicalize
           :param compose_paths: (optional)
               A list of additional paths that the server should compose
               with `path` before canonicalizing it
           :param check: (optional)
               One of `FXRP_NO_CHECK`, `FXRP_STAT_IF_EXISTS`, and
               `FXRP_STAT_ALWAYS`, specifying when to perform a
               stat operation on the resulting path, defaulting to
               `FXRP_NO_CHECK`
           :type path:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type compose_paths:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type check: int

           :returns: The canonical path as a `str` or `bytes`, matching
                     the type used to pass in the path if `check` is set
                     to `FXRP_NO_CHECK`, or an :class:`SFTPName`
                     containing the canonical path name and attributes
                     otherwise

           :raises: :exc:`SFTPError` if the server returns an error

        rNr�rbr�Too many names returnedr)rO)rr�rr#r�rirgr�r�rrAr�rrlrr�rBr�r�r@rr�r%r)r�r�rbrc�
path_bytesr+r;�cpathr�r�r�ri)sJ�!

�
��
�	����c�s*�|jdur|�d�IdH|_|�|j�S)z�Return the current remote working directory

           :returns: The current remote working directory, decoded using
                     the specified path encoding

           :raises: :exc:`SFTPError` if the server returns an error

        Nr�)r�rir@r�r�r�r��getcwdms�

zSFTPClient.getcwdc�s�|�|�|��IdH|_dS)a%Change the current remote working directory

           :param path:
               The path to set as the new remote working directory
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)rir�r�r�r�r�r��chdir|s�zSFTPClient.chdirc�r�r r�r�r�r�r�r�r�zSFTPClient.readlinkc�r�r r�r�r�r�r�r�r�c�sX�|�|�}|j�|�IdH\}}t|�dkrtd��|�tt|dj�t	|t
tf��S)a�Return the target of a remote symbolic link

           This method returns the target of a symbolic link.

           :param path:
               The path of the remote symbolic link to follow
           :type path: :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :returns: The target path of the link as a `str` or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        Nrrr)rr�rr�rr@rr�rrr%r)r�r��linkpathr+r;r�r�r�r�s�
�c�s.�|�|�}|�|�}|j�||�IdHdS)arCreate a remote symbolic link

           This method creates a symbolic link. The argument order here
           matches the standard Python :meth:`os.symlink` call. The
           argument order sent on the wire is automatically adapted
           depending on the version information sent by the server, as
           a number of servers (OpenSSH in particular) did not follow
           the SFTP standard when implementing this call.

           :param oldpath:
               The path the link should point to
           :param newpath:
               The path of where to create the remote symbolic link
           :type oldpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type newpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server returns an error

        N)r�rr�rrr�r�r�r�s�

zSFTPClient.symlinkc�r)a�Create a remote hard link

           This method creates a hard link to the remote file specified
           by oldpath at the location specified by newpath.

           This method may not be supported by all SFTP servers.

           :param oldpath:
               The path of the remote file the hard link should point to
           :param newpath:
               The path of where to create the remote hard link
           :type oldpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`
           :type newpath:
               :class:`PurePath <pathlib.PurePath>`, `str`, or `bytes`

           :raises: :exc:`SFTPError` if the server doesn't support this
                    extension or returns an error

        N)rr�rjrr�r�r�rj�s�

zSFTPClient.linkcCs|j��dS)z�Exit the SFTP client session

           This method exits the SFTP client session, closing the
           corresponding channel opened on the server.

        N)r�rvr�r�r�r�rv�szSFTPClient.exitc�s�|j��IdHdS)z*Wait for this SFTP client session to closeN)r�rxr�r�r�r�rx�rzSFTPClient.wait_closed)Tr r�)rrr)FNr�r�)r)r_).r)mr�r�r�r�r�rr%r�rr�rrrrr�r"r�r�r�r#rrrr#r�rr$r�r�r@r�
_SFTPStatFuncr�rr�rr�r��
_SFTPPathsr�r�r�r�r�r�r��_SFTPClientFileOrPathrrr�r�r�r��_SFTPOnErrorHandlerr�r�rDrr�rrQrTrMrDrbr�rLrr�rPr�rr�r�rrWr�r�rrr
rr
rrrrrr�rTr�rYrZr
r�r_rr�rr`rirArr rrrjrvrxr�r�r�r�r�s�	�
���
�	��
���
�

������������
�c��������������
�G�
����������
�p�
����������
�p�
�����������
�t�
����������
��
����������
��
�����������
������
�(��
�0��
���
�=��
�e�
�����������������_
��
��
���
���������/��
��
����
�#



��
�'�
�*����
�������������

�D


rcseZdZUdZeeBeBeBeBe	BZ
dZee
BZeeBeBeBeBZdZZee�ed�ee�ed�Zdej�d�fdefdd	d
ddd
dg	Ze e!e"e"fe#d<gZ$e e"e#d<e%ed�rgeddg7Zddddddde&f�fdd�Z'de(e)ddf�fd d!�Z*de"fd"d#�Z+d$e&d%e&d&e,ddfd'd(�Z-d&e,de"fd)d*�Z.d&e,ddfd+d,�Z/d&e,de!e"e0ffd-d.�Z1d&e,de&fd/d0�Z2d&e,de3fd1d2�Z4d&e,de3fd3d4�Z5d&e,ddfd5d6�Z6d&e,ddfd7d8�Z7d&e,de"fd9d:�Z8d&e,de9fd;d<�Z:d&e,ddfd=d>�Z;d&e,ddfd?d@�Z<d&e,ddfdAdB�Z=d&e,de9fdCdD�Z>d&e,de3fdEdF�Z?d&e,ddfdGdH�Z@d&e,de9fdIdJ�ZAd&e,ddfdKdL�ZBd&e,ddfdMdN�ZCd&e,ddfdOdP�ZDd&e,ddfdQdR�ZEd&e,ddfdSdT�ZFd&e,deGfdUdV�ZHd&e,deGfdWdX�ZId&e,ddfdYdZ�ZJd&e,ddfd[d\�ZKd&e,ddfd]d^�ZLd&e,deMfd_d`�ZNd&e,ddfdadb�ZOd&e,dePfdcdd�ZQieRe.�eSe/�eTe1�eUe2�eVe4�eWe5�eXe6�eYe7�eZe8�e[e:�e\e;�e]e<�e^e=�e_e>�e`e?�eae@�ebeA�eceBedeCeeeDefeEdeeFdfeHdgeIdheJdieKdjeLdkeNdleOdmeQi
�Zgeheie&e"fejfe#dn<dqdodp�Zk�ZlS)rr�zAn SFTP server session handlerrr�AsyncSSHsnewliner>r�)r2r3)r5r3)r6r3)r7r3)r�r3)r8r3)r�r3�_extensions�_attrib_extensionsrP)r�r4)r�r4�server�
SFTPServerr�r�r�r�r�cs6t��||�||_||_d|_d|_i|_i|_dS)NFr)r�r��_serverrr�_next_handle�
_file_handles�_dir_handles)r�r)r�r�r�r�r�r�r�s
zSFTPServerHandler.__init__r�r�Nc�s��|jr3t|j���D]}|j�|�}t�|�r|IdHq|j��}t�|�r-|IdHi|_i|_|j	�
d|r?dt|�nd�t��
|�IdHdS)z!Clean up this SFTP server sessionNzSFTP server exited%sr�ry)r+r�r-rr��inspect�isawaitablervr.r�rr%r�r�)r�r�r�r�r�r�r�r�$s�

�


zSFTPServerHandler._cleanupcCs:	|j�dd�}|jdd@|_||jvr||jvr|Sq)z0Get the next available unique file handle numberTr�r�rr)r,r�r-r.rEr�r�r��_get_next_handle:s

�z"SFTPServerHandler._get_next_handler�r�r(c
�s���z.|tkr|��}n|}�j�|�}|std|�����j�|t�}|�|�IdH}|tkrC�j�d�t	t
�td�td�}n�|tkrZt
t|�}	�j�d|	���t|	�}n�|tkr�t
tttf|�\}
}�j�dtt|
�d�|rwdnd�|r��jd	kr�t|�nd
}t|
�|}n�|tkr�t
t|�\}
}�j�dtt|
�d�|r�dnd�|
D]	}�j�d|�q�|r��jd	kr�t|�nd
}t	t|
��d
��fd
d�|
D��|}nVt|t�r�|��jd�|��}nEt|t�r�|��jd�|��}n4t|tj ��rt!�"t
tj |��}nt|tj#��rt$�"t
tj#|��}nt
t%|�}�j�d|�|��j�}W�n�t&�y]}z t}�j�dt'|��t	t(�tt'|��tt)�}WYd}~�n�d}~wt*�y�}z)t}|j+t,k�rt�j�d�n
�j�d|j-j.t'|j/��|��j�}WYd}~�nad}~wt0�y�|du�s�J�t}|j.dd�}�j�d|�t	t1�td|���tt)�}Y�n-t2�y�}z�t}|j3�p�t'|�}|j4t4j5k�r�j�d|�t6}n�|j4t4j7k�r��j�d|�t8}n�|j4t4j9k�r�j�d|�t:}n�|j4t4j;k�r�j�d|�t<}n�|j4t4j=k�r)�j�d|�t>}n�|j4t4j?k�r:�j�d|�t@}nr|j4t4jAk�rK�j�d|�tB}na|j4t4jCk�r\�j�d|�tD}nP|j4t4jEt4jFfv�rp�j�d|�tG}n<|j4t4jHk�r��j�d |�tI}n+|j4t4jJk�r��j�d!|�tK}n|j4t4jLk�r��j�d"|�tM}n	�j�d#|�tN}t*||���j�}WYd}~n4d}~wtO�y�}z#t}d$|��}�jjd#|tPjQd%�t	tN�t|�tt)�}WYd}~nd}~ww��R||t	|�|�dS)&zProcess incoming SFTP requestszUnsupported request type: Nz
Sending OKryzSending handle %szSending %s%sr'r�r�r;r3r*c3s�|]	}|��j�VqdSr )r�rr>r�r�r�r?ws��z4SFTPServerHandler._process_packet.<locals>.<genexpr>�Sendingz
Sending %szSending bad message error: %szSending EOFzSending %s: %s�	z#Sending operation not supported: %szOperation not supported: zSending no such file: %szSending permission denied: %szSending file already exists: %szSending write protect: %sz"Sending no space on filesystem: %szSending disk quota exceeded: %szSending directory not empty: %szSending not a directory: %szSending invalid filename: %szSending link loop: %szSending invalid parameter: %szSending file is a directory: %szSending failure: %szUncaught exception: )r�)Sr>r+rr�r1r�r9r�r�r�rlr�r:rr�r$r;rrr�r�rr�r<r^rArrr�r�r�r�r�r�rxr�r�r�r�r%rqr!r�rrmr�r�r r�rtr��strerrorr��ENOENTrn�EACCESro�EEXISTrx�EROFSry�ENOSPCr|�EDQUOTr}�	ENOTEMPTYr��ENOTDIRr��ENAMETOOLONG�EILSEQr��ELOOPr��EINVALr��EISDIRr�rpr�r�r�r�)r�r�r�r(�handler_typer�r!r��responser�r�r�r�r+r3rr��op_namer rr�r�r�r�Es�



�
�
���




�
������
���

����
z!SFTPServerHandler._process_packetc�s&�|��}|jdkr|��}|��}d|d�d|d��}n
|��}d|d��}t�||j�}|jdkr7|��|j�d||t|��|jdkrq||j	@}|rWt
d	|d
����||j@}	|	rgt
d|	d
����|j�
||||�}
n|j�|||�}
t�|
�r�ttt|
�IdH}
|��}|
|j|<|S)
z%Process an incoming SFTP open requestr�z	access=0x�04xrVz	pflags=0x�02xr�z"Received open request for %s, %s%szUnsupported access flags: 0xrkzUnsupported open flags: 0xN)r+rr&r�r@r'r�r�r��_supported_access_maskrH�_supported_open_flagsr+rDrr/r0rrrzr1r-)r�r(r�r�r��flagmsgr�r�unsupported_access�unsupported_flagsr�r�r�r�r��
_process_open�s@�


�

�
�

zSFTPServerHandler._process_openc�s��|��}|jdkr|��|j�d|���|j�|d�}|r2|j�	|�}t
�|�r0|IdHdS|j�|d�dur=dSt
d��)z&Process an incoming SFTP close requestr�zReceived close for handle %sN�Invalid file handle)r+rr'r�r�r$r-rr+r�r/r0r.r2�r�r(r�r�r�r�r�r��_process_closes�


z SFTPServerHandler._process_closec	�s��|��}|��}|��}|jdkr|��|j�dt|d�||���|j	�
|�}|rl|j�|||�}t
�|�rCttt|�IdH}|jdkr_|j�|j�|��IdH}|t|�|jk}nd}|rjtt|�|fSt�td��)z%Process an incoming SFTP read requestr�z.Received read for %s at offset %d in handle %srFNFrM)r+rAr&rr'r�r�r�r$r-r�r+r�r/r0rrr��
convert_attrsrMr�r�r�r2)	r�r(r�r�r�r�r�rr�r�r�r��
_process_read's0�
�



�zSFTPServerHandler._process_readc�s��|��}|��}|��}|jdkr|��|j�dtt|�d�||���|j	�
|�}|rJ|j�|||�}t
�|�rEttt|�IdH}tt|�Std��)z&Process an incoming SFTP write requestr�z/Received write for %s at offset %d in handle %srFNrM)r+rArr'r�r�r�r�r$r-r�r+r�r/r0rrr�r2)r�r(r�r�r�r�r�r�r�r��_process_writeMs �
�

z SFTPServerHandler._process_writec���|��}|jdkr|��nd}|jdkr|��|j�d||r&d|d��nd�|j�|�}t�	|�r>t
tt|�IdH}|S)	z&Process an incoming SFTP lstat requestr�rr�zReceived lstat for %s%srVrkryN)
r+rr&r'r�r�r+rLr/r0rr�_SFTPOSAttrs�r�r(r�r�r�r�r�r��_process_lstatfs�

�
z SFTPServerHandler._process_lstatc�s��|��}|jdkr|��nd}|jdkr|��|j�d|��|r(d|d��nd�|j�|�}|rJ|j	�
|�}t�|�rHt
tt|�IdH}|Std	��)
z&Process an incoming SFTP fstat requestr�rr�zReceived fstat for handle %s%srVrkryNrM)r+rr&r'r�r�r$r-r�r+rMr/r0rrrTr2)r�r(r�r�r�r�r�r�r��_process_fstat~s�
�
z SFTPServerHandler._process_fstatc��h�|��}t�||j�}|jdkr|��|j�d|t|��|j�	||�}t
�|�r2|IdHdSdS)z(Process an incoming SFTP setstat requestr�zReceived setstat for %s%sN)r+r�r@rr'r�r�r�r+rr/r0�r�r(r�rr�r�r�r��_process_setstat�s�

�z"SFTPServerHandler._process_setstatc�s��|��}t�||j�}|jdkr|��|j�d|��t|��|j	�
|�}|r>|j�||�}t
�|�r<|IdHdSdStd��)z)Process an incoming SFTP fsetstat requestr�z!Received fsetstat for handle %s%sNrM)r+r�r@rr'r�r�r$r�r-r�r+rNr/r0r2)r�r(r�rr�r�r�r�r��_process_fsetstat�s�
�
�z#SFTPServerHandler._process_fsetstatc�sH�|��}|jdkr|��|j�d|�|��}|j�|�|j|<|S)z(Process an incoming SFTP opendir requestr�zReceived opendir for %s)	r+rr'r�r�r1r+r�r.)r�r(r�r�r�r�r��_process_opendir�s�
z"SFTPServerHandler._process_opendirc�s��|��}|jdkr|��|j�d|���|j�|�}|r^d}g}|2z-3dHW}|jsC|jdkrC|j	�
|�}t�|�rC|IdH|�
|�|d7}|tkrRnq%6|r\||tkfSt�td��)z(Process an incoming SFTP readdir requestr�zReceived readdir for handle %srNr�rrM)r+rr'r�r�r$r.r�r�r+�format_longnamer/r0r,�_MAX_READDIR_NAMESr�r2)r�r(r�r+r�r�r3�longname_resultr�r�r��_process_readdir�s0�



��
z"SFTPServerHandler._process_readdirc��R�|��}|jdkr|��|j�d|�|j�|�}t�|�r'|IdHdSdS)z'Process an incoming SFTP remove requestr�zReceived remove for %sN)	r+rr'r�r�r+rTr/r0�r�r(r�r�r�r�r��_process_remove���

�z!SFTPServerHandler._process_removec�sb�|��}t�||j�}|jdkr|��|j�d|�|j�||�}t	�
|�r/|IdHdSdS)z&Process an incoming SFTP mkdir requestr�zReceived mkdir for %sN)r+r�r@rr'r�r�r+rr/r0rYr�r�r��_process_mkdirs�

�z SFTPServerHandler._process_mkdirc�ra)z&Process an incoming SFTP rmdir requestr�zReceived rmdir for %sN)	r+rr'r�r�r+r`r/r0rbr�r�r��_process_rmdirrdz SFTPServerHandler._process_rmdirc		�s0�|��}d}g}|jdkr4|��}|r|�|���|sz
d|j|��}Wnty3td�d�wt}|j�	d||rDdd�
|�nd	|�|D]}t�
||�}qJ|j�
|�}t�|�rhttt|�IdH}t�}|tkr�z|j�|j�|��IdH}Wnttfy�|tkr��Ynwt||d
�gdfS)z)Process an incoming SFTP realpath requestryr�rdzInvalid check valueNzReceived realpath for %s%s%srerfr;r�F)r+rrmr,r�rkrHrAr�r�rAr�r+rir/r0rrr�r�rPr�r�r�rCr�)	r�r(r�rhrcrbrr�rr�r�r��_process_realpath sJ�
�
�
��

���z#SFTPServerHandler._process_realpathc�rS)	z%Process an incoming SFTP stat requestr�rr�zReceived stat for %s%srVrkryN)
r+rr&r'r�r�r+r�r/r0rrrTrUr�r�r��
_process_statOs�

�
zSFTPServerHandler._process_statc�s��|��}|��}|jdkr|��}d|d��}nd}d}|jdkr&|��|j�d|||�|r9|j�||�}n|j�||�}t	�
|�rL|IdHdSdS)	z'Process an incoming SFTP rename requestr�rVrkrryr�z'Received rename request from %s to %s%sN)r+rr&r'r�r�r+rZrYr/r0)r�r(rrr�rKr�r�r�r��_process_renamefs&�

�
�z!SFTPServerHandler._process_renamec�sb�|��}|jdkr|��|j�d|�|j�|�}t�|�r*t	t
t|�IdH}t|�gdfS)z)Process an incoming SFTP readlink requestr�zReceived readlink for %sNF)
r+rr'r�r�r+rr/r0rrr�r�rbr�r�r��_process_readlink�s�

z#SFTPServerHandler._process_readlinkc�sl�|jr
|��}|��}n|��}|��}|��|j�d||�|j�||�}t�|�r4|IdHdSdS)z(Process an incoming SFTP symlink request�&Received symlink request from %s to %sN)	rr+r'r�r�r+rr/r0�r�r(rrr�r�r�r��_process_symlink�s�
�
�z"SFTPServerHandler._process_symlinkc�sx�|��}|��}|��}|r|j�d||�|j�||�}n|j�d||�|j�||�}t�|�r:|IdHdSdS)�*Process an incoming SFTP hard link requestrk�'Received hardlink request from %s to %sN)	r+r�r�r�r+rrjr/r0)r�r(rrrr�r�r�r��
_process_link�s ���
�zSFTPServerHandler._process_linkc�s��|��}|��}|��}|��}|j�d|��|||�|j�|�}|r<|j�	||||�}t
�|�r:|IdHdSdStd��)z0Process an incoming SFTP byte range lock requestzRReceived byte range lock request for handle %s, offset %d, length %d, flags 0x%04xNrM)
r+rAr&r�r�r$r-r�r+rlr/r0r2)r�r(r�r�r�r�r�r�r�r�r��
_process_lock�s ��
�zSFTPServerHandler._process_lockc�st�|��}|��}|��}|j�d|��||�|j�|�}|r6|j�|||�}t	�
|�r4|IdHdSdStd��)z2Process an incoming SFTP byte range unlock requestzDReceived byte range lock request for handle %s, offset %d, length %dNrM)r+rAr�r�r$r-r�r+rmr/r0r2)r�r(r�r�r�r�r�r�r�r��_process_unlock�s�
�
�z!SFTPServerHandler._process_unlockc��T�|��}|��}|��|j�d||�|j�||�}t�|�r(|IdHdSdS)z-Process an incoming SFTP POSIX rename requestz+Received POSIX rename request from %s to %sN)r+r'r�r�r+rZr/r0rlr�r�r��_process_posix_rename����
�z'SFTPServerHandler._process_posix_renamec�sN�|��}|��|j�d|�|j�|�}t�|�r%tt	t
|�IdH}|S)z(Process an incoming SFTP statvfs requestzReceived statvfs for %sN)r+r'r�r�r+rPr/r0rr�_SFTPOSVFSAttrsrbr�r�r��_process_statvfs�s�
z"SFTPServerHandler._process_statvfsc�sj�|��}|��|j�d|���|j�|�}|r1|j�|�}t	�
|�r/ttt
|�IdH}|Std��)z)Process an incoming SFTP fstatvfs requestzReceived fstatvfs for handle %sNrM)r+r'r�r�r$r-r�r+rRr/r0rrrvr2rNr�r�r��_process_fstatvfs	s�
z#SFTPServerHandler._process_fstatvfsc�rs)rnroN)r+r'r�r�r+rjr/r0rlr�r�r��_process_openssh_linkruz'SFTPServerHandler._process_openssh_linkc�sd�|��}|��|j�d|���|j�|�}|r.|j�|�}t	�
|�r,|IdHdSdStd��)z&Process an incoming SFTP fsync requestzReceived fsync for handle %sNrM)r+r'r�r�r$r-r�r+rnr/r0r2rNr�r�r��_process_fsync.s�
�z SFTPServerHandler._process_fsyncc�rX)z)Process an incoming SFTP lsetstat requestr�zReceived lsetstat for %s%sN)r+r�r@rr'r�r�r�r+�lsetstatr/r0rYr�r�r��_process_lsetstat@s�
�
�z#SFTPServerHandler._process_lsetstatc�s4�|��ttd�rt�d�dnd}tttt|�S)z'Process an incoming SFTP limits request�sysconf�SC_OPEN_MAXr�r)r'rbr�r}r�MAX_SFTP_PACKET_LENr��MAX_SFTP_WRITE_LEN)r�r(�nfilesr�r�r��_process_limitsQs��z!SFTPServerHandler._process_limitsc
�s*�|��}|��}|��}|��}|��}|��|j�d|��|||��|�|j�|�}|j�|�}|r�|r�|dk}	|	s@|r�|	rEt}
nt	|t�}
|j
�|||
�}t�
|�rattt|�IdH}|j
�|||�}t�
|�rs|IdHt|�|
kr{dS||
7}||
7}|	s�||
8}|	s@|s@dSdStd��)z%Process an incoming copy data requestzOReceived copy-data from handle %s, offset %d, length %d to handle %s, offset %drNrM)r+rAr'r�r�r$r-r��_COPY_DATA_BLOCK_SIZEr�r+r�r/r0rrr�r�r�r2)
r�r(rorprqrrrsr�r��read_to_endr�r�r�r�r�r��_process_copy_data[sD��



�z$SFTPServerHandler._process_copy_datac	�s��|��}|��}|��}|��|j�d|��||�tt|j�	|��}|rSd}g}t
|||�2z3dHW}|�|�|d7}|tkrFnq16|rQt
||tk�St�td��)z.Process an incoming sparse file ranges requestz;Received ranges request for handle %s, offset %d, length %drNrrM)r+rAr'r�r�r$r�_SFTPFileObjr-r�r�r,r�r�r�r2)	r�r(r�r�r�r�r�r��
data_ranger�r�r��_process_ranges�s0��
��z!SFTPServerHandler._process_rangesr2r�r�r5r6r7r�r8r�rc
�sT�|jdusJ�zG|��IdH}|��}|�|d|�|tkr,|�td��IdHWdS|��}g}|dkrJ|rI|��}|��}|�	||f�|s8n|�
�Wn9tyn}z|�tt|���IdHWYd}~dSd}~wt
y�}z|�|�IdHWYd}~dSd}~ww|j�d||r�dnd�|�|�t||j�|_g}d�dd	�|jD��}	d�d
d	�|jD��}
|jdkr�t|j�t|j�t|j�t|j�tt�|	|
}|�	d|f�nL|jd
k�r.td�}t|j�t|j�t|j�t|j�tt�t|j�t|j�tt|j��|
tt|j��|	}
|�	d|f�|�	d|
f�|� |j�|j�d|j|�r?dnd�|�|�dd	�|D�}z|j!t"dt|j�g|�R�Wnt#�yz}z|�|�IdHWYd}~dSd}~ww|jdk�r�t$t|j�%dd���t&�fdd	�|j'D���r�|j�d�d|_(|�)�IdHdS)zRun an SFTP serverNzExpected init messager�zReceived init, version=%d%sr1ryr;css�|]	\}}t|�VqdSr r=)r�r3r;r�r�r�r?�rz(SFTPServerHandler.run.<locals>.<genexpr>csr<r r=r>r�r�r�r?�r@r�r�r�rr�r�zSending version=%d%scsrgr r=)r�r3r�r�r�r�r?s�"�client_versionc3r�r r�r>�r�r�r�r?,r;r<T)*r�r�rmr�r"r�rr&r+r,r'r�r%r�r�r�r�r�rrAr'r(r��_supported_attr_mask�_supported_attrib_maskrHrGr�r��_supported_open_block_vector�_supported_block_vectorr�rir�r#r�rr?r�r�rr�)r�r(r�r#r@r3r�r�r�r2r:�	supported�
acl_supported�
supported2�sent_extensionsr�r�r�r��s��������

�

�
��������������	�
��
�����
�zSFTPServerHandler.runr)mr�r�r�r�rVrXr\r^r`rar�r�rJrPrHrQrRrSrTrUrGr�r�r�r�r�r��
_vendor_idr��linesepr�r'rrr�ryr(rbr�r�rr�r�r1r�r�rLrOrrQrRrTrVrWrZr[r\r^r`rcrerfrgrhrirjrmrprqrrrtrvrwrxryrzr|rr�r�r�r�r$r%r&r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8rr
r�_SFTPPacketHandlerr�r�r�r�r�r�r��s
�����������
�
��
�
�-&$/
1 ��������	�
���
������!c@s�eZdZdZ	dgdddeefdd�Zedhd	d
��Zedidd
��Z	ede
eeffdd��Zede
fdd��Zdeedefdd�Zdededefdd�Zdeedefdd�Zdeedefdd�Zdededfdd �Zd!edefd"d#�Zd!edefd$d%�Zd!ed&ed'edeefd(d)�Zd!ed*ed+ed'edeef
d,d-�Zd.ededfd/d0�Zd.ed1ed2edeefd3d4�Zd.ed1ed5edeefd6d7�Z d!edeefd8d9�Z!d.edeefd:d;�Z"d!ed'ededfd<d=�Z#d!ed'ededfd>d?�Z$d.ed'ededfd@dA�Z%d!ede&efdBdC�Z'd!ededfdDdE�Z(d!ed'ededfdFdG�Z)d!ededfdHdI�Z*d!edeefdJdK�Z+d!edeefdLdM�Z,dNedOededfdPdQ�Z-d!edeefdRdS�Z.dNedOededfdTdU�Z/dNedOededfdVdW�Z0d.ed1edXed+ededf
dYdZ�Z1d.ed1edXededfd[d\�Z2dNedOededfd]d^�Z3d!edee4fd_d`�Z5d.edee4fdadb�Z6d.ededfdcdd�Z7dedfdedf�Z8dS)jr*a2SFTP server

       Applications should subclass this when implementing an SFTP
       server. The methods listed below should be implemented to
       provide the desired application behavior.

           .. note:: Any method can optionally be defined as a
                     coroutine if that method needs to perform
                     blocking operations to determine its result.

       The `chan` object provided here is the :class:`SSHServerChannel`
       instance this SFTP server is associated with. It can be queried to
       determine which user the client authenticated as, environment
       variables set on the channel when it was opened, and key and
       certificate options or permissions associated with this session.

           .. note:: In AsyncSSH 1.x, this first argument was an
                     :class:`SSHServerConnection`, not an
                     :class:`SSHServerChannel`. When moving to AsyncSSH
                     2.x, subclasses of :class:`SFTPServer` which
                     implement an __init__ method will need to be
                     updated to account for this change, and pass this
                     through to the parent.

       If the `chroot` argument is specified when this object is
       created, the default :meth:`map_path` and :meth:`reverse_map_path`
       methods will enforce a virtual root directory starting in that
       location, limiting access to only files within that directory
       tree. This will also affect path names returned by the
       :meth:`realpath` and :meth:`readlink` methods.

    N�chanr��chrootcCs.||_||rttj�|��|_dSd|_dSr )�_chanr�r�r�ri�_chroot)r�r�r�r�r�r�r�Zs

zSFTPServer.__init__r�cCr�)�4The channel associated with this SFTP server session)r�r�r�r�r�rwer�zSFTPServer.channelr�cCstd|j���S)r�r�)rr��get_connectionr�r�r�r��
connectionkszSFTPServer.connectioncCs
|j��S)a!The environment associated with this SFTP server session

           This method returns the environment set by the client
           when this SFTP session was opened.

           :returns: A dictionary containing the environment variables
                     set by the client

        )r��get_environmentr�r�r�r��envqs
zSFTPServer.envcCr�)z)A logger associated with this SFTP server)r�r�r�r�r�r�r�r�zSFTPServer.loggerr�c�s:�t�|�rttt|�IdH}t|tj�rt�	|�}|S)z Convert stat result to SFTPAttrsN)
r/r0rrrTrr�r�r�rx�r�r�r�r�r�rP�s�

zSFTPServer.convert_attrsr	r3c�s0�t�||�}|�|�|��IdH}t||d�S)z3Construct an SFTPName for a filename in a directoryNr�)r�rArPrLr�)r�r	r3r�rr�r�r��_to_sftpname�s�zSFTPServer._to_sftpnamerncC�t|�S)a/Return the user name associated with a uid

           This method returns a user name string to insert into
           the `longname` field of an :class:`SFTPName` object.

           By default, it calls the Python :func:`pwd.getpwuid`
           function if it is available, or returns the numeric
           uid as a string if not. If there is no uid, it returns
           an empty string.

           :param uid:
               The uid value to look up
           :type uid: `int` or `None`

           :returns: The formatted user name string

        )r|)r�rnr�r�r��format_user��zSFTPServer.format_userrvcCr�)a2Return the group name associated with a gid

           This method returns a group name string to insert into
           the `longname` field of an :class:`SFTPName` object.

           By default, it calls the Python :func:`grp.getgrgid`
           function if it is available, or returns the numeric
           gid as a string if not. If there is no gid, it returns
           an empty string.

           :param gid:
               The gid value to look up
           :type gid: `int` or `None`

           :returns: The formatted group name string

        )r�)r�rvr�r�r��format_group�r�zSFTPServer.format_groupcCsn|jjdurt�|jj�}nd}|jjrt|jj�nd}|�|jj�}|�|jj	�}|jj
dur6t|jj
�nd}|jjdur�t��}t�
|jj�}t�d|�}	z
|	t�d|�7}	Wntyj|	t�d|�7}	Ynw|d|jjkry|kr�nn	|	t�d|�7}	n|	t�d|�7}	nd}	|d	�d
|d�d
|d�d
|d�d
|d
�d
|	d�d
�}
|
�d�tt|j�|_dS)aFormat the long name associated with an SFTP name

           This method fills in the `longname` field of a
           :class:`SFTPName` object. By default, it generates
           something similar to UNIX "ls -l" output. The `filename`
           and `attrs` fields of the :class:`SFTPName` should
           already be filled in before this method is called.

           :param name:
               The :class:`SFTPName` instance to format the long name for
           :type name: :class:`SFTPName`

        Nryz%b z%ez%dg8nAz %H:%Mz  %Y�10s� z>4s�8sz>8s�12sr>)rr�r��filemoderZr%r�rnr�rvr�r`rc�	localtime�strftimerlr�rr�rr�)r�r3rrZrerqr��nowr`�modtime�detailr�r�r�r]�s6�  ��zSFTPServer.format_longnamer�cCs2|jrt�t�d|��}t�|j|dd��S|S)a�Map the path requested by the client to a local path

           This method can be overridden to provide a custom mapping
           from path names requested by the client to paths in the local
           filesystem. By default, it will enforce a virtual "chroot"
           if one was specified when this server was created. Otherwise,
           path names are left unchanged, with relative paths being
           interpreted based on the working directory of the currently
           running process.

           :param path:
               The path name to map
           :type path: `bytes`

           :returns: bytes containing the local path name to operate on

        r�rN)r�r��normpathrA)r�r�r�r�r�r��map_path�szSFTPServer.map_pathcCsB|jr||jkr
dS|�|jd�r|t|j�d�Std��|S)a	Reverse map a local path into the path reported to the client

           This method can be overridden to provide a custom reverse
           mapping for the mapping provided by :meth:`map_path`. By
           default, it hides the portion of the local path associated
           with the virtual "chroot" if one was specified.

           :param path:
               The local path name to reverse map
           :type path: `bytes`

           :returns: bytes containing the path name to report to the client

        r�NzFile not found)r��
startswithr�r+r�r�r�r��reverse_map_paths
zSFTPServer.reverse_map_pathr�rcs&|t@rd}n|t@rd}n
|t@r|t@sd}nd}|t@r+|t@r+|d7}tj�n|t@r3tj�ntj�|t@r?�tjO�|t	@rH�tj
O�|t@rQ�tjO�|t@rZ�tj
O�z�tjO�Wn	tyjYnw|jdurrdn|j�tt|�|��|d��fd	d
�d�}|ddvr�t|�|S)
a�Open a file to serve to a remote client

           This method returns a file object which can be used to read
           and write data and get and set file attributes.

           The possible open mode flags and their meanings are:

             ========== ======================================================
             Mode       Description
             ========== ======================================================
             FXF_READ   Open the file for reading. If neither FXF_READ nor
                        FXF_WRITE are set, this is the default.
             FXF_WRITE  Open the file for writing. If both this and FXF_READ
                        are set, open the file for both reading and writing.
             FXF_APPEND Force writes to append data to the end of the file
                        regardless of seek position.
             FXF_CREAT  Create the file if it doesn't exist. Without this,
                        attempts to open a non-existent file will fail.
             FXF_TRUNC  Truncate the file to zero length if it already exists.
             FXF_EXCL   Return an error when trying to open a file which
                        already exists.
             ========== ======================================================

           The attrs argument is used to set initial attributes of the
           file if it needs to be created. Otherwise, this argument is
           ignored.

           :param path:
               The name of the file to open
           :param pflags:
               The access mode to use for the file (see above)
           :param attrs:
               File attributes to use if the file needs to be created
           :type path: `bytes`
           :type pflags: `int`
           :type attrs: :class:`SFTPAttrs`

           :returns: A file object to use to access the file

           :raises: :exc:`SFTPError` to return an error to the client

        �xb�abrr�+N�rc�t�|���Sr �r�r�r�r;�r��permsr�r��<lambda>~�z!SFTPServer.open.<locals>.<lambda>��	buffering�opener�wx)rIrFrErDr��O_RDWR�O_WRONLY�O_RDONLY�O_APPENDrG�O_CREATrH�O_TRUNC�O_EXCL�O_BINARYr�r�rr�r�r�)r�r�r�rrr�r�r�r�r,sB-



��zSFTPServer.openr�r�csZ|t@r|t@rtj�n|t@rtj�ntj�|t@}|tkr*d}�tjtj	BO�n/|t
kr9d}�tjtjBO�n |tkrEd}�tjO�n|t
krQd}�tjO�n|t@rWdnd}|t@sa|t@rhd}�tjO�|t@rt|t@rt|d7}z�tjO�Wn	ty�Ynw|jdur�dn|j�tt|�|��|d��fd	d
�d�}|ddvr�t|�|S)
a�Open a file to serve to a remote client (SFTPv5 and later)

           This method returns a file object which can be used to read
           and write data and get and set file attributes.

           Supported desired_access bits include `ACE4_READ_DATA`,
           `ACE4_WRITE_DATA`, `ACE4_APPEND_DATA`, `ACE4_READ_ATTRIBUTES`,
           and `ACE4_WRITE_ATTRIBUTES`.

           Supported disposition bits in flags and their meanings are:

             ===================== ============================================
             Disposition           Description
             ===================== ============================================
             FXF_OPEN_EXISTING     Open an existing file
             FXF_OPEN_OR_CREATE    Open an existing file or create a new one
             FXF_CREATE_NEW        Create a new file
             FXF_CREATE_TRUNCATE   Create a new file or truncate an existing
                                   one
             FXF_TRUNCATE_EXISTING Truncate an existing file
             ===================== ============================================

           Other supported flag bits are:

             ===================== ============================================
             Flag                  Description
             ===================== ============================================
             FXF_APPEND_DATA       Append data writes to the end of the file
             ===================== ============================================

           The attrs argument is used to set initial attributes of the
           file if it needs to be created. Otherwise, this argument is
           ignored.

           :param path:
               The name of the file to open
           :param desired_access:
               The access mode to use for the file (see above)
           :param flags:
               The access flags to use for the file (see above)
           :param attrs:
               File attributes to use if the file needs to be created
           :type path: `bytes`
           :type desired_access: `int`
           :type flags: `int`
           :type attrs: :class:`SFTPAttrs`

           :returns: A file object to use to access the file

           :raises: :exc:`SFTPError` to return an error to the client

        r�rrr�r�Nr�rcr�r r�r��r/r�r�r�r��r�z#SFTPServer.open56.<locals>.<lambda>r�r�)rQrRr�r�r�r�rJrKr�r�rLr�rNrOrSrPr�r�r�r�rr�r�r�)r�r�r�r�r�disprr�r�r�r�rD�sP7�
���zSFTPServer.open56r�cCstt|�}|��dS)z�Close an open file or directory

           :param file_obj:
               The file or directory object to close
           :type file_obj: file

           :raises: :exc:`SFTPError` to return an error to the client

        N)rr�r��r�r�r�r�r�r��s
zSFTPServer.closer�r�cC�tt|�}|�|�|�|�S)a�Read data from an open file

           :param file_obj:
               The file to read from
           :param offset:
               The offset from the beginning of the file to begin reading
           :param size:
               The number of bytes to read
           :type file_obj: file
           :type offset: `int`
           :type size: `int`

           :returns: bytes read from the file

           :raises: :exc:`SFTPError` to return an error to the client

        )rr�r�r�)r�r�r�r�r�r�r�r���


zSFTPServer.readr�cCr�)a�Write data to an open file

           :param file_obj:
               The file to write to
           :param offset:
               The offset from the beginning of the file to begin writing
           :param data:
               The data to write to the file
           :type file_obj: file
           :type offset: `int`
           :type data: `bytes`

           :returns: number of bytes written

           :raises: :exc:`SFTPError` to return an error to the client

        )rr�r�r�)r�r�r�r�r�r�r�r�r�zSFTPServer.writecC�t�t|�|���S)adGet attributes of a file, directory, or symlink

           This method queries the attributes of a file, directory,
           or symlink. Unlike :meth:`stat`, this method should
           return the attributes of a symlink itself rather than
           the target of that link.

           :param path:
               The path of the file, directory, or link to get attributes for
           :type path: `bytes`

           :returns: An :class:`SFTPAttrs` or an os.stat_result containing
                     the file attributes

           :raises: :exc:`SFTPError` to return an error to the client

        )r�rLr�r�r�r�r�r�rL+szSFTPServer.lstatcCs tt|�}|��t�|���S)aOGet attributes of an open file

           :param file_obj:
               The file to get attributes for
           :type file_obj: file

           :returns: An :class:`SFTPAttrs` or an os.stat_result containing
                     the file attributes

           :raises: :exc:`SFTPError` to return an error to the client

        )rr��flushr�rMr�r�r�r�r�rM@s
zSFTPServer.fstatcCstt|�|��|�dS)a�Set attributes of a file or directory

           This method sets attributes of a file or directory. If
           the path provided is a symbolic link, the attributes
           should be set on the target of the link. A subset of the
           fields in `attrs` can be initialized and only those
           attributes should be changed.

           :param path:
               The path of the remote file or directory to set attributes for
           :param attrs:
               File attributes to set
           :type path: `bytes`
           :type attrs: :class:`SFTPAttrs`

           :raises: :exc:`SFTPError` to return an error to the client

        N�r�r�r�r�r�r�r�rRszSFTPServer.setstatcCstt|�|��|dd�dS)a2Set attributes of a file, directory, or symlink

           This method sets attributes of a file, directory, or symlink.
           A subset of the fields in `attrs` can be initialized and only
           those attributes should be changed.

           :param path:
               The path of the remote file or directory to set attributes for
           :param attrs:
               File attributes to set
           :type path: `bytes`
           :type attrs: :class:`SFTPAttrs`

           :raises: :exc:`SFTPError` to return an error to the client

        FrNr�r�r�r�r�r{js�zSFTPServer.lsetstatcCs>tt|�}|��tjdkrt|j|�dSt|��|�dS)aPSet attributes of an open file

           :param file_obj:
               The file to set attributes for
           :param attrs:
               File attributes to set on the file
           :type file_obj: file
           :type attrs: :class:`SFTPAttrs`

           :raises: :exc:`SFTPError` to return an error to the client

        r�N)rr�r�r�r�r�r3r�)r�r�rr�r�r�rN�s

�zSFTPServer.fsetstatcCs�t|d�r:|�|�}t�|�r tttttt	f|�IdH}|D]}t
|t�r4|�||�IdHVq"|Vq"dSdD]}|�||�IdHVq<t�
t|�|����+}|D]}|j}tjdkret�|�}t�|jdd��}t	||d�VqVWd�dS1s�wYdS)a�Return names and attributes of the files in a directory

           This function returns an async iterator of :class:`SFTPName`
           entries corresponding to files in the requested directory.

           :param path:
               The path of the directory to scan
           :type path: `bytes`

           :returns: An async iterator of :class:`SFTPName`

           :raises: :exc:`SFTPError` to return an error to the client

        rNr�r�Frr�)rbrr/r0rrrrr�r�rr�r�r�r�r�r3r�r�r�r�rxr�)r�r��listdir_resultr3r�r�rrr�r�r�r��s4�



�
�


��"�zSFTPServer.scandircC�t�t|�|���dS)z�Remove a file or symbolic link

           :param path:
               The path of the file or link to remove
           :type path: `bytes`

           :raises: :exc:`SFTPError` to return an error to the client

        N)r�rTr�r�r�r�r�r�rT��zSFTPServer.removecCs.|jdurdn|j}t�t|�|��|�dS)a�Create a directory with the specified attributes

           :param path:
               The path of where the new directory should be created
           :param attrs:
               The file attributes to use when creating the directory
           :type path: `bytes`
           :type attrs: :class:`SFTPAttrs`

           :raises: :exc:`SFTPError` to return an error to the client

        Ni�)r�r�rr�r�)r�r�rrr�r�r�r�szSFTPServer.mkdircCr�)z�Remove a directory

           :param path:
               The path of the directory to remove
           :type path: `bytes`

           :raises: :exc:`SFTPError` to return an error to the client

        N)r�r`r�r�r�r�r�r�r`�r�zSFTPServer.rmdircCs$tj�t|�|���}|�t|��S)a"Return the canonical version of a path

           :param path:
               The path of the directory to canonicalize
           :type path: `bytes`

           :returns: bytes containing the canonical path

           :raises: :exc:`SFTPError` to return an error to the client

        )r�r�rir�r�r�r�r�r�r�r�ri�s
zSFTPServer.realpathcCr�)aPGet attributes of a file or directory, following symlinks

           This method queries the attributes of a file or directory.
           If the path provided is a symbolic link, the returned
           attributes should correspond to the target of the link.

           :param path:
               The path of the remote file or directory to get attributes for
           :type path: `bytes`

           :returns: An :class:`SFTPAttrs` or an os.stat_result containing
                     the file attributes

           :raises: :exc:`SFTPError` to return an error to the client

        )r�r�r�r�r�r�r�r�r�
szSFTPServer.statrrcCs@t|�|��}t|�|��}tj�|�rtd��t�||�dS)a@Rename a file, directory, or link

           This method renames a file, directory, or link.

           .. note:: This is a request for the standard SFTP version
                     of rename which will not overwrite the new path
                     if it already exists. The :meth:`posix_rename`
                     method will be called if the client requests the
                     POSIX behavior where an existing instance of the
                     new path is removed before the rename.

           :param oldpath:
               The path of the file, directory, or link to rename
           :param newpath:
               The new name for this file, directory, or link
           :type oldpath: `bytes`
           :type newpath: `bytes`

           :raises: :exc:`SFTPError` to return an error to the client

        zFile already existsN)r�r�r�r�r�r4rYrr�r�r�rY!szSFTPServer.renamecCsTt�t|�|���}tjdkr|�d�r|dd�}|jr#tj�	|�}|�
t|��S)a'Return the target of a symbolic link

           :param path:
               The path of the symbolic link to follow
           :type path: `bytes`

           :returns: bytes containing the target path of the link

           :raises: :exc:`SFTPError` to return an error to the client

        r��\\?\r�N)r�rr�r�r�r�r�r�r�rir�r�r�r�r�r�rAs
�zSFTPServer.readlinkcCs�t�|�r|�|�}n.t�|�}|�t�||��}|�|�}tj�||�}tj�|�tj�|�kr9tjj||d�}|�|�}t�	t
|�t
|��dS)aPCreate a symbolic link

           :param oldpath:
               The path the link should point to
           :param newpath:
               The path of where to create the symbolic link
           :type oldpath: `bytes`
           :type newpath: `bytes`

           :raises: :exc:`SFTPError` to return an error to the client

        )r�N)r�r�r��dirnamerAr�r�ri�relpathrr�)r�rr�newdir�abspath1�
mapped_newdir�abspath2r�r�r�rYs



zSFTPServer.symlinkcC�,t|�|��}t|�|��}t�||�dS)aYCreate a hard link

           :param oldpath:
               The path of the file the hard link should point to
           :param newpath:
               The path of where to create the hard link
           :type oldpath: `bytes`
           :type newpath: `bytes`

           :raises: :exc:`SFTPError` to return an error to the client

        N)r�r�r�rjrr�r�r�rjyszSFTPServer.linkr�cC�td��)z)Acquire a byte range lock on an open filerk�r1)r�r�r�r�r�r�r�r�rl�r�zSFTPServer.lockcCr�)z)Release a byte range lock on an open filerkr�)r�r�r�r�r�r�r�rm�r�zSFTPServer.unlockcCr�)aRename a file, directory, or link with POSIX semantics

           This method renames a file, directory, or link, removing
           the prior instance of new path if it previously existed.

           :param oldpath:
               The path of the file, directory, or link to rename
           :param newpath:
               The new name for this file, directory, or link
           :type oldpath: `bytes`
           :type newpath: `bytes`

           :raises: :exc:`SFTPError` to return an error to the client

        N)r�r�r�r�rr�r�r�rZ�szSFTPServer.posix_renamecCs0zt�t|�|���WStytd�d�w)aGet attributes of the file system containing a file

           :param path:
               The path of the file system to get attributes for
           :type path: `bytes`

           :returns: An :class:`SFTPVFSAttrs` or an os.statvfs_result
                     containing the file system attributes

           :raises: :exc:`SFTPError` to return an error to the client

        rON)r�rPr�r�r�r1r�r�r�r�rP�s

�zSFTPServer.statvfscCs4tt|�}zt�|���WStytd�d�w)a�Return attributes of the file system containing an open file

           :param file_obj:
               The open file to get file system attributes for
           :type file_obj: file

           :returns: An :class:`SFTPVFSAttrs` or an os.statvfs_result
                     containing the file system attributes

           :raises: :exc:`SFTPError` to return an error to the client

        rQN)rr�r�rPr�r�r1r�r�r�r�rR�s

�zSFTPServer.fstatvfscCstt|�}t�|���dS)z�Force file data to be written to disk

           :param file_obj:
               The open file containing the data to flush to disk
           :type file_obj: file

           :raises: :exc:`SFTPError` to return an error to the client

        N)rr�r�rnr�r�r�r�r�rn�s
zSFTPServer.fsynccCr�)zShut down this SFTP serverNr�r�r�r�r�rv�szSFTPServer.exitr )r�r�)r�r�)9r�r�r�r�rr�r�r"rwr�rr%r�r�r�r�rTr�rPr�r�r�r�r�r]r�r�rzrrDr�r�r�rLrMrr{rNr
r�rTrr`rir�rYrrrjrlrmrZrvrPrRrnrvr�r�r�r�r*5s�%�
�


�3
�Y�
�g
�
�6  �
�
�
�r*c	@s�eZdZdZdefdd�Zdefdd�Zdee	e
d	ee
d
eedefdd�Z
d
ededeeeeffdd�Zded
edefdd�Zded
edefdd�Zddd�ZdS)�	LocalFilez&An async wrapper around local file I/O�filecC�
||_dSr )�_file)r�r�r�r�r�r���
zLocalFile.__init__r�c�r�)z6Allow LocalFile to be used as an async context managerr�r�r�r�r�r��r�zLocalFile.__aenter__r�r�r�c�r�r�r�r�r�r�r�r����zLocalFile.__aexit__r�r�cCst|j||�S)z2Return data ranges containing data in a local file)r�r�r�r�r�r�r�szLocalFile.request_rangesr�c���|j�|�|j�|�S)r�)r�r�r�r�r�r�r�r���zLocalFile.readr�c�r�)r�)r�r�r�r�r�r�r�r�r�zLocalFile.writeNc�s�|j��dSr�)r�r�r�r�r�r�r�s�zLocalFile.closer)r�r�r�r�r�r�rr�rrrrrr�r�r
rr�r�r�r�r�r�r�r�r�r��s"��
�
	
�r�c
@s6eZdZdZedeed�Zede	de	fdd��Z
dede	fdd�Z		d)de	d
e
e	de	fdd�Zd
d�de	deddfdd�Zd
d�de	dddedd	fdd�Zde	defdd�Zde	defdd�Zde	deefdd�Zde	dd	fdd�Zde	de	fdd�Zd e	d!e	dd	fd"d#�Ze	$d*de	d%ed&edefd'd(��Zd	S)+�LocalFSz/An async wrapper around local filesystem accessrr�r�cCstj�|�S)z/Return the final component of a local file path)r�r�rrr�r�r�rszLocalFS.basenamecCs
t�|�S)zEncode path name using filesystem native encoding

           This method has no effect if the path is already bytes.

        )r�r�r�r�r�r�r�#s
	zLocalFS.encodeNr	cCs|�|�}|r
t�||�S|S)zVCompose a path

           If parent is not specified, just encode the path.

        )r�r�rAr
r�r�r�r.s
zLocalFS.compose_pathTrr
r�c�s�t�tjt|�|d��S)z5Get attributes of a local file, directory, or symlinkr)r�rxr�r�r�rr�r�r�r�:s��zLocalFS.statrc�s�tt|�||d�dS)z5Set attributes of a local file, directory, or symlinkrN)r�r�rr�r�r�rAs�zLocalFS.setstatc���tj�t|��S)z@Return if the local path exists and isn't a broken symbolic link)r�r�r�r�r�r�r�r�r�G��zLocalFS.existsc�r�)z.Return if the local path refers to a directory)r�r�rr�r�r�r�r�rLr�z
LocalFS.isdircCsz�t�t|���+}|D]}|j}tjdkrt�|�}t�|j	dd��}t
||d�VqWd�dS1s6wYdS)z=Return names and attributes of the files in a local directoryr�Frr�N)r�r�r�r3r�r�r�r�rxr�r�)r�r�r�r�rrr�r�r�r�Qs�

�"�zLocalFS.scandirc�s�t�t|��dS)z6Create a local directory with the specified attributesN)r�rr�r�r�r�r�r^s�z
LocalFS.mkdirc�s8�t�t|��}tjdkr|�d�r|dd�}t|�S)z*Return the target of a local symbolic linkr�r�r�N)r�rr�r�r�r�r�r�r�r�r�rcs�
�zLocalFS.readlinkrrc�s�t�t|�t|��dS)zCreate a local symbolic linkN)r�rr�rr�r�r�rns�zLocalFS.symlinkrrrc�s,�tt|�|�}|ddvrt|�t|�S)zOpen a local filerr�)rr�r�r�)r�r�rrr�r�r�r�rss
�zLocalFS.openr r!)r�r�r�r�rr�r�rr#r�rr$r�rrrr�rr�rr
r�r�rrrr�r%r�r�rr�r�r�r�r�sJ��
�
��
���
�
���r�c@s�eZdZdZdedefdd�Zdefdd�Zd	e	e
ed
e	ede	ede
fdd
�Zdededefdd�Zdededefdd�Zddd�ZdS)�SFTPServerFilez;A wrapper around SFTPServer used to access files it managesr)r�cCs||_||_dSr )r+�	_file_obj)r�r)r�r�r�r�r��s
zSFTPServerFile.__init__r�c�r�)z;Allow SFTPServerFile to be used as an async context managerr�r�r�r�r�r��r�zSFTPServerFile.__aenter__r�r�r�c�r�r�r�r�r�r�r�r��r�zSFTPServerFile.__aexit__r�r�c��6�|j�|j||�}t�|�rttt|�IdH}|S)zRead bytes from the fileN)r+r�r�r/r0rrr�)r�r�r�r�r�r�r�r���
�
zSFTPServerFile.readr�c�r�)zWrite bytes to the fileN)r+r�r�r/r0rrr�)r�r�r�r�r�r�r�r��r�zSFTPServerFile.writeNc�s,�|j�|j�}t�|�r|IdHdSdS)z1Close a file managed by the associated SFTPServerN)r+r�r�r/r0r�r�r�r�r���
�
�zSFTPServerFile.closer)r�r�r�r�r*rzr�rr�rrrrrr�r�r�r�r�r�r�r�r�r�r��s��
�	r�c@s�eZdZdZdefdd�Zededefdd��Zdede	fd	d
�Z
dede	ddfd
d�Zdedefdd�Z
dedefdd�Zdedefdd�Zdedeefdd�Zdeddfdd�Zedededefdd��ZdS)�SFTPServerFSz9A wrapper around SFTPServer used to access its filesystemr)cCr�r )r+)r�r)r�r�r�r��r�zSFTPServerFS.__init__r�r�cCr�r�r�rr�r�r�r�r�zSFTPServerFS.basenamec�sF�|j�|�}t�|�rttt|�IdH}t|tj	�r!t
�|�}|S)z9Get attributes of a file or directory, following symlinksN)r+r�r/r0rrrTrr�r�r�rxr�r�r�r�r��s�

zSFTPServerFS.statrNc�s,�|j�||�}t�|�r|IdHdSdS)z%Set attributes of a file or directoryN)r+rr/r0)r�r�rr�r�r�r�r�r�zSFTPServerFS.setstatc
�sn�z
|�|�IdHjWSty)}z|jtjtjfvr$tWYd}~S�d}~wttt	fy6tYSw)z<Return the file type of a path, or 0 if it can't be accessedN)
r�rOr�r�r5r6r�r+r3r-)r�r�r�r�r�r�r��s���zSFTPServerFS._typec�r)zReturn if a path existsNrr�r�r�r�r��rzSFTPServerFS.existsc�rrrr�r�r�r�r�rzSFTPServerFS.isdircCs|j�|�S)r�)r+r�r�r�r�r�r��szSFTPServerFS.scandirc�s.�|j�|t��}t�|�r|IdHdSdSr)r+rr�r/r0)r�r�r�r�r�r�r�s
�
�zSFTPServerFS.mkdirrc�sJ�t|�\}}|j�||t��}t�|�rttt|�IdH}t	|j|�Sr)
r�r+rr�r/r0rrrzr�)r�r�rr�r;r�r�r�r�r s�
zSFTPServerFS.open)r�r�r�r�r*r�r#r�rr�r�rr�r�rr�rr
r�r�rr�r%r�rr�r�r�r�r��s
r��connr�rr�r�r�r�r�r�r�c�sV�t||||�}|j�d�|��IdH|�|��|j�|��IdHt|||�S)r0zStarting SFTP clientN)r�r�rr��create_taskr�rBr)r�rr�r�r�r�r�r�r�r�r��start_sftp_client s�r��sftp_serverc�s6�t�|�r|IdH}t||||�}|��IdHdS)z)Run an SFTP server to handle this requestN)r/r0r�r�)r�r�r�r�r�r�r�r��
_sftp_handler$ s
�

r�cCs|j�d�t||||�S)z+Return a handler for an SFTP server sessionzStarting SFTP server)r�rr�)r�r�r�r�r�r�r��run_sftp_server2 sr�r�(Pr�r�r�rr/r�rrr�pathlibrr�r�r�rc�typesr�typingrr	r
rrr
rrrrrrrrrrrrrr�typing_extensionsrrrryr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rArBrCrDrErFrGrHrIrJrKrLrMrNrOrPrQrRrSrTrUrVrWrXrYrZr[r\r]r^r_r`rarbrcrdrerfrgrhrirjrkrlrmrnrorprqrrrsrtrurvrwrxryrzr{r|r}r~rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��loggingr��miscr�r�r�r�r�r�r�r�r�r�r�r�r(r�r�r�r�r�r�r�r�r�r#r�r��_pywin32_availabler�r�r�r�r�r�rjrwr�r�r�r��streamr�r��Futurer�rzr%�
_LocalPathr�r�r$r#r�r"r$rr^r�rTr�rvr%rzr�r�r�rr�r�r=r�r�r�r�rr�r�r^r�rS�__dict__r�ryrlr��itemsrdr�r�r�rr*r4r=rFrPrTrWrVrYrZrdrorwr|r�r�r�r�r�r�r�rbr�r�r�r�r�r�r+r-rrr/r0r1r2r3r4r5r6r7r8r9rBrCrDrErFrGrHrIrJrKrLrMrmrurNrr�r�r�rr�r�r�r�r�rr�r*r�r�r�r�r�ryr�r�r�r�r�r�r��<module>s�$(  �
��


��
/
��  
������1
��
��
��
H$iJ*��������	�
���
�����&�#tO41$;+EQ>,
j6T���������������