| 1 |
# Copyright (c) 2018, Ansible Project |
| 2 |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 3 |
# SPDX-License-Identifier: BSD-2-Clause |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
# NOTE: |
| 9 |
# This has been vendored from ansible.module_utils.common.file. This code has been removed from there for ansible-core 2.16. |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
import fcntl |
| 14 |
import os |
| 15 |
import stat |
| 16 |
import time |
| 17 |
import typing as t |
| 18 |
from contextlib import contextmanager |
| 19 |
|
| 20 |
if t.TYPE_CHECKING: |
| 21 |
from io import TextIOWrapper |
| 22 |
|
| 23 |
|
| 24 |
class LockTimeout(Exception): |
| 25 |
pass |
| 26 |
|
| 27 |
|
| 28 |
class FileLock: |
| 29 |
""" |
| 30 |
Currently FileLock is implemented via fcntl.flock on a lock file, however this |
| 31 |
behaviour may change in the future. Avoid mixing lock types fcntl.flock, |
| 32 |
fcntl.lockf and module_utils.common.file.FileLock as it will certainly cause |
| 33 |
unwanted and/or unexpected behaviour |
| 34 |
""" |
| 35 |
|
| 36 |
def __init__(self) -> None: |
| 37 |
self.lockfd: TextIOWrapper | None = None |
| 38 |
|
| 39 |
@contextmanager |
| 40 |
def lock_file( |
| 41 |
self, path: os.PathLike, tmpdir: os.PathLike, lock_timeout: int | float | None = None |
| 42 |
) -> t.Generator[None]: |
| 43 |
""" |
| 44 |
Context for lock acquisition |
| 45 |
""" |
| 46 |
try: |
| 47 |
self.set_lock(path, tmpdir, lock_timeout) |
| 48 |
yield |
| 49 |
finally: |
| 50 |
self.unlock() |
| 51 |
|
| 52 |
def set_lock( |
| 53 |
self, path: os.PathLike, tmpdir: os.PathLike, lock_timeout: int | float | None = None |
| 54 |
) -> t.Literal[True]: |
| 55 |
""" |
| 56 |
Create a lock file based on path with flock to prevent other processes |
| 57 |
using given path. |
| 58 |
Please note that currently file locking only works when it is executed by |
| 59 |
the same user, for example single user scenarios |
| 60 |
|
| 61 |
:kw path: Path (file) to lock |
| 62 |
:kw tmpdir: Path where to place the temporary .lock file |
| 63 |
:kw lock_timeout: |
| 64 |
Wait n seconds for lock acquisition, fail if timeout is reached. |
| 65 |
0 = Do not wait, fail if lock cannot be acquired immediately, |
| 66 |
Default is None, wait indefinitely until lock is released. |
| 67 |
:returns: True |
| 68 |
""" |
| 69 |
lock_path = os.path.join(tmpdir, f"ansible-{os.path.basename(path)}.lock") |
| 70 |
l_wait = 0.1 |
| 71 |
|
| 72 |
self.lockfd = open(lock_path, "w") |
| 73 |
|
| 74 |
if lock_timeout is not None and lock_timeout <= 0: |
| 75 |
fcntl.flock(self.lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 76 |
os.chmod(lock_path, stat.S_IWRITE | stat.S_IREAD) |
| 77 |
return True |
| 78 |
|
| 79 |
if lock_timeout: |
| 80 |
e_secs: float = 0 |
| 81 |
while e_secs < lock_timeout: |
| 82 |
try: |
| 83 |
fcntl.flock(self.lockfd, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 84 |
os.chmod(lock_path, stat.S_IWRITE | stat.S_IREAD) |
| 85 |
return True |
| 86 |
except BlockingIOError: |
| 87 |
time.sleep(l_wait) |
| 88 |
e_secs += l_wait |
| 89 |
continue |
| 90 |
|
| 91 |
self.lockfd.close() |
| 92 |
raise LockTimeout(f"{lock_timeout} sec") |
| 93 |
|
| 94 |
fcntl.flock(self.lockfd, fcntl.LOCK_EX) |
| 95 |
os.chmod(lock_path, stat.S_IWRITE | stat.S_IREAD) |
| 96 |
|
| 97 |
return True |
| 98 |
|
| 99 |
def unlock(self) -> t.Literal[True]: |
| 100 |
""" |
| 101 |
Make sure lock file is available for everyone and Unlock the file descriptor |
| 102 |
locked by set_lock |
| 103 |
|
| 104 |
:returns: True |
| 105 |
""" |
| 106 |
if not self.lockfd: |
| 107 |
return True |
| 108 |
|
| 109 |
try: |
| 110 |
fcntl.flock(self.lockfd, fcntl.LOCK_UN) |
| 111 |
self.lockfd.close() |
| 112 |
except ValueError: # file wasn't opened, let context manager fail gracefully |
| 113 |
pass |
| 114 |
|
| 115 |
return True |