1
# Copyright (C) 2005, 2006 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
41
from bzrlib import errors
42
from bzrlib.errors import LockError, LockContention
43
from bzrlib.osutils import realpath
44
from bzrlib.trace import mutter
47
class _base_Lock(object):
49
def _open(self, filename, filemode):
51
self.f = open(filename, filemode)
54
if e.errno != errno.ENOENT:
57
# maybe this is an old branch (before may 2005)
58
mutter("trying to create missing branch lock %r", filename)
60
self.f = open(filename, 'wb+')
65
from warnings import warn
66
warn("lock on %r not released" % self.f)
70
raise NotImplementedError()
73
############################################################
80
class _fcntl_FileLock(_base_Lock):
85
fcntl.lockf(self.f, fcntl.LOCK_UN)
89
"""Clear the self.f attribute cleanly."""
94
class _fcntl_WriteLock(_fcntl_FileLock):
98
def __init__(self, filename):
99
# standard IO errors get exposed directly.
101
self._open(filename, 'rb+')
103
if e.errno in (errno.EACCES, errno.EPERM):
104
raise errors.ReadOnlyLockError(e)
106
self.filename = realpath(filename)
107
if self.filename in self.open_locks:
109
raise LockContention("Lock already held.")
110
# reserve a slot for this lock - even if the lockf call fails,
111
# at thisi point unlock() will be called, because self.f is set.
112
# TODO: make this fully threadsafe, if we decide we care.
113
self.open_locks[self.filename] = self.filename
115
# LOCK_NB will cause IOError to be raised if we can't grab a
117
fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
119
if e.errno in (errno.EAGAIN, errno.EACCES):
120
# We couldn't grab the lock
122
# we should be more precise about whats a locking
123
# error and whats a random-other error
127
del self.open_locks[self.filename]
131
class _fcntl_ReadLock(_fcntl_FileLock):
135
def __init__(self, filename):
136
self._open(filename, 'rb')
138
# LOCK_NB will cause IOError to be raised if we can't grab a
140
fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
142
# we should be more precise about whats a locking
143
# error and whats a random-other error
150
WriteLock = _fcntl_WriteLock
151
ReadLock = _fcntl_ReadLock
156
import win32con, win32file, pywintypes
159
LOCK_SH = 0 # the default
160
LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
161
LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
163
class _w32c_FileLock(_base_Lock):
164
def _lock(self, filename, openmode, lockmode):
166
self._open(filename, openmode)
167
self.hfile = win32file._get_osfhandle(self.f.fileno())
168
overlapped = pywintypes.OVERLAPPED()
169
win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped)
178
overlapped = pywintypes.OVERLAPPED()
179
win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
186
class _w32c_ReadLock(_w32c_FileLock):
187
def __init__(self, filename):
188
_w32c_FileLock._lock(self, filename, 'rb',
191
class _w32c_WriteLock(_w32c_FileLock):
192
def __init__(self, filename):
193
_w32c_FileLock._lock(self, filename, 'rb+',
197
WriteLock = _w32c_WriteLock
198
ReadLock = _w32c_ReadLock
205
# Unfortunately, msvcrt.locking() doesn't distinguish between
206
# read locks and write locks. Also, the way the combinations
207
# work to get non-blocking is not the same, so we
208
# have to write extra special functions here.
211
class _msvc_FileLock(_base_Lock):
222
class _msvc_ReadLock(_msvc_FileLock):
223
def __init__(self, filename):
224
_msvc_lock(self._open(filename, 'rb'),
225
self.LOCK_SH | self.LOCK_NB)
228
class _msvc_WriteLock(_msvc_FileLock):
229
def __init__(self, filename):
230
_msvc_lock(self._open(filename, 'rb+'),
231
self.LOCK_EX | self.LOCK_NB)
234
def _msvc_lock(f, flags):
236
# Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
237
# according to the comments, LK_RLCK is open the lock for writing.
239
# Unfortunately, msvcrt.locking() also has the side effect that it
240
# will only block for 10 seconds at most, and then it will throw an
241
# exception, this isn't terrible, though.
248
fpos = os.lseek(fn, 0,0)
251
if flags & _msvc_FileLock.LOCK_SH:
252
if flags & _msvc_FileLock.LOCK_NB:
253
lock_mode = msvcrt.LK_NBLCK
255
lock_mode = msvcrt.LK_LOCK
256
elif flags & _msvc_FileLock.LOCK_EX:
257
if flags & _msvc_FileLock.LOCK_NB:
258
lock_mode = msvcrt.LK_NBRLCK
260
lock_mode = msvcrt.LK_RLCK
262
raise ValueError('Invalid lock mode: %r' % flags)
264
msvcrt.locking(fn, lock_mode, -1)
266
os.lseek(fn, fpos, 0)
278
fpos = os.lseek(fn, 0,0)
282
msvcrt.locking(fn, msvcrt.LK_UNLCK, -1)
284
os.lseek(fn, fpos, 0)
289
WriteLock = _msvc_WriteLock
290
ReadLock = _msvc_ReadLock
292
raise NotImplementedError("please write a locking method "
293
"for platform %r" % sys.platform)