
     j8&                       U d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ dZded<    e ed	d
             ed      hz
  Zded<   d9dZd:dZej(                  dk(  rod dlZd dlmZ  ej.                  dd      Zded<   ej2                  gej4                  _        ej8                  ej4                  _        ej<                  ej8                  ej<                  gej>                  _        ej2                  ej>                  _        ej2                   ej@                  ejB                         ej@                  ejB                         ej@                  ejB                         ej@                  ejB                        gejD                  _        ej8                  ejD                  _        dZ#ded<   dZ$ded<   dZ%ded<   dZ&ded<   d;d Z'd<d!Z(nd;d"Z'ej(                  d#v rd$Z)ded%<   d=d&Z* e*       Z+ded'<   d<d(Z(nlej(                  d)k(  rYd dlZd dl,Z, ejZ                  dd*      Z.d+ed,<   d-Z/ded.<   d/Z0ded0<   d-Z1ded1<   d2Z2d3ed4<    e,jf                  e2      Z4ded5<   d<d6Z(nd<d7Z(g d8Z5y)>    )annotationsN)EPERMESRCH)Path)Final   z
Final[int]_HOST_NAME_LIMIT!      ?zFinal[frozenset[int]]_HOST_NAME_VERBATIMc                     d} t        j                         j                  dd      D ]@  }|t        v rt	        |      nd|d}t        |       t        |      z   t        kD  r n| |z  } B | xs dS )a  
    The hostname recorded alongside an owner, so a marker written on another machine is never probed here.

    Marker formats here hold the field to printable non-space ASCII, while ``socket.gethostname()`` reports whatever
    the kernel stores: a space, a newline that forges an extra marker line, or a byte no codec encodes, which Python
    hands back as a surrogate. A holder that publishes one raw writes a marker it cannot read back, and loses the lock
    to the first peer that ages it out as malformed. ``?`` is illegal in a hostname, so escaping it along with every
    out-of-grammar byte as ``?<hex>`` leaves a real name untouched and still keeps two hosts apart, which
    :func:`owner_is_stale` relies on to refuse to probe a foreign PID.
     zutf-8surrogateescaper   02x)socketgethostnameencoder   chrlenr	   )namebytepieces      ^/home/min/hermes_workspace/demo_review/venv/lib/python3.12/site-packages/filelock/_identity.py	host_namer      st     D""$++G5FG!%88D	$snt9s5z!$44	 H ;3    c                h    |t               k7  ryt        |       sy|yt        |       }|duxr ||k7  S )a  
    Whether the recorded owner is provably gone, so reclaiming its marker cannot detach a live holder.

    Fail closed: return ``True`` only when this process can prove the exact recorded owner is dead. A marker from
    another host cannot be probed; a live PID whose start token still matches, or whose token cannot be read, is the
    holder or is indistinguishable from it; a live PID whose start token differs is a recycled PID, so the process that
    wrote the marker is gone. PostgreSQL, Qt ``QLockFile`` and Mercurial all break a stale lock only on proof of death
    and treat an unreadable or foreign owner as still holding.
    FTN)r   process_aliveprocess_start_token)pidhostnamestart_tokencurrents       r   owner_is_staler$   %   sD     9;!#&G$97k#99r   win32)wintypeskernel32T)use_last_errorzFinal[ctypes.WinDLL]	_KERNEL32i   _WIN_SYNCHRONIZEi   &_WIN_PROCESS_QUERY_LIMITED_INFORMATIONW   _WIN_ERROR_INVALID_PARAMETERFzFinal[bool]_WIN_INHERIT_HANDLEc                    t         j                  t        t        |       }|rt         j	                  |       yt        j                         t        k7  S )zSWhether a process with this PID exists, treating an access denial as proof it does.T)r)   OpenProcessr*   r.   CloseHandlectypesget_last_errorr-   )r    handles     r   r   r   P   sA    &&'79LcR!!&)$$&*FFFr   c           	        t         j                  t        t        |       }|syd t	        d      D        \  }}}}	 t         j                  |t        j                  |      t        j                  |      t        j                  |      t        j                  |            s	 t         j                  |       y	 t         j                  |       |j                  dz  |j                  z  S # t         j                  |       w xY w)zXThe process creation FILETIME as a 100ns tick count, or ``None`` when it cannot be read.Nc              3  D   K   | ]  }t        j                           y w)N)r&   FILETIME).0_s     r   	<genexpr>z&process_start_token.<locals>.<genexpr>]   s     6]T\qx7H7H7JT\s           )r)   r0   r+   r.   rangeGetProcessTimesr2   byrefr1   dwHighDateTimedwLowDateTime)r    r4   creation	exit_timekernel_time	user_times         r   r   r   X   s    &&'MObdgh6]TYZ[T\6]3)[)
	*,,X&Y'[)Y' !!&) !!&)''2-1G1GGG !!&)s   A%C) )D c                    	 t        j                  | d       y# t        $ r6}|j                  t        k(  rY d}~y|j                  t
        k(  rY d}~y d}~ww xY w)z_Whether a process with this PID exists, treating an access denial (``EPERM``) as proof it does.r   NFT)oskillOSErrorerrnor   r   )r    errors     r   r   r   m   sL    	GGCO   	{{e#{{e#	s    	AAAAA>   linuxandroid   _STARTTIME_INDEXc                     	 t        d      j                  d      } t        | j                         j	                  dd      d      S # t
        t        f$ r Y yw xY w)Nz/proc/sys/kernel/random/boot_idascii)encoding-r      r   )r   	read_textintstripreplacerI   
