24
24
import bzrlib.errors as errors
25
25
from bzrlib.errors import BzrError
26
26
from bzrlib.osutils import file_iterator, safe_unicode
27
from bzrlib.symbol_versioning import (deprecated_method,
27
from bzrlib.symbol_versioning import (deprecated_method,
29
29
from bzrlib.trace import mutter, note
30
30
import bzrlib.transactions as transactions
31
31
import bzrlib.urlutils as urlutils
132
132
self._dir_mode = 0755
133
133
self._file_mode = 0644
135
# Check the directory mode, but also make sure the created
136
# directories and files are read-write for this user. This is
137
# mostly a workaround for filesystems which lie about being able to
138
# write to a directory (cygwin & win32)
139
self._dir_mode = (st.st_mode & 07777) | 00700
135
self._dir_mode = st.st_mode & 07777
140
136
# Remove the sticky and execute bits for files
141
137
self._file_mode = self._dir_mode & ~07111
142
138
if not self._set_dir_mode:
148
144
"""Return location relative to branch."""
149
145
return self._transport.abspath(self._escape(file_or_path))
147
@deprecated_method(zero_eight)
148
def controlfile(self, file_or_path, mode='r'):
149
"""Open a control file for this branch.
151
There are two classes of file in a lockable directory: text
152
and binary. binary files are untranslated byte streams. Text
153
control files are stored with Unix newlines and in UTF-8, even
154
if the platform or locale defaults are different.
156
Such files are not openable in write mode : they are managed via
157
put and put_utf8 which atomically replace old versions using
161
relpath = self._escape(file_or_path)
162
# TODO: codecs.open() buffers linewise, so it was overloaded with
163
# a much larger buffer, do we need to do the same for getreader/getwriter?
165
return self.get(relpath)
167
raise BzrError("Branch.controlfile(mode='wb') is not supported, use put[_utf8]")
169
return self.get_utf8(relpath)
171
raise BzrError("Branch.controlfile(mode='w') is not supported, use put[_utf8]")
173
raise BzrError("invalid controlfile mode %r" % mode)
152
176
def get(self, relpath):
153
177
"""Get a file as a bytestream."""
172
196
self._transport.put_file(self._escape(path), file, mode=self._file_mode)
174
198
@needs_write_lock
175
def put_bytes(self, path, a_string):
176
"""Write a string of bytes.
178
:param path: The path to put the bytes, relative to the transport root.
179
:param string: A string object, whose exact bytes are to be copied.
181
self._transport.put_bytes(self._escape(path), a_string,
182
mode=self._file_mode)
185
199
def put_utf8(self, path, a_string):
186
200
"""Write a string, encoding as utf-8.
188
202
:param path: The path to put the string, relative to the transport root.
189
:param string: A string or unicode object whose contents should be copied.
203
:param string: A file-like or string object whose contents should be copied.
191
205
# IterableFile would not be needed if Transport.put took iterables
192
206
# instead of files. ADHB 2005-12-25
196
210
# these are valuable files which should have exact contents.
197
211
if not isinstance(a_string, basestring):
198
212
raise errors.BzrBadParameterNotString(a_string)
199
self.put_bytes(path, a_string.encode('utf-8'))
201
def leave_in_place(self):
202
"""Set this LockableFiles to not clear the physical lock on unlock."""
203
self._lock.leave_in_place()
205
def dont_leave_in_place(self):
206
"""Set this LockableFiles to clear the physical lock on unlock."""
207
self._lock.dont_leave_in_place()
209
def lock_write(self, token=None):
210
"""Lock this group of files for writing.
212
:param token: if this is already locked, then lock_write will fail
213
unless the token matches the existing lock.
214
:returns: a token if this instance supports tokens, otherwise None.
215
:raises TokenLockingNotSupported: when a token is given but this
216
instance doesn't support using token locks.
217
:raises MismatchedToken: if the specified token doesn't match the token
218
of the existing lock.
220
A token should be passed in if you know that you have locked the object
221
some other way, and need to synchronise this object's state with that
213
self.put(path, StringIO(a_string.encode('utf-8')))
215
def lock_write(self):
224
216
# mutter("lock write: %s (%s)", self, self._lock_count)
225
217
# TODO: Upgrade locking to support using a Transport,
226
218
# and potentially a remote locking protocol
227
219
if self._lock_mode:
228
220
if self._lock_mode != 'w' or not self.get_transaction().writeable():
229
221
raise errors.ReadOnlyError(self)
230
self._lock.validate_token(token)
231
222
self._lock_count += 1
232
return self._token_from_lock
234
token_from_lock = self._lock.lock_write(token=token)
224
self._lock.lock_write()
235
225
#note('write locking %s', self)
236
226
#traceback.print_stack()
237
227
self._lock_mode = 'w'
238
228
self._lock_count = 1
239
229
self._set_transaction(transactions.WriteTransaction())
240
self._token_from_lock = token_from_lock
241
return token_from_lock
243
231
def lock_read(self):
244
232
# mutter("lock read: %s (%s)", self, self._lock_count)
335
323
def break_lock(self):
336
324
raise NotImplementedError(self.break_lock)
338
def leave_in_place(self):
339
raise NotImplementedError(self.leave_in_place)
341
def dont_leave_in_place(self):
342
raise NotImplementedError(self.dont_leave_in_place)
344
def lock_write(self, token=None):
345
if token is not None:
346
raise errors.TokenLockingNotSupported(self)
326
def lock_write(self):
347
327
self._lock = self._transport.lock_write(self._escaped_name)
349
329
def lock_read(self):
361
341
# for old-style locks, create the file now
362
342
self._transport.put_bytes(self._escaped_name, '',
363
343
mode=self._file_modebits)
365
def validate_token(self, token):
366
if token is not None:
367
raise errors.TokenLockingNotSupported(self)