
    xjt>                    N   U d dl m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    rVd 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z  ez  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z  e z  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/<    ej+        d0          Z,d1ed2<    ej+        d3d45          Z-d1ed6<    G d7 d8ej.                  Z/ G d9 d:ej.                  Z0 G d; d<ej.                  Z1 G d= d>ej.                  Z2 G d? d@ej.                  Z3ej4        e,j5        _6         ej7        ej8                  ej9         ej7        e0           ej7        e1           ej7        ej:                  ej;        ej;        ej;        ej;        ej<        ej;        ge,j5        _=        ej4        e,j>        _6        ej?         ej7        e/          ej<        ej<        ge,j>        _=        de,j@        _6         ej7        e/          ge,j@        _=        ej;        e,jA        _6        ej4        ge,jA        _=        ej8        ge-jB        _=        ejC        e-jB        _6        ej8         ej7        e3          ge-jD        _=        ejC        e-jD        _6        ej8        ej9        ej9        ej9        ej9         ej7        e2          ge-jE        _=        ejC        e-jE        _6        ej8        ej9        ej9        ej9         ej7        e2          ge-jF        _=        ejC        e-jF        _6        dSdEZGdTdGZH G dH dIe          ZIdUdNZJdVdQZKn G dR dIe          ZIdIgZLdS )W    )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_STATUS_SUCCESSl   "    _STATUS_ACCESS_DENIEDl   C    _STATUS_SHARING_VIOLATIONl   V    _STATUS_DELETE_PENDINGntdllzFinal[ctypes.WinDLL]_ntdllkernel32T)use_last_error	_kernel32c                  B    e Zd Zdej        fdej        fdej        ffZdS )_UNICODE_STRINGLengthMaximumLengthBufferN)__name__
__module____qualname__r   USHORTLPWSTR_fields_     W/home/agent/.hermes/hermes-agent/venv/lib/python3.11/site-packages/filelock/_windows.pyr0   r0   8   s3        x'ho.x'
r;   r0   c                      e Zd Zdej        fdej        fd ej        e          fdej        fdej	        fdej	        ffZ
dS )_OBJECT_ATTRIBUTESr1   RootDirectory
ObjectName
AttributesSecurityDescriptorSecurityQualityOfServiceN)r4   r5   r6   r   ULONGHANDLEctypesPOINTERr0   c_void_pr9   r:   r;   r<   r>   r>   ?   s[        x~&ho.>6>/::;8>*!6?3'9
r;   r>   c                  2    e Zd Zdej        fdej        ffZdS )_IO_STATUS_BLOCKStatusInformationN)r4   r5   r6   rF   rH   r9   r:   r;   r<   rJ   rJ   I   s)        v'FO,
r;   rJ   c                  b    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 )_OVERLAPPEDInternalInternalHighOffset
OffsetHighhEventN)	r4   r5   r6   rF   rH   r   DWORDrE   r9   r:   r;   r<   rN   rN   O   sG        )V_-x~&8>*x'
r;   rN   c                      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dwFileAttributesftCreationTimeftLastAccessTimeftLastWriteTimedwVolumeSerialNumbernFileSizeHighnFileSizeLownNumberOfLinksnFileIndexHighnFileIndexLowN)r4   r5   r6   r   rT   FILETIMEr9   r:   r;   r<   rV   rV   X   s|        0x01!23 12#X^4hn-X^,x~.x~.hn-
r;   rV   fdintreturnboolc           
     .   t                      }t          t          z  }t                              t          j        |           |dddt          j        |                    rdS t          j	                    }|t          k    rdS t          j        |          )Nr   r   TF)rN   r#   r!   r.   
