36
from __future__ import absolute_import
50
from .hooks import Hooks
51
from .i18n import gettext
48
from bzrlib.hooks import HookPoint, Hooks
54
51
class LockHooks(Hooks):
56
53
def __init__(self):
57
Hooks.__init__(self, "breezy.lock", "Lock.hooks")
60
"Called with a breezy.lock.LockResult when a physical lock is "
64
"Called with a breezy.lock.LockResult when a physical lock is "
68
"Called with a breezy.lock.LockResult when a physical lock is "
55
self.create_hook(HookPoint('lock_acquired',
56
"Called with a bzrlib.lock.LockResult when a physical lock is "
57
"acquired.", (1, 8), None))
58
self.create_hook(HookPoint('lock_released',
59
"Called with a bzrlib.lock.LockResult when a physical lock is "
60
"released.", (1, 8), None))
61
self.create_hook(HookPoint('lock_broken',
62
"Called with a bzrlib.lock.LockResult when a physical lock is "
63
"broken.", (1, 15), None))
72
66
class Lock(object):
92
86
def __repr__(self):
93
87
return '%s(%s, %s)' % (self.__class__.__name__,
94
self.lock_url, self.details)
97
class LogicalLockResult(object):
98
"""The result of a lock_read/lock_write/lock_tree_write call on lockables.
100
:ivar unlock: A callable which will unlock the lock.
103
def __init__(self, unlock, token=None):
108
return "LogicalLockResult(%s)" % (self.unlock)
113
def __exit__(self, exc_type, exc_val, exc_tb):
114
# If there was an error raised, prefer the original one
117
except BaseException:
88
self.lock_url, self.details)
123
91
def cant_unlock_not_held(locked_object):
124
92
"""An attempt to unlock failed because the object was not locked.
126
This provides a policy point from which we can generate either a warning or
94
This provides a policy point from which we can generate either a warning
129
97
# This is typically masking some other error and called from a finally
130
98
# block, so it's useful to have the option not to generate a new error
346
330
self._read_lock = None
349
334
_lock_classes.append(('fcntl', _fcntl_WriteLock, _fcntl_ReadLock))
337
if have_pywin32 and sys.platform == 'win32':
338
if os.path.supports_unicode_filenames:
339
# for Windows NT/2K/XP/etc
340
win32file_CreateFile = win32file.CreateFileW
343
win32file_CreateFile = win32file.CreateFile
345
class _w32c_FileLock(_OSLock):
347
def _open(self, filename, access, share, cflags, pymode):
348
self.filename = osutils.realpath(filename)
350
self._handle = win32file_CreateFile(filename, access, share,
351
None, win32file.OPEN_ALWAYS,
352
win32file.FILE_ATTRIBUTE_NORMAL, None)
353
except pywintypes.error, e:
354
if e.args[0] == winerror.ERROR_ACCESS_DENIED:
355
raise errors.LockFailed(filename, e)
356
if e.args[0] == winerror.ERROR_SHARING_VIOLATION:
357
raise errors.LockContention(filename, e)
359
fd = win32file._open_osfhandle(self._handle, cflags)
360
self.f = os.fdopen(fd, pymode)
368
class _w32c_ReadLock(_w32c_FileLock):
369
def __init__(self, filename):
370
super(_w32c_ReadLock, self).__init__()
371
self._open(filename, win32file.GENERIC_READ,
372
win32file.FILE_SHARE_READ, os.O_RDONLY, "rb")
374
def temporary_write_lock(self):
375
"""Try to grab a write lock on the file.
377
On platforms that support it, this will upgrade to a write lock
378
without unlocking the file.
379
Otherwise, this will release the read lock, and try to acquire a
382
:return: A token which can be used to switch back to a read lock.
384
# I can't find a way to upgrade a read lock to a write lock without
385
# unlocking first. So here, we do just that.
388
wlock = _w32c_WriteLock(self.filename)
389
except errors.LockError:
390
return False, _w32c_ReadLock(self.filename)
394
class _w32c_WriteLock(_w32c_FileLock):
395
def __init__(self, filename):
396
super(_w32c_WriteLock, self).__init__()
398
win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0,
401
def restore_read_lock(self):
402
"""Restore the original ReadLock."""
403
# For win32 we had to completely let go of the original lock, so we
404
# just unlock and create a new read lock.
406
return _w32c_ReadLock(self.filename)
409
_lock_classes.append(('pywin32', _w32c_WriteLock, _w32c_ReadLock))
352
412
if have_ctypes_win32:
353
from ctypes.wintypes import DWORD, LPWSTR
354
LPSECURITY_ATTRIBUTES = ctypes.c_void_p # used as NULL no need to declare
355
HANDLE = ctypes.c_int # rather than unsigned as in ctypes.wintypes
356
_function_name = "CreateFileW"
413
from ctypes.wintypes import DWORD, LPCSTR, LPCWSTR
414
LPSECURITY_ATTRIBUTES = ctypes.c_void_p # used as NULL no need to declare
415
HANDLE = ctypes.c_int # rather than unsigned as in ctypes.wintypes
416
if os.path.supports_unicode_filenames:
417
_function_name = "CreateFileW"
420
_function_name = "CreateFileA"
421
class LPTSTR(LPCSTR):
422
def __new__(cls, obj):
423
return LPCSTR.__new__(cls, obj.encode("mbcs"))
358
425
# CreateFile <http://msdn.microsoft.com/en-us/library/aa363858.aspx>
359
426
_CreateFile = ctypes.WINFUNCTYPE(
360
HANDLE, # return value
362
DWORD, # dwDesiredAccess
364
LPSECURITY_ATTRIBUTES, # lpSecurityAttributes
365
DWORD, # dwCreationDisposition
366
DWORD, # dwFlagsAndAttributes
367
HANDLE # hTemplateFile
427
HANDLE, # return value
429
DWORD, # dwDesiredAccess
431
LPSECURITY_ATTRIBUTES, # lpSecurityAttributes
432
DWORD, # dwCreationDisposition
433
DWORD, # dwFlagsAndAttributes
434
HANDLE # hTemplateFile
368
435
)((_function_name, ctypes.windll.kernel32))
370
437
INVALID_HANDLE_VALUE = -1