o
    ôjÃ7  ã                   @  sÀ  U d dl mZ d dl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ded< dZded< dZded< dZded< dVdd„ZdVdd„ZdWd!d"„ZdWd#d$„Zejd%krÝd dlZd d&lmZ ejd'd(d)�Zd*ed+< ejgej _!ej"ej _#ej$ej"ej$gej%_!ejej%_#eje &ej'¡e &ej'¡e &ej'¡e &ej'¡gej(_!ej"ej(_#d,Z)ded-< d.Z*ded/< d0Z+ded1< d2Z,d3ed4< dXd5d6„Z-dYd7d8„Z.nrdXd9d6„Z-ejd:v �r
d;Z/ded<< dZd=d>„Z0e0ƒ Z1ded?< dYd@d8„Z.d[dAdB„Z2d\dDdE„Z3nEejdFk�rJd dlZd dl4Z4ej5dd(dG�Z6dHedI< dJZ7dedK< dLZ8dedM< dJZ9dedN< dOZ:dedP< e4 ;e:¡Z<dedQ< dYdRd8„Z.ndYdSd8„Z.ejd:v�rZd[dTdB„Z2g dU¢Z=dS )]é    )ÚannotationsN)ÚEPERMÚESRCH)ÚPath)ÚFinaléý   z
Final[int]Ú_HOST_NAME_LIMITé!   é   ú?zFinal[frozenset[int]]Ú_HOST_NAME_VERBATIMé   Ú_HOST_NAME_DIGEST_LENl   üÿ_ Ú_INITIAL_PID_NAMESPACEz?pidns-z
Final[str]Ú_NAMESPACE_MARKÚreturnÚstrc                  C  sz   d  dd„ t ¡  dd¡D ƒ¡tƒ  } t| ƒtkr9tt d }| d|… › dt 	| |d…  ¡ ¡ 
¡ dt… › �} | p<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.

    Linux appends the PID namespace, as Mercurial's lock prefix does. Two containers in one pod share the hostname and
    the boot id but not the PID namespace, so a PID recorded in one names another process, or none, in the other. The
    suffix starts with a ``?`` no escape can produce, so ``a`` in a namespace never reads as a host literally named
    ``a?pidns-...``. The initial namespace adds nothing, which keeps a host's name what older releases recorded.

    An over-long name ends in a digest of the part cut to fit the limit. A bare cut gives every name that shares the
    kept prefix one identity, and :func:`owner_is_stale` would then probe a local PID for another host's holder.
    Ú c                 s  s,   � | ]}|t v rt|ƒnd |d›�V  qdS )r   Z02xN)r   Úchr)Ú.0Zbyte© r   úe/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/filelock/_identity.pyÚ	<genexpr>+   s
   € ÿ
ÿzhost_name.<locals>.<genexpr>zutf-8Úsurrogateescapeé   Nú-r   )ÚjoinÚsocketÚgethostnameÚencodeÚ_pid_namespace_suffixÚlenr   r   ÚhashlibÚsha256Ú	hexdigest)ÚnameÚkeepr   r   r   Ú	host_name   s   
þüÿ4r'   c                  C  sB   z	t dƒ ¡ j} W n
 ty   Y dS w | tkrdS t› | d›�S )Nz/proc/self/ns/pidr   Úx)r   ÚstatÚst_inoÚOSErrorr   r   )Ú	namespacer   r   r   r    8   s   ÿr    ÚpidÚintÚhostnameÚstart_tokenú
int | NoneÚboolc                 C  sF   |t ƒ krdS t| ƒr|du st| ƒd|hv rdS t|v p"t| |ƒ 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.
    FN)r'   Úprocess_aliveÚprocess_start_tokenr   Ú_owner_runs_elsewhere©r-   r/   r0   r   r   r   Úowner_is_stale@   s
   

 r7   c                 C  s.   | t  ¡ ks|tƒ krdS |du p|t| ƒkS )a–  
    Whether the recorded owner is this process, so a marker a recycled PID left behind never reads as ours.

    Fail closed: a recorded start token must match this process's. One that differs was written by an earlier process
    that reused this PID, and when this process cannot read its own token nothing proves the marker is ours. A marker
    without a token falls back to the PID and hostname.
    FN)ÚosÚgetpidr'   r4   r6   r   r   r   Úowner_is_current_processR   s   r:   Úwin32)ÚwintypesZkernel32T)Zuse_last_errorzFinal[ctypes.WinDLL]Ú	_KERNEL32i   Ú_WIN_SYNCHRONIZEi   Ú&_WIN_PROCESS_QUERY_LIMITED_INFORMATIONéW   Ú_WIN_ERROR_INVALID_PARAMETERFzFinal[bool]Ú_WIN_INHERIT_HANDLEc                 C  s,   t  tt| ¡}|rt  |¡ dS t ¡ tkS )zSWhether a process with this PID exists, treating an access denial as proof it does.T)r=   ÚOpenProcessr>   rB   ÚCloseHandleÚctypesZget_last_errorrA   )r-   Úhandler   r   r   r3   v   s
   
