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.
27
from __future__ import absolute_import
31
from ... import urlutils
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 BRZ_NO_SMART_VFS environment variable is set.
49
:return: ``True`` if it is enabled.
51
return 'BRZ_NO_SMART_VFS' not 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 request.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 b'yes' or b'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((b'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((b'appended', str(old_length).encode('ascii')))
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((b'ok', ))
110
class IterFilesRecursiveRequest(VfsRequest):
112
def do(self, relpath):
113
if not relpath.endswith(b'/'):
115
relpath = self.translate_client_path(relpath)
116
transport = self._backing_transport.clone(relpath)
117
filenames = transport.iter_files_recursive()
118
return request.SuccessfulSmartServerResponse((b'names',) + tuple(filenames))
121
class ListDirRequest(VfsRequest):
123
def do(self, relpath):
124
if not relpath.endswith(b'/'):
126
relpath = self.translate_client_path(relpath)
127
filenames = self._backing_transport.list_dir(relpath)
128
return request.SuccessfulSmartServerResponse((b'names',) + tuple([filename.encode('utf-8') for filename in 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((b'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((b'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(
158
self._relpath, body_bytes, self._mode)
159
return request.SuccessfulSmartServerResponse((b'ok',))
162
class PutNonAtomicRequest(VfsRequest):
164
def do(self, relpath, mode, create_parent, dir_mode):
165
relpath = self.translate_client_path(relpath)
166
self._relpath = relpath
167
self._dir_mode = _deserialise_optional_mode(dir_mode)
168
self._mode = _deserialise_optional_mode(mode)
169
# a boolean would be nicer XXX
170
self._create_parent = (create_parent == b'T')
172
def do_body(self, body_bytes):
173
self._backing_transport.put_bytes_non_atomic(self._relpath,
176
create_parent_dir=self._create_parent,
177
dir_mode=self._dir_mode)
178
return request.SuccessfulSmartServerResponse((b'ok',))
181
class ReadvRequest(VfsRequest):
183
def do(self, relpath):
184
relpath = self.translate_client_path(relpath)
185
self._relpath = relpath
187
def do_body(self, body_bytes):
188
"""accept offsets for a readv request."""
189
offsets = self._deserialise_offsets(body_bytes)
190
backing_bytes = b''.join(bytes for offset, bytes in
191
self._backing_transport.readv(self._relpath, offsets))
192
return request.SuccessfulSmartServerResponse((b'readv',), backing_bytes)
194
def _deserialise_offsets(self, text):
195
# XXX: FIXME this should be on the protocol object.
197
for line in text.split(b'\n'):
200
start, length = line.split(b',')
201
offsets.append((int(start), int(length)))
205
class RenameRequest(VfsRequest):
207
def do(self, rel_from, rel_to):
208
rel_from = self.translate_client_path(rel_from)
209
rel_to = self.translate_client_path(rel_to)
210
self._backing_transport.rename(rel_from, rel_to)
211
return request.SuccessfulSmartServerResponse((b'ok', ))
214
class RmdirRequest(VfsRequest):
216
def do(self, relpath):
217
relpath = self.translate_client_path(relpath)
218
self._backing_transport.rmdir(relpath)
219
return request.SuccessfulSmartServerResponse((b'ok', ))
222
class StatRequest(VfsRequest):
224
def do(self, relpath):
225
if not relpath.endswith(b'/'):
227
relpath = self.translate_client_path(relpath)
228
stat = self._backing_transport.stat(relpath)
229
return request.SuccessfulSmartServerResponse(
230
(b'stat', str(stat.st_size).encode('ascii'), oct(stat.st_mode).encode('ascii')))