Use winutil for lock.py

This commit is contained in:
Kovid Goyal 2020-10-15 21:59:46 +05:30
parent 8ef10231ae
commit fe40196c06
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -11,15 +11,15 @@ import time
from functools import partial from functools import partial
from calibre.constants import ( from calibre.constants import (
__appname__, fcntl, filesystem_encoding, islinux, ismacos, iswindows, plugins __appname__, fcntl, filesystem_encoding, islinux, ismacos, iswindows
) )
from calibre_extensions import speedup
from calibre.utils.monotonic import monotonic from calibre.utils.monotonic import monotonic
from calibre.utils.shared_file import raise_winerror
speedup = plugins['speedup'][0]
if iswindows: if iswindows:
import msvcrt, win32file, pywintypes, winerror, win32api, win32event import msvcrt
from calibre.constants import get_windows_username from calibre.constants import get_windows_username
from calibre_extensions import winutil
excl_file_mode = stat.S_IREAD | stat.S_IWRITE excl_file_mode = stat.S_IREAD | stat.S_IWRITE
else: else:
excl_file_mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH excl_file_mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
@ -50,26 +50,23 @@ def unix_retry(err):
def windows_open(path): def windows_open(path):
if isinstance(path, bytes): if isinstance(path, bytes):
path = os.fsdecode(path) path = os.fsdecode(path)
try: h = winutil.create_file(
h = win32file.CreateFileW( path,
path, winutil.GENERIC_READ |
win32file.GENERIC_READ | winutil.GENERIC_WRITE, # Open for reading and writing
win32file.GENERIC_WRITE, # Open for reading and writing 0, # Open exclusive
0, # Open exclusive winutil.OPEN_ALWAYS, # If file does not exist, create it
None, # No security attributes, ensures handle is not inherited by children winutil.FILE_ATTRIBUTE_NORMAL, # Normal attributes
win32file.OPEN_ALWAYS, # If file does not exist, create it )
win32file.FILE_ATTRIBUTE_NORMAL, # Normal attributes fd = msvcrt.open_osfhandle(int(h), 0)
None, # No template file ans = os.fdopen(fd, 'r+b')
) h.detach()
except pywintypes.error as err: return ans
raise_winerror(err)
fd = msvcrt.open_osfhandle(h.Detach(), 0)
return os.fdopen(fd, 'r+b')
def windows_retry(err): def windows_retry(err):
return err.winerror in ( return err.winerror in (
winerror.ERROR_SHARING_VIOLATION, winerror.ERROR_LOCK_VIOLATION winutil.ERROR_SHARING_VIOLATION, winutil.ERROR_LOCK_VIOLATION
) )
@ -126,21 +123,15 @@ def _clean_lock_file(file_obj):
if iswindows: if iswindows:
def create_single_instance_mutex(name, per_user=True): def create_single_instance_mutex(name, per_user=True):
mutexname = '{}-singleinstance-{}-{}'.format( mutexname = '{}-singleinstance-{}-{}'.format(
__appname__, (get_windows_username() if per_user else ''), name __appname__, (get_windows_username() if per_user else ''), name
) )
mutex = win32event.CreateMutex(None, False, mutexname) try:
if not mutex: mutex = winutil.create_mutex(mutexname, False)
except FileExistsError:
return return
err = win32api.GetLastError() return mutex.close
if err == winerror.ERROR_ALREADY_EXISTS:
# Close this handle other wise this handle will prevent the mutex
# from being deleted when the process that created it exits.
win32api.CloseHandle(mutex)
return
return partial(win32api.CloseHandle, mutex)
elif islinux: elif islinux: