~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/vfs.py

  • Committer: Martin Pool
  • Date: 2005-09-02 02:05:26 UTC
  • Revision ID: mbp@sourcefrog.net-20050902020526-0ab28bd5a998df70
- fix off-by-one in 'bzr log -r'

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
16
 
 
17
 
"""VFS operations for the smart server.
18
 
 
19
 
This module defines the smart server methods that are low-level file operations
20
 
higher-level concepts like branches and revisions.
21
 
 
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.
24
 
"""
25
 
 
26
 
from __future__ import absolute_import
27
 
 
28
 
import os
29
 
 
30
 
from bzrlib import errors
31
 
from bzrlib import urlutils
32
 
from bzrlib.smart import request
33
 
 
34
 
 
35
 
def _deserialise_optional_mode(mode):
36
 
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
37
 
    # might serialise modes differently.
38
 
    if mode == '':
39
 
        return None
40
 
    else:
41
 
        return int(mode)
42
 
 
43
 
 
44
 
def vfs_enabled():
45
 
    """Is the VFS enabled ?
46
 
 
47
 
    the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
48
 
 
49
 
    :return: True if it is enabled.
50
 
    """
51
 
    return not 'BZR_NO_SMART_VFS' in os.environ
52
 
 
53
 
 
54
 
class VfsRequest(request.SmartServerRequest):
55
 
    """Base class for VFS requests.
56
 
 
57
 
    VFS requests are disabled if vfs_enabled() returns False.
58
 
    """
59
 
 
60
 
    def _check_enabled(self):
61
 
        if not vfs_enabled():
62
 
            raise errors.DisabledMethod(self.__class__.__name__)
63
 
 
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))
71
 
 
72
 
 
73
 
class HasRequest(VfsRequest):
74
 
 
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,))
79
 
 
80
 
 
81
 
class GetRequest(VfsRequest):
82
 
 
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)
87
 
 
88
 
 
89
 
class AppendRequest(VfsRequest):
90
 
 
91
 
    def do(self, relpath, mode):
92
 
        relpath = self.translate_client_path(relpath)
93
 
        self._relpath = relpath
94
 
        self._mode = _deserialise_optional_mode(mode)
95
 
 
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))
100
 
 
101
 
 
102
 
class DeleteRequest(VfsRequest):
103
 
 
104
 
    def do(self, relpath):
105
 
        relpath = self.translate_client_path(relpath)
106
 
        self._backing_transport.delete(relpath)
107
 
        return request.SuccessfulSmartServerResponse(('ok', ))
108
 
 
109
 
 
110
 
class IterFilesRecursiveRequest(VfsRequest):
111
 
 
112
 
    def do(self, relpath):
113
 
        if not relpath.endswith('/'):
114
 
            relpath += '/'
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))
119
 
 
120
 
 
121
 
class ListDirRequest(VfsRequest):
122
 
 
123
 
    def do(self, relpath):
124
 
        if not relpath.endswith('/'):
125
 
            relpath += '/'
126
 
        relpath = self.translate_client_path(relpath)
127
 
        filenames = self._backing_transport.list_dir(relpath)
128
 
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
129
 
 
130
 
 
131
 
class MkdirRequest(VfsRequest):
132
 
 
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',))
138
 
 
139
 
 
140
 
class MoveRequest(VfsRequest):
141
 
 
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',))
147
 
 
148
 
 
149
 
class PutRequest(VfsRequest):
150
 
 
151
 
    def do(self, relpath, mode):
152
 
        relpath = self.translate_client_path(relpath)
153
 
        self._relpath = relpath
154
 
        self._mode = _deserialise_optional_mode(mode)
155
 
 
156
 
    def do_body(self, body_bytes):
157
 
        self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
158
 
        return request.SuccessfulSmartServerResponse(('ok',))
159
 
 
160
 
 
161
 
class PutNonAtomicRequest(VfsRequest):
162
 
 
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')
170
 
 
171
 
    def do_body(self, body_bytes):
172
 
        self._backing_transport.put_bytes_non_atomic(self._relpath,
173
 
                body_bytes,
174
 
                mode=self._mode,
175
 
                create_parent_dir=self._create_parent,
176
 
                dir_mode=self._dir_mode)
177
 
        return request.SuccessfulSmartServerResponse(('ok',))
178
 
 
179
 
 
180
 
class ReadvRequest(VfsRequest):
181
 
 
182
 
    def do(self, relpath):
183
 
        relpath = self.translate_client_path(relpath)
184
 
        self._relpath = relpath
185
 
 
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)
192
 
 
193
 
    def _deserialise_offsets(self, text):
194
 
        # XXX: FIXME this should be on the protocol object.
195
 
        offsets = []
196
 
        for line in text.split('\n'):
197
 
            if not line:
198
 
                continue
199
 
            start, length = line.split(',')
200
 
            offsets.append((int(start), int(length)))
201
 
        return offsets
202
 
 
203
 
 
204
 
class RenameRequest(VfsRequest):
205
 
 
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', ))
211
 
 
212
 
 
213
 
class RmdirRequest(VfsRequest):
214
 
 
215
 
    def do(self, relpath):
216
 
        relpath = self.translate_client_path(relpath)
217
 
        self._backing_transport.rmdir(relpath)
218
 
        return request.SuccessfulSmartServerResponse(('ok', ))
219
 
 
220
 
 
221
 
class StatRequest(VfsRequest):
222
 
 
223
 
    def do(self, relpath):
224
 
        if not relpath.endswith('/'):
225
 
            relpath += '/'
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)))
230