LockFileExmsvcrtget_osfhandlerF   byrefget_last_errorr%   WinError)rb   
overlappedflagserrs       r<   _lock_fd_nonblockingrp      s     !]]
(+EE 4R 8 8%Aq&,WaJbJbcc 	4#%%'''5oc"""r;   Nonec           	         t                      }t                              t          j        |           dddt          j        |                    s%t          j        t          j                              d S )Nr   r   )	rN   r.   UnlockFileExrh   ri   rF   rj   rl   rk   )rb   rm   s     r<   
_unlock_fdrt      sb     ]]
%%f&:2&>&>1aV`IaIabb 	;/&"7"9"9:::	; 	;r;   c                  "    e Zd ZdZddZ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.
        rd   rq   c                r   t          | j                   t          | j                   t          | j        |                                           }|d S 	 t          |          }|r|                     |           n## t          $ r t          j	        |            w xY w|st          j	        |           d S d S N)
r   	lock_filer
   _open_non_reparse_fd
_open_moderp   _mark_descriptor_ownedBaseExceptionosclose)selfrb   lockeds      r<   _acquirezWindowsFileLock._acquire   s    &t~666#DN333 &dndoo6G6GHHBz-b11 4//333       s   &A< < Bc                f   t          d| j        j                  }t          |           |                                  |                     |d           | j        sUt          t                    5  t          | j
                                                   d d d            d S # 1 swxY w Y   d S d S )Nrc   F)default_suppresses)r   _contextlock_file_fdrt   _mark_descriptor_released_close_released_fd_preserve_lock_filer   OSErrorr   ry   unlink)r   rb   s     r<   _releasezWindowsFileLock._release   s    eT]788B rNNN**,,,##B5#AAA+ 2g&& 2 2((//1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2s   0'B$$B(+B(Nrd   rq   r4   r5   r6   __doc__r   r   r:   r;   r<   rv   rv      sF        	 		 	 	 	&
	2 
	2 
	2 
	2 
	2 
	2r;   rv   pathstrmode
int | Nonec                   t          j        d| dt          j        t          j        z             t          | |t          z             \  }}|t          k    rb|t          t          hv rdS t                              |          }t          dt          j        |                                          | |          t!                      }t"                              |t          j        |                    sAt          j                    }t"                              |           t          j        |          |j        t0          z  r.t"                              |           d|  }t          |          	 t3          j        |t          j        t          j        z            S # t8          $ r t"                              |            w xY 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.

        openN)	read_onlyz1Lock file is a reparse point (symlink/junction): )sysauditr~   O_RDWRO_CREAT_nt_openr    r&   r(   r)   r+   RtlNtStatusToDosErrorr   rF   FormatErrorstriprV   r.   GetFileInformationByHandlerj   rk   CloseHandlerl   rW   r   rh   open_osfhandleO_NOINHERITr}   )r   r   handlestatuswinerrorinforo   msgs           r<   rz   rz      s   @ 		&$bi"*&<===!$d\6I2IJJJ_$$35KLLLt33F;;H$ 28 < < B B D DdHUUU*,,33FFL<N<NOO 	''))C!!&)))/#&&& #@@ 	!!&)))LdLLC#,,	(R^1KLLL 	 	 	!!&)))	s   3+F &Gr   tuple[int, int]c               t   t                      }t                              | t          j        |          dd          dz  }|t
          k    rd|fS 	 t                      }t          j        t                    |_        t          j	        |          |_
        t          |_        t          j                    }t                      }t                              t          j        |          t"          t          j        |          t          j        |          d|rt$          nt&          t(          t*          t,          dd          dz  }t                              t          j        |                     n1# t                              t          j        |                     w xY w|t
          k    rd|fS |j        pd|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_WithStatusrF   rj   r&   r>   sizeofr1   pointerr@   r   rA   r   rE   rJ   NtCreateFiler   r   r   r   r   r   RtlFreeUnicodeStringvalue)r   r   nt_namer   
attributesr   	io_statuss          r<   r   r     s    "##??flSZF[F[]acghhkuu_$$f9	?+--J &.@ A AJ$*N7$;$;J!$9J!_&&F(**I##L((#L,,L++09U,,?U*!#   " ''W(=(=>>>>F''W(=(=>>>>_$$f9| q&((s   C1E/ /.Fc                  "    e Zd ZdZddZddZdS )rv   zRUses ``LockFileEx`` to hard lock a byte range of the lock file on Windows systems.rd   rq   c                    t           rx   NotImplementedErrorr   s    r<   r   zWindowsFileLock._acquire:      %%r;   c                    t           rx   r   r   s    r<   r   zWindowsFileLock._release=  r   r;   Nr   r   r:   r;   r<   rv   rv   7  sB        ``	& 	& 	& 	&	& 	& 	& 	& 	& 	&r;   )rb   rc   rd   re   )rb   rc   rd   rq   )r   r   r   rc   rd   r   )r   r   r   re   rd   r   )M
__future__r   r~   r   
contextlibr   pathlibr   typingr   r   _apir	   _utilr
   r   platformrF   rh   r   r   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r#   r%   r&   r'   r(   r)   WinDLLr+   r.   	Structurer0   r>   rJ   rN   rV   LONGr   restyperG   rE   rT   
c_longlongrD   rH   argtypesr   LPCWSTRr   r   r   BOOLr   rg   rs   rp   rt   rv   rz   r   __all__r:   r;   r<   <module>r      s   " " " " " " " 				 



                           F F F F F F F F<7MMMMMM *M****!+N++++)L))))"/."@<"OOOOOO      !"M!!!!+55555)333330:!:::: 1;!::::+55555+55555"?BZ"Z]u"uOuuuu(22222$L$$$$ .87777+55555(*****
 #-O,,,,(22222,66666)33333#06=#9#9F9999&3fmJt&T&T&TITTTT
 
 
 
 
&* 
 
 

 
 
 
 
V- 
 
 

 
 
 
 
6+ 
 
 

 
 
 
 
f& 
 
 

 
 
 
 
f&6 
 
 
 #+-Fx'')**'((v())$F  >F]F2:''	?F2; +/F',:FN?,K,K+LF(+3>F (-5]OF )&.o%6I"$,MI!5=_nfnUpFqFq4rI(13;=I(0{##%I! $,=I {##'I# &.]I"# # # #; ; ; ;&2 &2 &2 &2 &2, &2 &2 &2P8 8 8 8t') ') ') ') ')V& & & & &, & & & r;   