o
    ôj‰B  ã                   @  sØ  U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZ ddlmZ ddlmZmZ ejd	k�rÞd dlZd dlZd d
lmZ dZded< dZded< dZded< eeB eB Zded< dZded< dZded< dZded< dZded< dZded< dZded< dZ ded< dZ!ded < ee B e!B Z"ded!< dZ#ded"< dZ$ded#< dZ%ded$< d%Z&ded&< d'Z'ded(< d)Z(ded*< d Z)ded+< d,Z*ded-< d.Z+ded/< d0Z,ded1< d2Z-d3ed4< d5Z.d3ed6< e /d7¡Z0d8ed9< ej/d:d;d<�Z1d8ed=< G d>d?„ d?ej2ƒZ3G d@dA„ dAej2ƒZ4G dBdC„ dCej2ƒZ5G dDdE„ dEej2ƒZ6G dFdG„ dGej2ƒZ7ej8e0j9_:e ;ej<¡ej=e ;e4¡e ;e5¡e ;ej>¡ej?ej?ej?ej?ej@ej?ge0j9_Aej8e0jB_:ejCe ;e3¡ej@ej@ge0jB_Ade0jD_:e ;e3¡ge0jD_Aej?e0jE_:ej8ge0jE_Aej<ge1jF_AejGe1jF_:ej<e ;e7¡ge1jH_AejGe1jH_:ej<ej=ej=ej=ej=e ;e6¡ge1jI_AejGe1jI_:ej<ej=ej=ej=e ;e6¡ge1jJ_AejGe1jJ_:d^dLdM„ZKd_dOdP„ZLG dQdR„ dReƒZMd`dWdX„ZNdad[d\„ZOnG d]dR„ dReƒZMdRdXgZPdS )bé    )ÚannotationsN)Úsuppress)ÚPath)ÚFinalÚcasté   )ÚBaseFileLock)Úensure_directory_existsÚraise_on_not_writable_fileÚwin32)Úwintypesl        z
Final[int]Ú_GENERIC_READi   @Ú_GENERIC_WRITEi   Ú_SYNCHRONIZEÚ_DESIRED_ACCESSé   Ú_FILE_SHARE_READ_WRITEÚ_FILE_OPEN_IFÚ_FILE_ATTRIBUTE_READONLYé€   Ú_FILE_ATTRIBUTE_NORMALi   Ú_FILE_ATTRIBUTE_REPARSE_POINTé    Ú_FILE_SYNCHRONOUS_IO_NONALERTé@   Ú_FILE_NON_DIRECTORY_FILEi    Ú_FILE_OPEN_REPARSE_POINTÚ_CREATE_OPTIONSÚ_OBJ_CASE_INSENSITIVEÚ_OWNER_WRITEÚ_LOCKFILE_FAIL_IMMEDIATELYé   Ú_LOCKFILE_EXCLUSIVE_LOCKé!   Ú_ERROR_LOCK_VIOLATIONéž   Ú_ERROR_NOT_LOCKEDÚ_STATUS_SUCCESSl   "    Ú_STATUS_ACCESS_DENIEDl   C    Ú_STATUS_SHARING_VIOLATIONl   V    Ú_STATUS_DELETE_PENDINGg      à?zFinal[float]Ú_ACCESS_DENIED_GRACEgü©ñÒMb`?Ú_ACCESS_DENIED_RETRYZntdllzFinal[ctypes.WinDLL]Ú_ntdllZkernel32T)Zuse_last_errorÚ	_kernel32c                   @  s(   e Zd ZdejfdejfdejffZdS )Ú_UNICODE_STRINGÚLengthZMaximumLengthZBufferN)Ú__name__Ú
__module__Ú__qualname__r   ZUSHORTZLPWSTRÚ_fields_© r5   r5   úd/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/filelock/_windows.pyr/   >   s
    ýr/   c                   @  sD   e Zd Zdejfdejfde e¡fdejfdej	fdej	ffZ
