1
# Copyright (C) 2005 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
20
This only does local locking using OS locks for now.
22
This module causes two methods, lock() and unlock() to be defined in
23
any way that works on the current platform.
25
It is not specified whether these locks are reentrant (i.e. can be
26
taken repeatedly by a single process) or whether they exclude
27
different threads in a single process.
29
Eventually we may need to use some kind of lock representation that
30
will work on a dumb filesystem without actual locking primitives.
32
This defines two classes: ReadLock and WriteLock, which can be
33
implemented in different ways on different platforms. Both have an
40
from trace import mutter, note, warning
41
from errors import LockError
43
class _base_Lock(object):
44
def _open(self, filename, filemode):
45
self.f = open(filename, filemode)
51
from warnings import warn
52
warn("lock on %r not released" % self.f)
56
raise NotImplementedError()
63
############################################################
70
class _fcntl_FileLock(_base_Lock):
74
fcntl.flock(self.f, fcntl.LOCK_UN)
79
class _fcntl_WriteLock(_fcntl_FileLock):
80
def __init__(self, filename):
82
fcntl.flock(self._open(filename, 'wb'), fcntl.LOCK_EX)
87
class _fcntl_ReadLock(_fcntl_FileLock):
88
def __init__(self, filename):
90
fcntl.flock(self._open(filename, 'rb'), fcntl.LOCK_SH)
94
WriteLock = _fcntl_WriteLock
95
ReadLock = _fcntl_ReadLock
99
import win32con, win32file, pywintypes
102
#LOCK_SH = 0 # the default
103
#LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
104
#LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
106
class _w32c_FileLock(_base_Lock):
107
def _lock(self, filename, openmode, lockmode):
109
self._open(filename, openmode)
110
self.hfile = win32file._get_osfhandle(self.f.fileno())
111
overlapped = pywintypes.OVERLAPPED()
112
win32file.LockFileEx(self.hfile, lockmode, 0, 0x7fff0000, overlapped)
118
overlapped = pywintypes.OVERLAPPED()
119
win32file.UnlockFileEx(self.hfile, 0, 0x7fff0000, overlapped)
127
class _w32c_ReadLock(_w32c_FileLock):
128
def __init__(self, filename):
129
_w32c_FileLock._lock(self, filename, 'rb', 0)
131
class _w32c_WriteLock(_w32c_FileLock):
132
def __init__(self, filename):
133
_w32c_FileLock._lock(self, filename, 'wb',
134
win32con.LOCKFILE_EXCLUSIVE_LOCK)
138
WriteLock = _w32c_WriteLock
139
ReadLock = _w32c_ReadLock
146
# Unfortunately, msvcrt.locking() doesn't distinguish between
147
# read locks and write locks. Also, the way the combinations
148
# work to get non-blocking is not the same, so we
149
# have to write extra special functions here.
152
class _msvc_FileLock(_base_Lock):
160
class _msvc_ReadLock(_msvc_FileLock):
161
def __init__(self, filename):
162
_msvc_lock(self._open(filename, 'rb'), self.LOCK_SH)
165
class _msvc_WriteLock(_msvc_FileLock):
166
def __init__(self, filename):
167
_msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
171
def _msvc_lock(f, flags):
173
# Unfortunately, msvcrt.LK_RLCK is equivalent to msvcrt.LK_LOCK
174
# according to the comments, LK_RLCK is open the lock for writing.
176
# Unfortunately, msvcrt.locking() also has the side effect that it
177
# will only block for 10 seconds at most, and then it will throw an
178
# exception, this isn't terrible, though.
185
fpos = os.lseek(fn, 0,0)
188
if flags & self.LOCK_SH:
189
if flags & self.LOCK_NB:
190
lock_mode = msvcrt.LK_NBLCK
192
lock_mode = msvcrt.LK_LOCK
193
elif flags & self.LOCK_EX:
194
if flags & self.LOCK_NB:
195
lock_mode = msvcrt.LK_NBRLCK
197
lock_mode = msvcrt.LK_RLCK
199
raise ValueError('Invalid lock mode: %r' % flags)
201
msvcrt.locking(fn, lock_mode, -1)
203
os.lseek(fn, fpos, 0)
215
fpos = os.lseek(fn, 0,0)
219
msvcrt.locking(fn, msvcrt.LK_UNLCK, -1)
221
os.lseek(fn, fpos, 0)
227
WriteLock = _msvc_WriteLock
228
ReadLock = _msvc_ReadLock
230
raise NotImplementedError("please write a locking method "
231
"for platform %r" % sys.platform)