1
# Copyright (C) 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""VFS operations for the smart server.
19
This module defines the smart server methods that are low-level file operations
20
higher-level concepts like branches and revisions.
22
These methods, plus 'hello' and 'get_bundle', are version 1 of the smart server
23
protocol, as implemented in bzr 0.11 and later.
28
from bzrlib import errors
29
from bzrlib.smart import request
32
def _deserialise_optional_mode(mode):
33
# XXX: FIXME this should be on the protocol object. Later protocol versions
34
# might serialise modes differently.
42
"""Is the VFS enabled ?
44
the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
46
:return: True if it is enabled.
48
return not 'BZR_NO_SMART_VFS' in os.environ
51
class VfsRequest(request.SmartServerRequest):
52
"""Base class for VFS requests.
54
VFS requests are disabled if vfs_enabled() returns False.
57
def _check_enabled(self):
59
raise errors.DisabledMethod(self.__class__.__name__)
62
class HasRequest(VfsRequest):
64
def do(self, relpath):
65
r = self._backing_transport.has(relpath) and 'yes' or 'no'
66
return request.SuccessfulSmartServerResponse((r,))
69
class GetRequest(VfsRequest):
71
def do(self, relpath):
73
backing_bytes = self._backing_transport.get_bytes(relpath)
74
except errors.ReadError:
75
# cannot read the file
76
return request.FailedSmartServerResponse(('ReadError', ))
77
return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
80
class AppendRequest(VfsRequest):
82
def do(self, relpath, mode):
83
self._relpath = relpath
84
self._mode = _deserialise_optional_mode(mode)
86
def do_body(self, body_bytes):
87
old_length = self._backing_transport.append_bytes(
88
self._relpath, body_bytes, self._mode)
89
return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
92
class DeleteRequest(VfsRequest):
94
def do(self, relpath):
95
self._backing_transport.delete(relpath)
96
return request.SuccessfulSmartServerResponse(('ok', ))
99
class IterFilesRecursiveRequest(VfsRequest):
101
def do(self, relpath):
102
transport = self._backing_transport.clone(relpath)
103
filenames = transport.iter_files_recursive()
104
return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
107
class ListDirRequest(VfsRequest):
109
def do(self, relpath):
110
filenames = self._backing_transport.list_dir(relpath)
111
return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
114
class MkdirRequest(VfsRequest):
116
def do(self, relpath, mode):
117
self._backing_transport.mkdir(relpath,
118
_deserialise_optional_mode(mode))
119
return request.SuccessfulSmartServerResponse(('ok',))
122
class MoveRequest(VfsRequest):
124
def do(self, rel_from, rel_to):
125
self._backing_transport.move(rel_from, rel_to)
126
return request.SuccessfulSmartServerResponse(('ok',))
129
class PutRequest(VfsRequest):
131
def do(self, relpath, mode):
132
self._relpath = relpath
133
self._mode = _deserialise_optional_mode(mode)
135
def do_body(self, body_bytes):
136
self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
137
return request.SuccessfulSmartServerResponse(('ok',))
140
class PutNonAtomicRequest(VfsRequest):
142
def do(self, relpath, mode, create_parent, dir_mode):
143
self._relpath = relpath
144
self._dir_mode = _deserialise_optional_mode(dir_mode)
145
self._mode = _deserialise_optional_mode(mode)
146
# a boolean would be nicer XXX
147
self._create_parent = (create_parent == 'T')
149
def do_body(self, body_bytes):
150
self._backing_transport.put_bytes_non_atomic(self._relpath,
153
create_parent_dir=self._create_parent,
154
dir_mode=self._dir_mode)
155
return request.SuccessfulSmartServerResponse(('ok',))
158
class ReadvRequest(VfsRequest):
160
def do(self, relpath):
161
self._relpath = relpath
163
def do_body(self, body_bytes):
164
"""accept offsets for a readv request."""
165
offsets = self._deserialise_offsets(body_bytes)
166
backing_bytes = ''.join(bytes for offset, bytes in
167
self._backing_transport.readv(self._relpath, offsets))
168
return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
170
def _deserialise_offsets(self, text):
171
# XXX: FIXME this should be on the protocol object.
173
for line in text.split('\n'):
176
start, length = line.split(',')
177
offsets.append((int(start), int(length)))
181
class RenameRequest(VfsRequest):
183
def do(self, rel_from, rel_to):
184
self._backing_transport.rename(rel_from, rel_to)
185
return request.SuccessfulSmartServerResponse(('ok', ))
188
class RmdirRequest(VfsRequest):
190
def do(self, relpath):
191
self._backing_transport.rmdir(relpath)
192
return request.SuccessfulSmartServerResponse(('ok', ))
195
class StatRequest(VfsRequest):
197
def do(self, relpath):
198
stat = self._backing_transport.stat(relpath)
199
return request.SuccessfulSmartServerResponse(
200
('stat', str(stat.st_size), oct(stat.st_mode)))