dS )Ú_OBJECT_ATTRIBUTESr0   ZRootDirectoryÚ
ObjectNameÚ
AttributesZSecurityDescriptorZSecurityQualityOfServiceN)r1   r2   r3   r   ÚULONGÚHANDLEÚctypesÚPOINTERr/   Úc_void_pr4   r5   r5   r5   r6   r7   E   s    úr7   c                   @  s    e Zd ZdejfdejffZdS )Ú_IO_STATUS_BLOCKZStatusZInformationN)r1   r2   r3   r<   r>   r4   r5   r5   r5   r6   r?   O   s    þr?   c                   @  s8   e Zd ZdejfdejfdejfdejfdejffZdS )Ú_OVERLAPPEDZInternalZInternalHighZOffsetZ
OffsetHighZhEventN)	r1   r2   r3   r<   r>   r   ÚDWORDr;   r4   r5   r5   r5   r6   r@   U   s    ûr@   c                   @  s`   e Zd Zdejfdejfdejfdejfdejfdejfdejfdejfd	ejfd
ejff
ZdS )Ú_BY_HANDLE_FILE_INFORMATIONÚdwFileAttributesZftCreationTimeZftLastAccessTimeZftLastWriteTimeZdwVolumeSerialNumberZnFileSizeHighZnFileSizeLowZnNumberOfLinksZnFileIndexHighZnFileIndexLowN)r1   r2   r3   r   rA   ZFILETIMEr4   r5   r5   r5   r6   rB   ^   s    örB   ÚfdÚintÚreturnÚboolc              
   C  sP   t ƒ }ttB }t t | ¡|dddt |¡¡rdS t 	¡ }|t
kr#dS t |¡‚)Nr   r   TF)r@   r"   r    r.   Ú
LockFileExÚmsvcrtÚget_osfhandler<   ÚbyrefÚget_last_errorr$   ÚWinError)rD   Ú
overlappedÚflagsÚerrr5   r5   r6   Ú_lock_fd_nonblockingœ   s    
rQ   ÚNonec              	   C  sF   t ƒ }t t | ¡dddt |¡¡st ¡  }tkr!t 	|¡‚d S d S )Nr   r   )
r@   r.   ÚUnlockFileExrI   rJ   r<   rK   rL   r&   rM   )rD   rN   rP   r5   r5   r6   Ú
_unlock_fdª   s   ÿ
ürT   c                   @  ó$   e Zd ZdZd	dd„Zd	dd„ZdS )
ÚWindowsFileLockay  
        Uses ``LockFileEx`` to hard lock a byte range of the lock file on Windows systems.

        Lock file cleanup: Windows attempts to delete the lock file after release, but deletion is
        not guaranteed in multi-threaded scenarios where another thread holds an open handle. The lock
        file may persist on disk, which does not affect lock correctness.
        rF   rR   c                 C  s|   t | jƒ t| jƒ t| j|  ¡ ƒ}|d u rd S zt|ƒ}|r$|  |¡ W n ty2   t 	|¡ ‚ w |s<t 	|¡ d S d S ©N)
r
   Ú	lock_filer	   Ú_open_non_reparse_fdÚ
_open_moderQ   Ú_mark_descriptor_ownedÚBaseExceptionÚosÚclose)ÚselfrD   Úlockedr5   r5   r6   Ú_acquire»   s"   


€
þÿúWindowsFileLock._acquirec                 C  sr   t d| jjƒ}t|ƒ |  ¡  | j|dd� | js7ttƒ� t	| j
ƒ ¡  W d   ƒ d S 1 s0w   Y  d S d S )NrE   F)Údefault_suppresses)r   Ú_contextÚlock_file_fdrT   Ú_mark_descriptor_releasedÚ_close_released_fdÚ_preserve_lock_filer   ÚOSErrorr   rX   Úunlink)r_   rD   r5   r5   r6   Ú_releaseÎ   s   
"ÿÿúWindowsFileLock._releaseN©rF   rR   ©r1   r2   r3   Ú__doc__ra   rk   r5   r5   r5   r6   rV   ²   s    
rV   ÚpathÚstrÚmodeú
int | Nonec           
      C  sN  t  d| dtjtjB ¡ |t@  }t| |d�\}}|tkrDt 	¡ t
 }|tkrDt 	¡ |k rDt t¡ t| |d�\}}|tkrDt 	¡ |k s-|tkra|tthv rPdS t |¡}tdt |¡ ¡ | |ƒ‚tƒ }t |t |¡¡s{t ¡ }t |¡ t |¡‚|jt@ rŽt |¡ d| › �}	t|	ƒ‚zt  |tjtj!B ¡W S  t"y¦   t |¡ ‚ w )aÂ  
        Open *path* for locking while refusing reparse points, bound to the handle actually locked.

        The file is opened through ``NtCreateFile`` with ``FILE_OPEN_REPARSE_POINT`` so a symlink or junction planted
        at the path is not followed, and the reparse decision is read from *that* handle via
        ``GetFileInformationByHandle`` rather than from a prior pathname query. Reading the held handle closes the
        check-then-open race: an attacker cannot swap the path between validation and use because both act on the same
        handle. Share mode omits delete so a peer cannot unlink or rename the file out from under a live holder,
        matching ``os.open``'s ``_SH_DENYNO``.

        ``NtCreateFile`` is used instead of ``CreateFileW`` because its return value carries the raw ``NTSTATUS``.
        Windows collapses a transient delete-pending name and a permanent access denial into the same Win32
        ``ERROR_ACCESS_DENIED``; the status keeps them apart, so a real denial fails fast instead of spinning until the
        caller's timeout (#604).

        The reparse option only guards the final path component; Windows still follows reparse points in intermediate
        directories. This assumes the lock file sits in a lock directory untrusted users cannot modify. A path with
        attacker-controlled parent directories would need component-by-component handle validation.

        :param path: the lock file path.
        :param mode: the permission mode; as ``os.open`` does on Windows, a cleared owner-write bit creates the file
            read-only. The attribute only takes effect when the file is created, not when an existing one is opened.

        :returns: a file descriptor owning the opened handle, or ``None`` on a sharing violation or a delete-pending
            name the caller should treat as contention and retry.

        :raises OSError: if the path resolves to a reparse point, or the open fails for any other reason, raised with
            the Win32 error the status maps to. An access denial is raised only once it outlasts a short grace, since
            a peer unlinking the file as it releases can make the open answer ``STATUS_ACCESS_DENIED`` for a moment.

        ÚopenN)Ú	read_onlyz1Lock file is a reparse point (symlink/junction): )#ÚsysÚauditr]   ÚO_RDWRÚO_CREATr   Ú_nt_openr(   ÚtimeÚ	monotonicr+   Úsleepr,   r'   r)   r*   r-   ÚRtlNtStatusToDosErrorri   r<   ZFormatErrorÚstriprB   r.   ÚGetFileInformationByHandlerK   rL   ÚCloseHandlerM   rC   r   rI   Úopen_osfhandleZO_NOINHERITr\   )
rp   rr   ru   ÚhandleÚstatusÚdeadlineÚwinerrorÚinforP   Úmsgr5   r5   r6   rY   Ú   s:   !

þ






þrY   ru   útuple[int, int]c                C  sä   t ƒ }t | t |¡dd¡d@ }|tkrd|fS zAtƒ }t t¡|_t 	|¡|_
t|_t ¡ }tƒ }t t |¡tt |¡t |¡d|rFtnttttdd¡d@ }W t t |¡¡ n	t t |¡¡ w |tkrkd|fS |jpod|fS )a›  
        Open *path* through ``NtCreateFile`` and return ``(handle, status)``.

        ``RtlDosPathNameToNtPathName_U_WithStatus`` translates the Win32 path to the NT namespace, handling relative,
        drive, UNC and extended-length path forms as Win32 itself would, and allocates a buffer that
        ``RtlFreeUnicodeString`` releases. The handle is ``0`` unless the status is ``STATUS_SUCCESS``.
        Nl   ÿÿ r   )r/   r-   Ú'RtlDosPathNameToNtPathName_U_WithStatusr<   rK   r'   r7   Zsizeofr0   Úpointerr8   r   r9   r   r;   r?   ÚNtCreateFiler   r   r   r   r   r   ÚRtlFreeUnicodeStringÚvalue)rp   ru   Znt_namer„   Ú
attributesrƒ   Z	io_statusr5   r5   r6   rz     s>   
õóÿ$rz   c                   @  rU   )
rV   zRUses ``LockFileEx`` to hard lock a byte range of the lock file on Windows systems.rF   rR   c                 C  ó   t ‚rW   ©ÚNotImplementedError©r_   r5   r5   r6   ra   I  ó   rb   c                 C  r�   rW   r‘   r“   r5   r5   r6   rk   L  r”   rl   Nrm   rn   r5   r5   r5   r6   rV   F  s    
)rD   rE   rF   rG   )rD   rE   rF   rR   )rp   rq   rr   rE   rF   rs   )rp   rq   ru   rG   rF   r‰   )QÚ
__future__r   r]   rv   r{   Ú
contextlibr   Úpathlibr   Útypingr   r   Ú_apir   Ú_utilr	   r
   Úplatformr<   rI   r   r   Ú__annotations__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r"   r$   r&   r'   r(   r)   r*   r+   r,   ZWinDLLr-   r.   Z	Structurer/   r7   r?   r@   rB   ZLONGrŒ   Zrestyper=   r;   rA   Z
c_longlongr:   r>   ÚargtypesrŠ   ZLPCWSTRr�   r~   r�   ZBOOLr€   rH   rS   rQ   rT   rV   rY   rz   Ú__all__r5   r5   r5   r6   Ú<module>   sÂ    
ÿ
	


õ
ü


ú
û



(A+þ