~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lockable_files.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-11-04 18:51:39 UTC
  • mfrom: (2961.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20071104185139-kaio3sneodg2kp71
Authentication ring implementation (read-only)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from cStringIO import StringIO
18
 
 
19
 
from bzrlib.lazy_import import lazy_import
20
 
lazy_import(globals(), """
21
18
import codecs
22
 
import warnings
23
 
 
24
 
from bzrlib import (
25
 
    errors,
26
 
    osutils,
27
 
    transactions,
28
 
    urlutils,
29
 
    )
30
 
""")
31
 
 
32
 
from bzrlib.decorators import (
33
 
    needs_read_lock,
34
 
    needs_write_lock,
35
 
    )
36
 
from bzrlib.symbol_versioning import (
37
 
    deprecated_in,
38
 
    deprecated_method,
39
 
    )
 
19
#import traceback
 
20
 
 
21
import bzrlib
 
22
from bzrlib.decorators import (needs_read_lock,
 
23
        needs_write_lock)
 
24
import bzrlib.errors as errors
 
25
from bzrlib.errors import BzrError
 
26
from bzrlib.osutils import file_iterator, safe_unicode
 
27
from bzrlib.symbol_versioning import (deprecated_method,
 
28
        )
 
29
from bzrlib.trace import mutter, note
 
30
import bzrlib.transactions as transactions
 
31
import bzrlib.urlutils as urlutils
40
32
 
41
33
 
42
34
# XXX: The tracking here of lock counts and whether the lock is held is
64
56
    the object is constructed.  In older formats OSLocks are used everywhere.
65
57
    in newer formats a LockDir is used for Repositories and Branches, and 
66
58
    OSLocks for the local filesystem.
67
 
 
68
 
    This class is now deprecated; code should move to using the Transport 
69
 
    directly for file operations and using the lock or CountedLock for 
70
 
    locking.
71
59
    """
72
60
 
73
61
    # _lock_mode: None, or 'r' or 'w'
75
63
    # _lock_count: If _lock_mode is true, a positive count of the number of
76
64
    # times the lock has been taken *by this process*.   
77
65
    
 
66
    # If set to False (by a plugin, etc) BzrBranch will not set the
 
67
    # mode on created files or directories
 
68
    _set_file_mode = True
 
69
    _set_dir_mode = True
 
70
 
78
71
    def __init__(self, transport, lock_name, lock_class):
79
72
        """Create a LockableFiles group
80
73
 
111
104
 
112
105
    def __del__(self):
113
106
        if self.is_locked():
114
 
            # do not automatically unlock; there should have been a
115
 
            # try/finally to unlock this.
116
 
            warnings.warn("%r was gc'd while locked" % self)
 
107
            # XXX: This should show something every time, and be suitable for
 
108
            # headless operation and embedding
 
109
            from warnings import warn
 
110
            warn("file group %r was not explicitly unlocked" % self)
 
111
            self._lock.unlock()
117
112
 
118
113
    def break_lock(self):
119
114
        """Break the lock of this lockable files group if it is held.
127
122
            file_or_path = '/'.join(file_or_path)
128
123
        if file_or_path == '':
129
124
            return u''
130
 
        return urlutils.escape(osutils.safe_unicode(file_or_path))
 
125
        return urlutils.escape(safe_unicode(file_or_path))
131
126
 
132
127
    def _find_modes(self):
133
 
        """Determine the appropriate modes for files and directories.
134
 
        
135
 
        :deprecated: Replaced by BzrDir._find_modes.
136
 
        """
 
128
        """Determine the appropriate modes for files and directories."""
137
129
        try:
138
130
            st = self._transport.stat('.')
139
131
        except errors.TransportNotPossible:
140
132
            self._dir_mode = 0755
141
133
            self._file_mode = 0644
142
134
        else:
143
 
            # Check the directory mode, but also make sure the created
144
 
            # directories and files are read-write for this user. This is
145
 
            # mostly a workaround for filesystems which lie about being able to
146
 
            # write to a directory (cygwin & win32)
147
 
            self._dir_mode = (st.st_mode & 07777) | 00700
 
135
            self._dir_mode = st.st_mode & 07777
148
136
            # Remove the sticky and execute bits for files
149
137
            self._file_mode = self._dir_mode & ~07111
 
138
        if not self._set_dir_mode:
 
139
            self._dir_mode = None
 
140
        if not self._set_file_mode:
 
141
            self._file_mode = None
150
142
 
151
 
    @deprecated_method(deprecated_in((1, 6, 0)))
152
143
    def controlfilename(self, file_or_path):
153
 
        """Return location relative to branch.
154
 
        
155
 
        :deprecated: Use Transport methods instead.
156
 
        """
 
144
        """Return location relative to branch."""
157
145
        return self._transport.abspath(self._escape(file_or_path))
158
146
 
159
147
    @needs_read_lock
160
 
    @deprecated_method(deprecated_in((1, 5, 0)))
161
148
    def get(self, relpath):
162
 
        """Get a file as a bytestream.
163
 
        
164
 
        :deprecated: Use a Transport instead of LockableFiles.
165
 
        """
 
149
        """Get a file as a bytestream."""
166
150
        relpath = self._escape(relpath)
167
151
        return self._transport.get(relpath)
168
152
 
169
153
    @needs_read_lock
170
 
    @deprecated_method(deprecated_in((1, 5, 0)))
171
154
    def get_utf8(self, relpath):
172
 
        """Get a file as a unicode stream.
173
 
        
174
 
        :deprecated: Use a Transport instead of LockableFiles.
175
 
        """
 
155
        """Get a file as a unicode stream."""
176
156
        relpath = self._escape(relpath)
177
157
        # DO NOT introduce an errors=replace here.
178
158
        return codecs.getreader('utf-8')(self._transport.get(relpath))
179
159
 
180
160
    @needs_write_lock
181
 
    @deprecated_method(deprecated_in((1, 6, 0)))
182
161
    def put(self, path, file):
183
162
        """Write a file.
184
163
        
185
164
        :param path: The path to put the file, relative to the .bzr control
186
165
                     directory
187
 
        :param file: A file-like or string object whose contents should be copied.
188
 
 
189
 
        :deprecated: Use Transport methods instead.
 
166
        :param f: A file-like or string object whose contents should be copied.
190
167
        """
191
168
        self._transport.put_file(self._escape(path), file, mode=self._file_mode)
192
169
 
193
170
    @needs_write_lock
194
 
    @deprecated_method(deprecated_in((1, 6, 0)))
195
171
    def put_bytes(self, path, a_string):
196
172
        """Write a string of bytes.
197
173
 
198
174
        :param path: The path to put the bytes, relative to the transport root.
199
 
        :param a_string: A string object, whose exact bytes are to be copied.
200
 
 
201
 
        :deprecated: Use Transport methods instead.
 
175
        :param string: A string object, whose exact bytes are to be copied.
202
176
        """
203
177
        self._transport.put_bytes(self._escape(path), a_string,
204
178
                                  mode=self._file_mode)
205
179
 
206
180
    @needs_write_lock
207
 
    @deprecated_method(deprecated_in((1, 6, 0)))
208
181
    def put_utf8(self, path, a_string):
209
182
        """Write a string, encoding as utf-8.
210
183
 
211
184
        :param path: The path to put the string, relative to the transport root.
212
185
        :param string: A string or unicode object whose contents should be copied.
213
 
 
214
 
        :deprecated: Use Transport methods instead.
215
186
        """
216
187
        # IterableFile would not be needed if Transport.put took iterables
217
188
        # instead of files.  ADHB 2005-12-25
246
217
        some other way, and need to synchronise this object's state with that
247
218
        fact.
248
219
        """
 
220
        # mutter("lock write: %s (%s)", self, self._lock_count)
249
221
        # TODO: Upgrade locking to support using a Transport,
250
222
        # and potentially a remote locking protocol
251
223
        if self._lock_mode:
256
228
            return self._token_from_lock
257
229
        else:
258
230
            token_from_lock = self._lock.lock_write(token=token)
 
231
            #note('write locking %s', self)
259
232
            #traceback.print_stack()
260
233
            self._lock_mode = 'w'
261
234
            self._lock_count = 1
264
237
            return token_from_lock
265
238
 
266
239
    def lock_read(self):
 
240
        # mutter("lock read: %s (%s)", self, self._lock_count)
267
241
        if self._lock_mode:
268
 
            if self._lock_mode not in ('r', 'w'):
269
 
                raise ValueError("invalid lock mode %r" % (self._lock_mode,))
 
242
            assert self._lock_mode in ('r', 'w'), \
 
243
                   "invalid lock mode %r" % self._lock_mode
270
244
            self._lock_count += 1
271
245
        else:
272
246
            self._lock.lock_read()
 
247
            #note('read locking %s', self)
273
248
            #traceback.print_stack()
274
249
            self._lock_mode = 'r'
275
250
            self._lock_count = 1
278
253
            self.get_transaction().set_cache_size(5000)
279
254
                        
280
255
    def unlock(self):
 
256
        # mutter("unlock: %s (%s)", self, self._lock_count)
281
257
        if not self._lock_mode:
282
258
            raise errors.LockNotHeld(self)
283
259
        if self._lock_count > 1:
284
260
            self._lock_count -= 1
285
261
        else:
 
262
            #note('unlocking %s', self)
286
263
            #traceback.print_stack()
287
264
            self._finish_transaction()
288
265
            try: