M/e'dZddlZddlZddlmZddlmZddlmZddlm Z  ddl Z dZ ejeZGd d ZGd d ZGd deZGddeZdedefdZy#e $r ddl Z dZ YRwxYw)zZImplements file locks compatible with Linux and Windows for locking files and directories.N)Optional)errors) filesystem)osTFcHeZdZdZdeddfdZdefdZd dZd dZde fd Z y) LockFilea Platform independent file lock system. LockFile accepts a parameter, the path to a file acting as a lock. Once the LockFile, instance is created, the associated file is 'locked from the point of view of the OS, meaning that if another instance of Certbot try at the same time to acquire the same lock, it will raise an Exception. Calling release method will release the lock, and make it available to every other instance. Upon exit, Certbot will also release all the locks. This allows us to protect a file or directory from being concurrently accessed or modified by two Certbot instances. LockFile is platform independent: it will proceed to the appropriate OS lock mechanism depending on Linux or Windows. pathreturnNcp||_trtnt}|||_|j y)z Create a LockFile instance on the given file path, and acquire lock. :param str path: the path to the file that will hold a lock N)_path POSIX_MODE_UnixLockMechanism_WindowsLockMechanism_lock_mechanismacquire)selfr mechanisms 8/usr/lib/python3/dist-packages/certbot/_internal/lock.py__init__zLockFile.__init__$s+  *4&:O ( cdj|jj|j}|j r|dz }|S|dz }|S)Nz {0}({1}) z released>)format __class____name__r is_locked)rrepr_strs r__repr__zLockFile.__repr__/sN&&t~~'>'> K >>   #H  #Hrc8|jjy)z Acquire the lock on the file, forbidding any other Certbot instance to acquire it. :raises errors.LockError: if unable to acquire the lock N)rrrs rrzLockFile.acquire7s $$&rc8|jjy)zb Release the lock on the file, allowing any other Certbot instance to acquire it. N)rreleasers rr!zLockFile.release>s $$&rc6|jjS)zu Check if the file is currently locked. :return: True if the file is locked, False otherwise )rrrs rrzLockFile.is_lockedDs ##--//rr N) r __module__ __qualname____doc__strrrrr!boolrrrrrs<  S T #'' 040rrc8eZdZdeddfdZdefdZddZddZy) _BaseLockMechanismr r Nc ||_d|_y)zk Create a lock file mechanism for Unix. :param str path: the path to the lock file N)r _fd)rr s rrz_BaseLockMechanism.__init__Ms  "&rc|jduS)zvCheck if lock file is currently locked. :return: True if the lock file is locked :rtype: bool N)r-rs rrz_BaseLockMechanism.is_lockedUs xxt##rcyNr)rs rrz_BaseLockMechanism.acquire\ rcyr0r)rs rr!z_BaseLockMechanism.release_r1rr#) rr$r%r'rr(rrr!r)rrr+r+Ls+'S'T'$4$  rr+c@eZdZdZd dZdeddfdZdedefdZd dZ y) rz A UNIX lock file mechanism. This lock file is released when the locked file is closed or the process exits. It cannot be used to provide synchronization between threads. It is based on the lock_file package by Martin Horcicka. r Nc|jtj|jtj tj zd} |j||j|r||_|jt j| |jyy#|jt j|wwxYw)zAcquire the lock.N) r-ropenr rO_CREATO_WRONLY _try_lock _lock_successclose)rfds rrz_UnixLockMechanism.acquirejshhRZZ"++-EuMB !r"%%b)!DH88#HHRLhh88#HHRL$s )B(($C r<cV tj|tjtjzy#t$rg}|j t j t jfvr5tjd|jtjdd}~wwxYw)z Try to acquire the lock file without blocking. :param int fd: file descriptor of the opened file to lock (A lock on %s is held by another process./Another instance of Certbot is already running.N) fcntllockfLOCK_EXLOCK_NBIOErrorerrnoEACCESEAGAINloggerdebugr r LockError)rr<errs rr9z_UnixLockMechanism._try_lockxss   KKEMMEMM9 : yyU\\5<<88 GT&&'XYY   s58 B(A"B##B(c ddlm}ddlm} ||j}||}|j|jk(xr|j|jk(S#t$r(}|j t j k(rYd}~yd}~wwxYw)a Did we successfully grab the lock? Because this class deletes the locked file when the lock is released, it is possible another process removed and recreated the file between us opening the file and acquiring the lock. :param int fd: file descriptor of the opened file to lock :returns: True if the lock was successfully acquired :rtype: bool r)fstat)statNF) rrMrNr OSErrorrEENOENTst_devst_ino)rr<rMrNstat1rKstat2s rr:z _UnixLockMechanism._lock_successsw  $E b ||u||+L  0LL yyELL(  sA B %BBB cv tj|j|j t d tj |jd|_y#d|_wxYw#|j t d tj |jd|_w#d|_wxYwxYw)z)Remove, close, and release the lock file.NzError, self._fd is None.)rremover r- TypeErrorr;rs rr!z_UnixLockMechanism.releases IIdjj !xx :;; "4 xx :;; "4s.A,A A),B8B,$B8, B55B8r#) rr$r%r&rintr9r(r:r!r)rrrrcs: ! C D MMM6 rrc eZdZdZddZddZy)ra" A Windows lock file mechanism. By default on Windows, acquiring a file handler gives exclusive access to the process and results in an effective lock. However, it is possible to explicitly acquire the file handler in shared access in terms of read and write, and this is done by os.open and io.open in Python. So an explicit lock needs to be done through the call of msvcrt.locking, that will lock the first byte of the file. In theory, it is also possible to access a file in shared delete access, allowing other processes to delete an opened file. But this needs also to be done explicitly by all processes using the Windows low level APIs, and Python does not do it. As of Python 3.7 and below, Python developers state that deleting a file opened by a process from another process is not possible with os.open and io.open. Consequently, msvcrt.locking is sufficient to obtain an effective lock, and the race condition encountered on Linux is not possible on Windows, leading to a simpler workflow. Nc tjtjztjz}d} t j |j |d}tj|tjd||_y#ttf$ro}|rtj||jtjk7rtj!d|j t#j$dd}~wwxYw)zAcquire the lockNr5r>r?)rO_RDWRr7O_TRUNCrr6r msvcrtlockingLK_NBLCKrDrOr;rErFrHrIrrJr-)r open_moder<rKs rrz_WindowsLockMechanism.acquiresII *RZZ7  VY>B NN2v 2! V yyELL( LLCTZZ P""#TU U VsABDA*C==Dc |jstjdtj|jtj dt j|j t j|jd|_y#t$r(}tjt|Yd}~4d}~wwxYw#d|_wxYw)zRelease the lock.z%The lock has not been acquired first.r[N)r-rErrorr^r_LK_UNLCKrr;rVr rOrHrIr')res rr!z_WindowsLockMechanism.releases 88ll#JKK NN488V__a 8 HHTXX  % $**% DH  % SV$$ % DHs0A/C 2B C "CC C  C Cr#)rr$r%r&rr!r)rrrrs0rrdir_pathr cTttjj|dS)a3Place a lock file on the directory at dir_path. The lock file is placed in the root of dir_path with the name .certbot.lock. :param str dir_path: path to directory :returns: the locked LockFile object :rtype: LockFile :raises errors.LockError: if unable to acquire the lock z .certbot.lock)rrr join)rfs rlock_dirris BGGLL?; <rps` % J   8 $3030l  .R +R j:.:z=s=x=SJsA// A=<A=