14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18
"""Locking using OS file locks or file existence.
19
20
Note: This method of locking is generally deprecated in favour of LockDir, but
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
174
140
self.f = open(self.filename, filemode)
177
143
if e.errno in (errno.EACCES, errno.EPERM):
178
144
raise errors.LockFailed(self.filename, str(e))
179
145
if e.errno != errno.ENOENT:
234
207
# LOCK_NB will cause IOError to be raised if we can't grab a
235
208
# lock right away.
236
209
fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
238
211
if e.errno in (errno.EAGAIN, errno.EACCES):
239
212
# We couldn't grab the lock
268
242
# LOCK_NB will cause IOError to be raised if we can't grab a
269
243
# lock right away.
270
244
fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
272
246
# we should be more precise about whats a locking
273
247
# error and whats a random-other error
274
248
raise errors.LockContention(self.filename, e)
321
296
if self.filename in _fcntl_WriteLock._open_locks:
322
297
raise AssertionError('file already locked: %r'
325
300
# See if we can open the file for writing. Another process might
326
301
# have a read lock. We don't use self._open() because we don't want
328
303
# done by _fcntl_ReadLock
330
305
new_f = open(self.filename, 'rb+')
332
307
if e.errno in (errno.EACCES, errno.EPERM):
333
308
raise errors.LockFailed(self.filename, str(e))
336
311
# LOCK_NB will cause IOError to be raised if we can't grab a
337
312
# lock right away.
338
313
fcntl.lockf(new_f, fcntl.LOCK_EX | fcntl.LOCK_NB)
340
315
# TODO: Raise a more specific error based on the type of error
341
316
raise errors.LockContention(self.filename, e)
342
317
_fcntl_WriteLock._open_locks.add(self.filename)
346
321
def restore_read_lock(self):
347
322
"""Restore the original ReadLock."""
348
# For fcntl, since we never released the read lock, just release
349
# the write lock, and return the original lock.
323
# For fcntl, since we never released the read lock, just release the
324
# write lock, and return the original lock.
350
325
fcntl.lockf(self.f, fcntl.LOCK_UN)
352
327
_fcntl_WriteLock._open_locks.remove(self.filename)
355
330
self._read_lock = None
358
334
_lock_classes.append(('fcntl', _fcntl_WriteLock, _fcntl_ReadLock))
361
337
if have_pywin32 and sys.platform == 'win32':
362
win32file_CreateFile = win32file.CreateFileW
338
if os.path.supports_unicode_filenames:
339
# for Windows NT/2K/XP/etc
340
win32file_CreateFile = win32file.CreateFileW
343
win32file_CreateFile = win32file.CreateFile
364
345
class _w32c_FileLock(_OSLock):
366
347
def _open(self, filename, access, share, cflags, pymode):
367
348
self.filename = osutils.realpath(filename)
369
self._handle = win32file_CreateFile(
370
filename, access, share, None, win32file.OPEN_ALWAYS,
350
self._handle = win32file_CreateFile(filename, access, share,
351
None, win32file.OPEN_ALWAYS,
371
352
win32file.FILE_ATTRIBUTE_NORMAL, None)
372
except pywintypes.error as e:
353
except pywintypes.error, e:
373
354
if e.args[0] == winerror.ERROR_ACCESS_DENIED:
374
355
raise errors.LockFailed(filename, e)
375
356
if e.args[0] == winerror.ERROR_SHARING_VIOLATION:
384
365
self._handle = None
386
368
class _w32c_ReadLock(_w32c_FileLock):
387
369
def __init__(self, filename):
388
370
super(_w32c_ReadLock, self).__init__()
389
371
self._open(filename, win32file.GENERIC_READ,
390
win32file.FILE_SHARE_READ, os.O_RDONLY, "rb")
372
win32file.FILE_SHARE_READ, os.O_RDONLY, "rb")
392
374
def temporary_write_lock(self):
393
375
"""Try to grab a write lock on the file.
408
390
return False, _w32c_ReadLock(self.filename)
409
391
return True, wlock
411
394
class _w32c_WriteLock(_w32c_FileLock):
412
395
def __init__(self, filename):
413
396
super(_w32c_WriteLock, self).__init__()
414
397
self._open(filename,
415
win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0,
398
win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0,
418
401
def restore_read_lock(self):
419
402
"""Restore the original ReadLock."""
423
406
return _w32c_ReadLock(self.filename)
425
409
_lock_classes.append(('pywin32', _w32c_WriteLock, _w32c_ReadLock))
428
412
if have_ctypes_win32:
429
from ctypes.wintypes import DWORD, LPWSTR
430
LPSECURITY_ATTRIBUTES = ctypes.c_void_p # used as NULL no need to declare
431
HANDLE = ctypes.c_int # rather than unsigned as in ctypes.wintypes
432
_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"))
434
425
# CreateFile <http://msdn.microsoft.com/en-us/library/aa363858.aspx>
435
426
_CreateFile = ctypes.WINFUNCTYPE(
436
HANDLE, # return value
438
DWORD, # dwDesiredAccess
440
LPSECURITY_ATTRIBUTES, # lpSecurityAttributes
441
DWORD, # dwCreationDisposition
442
DWORD, # dwFlagsAndAttributes
443
HANDLE # hTemplateFile
427
HANDLE, # return value
429
DWORD, # dwDesiredAccess
431
LPSECURITY_ATTRIBUTES, # lpSecurityAttributes
432
DWORD, # dwCreationDisposition
433
DWORD, # dwFlagsAndAttributes
434
HANDLE # hTemplateFile
444
435
)((_function_name, ctypes.windll.kernel32))
446
437
INVALID_HANDLE_VALUE = -1
459
450
def _open(self, filename, access, share, cflags, pymode):
460
451
self.filename = osutils.realpath(filename)
461
452
handle = _CreateFile(filename, access, share, None, OPEN_ALWAYS,
462
FILE_ATTRIBUTE_NORMAL, 0)
453
FILE_ATTRIBUTE_NORMAL, 0)
463
454
if handle in (INVALID_HANDLE_VALUE, 0):
464
455
e = ctypes.WinError()
465
456
if e.args[0] == ERROR_ACCESS_DENIED:
474
465
def unlock(self):
477
469
class _ctypes_ReadLock(_ctypes_FileLock):
478
470
def __init__(self, filename):
479
471
super(_ctypes_ReadLock, self).__init__()
480
472
self._open(filename, GENERIC_READ, FILE_SHARE_READ, os.O_RDONLY,
483
475
def temporary_write_lock(self):
484
476
"""Try to grab a write lock on the file.
503
495
def __init__(self, filename):
504
496
super(_ctypes_WriteLock, self).__init__()
505
497
self._open(filename, GENERIC_READ | GENERIC_WRITE, 0, os.O_RDWR,
508
500
def restore_read_lock(self):
509
501
"""Restore the original ReadLock."""
543
536
type_name = 'read'
545
538
type_name = 'write'
546
trace.note(gettext('{0!r} was {1} locked again'), self, type_name)
539
trace.note('%r was %s locked again', self, type_name)
547
540
self._prev_lock = lock_type
550
@contextlib.contextmanager
551
def write_locked(lockable):
552
lockable.lock_write()