1
1
# Copyright (C) 2005, 2006 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41
from bzrlib.trace import mutter, note, warning
42
from bzrlib.errors import LockError
41
from bzrlib.errors import LockError, LockContention
42
from bzrlib.osutils import realpath
43
from bzrlib.trace import mutter
44
46
class _base_Lock(object):
45
48
def _open(self, filename, filemode):
47
50
self.f = open(filename, filemode)
80
79
class _fcntl_FileLock(_base_Lock):
84
84
fcntl.lockf(self.f, fcntl.LOCK_UN)
88
"""Clear the self.f attribute cleanly."""
88
93
class _fcntl_WriteLock(_fcntl_FileLock):
89
97
def __init__(self, filename):
90
98
# standard IO errors get exposed directly.
91
self._open(filename, 'wb')
99
self._open(filename, 'rb+')
100
self.filename = realpath(filename)
101
if self.filename in self.open_locks:
103
raise LockContention("Lock already held.")
104
# reserve a slot for this lock - even if the lockf call fails,
105
# at thisi point unlock() will be called, because self.f is set.
106
# TODO: make this fully threadsafe, if we decide we care.
107
self.open_locks[self.filename] = self.filename
93
fcntl.lockf(self.f, fcntl.LOCK_EX)
109
# LOCK_NB will cause IOError to be raised if we can't grab a
111
fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB)
94
112
except IOError, e:
113
if e.errno in (errno.EAGAIN, errno.EACCES):
114
# We couldn't grab the lock
95
116
# we should be more precise about whats a locking
96
117
# error and whats a random-other error
97
118
raise LockError(e)
121
del self.open_locks[self.filename]
99
125
class _fcntl_ReadLock(_fcntl_FileLock):
101
127
def __init__(self, filename):
102
128
# standard IO errors get exposed directly.
103
129
self._open(filename, 'rb')
105
fcntl.lockf(self.f, fcntl.LOCK_SH)
131
# LOCK_NB will cause IOError to be raised if we can't grab a
133
fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
106
134
except IOError, e:
107
135
# we should be more precise about whats a locking
108
136
# error and whats a random-other error
109
137
raise LockError(e)
111
143
WriteLock = _fcntl_WriteLock
112
144
ReadLock = _fcntl_ReadLock
150
184
class _w32c_WriteLock(_w32c_FileLock):
151
185
def __init__(self, filename):
152
_w32c_FileLock._lock(self, filename, 'wb',
186
_w32c_FileLock._lock(self, filename, 'rb+',
153
187
LOCK_EX + LOCK_NB)
157
190
WriteLock = _w32c_WriteLock
158
191
ReadLock = _w32c_ReadLock
181
215
class _msvc_ReadLock(_msvc_FileLock):
182
216
def __init__(self, filename):
183
_msvc_lock(self._open(filename, 'rb'), self.LOCK_SH)
217
_msvc_lock(self._open(filename, 'rb'),
218
self.LOCK_SH | self.LOCK_NB)
186
221
class _msvc_WriteLock(_msvc_FileLock):
187
222
def __init__(self, filename):
188
_msvc_lock(self._open(filename, 'wb'), self.LOCK_EX)
223
_msvc_lock(self._open(filename, 'rb+'),
224
self.LOCK_EX | self.LOCK_NB)
192
227
def _msvc_lock(f, flags):