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.SmartServerResponse((r,))
69
class GetRequest(VfsRequest):
71
def do(self, relpath):
72
backing_bytes = self._backing_transport.get_bytes(relpath)
73
return request.SmartServerResponse(('ok',), backing_bytes)
76
class AppendRequest(VfsRequest):
78
def do(self, relpath, mode):
79
self._relpath = relpath
80
self._mode = _deserialise_optional_mode(mode)
82
def do_body(self, body_bytes):
83
old_length = self._backing_transport.append_bytes(
84
self._relpath, body_bytes, self._mode)
85
return request.SmartServerResponse(('appended', '%d' % old_length))
88
class DeleteRequest(VfsRequest):
90
def do(self, relpath):
91
self._backing_transport.delete(relpath)
92
return request.SmartServerResponse(('ok', ))
95
class IterFilesRecursiveRequest(VfsRequest):
97
def do(self, relpath):
98
transport = self._backing_transport.clone(relpath)
99
filenames = transport.iter_files_recursive()
100
return request.SmartServerResponse(('names',) + tuple(filenames))
103
class ListDirRequest(VfsRequest):
105
def do(self, relpath):
106
filenames = self._backing_transport.list_dir(relpath)
107
return request.SmartServerResponse(('names',) + tuple(filenames))
110
class MkdirRequest(VfsRequest):
112
def do(self, relpath, mode):
113
self._backing_transport.mkdir(relpath,
114
_deserialise_optional_mode(mode))
115
return request.SmartServerResponse(('ok',))
118
class MoveRequest(VfsRequest):
120
def do(self, rel_from, rel_to):
121
self._backing_transport.move(rel_from, rel_to)
122
return request.SmartServerResponse(('ok',))
125
class PutRequest(VfsRequest):
127
def do(self, relpath, mode):
128
self._relpath = relpath
129
self._mode = _deserialise_optional_mode(mode)
131
def do_body(self, body_bytes):
132
self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
133
return request.SmartServerResponse(('ok',))
136
class PutNonAtomicRequest(VfsRequest):
138
def do(self, relpath, mode, create_parent, dir_mode):
139
self._relpath = relpath
140
self._dir_mode = _deserialise_optional_mode(dir_mode)
141
self._mode = _deserialise_optional_mode(mode)
142
# a boolean would be nicer XXX
143
self._create_parent = (create_parent == 'T')
145
def do_body(self, body_bytes):
146
self._backing_transport.put_bytes_non_atomic(self._relpath,
149
create_parent_dir=self._create_parent,
150
dir_mode=self._dir_mode)
151
return request.SmartServerResponse(('ok',))
154
class ReadvRequest(VfsRequest):
156
def do(self, relpath):
157
self._relpath = relpath
159
def do_body(self, body_bytes):
160
"""accept offsets for a readv request."""
161
offsets = self._deserialise_offsets(body_bytes)
162
backing_bytes = ''.join(bytes for offset, bytes in
163
self._backing_transport.readv(self._relpath, offsets))
164
return request.SmartServerResponse(('readv',), backing_bytes)
166
def _deserialise_offsets(self, text):
167
# XXX: FIXME this should be on the protocol object.
169
for line in text.split('\n'):
172
start, length = line.split(',')
173
offsets.append((int(start), int(length)))
177
class RenameRequest(VfsRequest):
179
def do(self, rel_from, rel_to):
180
self._backing_transport.rename(rel_from, rel_to)
181
return request.SmartServerResponse(('ok', ))
184
class RmdirRequest(VfsRequest):
186
def do(self, relpath):
187
self._backing_transport.rmdir(relpath)
188
return request.SmartServerResponse(('ok', ))
191
class StatRequest(VfsRequest):
193
def do(self, relpath):
194
stat = self._backing_transport.stat(relpath)
195
return request.SmartServerResponse(
196
('stat', str(stat.st_size), oct(stat.st_mode)))