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)Ú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MaximumLengthÚBufferN)Ú__name__Ú
__module__Ú__qualname__r   ÚUSHORTZLPWSTRÚ_fields_© r8   r8   úb/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/filelock/_windows.pyr0   >   s
    ýr0   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_ATTRIBUTESr1   ZRootDirectoryÚ
ObjectNameÚ
AttributesZSecurityDescriptorZSecurityQualityOfServiceN)r3   r4   r5   r   ÚULONGÚHANDLEÚctypesÚPOINTERr0   Úc_void_pr7   r8   r8   r8   r9   r:   E   s    úr:   c                   @  s    e Zd ZdejfdejffZdS )Ú_IO_STATUS_BLOCKZStatusZInformationN)r3   r4   r5   r?   rA   r7   r8   r8   r8   r9   rB   O   s    þrB   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)	r3   r4   r5   r?   rA   r   ÚDWORDr>   r7   r8   r8   r8   r9   rC   U   s    ûrC   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)r3   r4   r5   r   rD   ZFILETIMEr7   r8   r8   r8   r9   rE   ^   s    örE   Ú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)rC   r"   r    r/   Ú
LockFileExÚmsvcrtÚget_osfhandler?   ÚbyrefÚget_last_errorr$   ÚWinError)rG   Ú
overlappedÚflagsÚerrr8   r8   r9   Ú_lock_fd_nonblockingœ   s    
rT   ÚNonec              	   C  sF   t ƒ }t t | ¡dddt |¡¡st ¡  }tkr!t 	|¡‚d S d S )Nr   r   )
rC   r/   ÚUnlockFileExrL   rM   r?   rN   rO   r&   rP   )rG   rQ   rS   r8   r8   r9   Ú
_unlock_fdª   s   ÿ
ürW   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.
        rI   rU   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_moderT   Ú_mark_descriptor_ownedÚBaseExceptionÚosÚclose)ÚselfrG   Úlockedr8   r8   r9   Ú_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 )NrH   F)Údefault_suppresses)r   Ú_contextÚlock_file_fdrW   Ú_mark_descriptor_releasedÚ_close_released_fdÚ_preserve_lock_filer   ÚOSErrorr   r[   Úunlink)rb   rG   r8   r8   r9   Ú_releaseÎ   s   
"ÿÿúWindowsFileLock._releaseN©rI   rU   ©r3   r4   r5   Ú__doc__rd   rn   r8   r8   r8   r9   rY   ²   s    
rY   Ú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-   ÚRtlNtStatusToDosErrorrl   r?   ZFormatErrorÚstriprE   r/   ÚGetFileInformationByHandlerN   rO   ÚCloseHandlerP   rF   r   rL   Úopen_osfhandleZO_NOINHERITr_   )
rs   ru   rx   ÚhandleÚstatusÚdeadlineÚwinerrorÚinforS   Úmsgr8   r8   r9   r\   Ú   s:   !

þ






þr\   rx   ú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   )r0   r-   Ú'RtlDosPathNameToNtPathName_U_WithStatusr?   rN   r'   r:   Úsizeofr1   Úpointerr;   r   r<   r   r>   rB   ÚNtCreateFiler   r   r   r   r   r   ÚRtlFreeUnicodeStringÚvalue)rs   rx   Znt_namer‡   Ú
attributesr†   Z	io_statusr8   r8   r9   r}     s>   
õóÿ$r}   c                   @  rX   )
rY   zRUses ``LockFileEx`` to hard lock a byte range of the lock file on Windows systems.rI   rU   c                 C  ó   t ‚rZ   ©ÚNotImplementedError©rb   r8   r8   r9   rd   I  ó   re   c                 C  r”   rZ   r•   r—   r8   r8   r9   rn   L  r˜   ro   Nrp   rq   r8   r8   r8   r9   rY   F  s    
)rG   rH   rI   rJ   )rG   rH   rI   rU   )rs   rt   ru   rH   rI   rv   )rs   rt   rx   rJ   rI   rŒ   )QÚ
__future__r   r`   ry   r~   Ú
contextlibr   Úpathlibr   Útypingr   r   Ú_apir   Ú_utilr	   r
   Úplatformr?   rL   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/   Ú	Structurer0   r:   rB   rC   rE   ÚLONGr�   Úrestyper@   r>   rD   Ú
c_longlongr=   rA   Úargtypesr�   ZLPCWSTRr‘   r�   r„   ÚBOOLrƒ   rK   rV   rT   rW   rY   r\   r}   Ú__all__r8   r8   r8   r9   Ú<module>   sÂ    
ÿ
	


õ
ü


ú
û



(A+þ