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
-- 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.smart import request
 
 
33
def _deserialise_optional_mode(mode):
 
 
34
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
 
 
35
    # might serialise modes differently.
 
 
43
    """Is the VFS enabled ?
 
 
45
    the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
 
 
47
    :return: True if it is enabled.
 
 
49
    return not 'BZR_NO_SMART_VFS' in os.environ
 
 
52
class VfsRequest(request.SmartServerRequest):
 
 
53
    """Base class for VFS requests.
 
 
55
    VFS requests are disabled if vfs_enabled() returns False.
 
 
58
    def _check_enabled(self):
 
 
60
            raise errors.DisabledMethod(self.__class__.__name__)
 
 
63
class HasRequest(VfsRequest):
 
 
65
    def do(self, 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):
 
 
74
            backing_bytes = self._backing_transport.get_bytes(relpath)
 
 
75
        except errors.ReadError:
 
 
76
            # cannot read the file
 
 
77
            return request.FailedSmartServerResponse(('ReadError', ))
 
 
78
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
 
 
81
class AppendRequest(VfsRequest):
 
 
83
    def do(self, relpath, mode):
 
 
84
        self._relpath = relpath
 
 
85
        self._mode = _deserialise_optional_mode(mode)
 
 
87
    def do_body(self, body_bytes):
 
 
88
        old_length = self._backing_transport.append_bytes(
 
 
89
            self._relpath, body_bytes, self._mode)
 
 
90
        return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
 
 
93
class DeleteRequest(VfsRequest):
 
 
95
    def do(self, relpath):
 
 
96
        self._backing_transport.delete(relpath)
 
 
97
        return request.SuccessfulSmartServerResponse(('ok', ))
 
 
100
class IterFilesRecursiveRequest(VfsRequest):
 
 
102
    def do(self, relpath):
 
 
103
        transport = self._backing_transport.clone(relpath)
 
 
104
        filenames = transport.iter_files_recursive()
 
 
105
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
 
 
108
class ListDirRequest(VfsRequest):
 
 
110
    def do(self, relpath):
 
 
111
        filenames = self._backing_transport.list_dir(relpath)
 
 
112
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
 
 
115
class MkdirRequest(VfsRequest):
 
 
117
    def do(self, relpath, mode):
 
 
118
        self._backing_transport.mkdir(relpath,
 
 
119
                                      _deserialise_optional_mode(mode))
 
 
120
        return request.SuccessfulSmartServerResponse(('ok',))
 
 
123
class MoveRequest(VfsRequest):
 
 
125
    def do(self, rel_from, rel_to):
 
 
126
        self._backing_transport.move(rel_from, rel_to)
 
 
127
        return request.SuccessfulSmartServerResponse(('ok',))
 
 
130
class PutRequest(VfsRequest):
 
 
132
    def do(self, relpath, mode):
 
 
133
        self._relpath = relpath
 
 
134
        self._mode = _deserialise_optional_mode(mode)
 
 
136
    def do_body(self, body_bytes):
 
 
137
        self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
 
 
138
        return request.SuccessfulSmartServerResponse(('ok',))
 
 
141
class PutNonAtomicRequest(VfsRequest):
 
 
143
    def do(self, relpath, mode, create_parent, dir_mode):
 
 
144
        self._relpath = relpath
 
 
145
        self._dir_mode = _deserialise_optional_mode(dir_mode)
 
 
146
        self._mode = _deserialise_optional_mode(mode)
 
 
147
        # a boolean would be nicer XXX
 
 
148
        self._create_parent = (create_parent == 'T')
 
 
150
    def do_body(self, body_bytes):
 
 
151
        self._backing_transport.put_bytes_non_atomic(self._relpath,
 
 
154
                create_parent_dir=self._create_parent,
 
 
155
                dir_mode=self._dir_mode)
 
 
156
        return request.SuccessfulSmartServerResponse(('ok',))
 
 
159
class ReadvRequest(VfsRequest):
 
 
161
    def do(self, relpath):
 
 
162
        self._relpath = relpath
 
 
164
    def do_body(self, body_bytes):
 
 
165
        """accept offsets for a readv request."""
 
 
166
        offsets = self._deserialise_offsets(body_bytes)
 
 
167
        backing_bytes = ''.join(bytes for offset, bytes in
 
 
168
            self._backing_transport.readv(self._relpath, offsets))
 
 
169
        return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
 
 
171
    def _deserialise_offsets(self, text):
 
 
172
        # XXX: FIXME this should be on the protocol object.
 
 
174
        for line in text.split('\n'):
 
 
177
            start, length = line.split(',')
 
 
178
            offsets.append((int(start), int(length)))
 
 
182
class RenameRequest(VfsRequest):
 
 
184
    def do(self, rel_from, rel_to):
 
 
185
        self._backing_transport.rename(rel_from, rel_to)
 
 
186
        return request.SuccessfulSmartServerResponse(('ok', ))
 
 
189
class RmdirRequest(VfsRequest):
 
 
191
    def do(self, relpath):
 
 
192
        self._backing_transport.rmdir(relpath)
 
 
193
        return request.SuccessfulSmartServerResponse(('ok', ))
 
 
196
class StatRequest(VfsRequest):
 
 
198
    def do(self, relpath):
 
 
199
        stat = self._backing_transport.stat(relpath)
 
 
200
        return request.SuccessfulSmartServerResponse(
 
 
201
            ('stat', str(stat.st_size), oct(stat.st_mode)))