/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008, 2009 Canonical Ltd
2
2
#
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
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
16
16
 
17
 
from .lazy_import import lazy_import
 
17
from cStringIO import StringIO
 
18
 
 
19
from bzrlib.lazy_import import lazy_import
18
20
lazy_import(globals(), """
19
 
from breezy import (
 
21
import codecs
 
22
import warnings
 
23
 
 
24
from bzrlib import (
20
25
    counted_lock,
 
26
    errors,
21
27
    lock,
 
28
    osutils,
22
29
    transactions,
23
30
    urlutils,
24
31
    )
25
32
""")
26
33
 
27
 
from . import (
28
 
    errors,
29
 
    )
30
 
from .decorators import (
31
 
    only_raises,
32
 
    )
 
34
from bzrlib.decorators import (
 
35
    needs_read_lock,
 
36
    needs_write_lock,
 
37
    )
 
38
from bzrlib.symbol_versioning import (
 
39
    deprecated_in,
 
40
    deprecated_method,
 
41
    )
 
42
 
 
43
 
 
44
# XXX: The tracking here of lock counts and whether the lock is held is
 
45
# somewhat redundant with what's done in LockDir; the main difference is that
 
46
# LockableFiles permits reentrancy.
 
47
 
 
48
class _LockWarner(object):
 
49
    """Hold a counter for a lock and warn if GCed while the count is >= 1.
 
50
 
 
51
    This is separate from LockableFiles because putting a __del__ on
 
52
    LockableFiles can result in uncollectable cycles.
 
53
    """
 
54
 
 
55
    def __init__(self, repr):
 
56
        self.lock_count = 0
 
57
        self.repr = repr
 
58
 
 
59
    def __del__(self):
 
60
        if self.lock_count >= 1:
 
61
            # There should have been a try/finally to unlock this.
 
62
            warnings.warn("%r was gc'd while locked" % self.repr)
33
63
 
34
64
 
35
65
class LockableFiles(object):
46
76
    This class is now deprecated; code should move to using the Transport
47
77
    directly for file operations and using the lock or CountedLock for
48
78
    locking.
49
 
 
 
79
    
50
80
    :ivar _lock: The real underlying lock (e.g. a LockDir)
51
 
    :ivar _lock_count: If _lock_mode is true, a positive count of the number
52
 
        of times the lock has been taken (and not yet released) *by this
53
 
        process*, through this particular object instance.
54
 
    :ivar _lock_mode: None, or 'r' or 'w'
 
81
    :ivar _counted_lock: A lock decorated with a semaphore, so that it 
 
82
        can be re-entered.
55
83
    """
56
84
 
 
85
    # _lock_mode: None, or 'r' or 'w'
 
86
 
 
87
    # _lock_count: If _lock_mode is true, a positive count of the number of
 
88
    # times the lock has been taken *by this process*.
 
89
 
57
90
    def __init__(self, transport, lock_name, lock_class):
58
91
        """Create a LockableFiles group
59
92
 
67
100
        self.lock_name = lock_name
68
101
        self._transaction = None
69
102
        self._lock_mode = None
70
 
        self._lock_count = 0
 
103
        self._lock_warner = _LockWarner(repr(self))
71
104
        self._find_modes()
72
105
        esc_name = self._escape(lock_name)
