a }|ägÏ'ã@sÎdZddlZddlZddlmZddlmZddlmZddlm Z z ddl Z Wne ynddl Z dZ Yn0dZ e e¡ZGd d „d ƒZGd d „d ƒZGd d„deƒZGdd„deƒZeedœdd„ZdS)zZImplements file locks compatible with Linux and Windows for locking files and directories.éN)ÚOptional)Úerrors)Ú filesystem)ÚosFTc@sXeZdZdZeddœdd„Zedœdd„Zddœd d „Zddœd d „Ze dœd d„Z dS)Ú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. N©ÚpathÚreturncCs(||_trtnt}||ƒ|_| ¡dS)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_mechanismÚacquire)ÚselfrZ mechanism©rú:/usr/lib/python3.9/site-packages/certbot/_internal/lock.pyÚ__init__$s  zLockFile.__init__©r cCs0d |jj|j¡}| ¡r$|d7}n|d7}|S)Nz {0}({1}) z released>)ÚformatÚ __class__Ú__name__r Ú is_locked)rÚrepr_strrrrÚ__repr__/s  zLockFile.__repr__cCs|j ¡dS)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)rr©rrrrr7szLockFile.acquirecCs|j ¡dS)zb Release the lock on the file, allowing any other Certbot instance to acquire it. N)rÚreleaserrrrr>szLockFile.releasecCs |j ¡S)zu Check if the file is currently locked. :return: True if the file is locked, False otherwise )rrrrrrrDszLockFile.is_locked) rÚ __module__Ú __qualname__Ú__doc__ÚstrrrrrÚboolrrrrrrs   rc@sFeZdZeddœdd„Zedœdd„Zddœdd „Zddœd d „ZdS) Ú_BaseLockMechanismNrcCs||_d|_dS)zk Create a lock file mechanism for Unix. :param str path: the path to the lock file N)r Ú_fd)rrrrrrMsz_BaseLockMechanism.__init__rcCs |jduS)zvCheck if lock file is currently locked. :return: True if the lock file is locked :rtype: bool N)r#rrrrrUsz_BaseLockMechanism.is_lockedcCsdS©Nrrrrrr\sz_BaseLockMechanism.acquirecCsdSr$rrrrrr_sz_BaseLockMechanism.release) rrrr rr!rrrrrrrr"Lsr"c@sLeZdZdZddœdd„Zeddœdd„Zeedœd d „Zddœd d „Z dS) r zø 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. Nrc Csr|jdurnt |jtjtjBd¡}z2| |¡| |¡r>||_W|jdurlt  |¡q|jdurjt  |¡0qdS)zAcquire the lock.Né€) r#rÚopenr rÚO_CREATÚO_WRONLYÚ _try_lockÚ _lock_successÚclose)rÚfdrrrrjs     ÿ z_UnixLockMechanism.acquire)r,r c Csnzt |tjtjB¡WnPtyh}z8|jtjtjfvrRt  d|j ¡t   d¡‚‚WYd}~n d}~00dS)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) ÚfcntlÚlockfZLOCK_EXZLOCK_NBÚOSErrorÚerrnoÚEACCESZEAGAINÚloggerÚdebugr rÚ LockError)rr,Úerrrrrr)xs z_UnixLockMechanism._try_lockc Csˆddlm}ddlm}z||jƒ}Wn@tyf}z(|jtjkrPWYd}~dS‚WYd}~n d}~00||ƒ}|j|jko†|j|jkS)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) rr8r9r r1r2ÚENOENTÚst_devÚst_ino)rr,r8r9Zstat1r7Zstat2rrrr*…s   z _UnixLockMechanism._lock_successcCs|zBt |j¡W|jdur"tdƒ‚zt |j¡Wd|_qxd|_0n4|jdurVtdƒ‚zt |j¡Wd|_nd|_00dS)z)Remove, close, and release the lock file.NzError, self._fd is None.)rÚremover r#Ú TypeErrorr+rrrrr s  û z_UnixLockMechanism.release) rrrrrÚintr)r!r*rrrrrr cs  r c@s,eZdZdZddœdd„Zddœdd„ZdS)r a  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 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. 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. Nrc CsžtjtjBtjB}d}z$t |j|d¡}t |tj d¡WnXt y’}z@|rXt  |¡|j t j krf‚t d|j¡t d¡‚WYd}~n d}~00||_dS)zAcquire the lockNr%ér-r.)rÚO_RDWRr'ÚO_TRUNCrr&r ÚmsvcrtÚlockingZLK_NBLCKr1r+r2r3r4r5rr6r#)rZ open_moder,r7rrrrÈs   z_WindowsLockMechanism.acquirec CsŠz||jst d¡‚t |jtjd¡t |j¡zt |j ¡Wn2t yr}zt   t |ƒ¡WYd}~n d}~00Wd|_nd|_0dS)zRelease the lock.z%The lock has not been acquired first.r@N)r#rÚErrorrCrDZLK_UNLCKrr+r=r r1r4r5r )rÚerrrràs  &z_WindowsLockMechanism.release)rrrrrrrrrrr ¸sr )Údir_pathr cCsttj |d¡ƒS)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)rGrrrÚlock_dirõsrI)rr2ZloggingÚtypingrZcertbotrZcertbot.compatrrr/Ú ImportErrorrCr Z getLoggerrr4rr"r r r rIrrrrÚs$        6U=