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
194
194
:param f: A file-like or string object whose contents should be copied.
196
self._transport.put(self._escape(path), file, mode=self._file_mode)
196
self._transport.put_file(self._escape(path), file, mode=self._file_mode)
199
def put_bytes(self, path, a_string):
200
"""Write a string of bytes.
202
:param path: The path to put the bytes, relative to the transport root.
203
:param string: A string object, whose exact bytes are to be copied.
205
self._transport.put_bytes(self._escape(path), a_string,
206
mode=self._file_mode)
198
208
@needs_write_lock
199
209
def put_utf8(self, path, a_string):
200
210
"""Write a string, encoding as utf-8.
202
212
:param path: The path to put the string, relative to the transport root.
203
:param string: A file-like or string object whose contents should be copied.
213
:param string: A string or unicode object whose contents should be copied.
205
215
# IterableFile would not be needed if Transport.put took iterables
206
216
# instead of files. ADHB 2005-12-25
210
220
# these are valuable files which should have exact contents.
211
221
if not isinstance(a_string, basestring):
212
222
raise errors.BzrBadParameterNotString(a_string)
213
self.put(path, StringIO(a_string.encode('utf-8')))
215
def lock_write(self):
223
self.put_bytes(path, a_string.encode('utf-8'))
225
def leave_in_place(self):
226
"""Set this LockableFiles to not clear the physical lock on unlock."""
227
self._lock.leave_in_place()
229
def dont_leave_in_place(self):
230
"""Set this LockableFiles to clear the physical lock on unlock."""
231
self._lock.dont_leave_in_place()
233
def lock_write(self, token=None):
234
"""Lock this group of files for writing.
236
:param token: if this is already locked, then lock_write will fail
237
unless the token matches the existing lock.
238
:returns: a token if this instance supports tokens, otherwise None.
239
:raises TokenLockingNotSupported: when a token is given but this
240
instance doesn't support using token locks.
241
:raises MismatchedToken: if the specified token doesn't match the token
242
of the existing lock.
244
A token should be passed in if you know that you have locked the object
245
some other way, and need to synchronise this object's state with that
216
248
# mutter("lock write: %s (%s)", self, self._lock_count)
217
249
# TODO: Upgrade locking to support using a Transport,
218
250
# and potentially a remote locking protocol
219
251
if self._lock_mode:
220
252
if self._lock_mode != 'w' or not self.get_transaction().writeable():
221
253
raise errors.ReadOnlyError(self)
254
self._lock.validate_token(token)
222
255
self._lock_count += 1
256
return self._token_from_lock
224
self._lock.lock_write()
258
token_from_lock = self._lock.lock_write(token=token)
225
259
#note('write locking %s', self)
226
260
#traceback.print_stack()
227
261
self._lock_mode = 'w'
228
262
self._lock_count = 1
229
263
self._set_transaction(transactions.WriteTransaction())
264
self._token_from_lock = token_from_lock
265
return token_from_lock
231
267
def lock_read(self):
232
268
# mutter("lock read: %s (%s)", self, self._lock_count)
323
359
def break_lock(self):
324
360
raise NotImplementedError(self.break_lock)
326
def lock_write(self):
362
def leave_in_place(self):
363
raise NotImplementedError(self.leave_in_place)
365
def dont_leave_in_place(self):
366
raise NotImplementedError(self.dont_leave_in_place)
368
def lock_write(self, token=None):
369
if token is not None:
370
raise errors.TokenLockingNotSupported(self)
327
371
self._lock = self._transport.lock_write(self._escaped_name)
329
373
def lock_read(self):
339
383
def create(self, mode=None):
340
384
"""Create lock mechanism"""
341
385
# for old-style locks, create the file now
342
self._transport.put(self._escaped_name, StringIO(),
386
self._transport.put_bytes(self._escaped_name, '',
343
387
mode=self._file_modebits)
389
def validate_token(self, token):
390
if token is not None:
391
raise errors.TokenLockingNotSupported(self)