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
relpath = self.translate_client_path(relpath)
66
r = self._backing_transport.has(relpath) and 'yes' or 'no'
67
return request.SuccessfulSmartServerResponse((r,))
70
class GetRequest(VfsRequest):
72
def do(self, relpath):
73
relpath = self.translate_client_path(relpath)
75
backing_bytes = self._backing_transport.get_bytes(relpath)
76
except errors.ReadError:
77
# cannot read the file
78
return request.FailedSmartServerResponse(('ReadError', ))
79
except errors.PermissionDenied:
80
return request.FailedSmartServerResponse(('PermissionDenied',))
81
return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
84
class AppendRequest(VfsRequest):
86
def do(self, relpath, mode):
87
relpath = self.translate_client_path(relpath)
88
self._relpath = relpath
89
self._mode = _deserialise_optional_mode(mode)
91
def do_body(self, body_bytes):
92
old_length = self._backing_transport.append_bytes(
93
self._relpath, body_bytes, self._mode)
94
return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
97
class DeleteRequest(VfsRequest):
99
def do(self, relpath):
100
relpath = self.translate_client_path(relpath)
101
self._backing_transport.delete(relpath)
102
return request.SuccessfulSmartServerResponse(('ok', ))
105
class IterFilesRecursiveRequest(VfsRequest):
107
def do(self, relpath):
108
if not relpath.endswith('/'):
110
relpath = self.translate_client_path(relpath)
111
transport = self._backing_transport.clone(relpath)
112
filenames = transport.iter_files_recursive()
113
return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
116
class ListDirRequest(VfsRequest):
118
def do(self, relpath):
119
if not relpath.endswith('/'):
121
relpath = self.translate_client_path(relpath)
122
filenames = self._backing_transport.list_dir(relpath)
123
return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
126
class MkdirRequest(VfsRequest):
128
def do(self, relpath, mode):
129
relpath = self.translate_client_path(relpath)
130
self._backing_transport.mkdir(relpath,
131
_deserialise_optional_mode(mode))
132
return request.SuccessfulSmartServerResponse(('ok',))
135
class MoveRequest(VfsRequest):
137
def do(self, rel_from, rel_to):
138
rel_from = self.translate_client_path(rel_from)
139
rel_to = self.translate_client_path(rel_to)
140
self._backing_transport.move(rel_from, rel_to)
141
return request.SuccessfulSmartServerResponse(('ok',))
144
class PutRequest(VfsRequest):
146
def do(self, relpath, mode):
147
relpath = self.translate_client_path(relpath)
148
self._relpath = relpath
149
self._mode = _deserialise_optional_mode(mode)
151
def do_body(self, body_bytes):
152
self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
153
return request.SuccessfulSmartServerResponse(('ok',))
156
class PutNonAtomicRequest(VfsRequest):
158
def do(self, relpath, mode, create_parent, dir_mode):
159
relpath = self.translate_client_path(relpath)
160
self._relpath = relpath
161
self._dir_mode = _deserialise_optional_mode(dir_mode)
162
self._mode = _deserialise_optional_mode(mode)
163
# a boolean would be nicer XXX
164
self._create_parent = (create_parent == 'T')
166
def do_body(self, body_bytes):
167
self._backing_transport.put_bytes_non_atomic(self._relpath,
170
create_parent_dir=self._create_parent,
171
dir_mode=self._dir_mode)
172
return request.SuccessfulSmartServerResponse(('ok',))
175
class ReadvRequest(VfsRequest):
177
def do(self, relpath):
178
relpath = self.translate_client_path(relpath)
179
self._relpath = relpath
181
def do_body(self, body_bytes):
182
"""accept offsets for a readv request."""
183
offsets = self._deserialise_offsets(body_bytes)
184
backing_bytes = ''.join(bytes for offset, bytes in
185
self._backing_transport.readv(self._relpath, offsets))
186
return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
188
def _deserialise_offsets(self, text):
189
# XXX: FIXME this should be on the protocol object.
191
for line in text.split('\n'):
194
start, length = line.split(',')
195
offsets.append((int(start), int(length)))
199
class RenameRequest(VfsRequest):
201
def do(self, rel_from, rel_to):
202
rel_from = self.translate_client_path(rel_from)
203
rel_to = self.translate_client_path(rel_to)
204
self._backing_transport.rename(rel_from, rel_to)
205
return request.SuccessfulSmartServerResponse(('ok', ))
208
class RmdirRequest(VfsRequest):
210
def do(self, relpath):
211
relpath = self.translate_client_path(relpath)
212
self._backing_transport.rmdir(relpath)
213
return request.SuccessfulSmartServerResponse(('ok', ))
216
class StatRequest(VfsRequest):
218
def do(self, relpath):
219
if not relpath.endswith('/'):
221
relpath = self.translate_client_path(relpath)
222
stat = self._backing_transport.stat(relpath)
223
return request.SuccessfulSmartServerResponse(
224
('stat', str(stat.st_size), oct(stat.st_mode)))