1
# Copyright (C) 2006-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.
26
from __future__ import absolute_import
30
from bzrlib import errors
31
from bzrlib import urlutils
32
from bzrlib.smart import request
35
def _deserialise_optional_mode(mode):
36
# XXX: FIXME this should be on the protocol object. Later protocol versions
37
# might serialise modes differently.
45
"""Is the VFS enabled ?
47
the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
49
:return: True if it is enabled.
51
return not 'BZR_NO_SMART_VFS' in os.environ
54
class VfsRequest(request.SmartServerRequest):
55
"""Base class for VFS requests.
57
VFS requests are disabled if vfs_enabled() returns False.
60
def _check_enabled(self):
62
raise errors.DisabledMethod(self.__class__.__name__)
64
def translate_client_path(self, relpath):
65
# VFS requests are made with escaped paths so the escaping done in
66
# SmartServerRequest.translate_client_path leads to double escaping.
67
# Remove it here -- the fact that the result is still escaped means
68
# that the str() will not fail on valid input.
69
x = request.SmartServerRequest.translate_client_path(self, relpath)
70
return str(urlutils.unescape(x))
73
class HasRequest(VfsRequest):
75
def do(self, relpath):
76
relpath = self.translate_client_path(relpath)
77
r = self._backing_transport.has(relpath) and 'yes' or 'no'
78
return request.SuccessfulSmartServerResponse((r,))
81
class GetRequest(VfsRequest):
83
def do(self, relpath):
84
relpath = self.translate_client_path(relpath)
85
backing_bytes = self._backing_transport.get_bytes(relpath)
86
return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
89
class AppendRequest(VfsRequest):
91
def do(self, relpath, mode):
92
relpath = self.translate_client_path(relpath)
93
self._relpath = relpath
94
self._mode = _deserialise_optional_mode(mode)
96
def do_body(self, body_bytes):
97
old_length = self._backing_transport.append_bytes(
98
self._relpath, body_bytes, self._mode)
99
return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
102
class DeleteRequest(VfsRequest):
104
def do(self, relpath):
105
relpath = self.translate_client_path(relpath)
106
self._backing_transport.delete(relpath)
107
return request.SuccessfulSmartServerResponse(('ok', ))
110
class IterFilesRecursiveRequest(VfsRequest):
112
def do(self, relpath):
113
if not relpath.endswith('/'):
115
relpath = self.translate_client_path(relpath)
116
transport = self._backing_transport.clone(relpath)
117
filenames = transport.iter_files_recursive()
118
return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
121
class ListDirRequest(VfsRequest):
123
def do(self, relpath):
124
if not relpath.endswith('/'):
126
relpath = self.translate_client_path(relpath)
127
filenames = self._backing_transport.list_dir(relpath)
128
return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
131
class MkdirRequest(VfsRequest):
133
def do(self, relpath, mode):
134
relpath = self.translate_client_path(relpath)
135
self._backing_transport.mkdir(relpath,
136
_deserialise_optional_mode(mode))
137
return request.SuccessfulSmartServerResponse(('ok',))
140
class MoveRequest(VfsRequest):
142
def do(self, rel_from, rel_to):
143
rel_from = self.translate_client_path(rel_from)
144
rel_to = self.translate_client_path(rel_to)
145
self._backing_transport.move(rel_from, rel_to)
146
return request.SuccessfulSmartServerResponse(('ok',))
149
class PutRequest(VfsRequest):
151
def do(self, relpath, mode):
152
relpath = self.translate_client_path(relpath)
153
self._relpath = relpath
154
self._mode = _deserialise_optional_mode(mode)
156
def do_body(self, body_bytes):
157
self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
158
return request.SuccessfulSmartServerResponse(('ok',))
161
class PutNonAtomicRequest(VfsRequest):
163
def do(self, relpath, mode, create_parent, dir_mode):
164
relpath = self.translate_client_path(relpath)
165
self._relpath = relpath
166
self._dir_mode = _deserialise_optional_mode(dir_mode)
167
self._mode = _deserialise_optional_mode(mode)
168
# a boolean would be nicer XXX
169
self._create_parent = (create_parent == 'T')
171
def do_body(self, body_bytes):
172
self._backing_transport.put_bytes_non_atomic(self._relpath,
175
create_parent_dir=self._create_parent,
176
dir_mode=self._dir_mode)
177
return request.SuccessfulSmartServerResponse(('ok',))
180
class ReadvRequest(VfsRequest):
182
def do(self, relpath):
183
relpath = self.translate_client_path(relpath)
184
self._relpath = relpath
186
def do_body(self, body_bytes):
187
"""accept offsets for a readv request."""
188
offsets = self._deserialise_offsets(body_bytes)
189
backing_bytes = ''.join(bytes for offset, bytes in
190
self._backing_transport.readv(self._relpath, offsets))
191
return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
193
def _deserialise_offsets(self, text):
194
# XXX: FIXME this should be on the protocol object.
196
for line in text.split('\n'):
199
start, length = line.split(',')
200
offsets.append((int(start), int(length)))
204
class RenameRequest(VfsRequest):
206
def do(self, rel_from, rel_to):
207
rel_from = self.translate_client_path(rel_from)
208
rel_to = self.translate_client_path(rel_to)
209
self._backing_transport.rename(rel_from, rel_to)
210
return request.SuccessfulSmartServerResponse(('ok', ))
213
class RmdirRequest(VfsRequest):
215
def do(self, relpath):
216
relpath = self.translate_client_path(relpath)
217
self._backing_transport.rmdir(relpath)
218
return request.SuccessfulSmartServerResponse(('ok', ))
221
class StatRequest(VfsRequest):
223
def do(self, relpath):
224
if not relpath.endswith('/'):
226
relpath = self.translate_client_path(relpath)
227
stat = self._backing_transport.stat(relpath)
228
return request.SuccessfulSmartServerResponse(
229
('stat', str(stat.st_size), oct(stat.st_mode)))