r3   c              	   C  s–   t  tt| ¡}|sdS dd„ tdƒD ƒ\}}}}z$t  |t |¡t |¡t |¡t |¡¡s6W t  |¡ dS W t  |¡ nt  |¡ w |j	d> |j
B S )zXThe process creation FILETIME as a 100ns tick count, or ``None`` when it cannot be read.Nc                 s  s   � | ]}t  ¡ V  qd S ©N)r<   ÚFILETIME)r   Ú_r   r   r   r   ƒ   s   € z&process_start_token.<locals>.<genexpr>é   é    )r=   rC   r?   rB   ÚrangeÚGetProcessTimesrE   ÚbyrefrD   ZdwHighDateTimeZdwLowDateTime)r-   rF   ZcreationZ	exit_timeZkernel_timeZ	user_timer   r   r   r4   ~   s"   û÷	r4   c              
   C  s^   z	t  | d¡ W dS  ty. } z|jtkrW Y d}~dS |jtkr)W Y d}~dS ‚ d}~ww )z_Whether a process with this PID exists, treating an access denial (``EPERM``) as proof it does.r   NFT)r8   Úkillr+   Úerrnor   r   )r-   Úerrorr   r   r   r3   “   s   ú

€û>   ZandroidÚlinuxé   Ú_STARTTIME_INDEXc               	   C  sB   zt dƒjdd�} t|  ¡  dd¡dƒW S  ttfy    Y dS w )Nz/proc/sys/kernel/random/boot_idÚascii)Úencodingr   r   é   r   )r   Ú	read_textr.   ÚstripÚreplacer+   Ú
ValueError)Zboot_idr   r   r   Ú_read_boot_id¦   s   ÿr\   Ú_BOOT_IDc                 C  sŠ   zt d| › d�ƒ ¡ }W n
 ty   Y dS w || d¡d d…  ¡ }t|ƒtkr,dS zt|t ƒ}W n
 ty>   Y dS w t	d> |B S )zeThe ``/proc/<pid>/stat`` ``starttime`` folded with the boot id, or ``None`` when the process is gone.ú/proc/z/statNó   )r   é@   )
r   Ú
read_bytesr+   ÚrfindÚsplitr!   rT   r.   r[   r]   )r-   ÚdataÚfieldsZ	starttimer   r   r   r4   ´   s   ÿÿc                   sz   ˆdu s
ˆd? t krdS z&t d¡�}t‡ ‡‡fdd„|D ƒƒW  d  ƒ W S 1 s+w   Y  W dS  ty<   Y dS w )aŽ  
            Whether the owner runs in another PID namespace as *pid*.

            An older release in a container sharing this hostname recorded its in-container PID, which means nothing
            here. starttime and the boot id are kernel-wide, so a task with that start token whose innermost PID is
            *pid* is the owner; one that merely started in the same tick is not.
            Nr`   Fz/procc                 3  sD   � | ]}|j  ¡ ot|j ƒ ‰ˆ kotˆƒˆkotˆƒˆ kV  qd S rG   )r%   Úisdigitr.   r4   Ú_innermost_pid)r   Úentry©r-   r0   Útaskr   r   r   Ð   s   € 
üÿ
þ

üz(_owner_runs_elsewhere.<locals>.<genexpr>)r]   r8   ÚscandirÚanyr+   )r-   r0   Úentriesr   ri   r   r5   Ä   s   û(ÿþr5   rj   c                 C  sL   zt d| › d�ƒjddd�}W n
 ty   Y d S w tdd„ | ¡ D ƒd ƒS )Nr^   z/statusrU   rZ   )rV   Úerrorsc                 s  s*   � | ]}|  d ¡rt| ¡ d ƒV  qdS )zNSpid:éÿÿÿÿN)Ú
startswithr.   rc   )r   Úliner   r   r   r   á   s   €( z!_innermost_pid.<locals>.<genexpr>)r   rX   r+   ÚnextÚ
splitlines)rj   Ústatusr   r   r   rg   Û   s   ÿþrg   Údarwin)Z	use_errnozFinal[ctypes.CDLL]Ú_LIBCr   Ú	_CTL_KERNé   Ú
_KERN_PROCÚ_KERN_PROC_PIDz<qiÚ_TIMEVAL_AT_ZEROÚ_TIMEVAL_SIZEc                 C  sš   t jd ttt| ƒ}t  d¡}t |ddt  |¡dd¡dkr dS t j	|j
 ƒ }t |d|t  |¡dd¡dks;|j
tk r=dS t t|jd¡\}}|d | S )zcThe process start time in microseconds from ``sysctl(KERN_PROC_PID)``, or ``None`` when it is gone.rJ   r   Ni@B )rE   Zc_intrw   ry   rz   Zc_size_trv   ZsysctlrN   Zc_charÚvaluer|   ÚstructÚunpack_fromr{   Úraw)r-   ZmibÚlengthÚbufferÚsecondsÚmicrosecondsr   r   r   r4   ò   s   
(c                 C  s   ~ dS )z_No proven start-time source, so the owner carries no token and liveness rests on the PID alone.Nr   )r-   r   r   r   r4     s   c                 C  s   ~ ~dS )zbWithout Linux PID namespaces, a PID that is gone or recycled leaves its owner nowhere else to run.Fr   )r-   r0   r   r   r   r5   
  s   )r'   r:   r7   r3   r4   )r   r   )r-   r.   r/   r   r0   r1   r   r2   )r-   r.   r   r2   )r-   r.   r   r1   )r   r.   )r-   r.   r0   r1   r   r2   )rj   r.   r   r1   )>Ú
__future__r   r"   r8   r   ÚsysrP   r   r   Úpathlibr   Útypingr   r   Ú__annotations__Ú	frozensetrL   Úordr   r   r   r   r'   r    r7   r:   ÚplatformrE   r<   ZWinDLLr=   ZHANDLErD   ÚargtypesZBOOLZrestypeZDWORDrC   ZPOINTERrH   rM   r>   r?   rA   rB   r3   r4   rT   r\   r]   r5   rg   r~   ZCDLLrv   rw   ry   rz   r{   Úcalcsizer|   Ú__all__r   r   r   r   Ú<module>   sx     

!








û








