1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Locking using OS file locks or file existence.
20
Note: This method of locking is generally deprecated in favour of LockDir, but
21
is used to lock local WorkingTrees, and by some old formats. It's accessed
22
through Transport.lock_read(), etc.
24
This module causes two methods, lock() and unlock() to be defined in
25
any way that works on the current platform.
27
It is not specified whether these locks are reentrant (i.e. can be
28
taken repeatedly by a single process) or whether they exclude
29
different threads in a single process. That reentrancy is provided by
32
This defines two classes: ReadLock and WriteLock, which can be
33
implemented in different ways on different platforms. Both have an
45
from bzrlib.hooks import Hooks
48
class PhysicalLock(object):
49
"""Base class for external on-disk physical locks.
51
At present only LockDir descends from this, but all locks should.
53
:cvar hooks: Hook dictionary for operations on locks.
57
hooks['lock_acquired'] = []
58
hooks['lock_released'] = []
61
class LockResult(object):
62
"""Result of an operation on a lock; passed to a hook"""
64
def __init__(self, lock_url, details=None):
65
"""Create a lock result for lock with optional details about the lock."""
66
self.lock_url = lock_url
67
self.details = details
69
def __eq__(self, other):
70
return self.lock_url == other.lock_url and self.details == other.details
73
class _OSLock(object):
79
def _open(self, filename, filemode):
80
self.filename = osutils.realpath(filename)
82
self.f = open(self.filename, filemode)
85
if e.errno in (errno.EACCES, errno.EPERM):
86
raise errors.LockFailed(self.filename, str(e))
87
if e.errno != errno.ENOENT:
90
# maybe this is an old branch (before may 2005)
91
trace.mutter("trying to create missing lock %r", self.filename)
93
self.f = open(self.filename, 'wb+')
97
"""Clear the self.f attribute cleanly."""
104
from warnings import warn
105
warn("lock on %r not released" % self.f)
109
raise NotImplementedError()
118
import win32con, win32file, pywintypes, winerror, msvcrt
123
import ctypes, msvcrt
133
LOCK_SH = fcntl.LOCK_SH
134
LOCK_NB = fcntl.LOCK_NB
135
lock_EX = fcntl.LOCK_EX
138
class _fcntl_FileLock(_OSLock):
141
fcntl.lockf(self.f, fcntl.LOCK_UN)
145
class _fcntl_WriteLock(_fcntl_FileLock):
149
def __init__(self, filename):
150
super(_fcntl_WriteLock, self).__init__()
151
# Check we can grab a lock before we actually open the file.
152
self.filename = osutils.realpath(filename)
153
if self.filename in _fcntl_WriteLock._open_locks:
155
raise errors.LockContention(self.filename)
157
self._open(self.filename, 'rb+')
158
# reserve a slot for this lock - even if the lockf call fails,
159
# at thisi point unlock() will be called, because self.f is set.
160
# TODO: make this fully threadsafe, if we decide we care.
161
_fcntl_WriteLock._open_locks.add(self.filename)
163
# LOCK_NB will cause IOError to be raised if we can't grab a
165
fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
167
if e.errno in (errno.EAGAIN, errno.EACCES):
168
# We couldn't grab the lock
170
# we should be more precise about whats a locking
171
# error and whats a random-other error
172
raise errors.LockContention(e)
175
_fcntl_WriteLock._open_locks.remove(self.filename)
179
class _fcntl_ReadLock(_fcntl_FileLock):
183
def __init__(self, filename):
184
super(_fcntl_ReadLock, self).__init__()
185
self.filename = osutils.realpath(filename)
186
_fcntl_ReadLock._open_locks.setdefault(self.filename, 0)
187
_fcntl_ReadLock._open_locks[self.filename] += 1
188
self._open(filename, 'rb')
190
# LOCK_NB will cause IOError to be raised if we can't grab a
192
fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
194
# we should be more precise about whats a locking
195
# error and whats a random-other error
196
raise errors.LockContention(e)
199
count = _fcntl_ReadLock._open_locks[self.filename]
201
del _fcntl_ReadLock._open_locks[self.filename]
203
_fcntl_ReadLock._open_locks[self.filename] = count - 1
206
def temporary_write_lock(self):
207
"""Try to grab a write lock on the file.
209
On platforms that support it, this will upgrade to a write lock
210
without unlocking the file.
211
Otherwise, this will release the read lock, and try to acquire a
214
:return: A token which can be used to switch back to a read lock.
216
if self.filename in _fcntl_WriteLock._open_locks:
217
raise AssertionError('file already locked: %r'
220
wlock = _fcntl_TemporaryWriteLock(self)
221
except errors.LockError:
222
# We didn't unlock, so we can just return 'self'
227
class _fcntl_TemporaryWriteLock(_OSLock):
228
"""A token used when grabbing a temporary_write_lock.
230
Call restore_read_lock() when you are done with the write lock.
233
def __init__(self, read_lock):
234
super(_fcntl_TemporaryWriteLock, self).__init__()
235
self._read_lock = read_lock
236
self.filename = read_lock.filename
238
count = _fcntl_ReadLock._open_locks[self.filename]
240
# Something else also has a read-lock, so we cannot grab a
242
raise errors.LockContention(self.filename)
244
if self.filename in _fcntl_WriteLock._open_locks:
245
raise AssertionError('file already locked: %r'
248
# See if we can open the file for writing. Another process might
249
# have a read lock. We don't use self._open() because we don't want
250
# to create the file if it exists. That would have already been
251
# done by _fcntl_ReadLock
253
new_f = open(self.filename, 'rb+')
255
if e.errno in (errno.EACCES, errno.EPERM):
256
raise errors.LockFailed(self.filename, str(e))
259
# LOCK_NB will cause IOError to be raised if we can't grab a
261
fcntl.lockf(new_f, fcntl.LOCK_EX | fcntl.LOCK_NB)
263
# TODO: Raise a more specific error based on the type of error
264
raise errors.LockContention(e)
265
_fcntl_WriteLock._open_locks.add(self.filename)
269
def restore_read_lock(self):
270
"""Restore the original ReadLock."""
271
# For fcntl, since we never released the read lock, just release the
272
# write lock, and return the original lock.
273
fcntl.lockf(self.f, fcntl.LOCK_UN)
275
_fcntl_WriteLock._open_locks.remove(self.filename)
276
# Avoid reference cycles
277
read_lock = self._read_lock
278
self._read_lock = None
282
_lock_classes.append(('fcntl', _fcntl_WriteLock, _fcntl_ReadLock))
285
if have_pywin32 and sys.platform == 'win32':
286
LOCK_SH = 0 # the default
287
LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
288
LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
291
class _w32c_FileLock(_OSLock):
293
def _lock(self, filename, openmode, lockmode):
294
self._open(filename, openmode)
296
self.hfile = msvcrt.get_osfhandle(self.f.fileno())
297
overlapped = pywintypes.OVERLAPPED()
299
win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000,
301
except pywintypes.error, e:
303
if e.args[0] in (winerror.ERROR_LOCK_VIOLATION,):
304
raise errors.LockContention(filename)
305
## import pdb; pdb.set_trace()
309
raise errors.LockContention(e)
312
overlapped = pywintypes.OVERLAPPED()
314
win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
317
raise errors.LockContention(e)
320
class _w32c_ReadLock(_w32c_FileLock):
321
def __init__(self, filename):
322
super(_w32c_ReadLock, self).__init__()
323
self._lock(filename, 'rb', LOCK_SH + LOCK_NB)
325
def temporary_write_lock(self):
326
"""Try to grab a write lock on the file.
328
On platforms that support it, this will upgrade to a write lock
329
without unlocking the file.
330
Otherwise, this will release the read lock, and try to acquire a
333
:return: A token which can be used to switch back to a read lock.
335
# I can't find a way to upgrade a read lock to a write lock without
336
# unlocking first. So here, we do just that.
339
wlock = _w32c_WriteLock(self.filename)
340
except errors.LockError:
341
return False, _w32c_ReadLock(self.filename)
345
class _w32c_WriteLock(_w32c_FileLock):
346
def __init__(self, filename):
347
super(_w32c_WriteLock, self).__init__()
348
self._lock(filename, 'rb+', LOCK_EX + LOCK_NB)
350
def restore_read_lock(self):
351
"""Restore the original ReadLock."""
352
# For win32 we had to completely let go of the original lock, so we
353
# just unlock and create a new read lock.
355
return _w32c_ReadLock(self.filename)
358
_lock_classes.append(('pywin32', _w32c_WriteLock, _w32c_ReadLock))
361
if have_ctypes and sys.platform == 'win32':
362
# These constants were copied from the win32con.py module.
363
LOCKFILE_FAIL_IMMEDIATELY = 1
364
LOCKFILE_EXCLUSIVE_LOCK = 2
365
# Constant taken from winerror.py module
366
ERROR_LOCK_VIOLATION = 33
369
LOCK_EX = LOCKFILE_EXCLUSIVE_LOCK
370
LOCK_NB = LOCKFILE_FAIL_IMMEDIATELY
371
_LockFileEx = ctypes.windll.kernel32.LockFileEx
372
_UnlockFileEx = ctypes.windll.kernel32.UnlockFileEx
373
_GetLastError = ctypes.windll.kernel32.GetLastError
375
### Define the OVERLAPPED structure.
376
# http://msdn2.microsoft.com/en-us/library/ms684342.aspx
377
# typedef struct _OVERLAPPED {
378
# ULONG_PTR Internal;
379
# ULONG_PTR InternalHigh;
390
class _inner_struct(ctypes.Structure):
391
_fields_ = [('Offset', ctypes.c_uint), # DWORD
392
('OffsetHigh', ctypes.c_uint), # DWORD
395
class _inner_union(ctypes.Union):
396
_fields_ = [('anon_struct', _inner_struct), # struct
397
('Pointer', ctypes.c_void_p), # PVOID
400
class OVERLAPPED(ctypes.Structure):
401
_fields_ = [('Internal', ctypes.c_void_p), # ULONG_PTR
402
('InternalHigh', ctypes.c_void_p), # ULONG_PTR
403
('_inner_union', _inner_union),
404
('hEvent', ctypes.c_void_p), # HANDLE
407
class _ctypes_FileLock(_OSLock):
409
def _lock(self, filename, openmode, lockmode):
410
self._open(filename, openmode)
412
self.hfile = msvcrt.get_osfhandle(self.f.fileno())
413
overlapped = OVERLAPPED()
414
result = _LockFileEx(self.hfile, # HANDLE hFile
415
lockmode, # DWORD dwFlags
416
0, # DWORD dwReserved
417
0x7fffffff, # DWORD nNumberOfBytesToLockLow
418
0x00000000, # DWORD nNumberOfBytesToLockHigh
419
ctypes.byref(overlapped), # lpOverlapped
423
last_err = _GetLastError()
424
if last_err in (ERROR_LOCK_VIOLATION,):
425
raise errors.LockContention(filename)
426
raise errors.LockContention('Unknown locking error: %s'
430
overlapped = OVERLAPPED()
431
result = _UnlockFileEx(self.hfile, # HANDLE hFile
432
0, # DWORD dwReserved
433
0x7fffffff, # DWORD nNumberOfBytesToLockLow
434
0x00000000, # DWORD nNumberOfBytesToLockHigh
435
ctypes.byref(overlapped), # lpOverlapped
440
last_err = _GetLastError()
441
raise errors.LockContention('Unknown unlocking error: %s'
445
class _ctypes_ReadLock(_ctypes_FileLock):
446
def __init__(self, filename):
447
super(_ctypes_ReadLock, self).__init__()
448
self._lock(filename, 'rb', LOCK_SH + LOCK_NB)
450
def temporary_write_lock(self):
451
"""Try to grab a write lock on the file.
453
On platforms that support it, this will upgrade to a write lock
454
without unlocking the file.
455
Otherwise, this will release the read lock, and try to acquire a
458
:return: A token which can be used to switch back to a read lock.
460
# I can't find a way to upgrade a read lock to a write lock without
461
# unlocking first. So here, we do just that.
464
wlock = _ctypes_WriteLock(self.filename)
465
except errors.LockError:
466
return False, _ctypes_ReadLock(self.filename)
469
class _ctypes_WriteLock(_ctypes_FileLock):
470
def __init__(self, filename):
471
super(_ctypes_WriteLock, self).__init__()
472
self._lock(filename, 'rb+', LOCK_EX + LOCK_NB)
474
def restore_read_lock(self):
475
"""Restore the original ReadLock."""
476
# For win32 we had to completely let go of the original lock, so we
477
# just unlock and create a new read lock.
479
return _ctypes_ReadLock(self.filename)
482
_lock_classes.append(('ctypes', _ctypes_WriteLock, _ctypes_ReadLock))
485
if len(_lock_classes) == 0:
486
raise NotImplementedError(
487
"We must have one of fcntl, pywin32, or ctypes available"
488
" to support OS locking."
492
# We default to using the first available lock class.
493
_lock_type, WriteLock, ReadLock = _lock_classes[0]