mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Mordernize utils.lock
This commit is contained in:
parent
e7a906241c
commit
5fd8c81661
@ -1,9 +1,8 @@
|
|||||||
__license__ = 'GPL v3'
|
#!/usr/bin/env python2
|
||||||
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
|
# vim:fileencoding=utf-8
|
||||||
__docformat__ = 'restructuredtext en'
|
# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
'''
|
|
||||||
Secure access to locked files from multiple processes.
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
'''
|
|
||||||
|
|
||||||
import atexit
|
import atexit
|
||||||
import errno
|
import errno
|
||||||
@ -14,10 +13,11 @@ import time
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from calibre.constants import (
|
from calibre.constants import (
|
||||||
__appname__, fcntl, filesystem_encoding, islinux, isosx, iswindows
|
__appname__, fcntl, filesystem_encoding, islinux, isosx, iswindows, plugins
|
||||||
)
|
)
|
||||||
from calibre.utils.monotonic import monotonic
|
from calibre.utils.monotonic import monotonic
|
||||||
|
|
||||||
|
speedup = plugins['speedup'][0]
|
||||||
if iswindows:
|
if iswindows:
|
||||||
import msvcrt, win32file, pywintypes, winerror, win32api, win32event
|
import msvcrt, win32file, pywintypes, winerror, win32api, win32event
|
||||||
from calibre.constants import get_windows_username
|
from calibre.constants import get_windows_username
|
||||||
@ -28,8 +28,6 @@ else:
|
|||||||
|
|
||||||
def unix_open(path):
|
def unix_open(path):
|
||||||
flags = os.O_RDWR | os.O_CREAT
|
flags = os.O_RDWR | os.O_CREAT
|
||||||
from calibre.constants import plugins
|
|
||||||
speedup = plugins['speedup'][0]
|
|
||||||
has_cloexec = False
|
has_cloexec = False
|
||||||
if hasattr(speedup, 'O_CLOEXEC'):
|
if hasattr(speedup, 'O_CLOEXEC'):
|
||||||
try:
|
try:
|
||||||
@ -149,8 +147,7 @@ elif islinux:
|
|||||||
name = '%s-singleinstance-%s-%s' % (
|
name = '%s-singleinstance-%s-%s' % (
|
||||||
__appname__, (os.geteuid() if per_user else ''), name
|
__appname__, (os.geteuid() if per_user else ''), name
|
||||||
)
|
)
|
||||||
if not isinstance(name, bytes):
|
name = name.encode('utf-8')
|
||||||
name = name.encode('utf-8')
|
|
||||||
address = b'\0' + name.replace(b' ', b'_')
|
address = b'\0' + name.replace(b' ', b'_')
|
||||||
sock = socket.socket(family=socket.AF_UNIX)
|
sock = socket.socket(family=socket.AF_UNIX)
|
||||||
try:
|
try:
|
||||||
@ -177,7 +174,9 @@ else:
|
|||||||
for loc in locs:
|
for loc in locs:
|
||||||
if os.access(loc, os.W_OK | os.R_OK | os.X_OK):
|
if os.access(loc, os.W_OK | os.R_OK | os.X_OK):
|
||||||
return os.path.join(loc, ('.' if loc is home else '') + name)
|
return os.path.join(loc, ('.' if loc is home else '') + name)
|
||||||
raise EnvironmentError('Failed to find a suitable filesystem location for the lock file')
|
raise EnvironmentError(
|
||||||
|
'Failed to find a suitable filesystem location for the lock file'
|
||||||
|
)
|
||||||
|
|
||||||
def create_single_instance_mutex(name, per_user=True):
|
def create_single_instance_mutex(name, per_user=True):
|
||||||
from calibre.utils.ipc import eintr_retry_call
|
from calibre.utils.ipc import eintr_retry_call
|
||||||
@ -192,6 +191,7 @@ else:
|
|||||||
|
|
||||||
|
|
||||||
def singleinstance(name):
|
def singleinstance(name):
|
||||||
|
' Ensure that only a single process holding exists with the specified mutex key '
|
||||||
release_mutex = create_single_instance_mutex(name)
|
release_mutex = create_single_instance_mutex(name)
|
||||||
if release_mutex is None:
|
if release_mutex is None:
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user