
    Lojt>                    b	   % S SK Jr  S SKrS SKrS SKJr  S SKJr  S SKJ	r	J
r
  SSKJr  SSKJrJr  \R                   S	:X  Ga_  S SKrS SKrS S
KJr  SrS\S'   SrS\S'   SrS\S'   \\-  \-  rS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   Sr S\S '   \\-  \ -  r!S\S!'   Sr"S\S"'   Sr#S\S#'   Sr$S\S$'   S%r%S\S&'   S'r&S\S('   S r'S\S)'   S*r(S\S+'   S,r)S\S-'   S.r*S\S/'   \RV                  " S05      r,S1\S2'   \RV                  " S3S4S59r-S1\S6'    " S7 S8\R\                  5      r/ " S9 S:\R\                  5      r0 " S; S<\R\                  5      r1 " S= S>\R\                  5      r2 " S? S@\R\                  5      r3\Rh                  \,Rj                  l6        \Rn                  " \Rp                  5      \Rr                  \Rn                  " \05      \Rn                  " \15      \Rn                  " \Rt                  5      \Rv                  \Rv                  \Rv                  \Rv                  \Rx                  \Rv                  /\,Rj                  l=        \Rh                  \,R|                  l6        \R~                  \Rn                  " \/5      \Rx                  \Rx                  /\,R|                  l=        S\,R                  l6        \Rn                  " \/5      /\,R                  l=        \Rv                  \,R                  l6        \Rh                  /\,R                  l=        \Rp                  /\-R                  l=        \R                  \-R                  l6        \Rp                  \Rn                  " \35      /\-R                  l=        \R                  \-R                  l6        \Rp                  \Rr                  \Rr                  \Rr                  \Rr                  \Rn                  " \25      /\-R                  l=        \R                  \-R                  l6        \Rp                  \Rr                  \Rr                  \Rr                  \Rn                  " \25      /\-R                  l=        \R                  \-R                  l6        SHSA jrGSISB jrH " SC SD\5      rISJSE jrJSKSF jrKO " SG SD\5      rISD/rLg)L    )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                  f    \ rS rSrS\R
                  4S\R
                  4S\R                  44rSrg)_UNICODE_STRING8   LengthMaximumLengthBuffer N)	__name__
__module____qualname____firstlineno__r   USHORTLPWSTR_fields___static_attributes__r5       K/mnt/workspace/venv-train/lib/python3.13/site-packages/filelock/_windows.pyr0   r0   8   s-    x'hoo.x'
r>   r0   c                      \ rS rSrS\R
                  4S\R                  4S\R                  " \	5      4S\R
                  4S\R                  4S\R                  44rSrg	)
_OBJECT_ATTRIBUTES?   r2   RootDirectory
ObjectName
AttributesSecurityDescriptorSecurityQualityOfServicer5   N)r6   r7   r8   r9   r   ULONGHANDLEctypesPOINTERr0   c_void_pr<   r=   r5   r>   r?   rA   rA   ?   sV    x~~&hoo.6>>/:;8>>*!6??3'9
r>   rA   c                  L    \ rS rSrS\R
                  4S\R
                  44rSrg)_IO_STATUS_BLOCKI   StatusInformationr5   N)r6   r7   r8   r9   rJ   rL   r<   r=   r5   r>   r?   rN   rN   I   s!    v'FOO,
r>   rN   c                      \ rS rSrS\R
                  4S\R
                  4S\R                  4S\R                  4S\R                  44r	Sr
g)	_OVERLAPPEDO   InternalInternalHighOffset
OffsetHighhEventr5   N)r6   r7   r8   r9   rJ   rL   r   DWORDrI   r<   r=   r5   r>   r?   rS   rS   O   sE    )V__-x~~&8>>*x'
r>   rS   c                     \ rS rSrS\R
                  4S\R                  4S\R                  4S\R                  4S\R
                  4S\R
                  4S\R
                  4S	\R
                  4S
\R
                  4S\R
                  44
