
    Z1,j*                    j    S SK Jr  S SKrS SKrS SKrS SKJrJr  S SKJ	r	  S	S jr
S
S jrSS jr/ SQrg)    )annotationsN)EACCESEISDIR)Pathc                    [         R                  " U 5      nUR                  S:w  a  UR                  [        R
                  -  (       d  [        [        SU 5      e[        R                  " UR                  5      (       a6  [        R                  S:X  a  [        [        SU 5      e[        [        SU 5      egg! [         a     gf = f)a  
Raise an exception if attempting to open the file for writing would fail.

This is done so files that will never be writable can be separated from files that are writable but currently
locked.

:param filename: file to check

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

Nr   zPermission deniedwin32zIs a directory)osstatOSErrorst_mtimest_modeS_IWUSRPermissionErrorr   S_ISDIRsysplatformIsADirectoryErrorr   )filename	file_stats     d/home/rurouni/.local/share/pipx/venvs/huggingface-hub/lib/python3.13/site-packages/filelock/_util.pyraise_on_not_writable_filer   
   s    GGH%	 Q!!DLL0!&*=xHH<<	))**||w&%f.A8LL (0@(KK +	   s   B8 8
CCc                J    [        U 5      R                  R                  SSS9  g)zd
Ensure the directory containing the file exists (create it if necessary).

:param filename: file.

T)parentsexist_okN)r   parentmkdir)r   s    r   ensure_directory_existsr   (   s!     	Nt<    c                   U  S[         R                  " 5        3n[        U 5      R                  U5         [         R                  " U5      R
                  nX1:  a  g[        U5      R                  5         g! [         a     gf = f)u  
Atomically break a stale lock file that was judged stale at modification time *mtime_before*.

The file is renamed to a process-private name before being unlinked, so two processes breaking the same lock
cannot delete each other's work (only one rename of a given inode succeeds; the loser gets ``OSError``). After the
rename the modification time is re-checked: a value newer than *mtime_before* means a peer recreated the lock
between the stale decision and the rename, so we grabbed a live file and must abort, leaving the renamed file in
place rather than rolling back (a rollback rename is itself racy — same trade-off as the soft read/write marker
break). ``lstat`` is used so a hostile symlink swapped in after the decision is not followed.

:param lock_file: path to the lock file to break.
:param mtime_before: modification time observed when the lock was judged stale.

:raises OSError: if the rename fails (e.g. the file vanished or is not owned in a sticky directory).

z.break.N)r	   getpidr   renamelstatr   r   unlink)	lock_filemtime_before
break_pathmtime_afters       r   break_lock_filer(   2   su    " ;gbiik]3JO:&hhz*33 !	  s    A6 6
BB)r(   r   r   )r   strreturnNone)r   z
Path | strr*   r+   )r$   r)   r%   floatr*   r+   )
__future__r   r	   r
   r   errnor   r   pathlibr   r   r   r(   __all__ r   r   <module>r2      s.    " 	  
   L<=8r   