Only one lock file per process (lockf() works now)

lockf() is more portable than flock()
This commit is contained in:
Unrud 2016-05-22 09:47:36 +02:00
parent eb9218354c
commit 49bc0728e3

View File

@ -518,6 +518,7 @@ class Collection(BaseCollection):
return "" return ""
_lock = threading.Condition() _lock = threading.Condition()
_lock_file = None
_readers = 0 _readers = 0
_writer = False _writer = False
@ -542,34 +543,29 @@ class Collection(BaseCollection):
cls._lock.notify() cls._lock.notify()
else: else:
cls._writer = True cls._writer = True
if not cls._lock_file:
folder = os.path.expanduser( folder = os.path.expanduser(
cls.configuration.get("storage", "filesystem_folder")) cls.configuration.get("storage", "filesystem_folder"))
if not os.path.exists(folder): if not os.path.exists(folder):
os.makedirs(folder, exist_ok=True) os.makedirs(folder, exist_ok=True)
lock_path = os.path.join(folder, "Radicale.lock") lock_path = os.path.join(folder, "Radicale.lock")
lock_file = open(lock_path, "w+") cls._lock_file = open(lock_path, "w+")
# set access rights to a necessary minimum to prevent locking by # set access rights to a necessary minimum to prevent locking
# arbitrary users # by arbitrary users
try: try:
os.chmod(lock_path, stat.S_IWUSR | stat.S_IRUSR) os.chmod(lock_path, stat.S_IWUSR | stat.S_IRUSR)
except OSError: except OSError:
cls.logger.debug("Failed to set permissions on lock file") cls.logger.debug("Failed to set permissions on lock file")
if os.name == "nt": if os.name == "nt":
handle = msvcrt.get_osfhandle(lock_file.fileno()) handle = msvcrt.get_osfhandle(cls._lock_file.fileno())
flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0 flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0
overlapped = Overlapped() overlapped = Overlapped()
if not lock_file_ex(handle, flags, 0, 1, 0, overlapped): if not lock_file_ex(handle, flags, 0, 1, 0, overlapped):
cls.logger.debug("Locking not supported") cls.logger.debug("Locking not supported")
elif os.name == "posix": elif os.name == "posix":
operation = fcntl.LOCK_EX if mode == "w" else fcntl.LOCK_SH _cmd = fcntl.LOCK_EX if mode == "w" else fcntl.LOCK_SH
# According to documentation flock() is emulated with fcntl() on
# some platforms. fcntl() locks are not associated with an open
# file descriptor. The same file can be locked multiple times
# within the same process and if any fd of the file is closed,
# all locks are released.
# flock() does not work on NFS shares.
try: try:
fcntl.flock(lock_file.fileno(), operation) fcntl.lockf(cls._lock_file.fileno(), _cmd)
except OSError: except OSError:
cls.logger.debug("Locking not supported") cls.logger.debug("Locking not supported")
yield yield
@ -578,4 +574,7 @@ class Collection(BaseCollection):
cls._readers -= 1 cls._readers -= 1
else: else:
cls._writer = False cls._writer = False
if cls._readers == 0:
cls._lock_file.close()
cls._lock_file = None
cls._lock.notify() cls._lock.notify()