o
    ôjì  ã                   @  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 d dlm	Z	m
Z
mZ d dlmZ d dlmZ d&dd„Zd'dd„Zd(dd„Zd)dd„Zd*dd„ZeedƒZd ed!< ejd"ko_ejejv Zd ed#< ejejv Zd ed$< g d%¢ZdS )+é    )ÚannotationsN)Úsuppress)ÚEACCESÚEIOÚEISDIR)ÚPath)ÚFinalÚfdÚintÚdataÚbytesÚreturnÚNonec                 C  sB   t |ƒ}|rt | |¡ }dkrttdƒ‚||d… }|sdS dS )a  
    Write the whole buffer to *fd*, looping over the short writes POSIX lets ``os.write`` make.

    A caller that stopped after one short write would leave a truncated marker behind. A peer that reads between two
    writes sees a prefix of the record; each caller has to tolerate that. Lock records need not survive a power loss, so
    we skip ``fsync``.

    :param fd: file descriptor open for writing.
    :param data: bytes to write in full.

    :raises OSError: if a write reports zero progress before the record is complete.

    r   z5os.write wrote 0 bytes before the record was completeN)Ú
memoryviewÚosÚwriteÚOSErrorr   )r	   r   Ú	remainingZwritten© r   úa/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/filelock/_util.pyÚ	write_all   s   
ýr   ÚfilenameÚstrc                 C  s¬   zt  | ¡}W n
 ty   Y dS w |jtj@ s=t j| t jt jt jv d�s=zt  | ¡ W n
 t	y6   Y dS w t
td| ƒ‚t |j¡rTtjdkrNt
td| ƒ‚ttd| ƒ‚dS )a  
    Raise an exception if attempting to open the file for writing would fail.

    Separates files that can never be written from files that are writable but currently locked.

    :param filename: file to check

    :raises OSError: as if the file was opened for writing.

    N)Zeffective_idszPermission deniedÚwin32zIs a directory)r   Úlstatr   Úst_modeÚstatÚS_IWUSRÚaccessÚW_OKÚsupports_effective_idsÚFileNotFoundErrorÚPermissionErrorr   ÚS_ISDIRÚsysÚplatformÚIsADirectoryErrorr   )r   Z	file_statr   r   r   Úraise_on_not_writable_file"   s&   ÿÿþ
ýr'   ú
Path | strc                 C  s   t | ƒjjddd� dS )zp
    Ensure the directory containing the file exists (create it if necessary).

    :param filename: file.

    T)ÚparentsÚexist_okN)r   ÚparentÚmkdir)r   r   r   r   Úensure_directory_existsJ   s   r-   Ú	lock_fileÚmtime_beforeÚfloatÚ
ino_beforec                 C  sÚ   t  | ¡ }j|ks|j|krdS | › dt  ¡ › dt d¡› �}t| ƒ |¡ t  |¡ }j|kr>|j|kr>t|ƒ 	¡  dS t
 |j¡ritrkttƒ� t  || ¡ t|ƒ 	¡  W d  ƒ dS 1 sbw   Y  dS dS dS )a‚  
    Remove the lock file a caller found stale with modification time *mtime_before* and inode *ino_before*.

    Right before renaming we ``lstat`` the lock again and leave it alone if a peer recreated it since the caller judged
    it stale, as Mercurial re-reads a lock under its break lock. We rename before unlinking so that, of several
    processes racing to break the same lock, one takes the file and the rest get ``OSError``. A newer modification time
    or another inode on the renamed file means a peer recreated the lock between that ``lstat`` and the rename, so we
    hard-link it back and drop the break name. ``link`` never replaces a name, so a marker a third process created after
    our rename wins and the live one stays under the break name. In the two syscalls between rename and link a third
    process can acquire beside the live holder, a soft lock by creating its marker or a StrictSoftFileLock by
    publishing its sentinel at the empty path. Without ``os.link``, or on a filesystem that refuses hard links, the live
    marker stays under the break name. We compare inodes as well as modification times because NFS and FAT store
    coarse modification times, and a peer that recreates the lock within that granularity leaves the old mtime on it.
    We call ``lstat`` to avoid following a symlink a peer swaps in after our stale check, and put back only a regular
    file because ``link`` would follow a symlink to its target.

    We add a random token to the break name so other processes cannot guess it and two breakers in one process do not
    share ``<lock>.break.<pid>``. With a shared name, the second breaker could rename a recreated live lock onto that
    path between our ``lstat`` and ``unlink``, and our ``unlink`` would delete it.

    :param lock_file: path to the lock file to break.
    :param mtime_before: modification time the caller saw when it judged the lock stale.
    :param ino_before: inode number the caller saw when it judged the lock stale.

    :raises OSError: if the rename, the re-check or the restore fails, for example because the file vanished or another
        user owns it in a sticky directory.

    Nz.break.Ú.é   )r   r   Úst_mtimeÚst_inoÚgetpidÚsecretsÚ	token_hexr   ÚrenameÚunlinkr   ÚS_ISREGr   Ú	_HAS_LINKr   ÚFileExistsErrorÚlink)r.   r/   r1   ÚcurrentZ
break_pathZst_afterr   r   r   Úbreak_lock_fileT   s   
"þÿr@   Únamec                C  s*   t r
t |d ¡ d S tj| d t d� d S )N)Úfollow_symlinks)Ú_SUPPORTS_UTIME_FDr   ÚutimeÚ_SUPPORTS_UTIME_NOFOLLOW)rA   r	   r   r   r   Útouch}   s   rF   r>   zFinal[bool]r<   r   rC   rE   )r@   r-   r'   rF   r   )r	   r
   r   r   r   r   )r   r   r   r   )r   r(   r   r   )r.   r   r/   r0   r1   r
   r   r   )rA   r   r	   r
   r   r   )Ú
__future__r   r   r7   r   r$   Ú
contextlibr   Úerrnor   r   r   Úpathlibr   Útypingr   r   r'   r-   r@   rF   Úhasattrr<   Ú__annotations__r%   rD   Úsupports_fdrC   Úsupports_follow_symlinksrE   Ú__all__r   r   r   r   Ú<module>   s$    


(


)