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
-- i.e. methods that operate directly on files and directories, rather than
21
higher-level concepts like branches and revisions.
23
These methods, plus 'hello' and 'get_bundle', are version 1 of the smart server
24
protocol, as implemented in bzr 0.11 and later.
29
from bzrlib import errors
30
from bzrlib import urlutils
31
from bzrlib.smart import request
34
def _deserialise_optional_mode(mode):
35
# XXX: FIXME this should be on the protocol object. Later protocol versions
36
# might serialise modes differently.
44
"""Is the VFS enabled ?
46
the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
48
:return: True if it is enabled.
50
return not 'BZR_NO_SMART_VFS' in os.environ
53
class VfsRequest(request.SmartServerRequest):
54
"""Base class for VFS requests.
56
VFS requests are disabled if vfs_enabled() returns False.
59
def _check_enabled(self):
61
raise errors.DisabledMethod(self.__class__.__name__)
63
def translate_client_path(self, relpath):
64
# VFS requests are made with escaped paths so the escaping done in
65
# SmartServerRequest.translate_client_path leads to double escaping.
66
# Remove it here -- the fact that the result is still escaped means
67
# that the str() will not fail on valid input.
68
x = request.SmartServerRequest.translate_client_path(self, relpath)
69
return str(urlutils.unescape(x))
72
class HasRequest(VfsRequest):
74
def do(self, relpath):
75
relpath = self.translate_client_path(relpath)
76
r = self._backing_transport.has(relpath) and 'yes' or 'no'
77
return request.SuccessfulSmartServerResponse((r,))
80
class GetRequest(VfsRequest):
82
def do(self, relpath):
83
relpath = self.translate_client_path(relpath)
84
backing_bytes = self._backing_transport.get_bytes(relpath)
85
return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
88
class AppendRequest(VfsRequest):
90
def do(self, relpath, mode):
91
relpath = self.translate_client_path(relpath)
92
self._relpath = relpath
93
self._mode = _deserialise_optional_mode(mode)
95
def do_body(self, body_bytes):
96
old_length = self._backing_transport.append_bytes(
97
self._relpath, body_bytes, self._mode)
98
return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
101
class DeleteRequest(VfsRequest):
103
def do(self, relpath):
104
relpath = self.translate_client_path(relpath)
105
self._backing_transport.delete(relpath)
106
return request.SuccessfulSmartServerResponse(('ok', ))
109
class IterFilesRecursiveRequest(VfsRequest):
111
def do(self, relpath):
112
if not relpath.endswith('/'):
114
relpath = self.translate_client_path(relpath)
115
transport = self._backing_transport.clone(relpath)
116
filenames = transport.iter_files_recursive()
117
return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
120
class ListDirRequest(VfsRequest):
122
def do(self, relpath):
123
if not relpath.endswith('/'):
125
relpath = self.translate_client_path(relpath)
126
filenames = self._backing_transport.list_dir(relpath)
127
return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
130
class MkdirRequest(VfsRequest):
132
def do(self, relpath, mode):
133
relpath = self.translate_client_path(relpath)
134
self._backing_transport.mkdir(relpath,
135
_deserialise_optional_mode(mode))
136
return request.SuccessfulSmartServerResponse(('ok',))
139
class MoveRequest(VfsRequest):
141
def do(self, rel_from, rel_to):
142
rel_from = self.translate_client_path(rel_from)
143
rel_to = self.translate_client_path(rel_to)
144
self._backing_transport.move(rel_from, rel_to)
145
return request.SuccessfulSmartServerResponse(('ok',))
148
class PutRequest(VfsRequest):
150
def do(self, relpath, mode):
151
relpath = self.translate_client_path(relpath)
152
self._relpath = relpath
153
self._mode = _deserialise_optional_mode(mode)
155
def do_body(self, body_bytes):
156
self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
157
return request.SuccessfulSmartServerResponse(('ok',))
160
class PutNonAtomicRequest(VfsRequest):
162
def do(self, relpath, mode, create_parent, dir_mode):
163
relpath = self.translate_client_path(relpath)
164
self._relpath = relpath
165
self._dir_mode = _deserialise_optional_mode(dir_mode)
166
self._mode = _deserialise_optional_mode(mode)
167
# a boolean would be nicer XXX
168
self._create_parent = (create_parent == 'T')
170
def do_body(self, body_bytes):
171
self._backing_transport.put_bytes_non_atomic(self._relpath,
174
create_parent_dir=self._create_parent,
175
dir_mode=self._dir_mode)
176
return request.SuccessfulSmartServerResponse(('ok',))
179
class ReadvRequest(VfsRequest):
181
def do(self, relpath):
182
relpath = self.translate_client_path(relpath)
183
self._relpath = relpath
185
def do_body(self, body_bytes):
186
"""accept offsets for a readv request."""
187
offsets = self._deserialise_offsets(body_bytes)
188
backing_bytes = ''.join(bytes for offset, bytes in
189
self._backing_transport.readv(self._relpath, offsets))
190
return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
192
def _deserialise_offsets(self, text):
193
# XXX: FIXME this should be on the protocol object.
195
for line in text.split('\n'):
198
start, length = line.split(',')
199
offsets.append((int(start), int(length)))
203
class RenameRequest(VfsRequest):
205
def do(self, rel_from, rel_to):
206
rel_from = self.translate_client_path(rel_from)
207
rel_to = self.translate_client_path(rel_to)
208
self._backing_transport.rename(rel_from, rel_to)
209
return request.SuccessfulSmartServerResponse(('ok', ))
212
class RmdirRequest(VfsRequest):
214
def do(self, relpath):
215
relpath = self.translate_client_path(relpath)
216
self._backing_transport.rmdir(relpath)
217
return request.SuccessfulSmartServerResponse(('ok', ))
220
class StatRequest(VfsRequest):
222
def do(self, relpath):
223
if not relpath.endswith('/'):
225
relpath = self.translate_client_path(relpath)
226
stat = self._backing_transport.stat(relpath)
227
return request.SuccessfulSmartServerResponse(
228
('stat', str(stat.st_size), oct(stat.st_mode)))