
     jA                    	   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(  rod 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/<   d0Z,d1ed2<   d3Z-d1ed4<    ej\                  d5      Z/d6ed7<    ej\                  d8d9:      Z0d6ed;<    G d< d=ejb                        Z2 G d> d?ejb                        Z3 G d@ dAejb                        Z4 G dB dCejb                        Z5 G dD dEejb                        Z6ejn                  e/jp                  _9         ejt                  ejv                        ejx                   ejt                  e3       ejt                  e4       ejt                  ejz                        ej|                  ej|                  ej|                  ej|                  ej~                  ej|                  ge/jp                  _@        ejn                  e/j                  _9        ej                   ejt                  e2      ej~                  ej~                  ge/j                  _@        de/j                  _9         ejt                  e2      ge/j                  _@        ej|                  e/j                  _9        ejn                  ge/j                  _@        ejv                  ge0j                  _@        ej                  e0j                  _9        ejv                   ejt                  e6      ge0j                  _@        ej                  e0j                  _9        ejv                  ejx                  ejx                  ejx                  ejx                   ejt                  e5      ge0j                  _@        ej                  e0j                  _9        ejv                  ejx                  ejx                  ejx                   ejt                  e5      ge0j                  _@        ej                  e0j                  _9        dMdFZJdNdGZK G dH dIe      ZLdOdJZMdPdKZNdIgZOy G dL dIe      ZLdIgZOy)Q    )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_PENDINGg      ?zFinal[float]_ACCESS_DENIED_GRACEgMb`?_ACCESS_DENIED_RETRYntdllzFinal[ctypes.WinDLL]_ntdllkernel32T)use_last_error	_kernel32c                  ^    e Zd Zdej                  fdej                  fdej
                  ffZy)_UNICODE_STRINGLengthMaximumLengthBufferN)__name__
__module____qualname__r   USHORTLPWSTR_fields_     ]/home/min/hermes_workspace/demo_review/venv/lib/python3.12/site-packages/filelock/_windows.pyr2   r2   =   s-    x'hoo.x'
r=   r2   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
y)_OBJECT_ATTRIBUTESr3   RootDirectory
ObjectName
AttributesSecurityDescriptorSecurityQualityOfServiceN)r6   r7   r8   r   ULONGHANDLEctypesPOINTERr2   c_void_pr;   r<   r=   r>   r@   r@   D   sX    x~~&hoo.>6>>/:;8>>*!6??3'9
r=   r@   c                  D    e Zd Zdej                  fdej                  ffZy)_IO_STATUS_BLOCKStatusInformationN)r6   r7   r8   rH   rJ   r;   r<   r=   r>   rL   rL   N   s!    v'FOO,
r=   rL   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fZy)_OVERLAPPEDInternalInternalHighOffset
OffsetHighhEventN)	r6   r7   r8   rH   rJ   r   DWORDrG   r;   r<   r=   r>   rP   rP   T   sE    )V__-x~~&8>>*x'
r=   rP   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y)_BY_HANDLE_FILE_INFORMATIONdwFileAttributesftCreationTimeftLastAccessTimeftLastWriteTimedwVolumeSerialNumbernFileSizeHighnFileSizeLownNumberOfLinksnFileIndexHighnFileIndexLowN)r6   r7   r8   r   rV   FILETIMEr;   r<   r=   r>   rX   rX   ]   s    0x001!2!23 1 12#X^^4hnn-X^^,x~~.x~~.hnn-
r=   rX   c           
        t               }t        t        z  }t        j	                  t        j                  |       |dddt        j                  |            ryt        j                         }|t        k(  ryt        j                  |      )Nr   r   TF)rP   r#   r!   r0   
LockFileExmsvcrtget_osfhandlerH   byrefget_last_errorr%   WinError)fd
overlappedflagserrs       r>   _lock_fd_nonblockingro      so     !]
(+EE 4 4R 8%Aq&,,WaJbc##%''ooc""r=   c           	         t               }t        j                  t        j                  |       dddt        j                  |            s't        j                  t        j                               y )Nr   r   )	rP   r0   UnlockFileExrf   rg   rH   rh   rj   ri   )rk   rl   s     r>   
_unlock_fdrr      sR     ]
%%f&:&:2&>1aV`Iab//&"7"7"9:: cr=   c                       e Zd ZdZddZddZy)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.
        c                Z   t        | j                         t        | j                         t        | j                  | j	                               }|y 	 t        |      }|r| j                  |       |st        j                  |       y y # t        $ r t        j                  |        w xY wN)
r   	lock_filer
   _open_non_reparse_fd