73
106
        self._lock = lock_class(transport, esc_name,
86
119
    def __repr__(self):
87
120
        return '%s(%r)' % (self.__class__.__name__,
88
121
                           self._transport)
89
 
 
90
122
    def __str__(self):
91
123
        return 'LockableFiles(%s, %s)' % (self.lock_name, self._transport.base)
92
124
 
99
131
 
100
132
    def _escape(self, file_or_path):
101
133
        """DEPRECATED: Do not use outside this class"""
 
134
        if not isinstance(file_or_path, basestring):
 
135
            file_or_path = '/'.join(file_or_path)
102
136
        if file_or_path == '':
103
137
            return u''
104
 
        return urlutils.escape(file_or_path)
 
138
        return urlutils.escape(osutils.safe_unicode(file_or_path))
105
139
 
106
140
    def _find_modes(self):
107
141
        """Determine the appropriate modes for files and directories.
114
148
        try:
115
149
            st = self._transport.stat('.')
116
150
        except errors.TransportNotPossible:
117
 
            self._dir_mode = 0o755
118
 
            self._file_mode = 0o644
 
151
            self._dir_mode = 0755
 
152
            self._file_mode = 0644
119
153
        else:
120
154
            # Check the directory mode, but also make sure the created
121
155
            # directories and files are read-write for this user. This is
122
156
            # mostly a workaround for filesystems which lie about being able to
123
157
            # write to a directory (cygwin & win32)
124
 
            self._dir_mode = (st.st_mode & 0o7777) | 0o0700
 
158
            self._dir_mode = (st.st_mode & 07777) | 00700
125
159
            # Remove the sticky and execute bits for files
126
 
            self._file_mode = self._dir_mode & ~0o7111
 
160
            self._file_mode = self._dir_mode & ~07111
127
161
 
128
162
    def leave_in_place(self):
129
163
        """Set this LockableFiles to not clear the physical lock on unlock."""
148
182
        some other way, and need to synchronise this object's state with that
149
183
        fact.
150
184
        """
 
185
        # TODO: Upgrade locking to support using a Transport,
 
186
        # and potentially a remote locking protocol
151
187
        if self._lock_mode:
152
 
            if (self._lock_mode != 'w'
153
 
                    or not self.get_transaction().writeable()):
 
188
            if self._lock_mode != 'w' or not self.get_transaction().writeable():
154
189
                raise errors.ReadOnlyError(self)
155
190
            self._lock.validate_token(token)
156
 
            self._lock_count += 1
 
191
            self._lock_warner.lock_count += 1
157
192
            return self._token_from_lock
158
193
        else:
159
194
            token_from_lock = self._lock.lock_write(token=token)
160
 
            # traceback.print_stack()
 
195
            #traceback.print_stack()
161
196
            self._lock_mode = 'w'
162
 
            self._lock_count = 1
 
197
            self._lock_warner.lock_count = 1
163
198
            self._set_write_transaction()
164
199
            self._token_from_lock = token_from_lock
165
200
            return token_from_lock
168
203
        if self._lock_mode:
169
204
            if self._lock_mode not in ('r', 'w'):
170
205
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
171
 
            self._lock_count += 1
 
206
            self._lock_warner.lock_count += 1
172
207
        else:
173
208
            self._lock.lock_read()
174
 
            # traceback.print_stack()
 
209
            #traceback.print_stack()
175
210
            self._lock_mode = 'r'
176
 
            self._lock_count = 1
 
211
            self._lock_warner.lock_count = 1
177
212
            self._set_read_transaction()
178
213
 
179
214
    def _set_read_transaction(self):
186
221
        """Setup a write transaction."""
187
222
        self._set_transaction(transactions.WriteTransaction())
188
223
 
189
 
    @only_raises(errors.LockNotHeld, errors.LockBroken)
190
224
    def unlock(self):
191
225
        if not self._lock_mode:
192
226
            return lock.cant_unlock_not_held(self)
193
 
        if self._lock_count > 1:
194
 
            self._lock_count -= 1
 
227
        if self._lock_warner.lock_count > 1:
 
228
            self._lock_warner.lock_count -= 1
195
229
        else:
196
 
            # traceback.print_stack()
 
230
            #traceback.print_stack()
197
231
            self._finish_transaction()
198
232
            try:
199
233
                self._lock.unlock()
200
234
            finally:
201
 
                self._lock_count = 0
202
 
                self._lock_mode = None
 
235
                self._lock_mode = self._lock_warner.lock_count = None
 
236
 
 
237
    @property
 
238
    def _lock_count(self):
 
239
        return self._lock_warner.lock_count
203
240
 
204
241
    def is_locked(self):
205
242
        """Return true if this LockableFiles group is locked"""
206
 
        return self._lock_count >= 1
 
243
        return self._lock_warner.lock_count >= 1
207
244
 
208
245
    def get_physical_lock_status(self):
209
246
        """Return physical lock status.
256
293
    This is suitable for use only in WorkingTrees (which are at present
257
294
    always local).
258
295
    """
259
 
 
260
296
    def __init__(self, transport, escaped_name, file_modebits, dir_modebits):
261
297
        self._transport = transport
262
298
        self._escaped_name = escaped_name
290
326
    def create(self, mode=None):
291
327
        """Create lock mechanism"""
292
328
        # for old-style locks, create the file now
293
 
        self._transport.put_bytes(self._escaped_name, b'',
294
 
                                  mode=self._file_modebits)
 
329
        self._transport.put_bytes(self._escaped_name, '',
 
330
                            mode=self._file_modebits)
295
331
 
296
332
    def validate_token(self, token):
297
333
        if token is not None:
298
334
            raise errors.TokenLockingNotSupported(self)
 
335