ValueError)boot_ids    r   _read_boot_idr[      sX    @AKKU\K]7==?223;R@@Z( s   AA AA_BOOT_IDc                (   	 t        d|  d      j                         }||j                  d      dz   d j	                         }t        |      t        k  ry	 t        |t                 }t        dz  |z  S # t        $ r Y yw xY w# t        $ r Y yw xY w)zeThe ``/proc/<pid>/stat`` ``starttime`` folded with the boot id, or ``None`` when the process is gone.z/proc/z/statN   )   @   )
r   
read_bytesrI   rfindsplitr   rO   rV   rY   r\   )r    datafields	starttimes       r   r   r      s    fSE/0;;= $**T*Q.01779F6{..'7 89	 Ni//    s#   A6 B 6	BB	BBdarwin)	use_errnozFinal[ctypes.CDLL]_LIBCr_   	_CTL_KERN   
_KERN_PROC_KERN_PROC_PIDz<qiz
Final[str]_TIMEVAL_AT_ZERO_TIMEVAL_SIZEc                   t        j                  dz  t        t        t        |       }t        j
                  d      }t        j                  |ddt        j                  |      dd      dk7  ryt        j                  |j                  z         }t        j                  |d|t        j                  |      dd      dk7  s|j                  t        k  ryt        j                  t        |j                  d      \  }}|dz  |z   S )zcThe process start time in microseconds from ``sysctl(KERN_PROC_PID)``, or ``None`` when it is gone.r;   r   Ni@B )r2   c_intrj   rl   rm   c_size_tri   sysctlr?   c_charvaluero   structunpack_fromrn   raw)r    miblengthbuffersecondsmicrosecondss         r   r   r      s    <<!#Y
NCPC__Q'F ||CD&,,v*>aHAMmmfll25F||CFFLL,@$JaOSYS_S_boSo$*$6$67GUV$W!G\Y&55r   c                     ~ y)z_No proven start-time source, so the owner carries no token and liveness rests on the PID alone.N )r    s    r   r   r      s    r   )r   r$   r   r   )returnstr)r    rV   r!   r   r"   
int | Noner   bool)r    rV   r   r   )r    rV   r   r   )r   rV   )6
__future__r   rG   r   sysrJ   r   r   pathlibr   typingr   r	   __annotations__	frozensetr=   ordr   r   r$   platformr2   r&   WinDLLr)   HANDLEr1   argtypesBOOLrestypeDWORDr0   POINTERr7   r>   r*   r+   r-   r.   r   r   rO   r[   r\   rv   CDLLri   rj   rl   rm   rn   calcsizero   __all__r   r   r   <module>r      sE   " 	  
     # * "-6uT47H-ISQTXJ-V * V*:( <<7&3fmmJt&TI#T&.oo%6I"$,MMI!&.nnhmmX^^%TI"$,OOI!x(()x(()x(()x(()*I& )1I%#+j+9?*J?/1 *1',,GH*
 ||++
 (**)
	  -*.	0  
	!$/FKK$E!E !	:!!#
J#%&
& (-*,$3FOO4D$EzE	6 	r   