_open_modero   _mark_descriptor_ownedBaseExceptionosclose)selfrk   lockeds      r>   _acquirezWindowsFileLock._acquire   s    &t~~6#DNN3 &dnndoo6GHBz-b1//3   ! s   B
 
 B*c                H   t        d| j                  j                        }t        |       | j	                          | j                  |d       | j                  s<t        t              5  t        | j                        j                          d d d        y y # 1 sw Y   y xY w)NintF)default_suppresses)r   _contextlock_file_fdrr   _mark_descriptor_released_close_released_fd_preserve_lock_filer   OSErrorr   rw   unlink)r~   rk   s     r>   _releasezWindowsFileLock._release   sz    eT]]778B rN**,##B5#A++g&(//1 '& ,&&s   *$BB!NreturnNoner6   r7   r8   __doc__r   r   r<   r=   r>   rt   rt      s    		&
	2r=   rt   c                |   t        j                  d| dt        j                  t        j                  z         |t
        z   }t        | |      \  }}|t        k(  rt        j                         t        z   }|t        k(  rat        j                         |k  rJt        j                  t               t        | |      \  }}|t        k(  rt        j                         |k  rJ|t        k7  rS|t        t        hv ryt         j#                  |      }t%        dt'        j(                  |      j+                         | |      t-               }t.        j1                  |t'        j2                  |            s>t'        j4                         }t.        j7                  |       t'        j8                  |      |j:                  t<        z  r%t.        j7                  |       d|  }	t%        |	      	 t?        j@                  |t        j                  t        jB                  z        S # tD        $ r t.        j7                  |        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. 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-   RtlNtStatusToDosErrorr   rH   FormatErrorstriprX   r0   GetFileInformationByHandlerh   ri   CloseHandlerj   rY   r   rf   open_osfhandleO_NOINHERITr{   )
pathmoder   handlestatusdeadlinewinerrorinforn   msgs
             r>   rx   rx      s   B 			&$bii"**&<=|++	!$)<**~~'*>>H11dnn6F6Q

/0!)$)!D 11dnn6F6Q _$35KLL33F;H$ 2 28 < B B DdHUU*,33FFLL<NO'')C!!&)//#&&  #@@!!&)EdVLC#,	((R^^1KLL 	!!&)	s   &4H  H;c               :   t               }t        j                  | t        j                  |      dd      dz  }|t
        k7  rd|fS 	 t               }t        j                  t              |_        t        j                  |      |_
        t        |_        t        j                         }t               }t        j!                  t        j                  |      t"        t        j                  |      t        j                  |      d|rt$        nt&        t(        t*        t,        dd      dz  }t        j/                  t        j                  |             |t
        k7  rd|fS |j0                  xs d|fS # t        j/                  t        j                  |             w xY w)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   )r2   r-   'RtlDosPathNameToNtPathName_U_WithStatusrH   rh   r&   r@   sizeofr3   pointerrB   r   rC   r   rG   rL   NtCreateFiler   r   r   r   r   r   RtlFreeUnicodeStringvalue)r   r   nt_namer   
attributesr   	io_statuss          r>   r   r     sK    "#??fllSZF[]acghkuu_$f9	?+-J &.@ AJ$*NN7$;J!$9J!__&F(*I##LL(#LL,LL+09,?U*!#  " ''W(=>_$f9|| q&(( ''W(=>s   C"E0 0*Fc                       e Zd ZdZddZddZy)rt   zRUses ``LockFileEx`` to hard lock a byte range of the lock file on Windows systems.c                    t         rv   NotImplementedErrorr~   s    r>   r   zWindowsFileLock._acquireF      %%r=   c                    t         rv   r   r   s    r>   r   zWindowsFileLock._releaseI  r   r=   Nr   r   r<   r=   r>   rt   rt   C  s    `	&	&r=   )rk   r   r   bool)rk   r   r   r   )r   strr   r   r   z
int | None)r   r   r   r   r   ztuple[int, int])P
__future__r   r|   r   r   
contextlibr   pathlibr   typingr   r   _apir	   _utilr
   r   platformrH   rf   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+   WinDLLr-   r0   	Structurer2   r@   rL   rP   rX   LONGr   restyperI   rG   rV   
c_longlongrF   rJ   argtypesr   LPCWSTRr   r   r   BOOLr   re   rq   ro   rr   rt   rx   r   __all__r<   r=   r>   <module>r      s\   " 	 
      F<<7 *M:*!+NJ+)L*)"/."@<"OOZO J  !"M:!+5j5)3J30:!:: 1;!::+5j5+5j5"?BZ"Z]u"uOZu(2:2$L*$ .8
7+5j5(*:*
 #-OZ,(2:2,6z6)3J3 *-,,).,.#06==#9F 9&3fmmJt&TI#T
&** 

V-- 

6++ 

f&& 

f&6&6 
 #+--Fx')*'(v(()$F  >F]]F22:'	?F22; +/F',:FNN?,K+LF(+3>>F  (-5]]OF  )&.oo%6I"$,MMI!5=__nfnnUpFq4rI((13;==I((0{#%I! $,==I {#'I# &.]]I"#;&2, &2P?B')l &, & r=   