rSrg)_BY_HANDLE_FILE_INFORMATIONX   dwFileAttributesftCreationTimeftLastAccessTimeftLastWriteTimedwVolumeSerialNumbernFileSizeHighnFileSizeLownNumberOfLinksnFileIndexHighnFileIndexLowr5   N)	r6   r7   r8   r9   r   rZ   FILETIMEr<   r=   r5   r>   r?   r\   r\   X   s    0x001!2!23 1 12#X^^4hnn-X^^,x~~.x~~.hnn-
r>   r\   c           
     ,   [        5       n[        [        -  n[        R	                  [
        R                  " U 5      USSS[        R                  " U5      5      (       a  g[        R                  " 5       nU[        :X  a  g[        R                  " U5      e)Nr   r   TF)rS   r#   r!   r.   
LockFileExmsvcrtget_osfhandlerJ   byrefget_last_errorr%   WinError)fd
overlappedflagserrs       r?   _lock_fd_nonblockingrt      sr     !]
(+EE 4 4R 8%Aq&,,WaJbcc##%''ooc""r>   c           	         [        5       n[        R                  [        R                  " U 5      SSS[
        R                  " U5      5      (       d)  [
        R                  " [
        R                  " 5       5      eg )Nr   r   )	rS   r.   UnlockFileExrk   rl   rJ   rm   ro   rn   )rp   rq   s     r?   
_unlock_fdrw      sU     ]
%%f&:&:2&>1aV`Iabb//&"7"7"9:: cr>   c                  ,    \ rS rSrSrSS jrSS jrSrg)WindowsFileLock   aQ  
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                v   [        U R                  5        [        U R                  5        [        U R                  U R	                  5       5      nUc  g  [        U5      nU(       a  U R                  U5        U(       d  [        R                  " U5        g g ! [         a    [        R                  " U5        e f = fN)
r   	lock_filer
   _open_non_reparse_fd
_open_modert   _mark_descriptor_ownedBaseExceptionosclose)selfrp   lockeds      r?   _acquireWindowsFileLock._acquire   s    &t~~6#DNN3 &dnndoo6GHBz-b1//3   ! s   #B "B8c                V   [        SU R                  R                  5      n[        U5        U R	                  5         U R                  USS9  U R                  (       d<  [        [        5         [        U R                  5      R                  5         S S S 5        g g ! , (       d  f       g = f)NintF)default_suppresses)r   _contextlock_file_fdrw   _mark_descriptor_released_close_released_fd_preserve_lock_filer   OSErrorr   r}   unlink)r   rp   s     r?   _releaseWindowsFileLock._release   sz    eT]]778B rN**,##B5#A++g&(//1 '& ,&&s   ,$B
B(r5   NreturnNoner6   r7   r8   r9   __doc__r   r   r=   r5   r>   r?   ry   ry      s    		&
	2r>   ry   c                   [         R                  " SU S[        R                  [        R                  -  5        [        X[        -  (       + S9u  p#U[        :w  aU  U[        [        1;   a  g[        R                  U5      n[        S[        R                  " U5      R                  5       X5      e[!        5       n["        R%                  U[        R&                  " U5      5      (       d@  [        R(                  " 5       n["        R+                  U5        [        R,                  " U5      eUR.                  [0        -  (       a%  ["        R+                  U5        SU  3n[        U5      e [2        R4                  " U[        R                  [        R6                  -  5      $ ! [8         a    ["        R+                  U5        e f = f)aW  
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   rJ   FormatErrorstripr\   r.   GetFileInformationByHandlerm   rn   CloseHandlero   r^   r   rk   open_osfhandleO_NOINHERITr   )pathmodehandlestatuswinerrorinfors   msgs           r?   r~   r~      sL   @ 			&$bii"**&<=!$\6I2IJ_$35KLL33F;H$ 2 28 < B B DdUU*,33FFLL<NOO'')C!!&)//#&&  #@@!!&)EdVLC#,	((R^^1KLL 	!!&)	s   &5F !F=c               d   [        5       n[        R                  U [        R                  " U5      SS5      S-  nU[
        :w  a  SU4$  [        5       n[        R                  " [        5      Ul        [        R                  " U5      Ul
        [        Ul        [        R                  " 5       n[        5       n[        R!                  [        R                  " U5      ["        [        R                  " U5      [        R                  " U5      SU(       a  [$        O[&        [(        [*        [,        SS5      S-  n[        R/                  [        R                  " U5      5        U[
        :w  a  SU4$ UR0                  =(       d    SU4$ ! [        R/                  [        R                  " U5      5        f = f)as  
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_WithStatusrJ   rm   r&   rA   sizeofr2   pointerrD   r   rE   r   rI   rN   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-F +F/c                  ,    \ rS rSrSrSS jrSS jrSrg)ry   i7  zRUses ``LockFileEx`` to hard lock a byte range of the lock file on Windows systems.c                    [         er|   NotImplementedErrorr   s    r?   r   r   :      %%r>   c                    [         er|   r   r   s    r?   r   r   =  r   r>   r5   Nr   r   r5   r>   r?   ry   ry   7  s    `	&	&r>   )rp   r   r   bool)rp   r   r   r   )r   strr   r   r   z
int | None)r   r   r   r   r   ztuple[int, int])M
__future__r   r   r   
contextlibr   pathlibr   typingr   r   _apir	   _utilr
   r   platformrJ   rk   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   rA   rN   rS   r\   LONGr   restyperK   rI   rZ   
c_longlongrH   rL   argtypesr   LPCWSTRr   r   r   BOOLr   rj   rv   rt   rw   ry   r~   r   __all__r5   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#)==#9F 9&,mmJt&TI#T
&** 

V-- 

6++ 

f&& 

f&6&6 
 #+--Fx')*'(v(()$F  >F]]F22:'	?F22; +/F',2NN?,K+LF(+3>>F  (-5]]OF  )&.oo%6I"$,MMI!5=__fnnUpFq4rI((13;==I((0{#%I! $,==I {#'I# &.]]I"#;&2, &2P8t')V&, & r>   