/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/vfs.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
protocol, as implemented in bzr 0.11 and later.
25
25
"""
26
26
 
27
 
from __future__ import absolute_import
28
 
 
29
27
import os
30
28
 
31
 
from ... import errors
32
 
from ... import urlutils
33
 
from . import request
 
29
from bzrlib import errors
 
30
from bzrlib import urlutils
 
31
from bzrlib.smart import request
34
32
 
35
33
 
36
34
def _deserialise_optional_mode(mode):
37
35
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
38
36
    # might serialise modes differently.
39
 
    if mode == b'':
 
37
    if mode == '':
40
38
        return None
41
39
    else:
42
40
        return int(mode)
45
43
def vfs_enabled():
46
44
    """Is the VFS enabled ?
47
45
 
48
 
    the VFS is disabled when the BRZ_NO_SMART_VFS environment variable is set.
 
46
    the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
49
47
 
50
48
    :return: True if it is enabled.
51
49
    """
52
 
    return not 'BRZ_NO_SMART_VFS' in os.environ
 
50
    return not 'BZR_NO_SMART_VFS' in os.environ
53
51
 
54
52
 
55
53
class VfsRequest(request.SmartServerRequest):
60
58
 
61
59
    def _check_enabled(self):
62
60
        if not vfs_enabled():
63
 
            raise request.DisabledMethod(self.__class__.__name__)
 
61
            raise errors.DisabledMethod(self.__class__.__name__)
64
62
 
65
63
    def translate_client_path(self, relpath):
66
64
        # VFS requests are made with escaped paths so the escaping done in
75
73
 
76
74
    def do(self, relpath):
77
75
        relpath = self.translate_client_path(relpath)
78
 
        r = self._backing_transport.has(relpath) and b'yes' or b'no'
 
76
        r = self._backing_transport.has(relpath) and 'yes' or 'no'
79
77
        return request.SuccessfulSmartServerResponse((r,))
80
78
 
81
79
 
84
82
    def do(self, relpath):
85
83
        relpath = self.translate_client_path(relpath)
86
84
        backing_bytes = self._backing_transport.get_bytes(relpath)
87
 
        return request.SuccessfulSmartServerResponse((b'ok',), backing_bytes)
 
85
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
88
86
 
89
87
 
90
88
class AppendRequest(VfsRequest):
97
95
    def do_body(self, body_bytes):
98
96
        old_length = self._backing_transport.append_bytes(
99
97
            self._relpath, body_bytes, self._mode)
100
 
        return request.SuccessfulSmartServerResponse((b'appended', str(old_length).encode('ascii')))
 
98
        return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
101
99
 
102
100
 
103
101
class DeleteRequest(VfsRequest):
105
103
    def do(self, relpath):
106
104
        relpath = self.translate_client_path(relpath)
107
105
        self._backing_transport.delete(relpath)
108
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
106
        return request.SuccessfulSmartServerResponse(('ok', ))
109
107
 
110
108
 
111
109
class IterFilesRecursiveRequest(VfsRequest):
112
110
 
113
111
    def do(self, relpath):
114
 
        if not relpath.endswith(b'/'):
115
 
            relpath += b'/'
 
112
        if not relpath.endswith('/'):
 
113
            relpath += '/'
116
114
        relpath = self.translate_client_path(relpath)
117
115
        transport = self._backing_transport.clone(relpath)
118
116
        filenames = transport.iter_files_recursive()
119
 
        return request.SuccessfulSmartServerResponse((b'names',) + tuple(filenames))
 
117
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
120
118
 
121
119
 
122
120
class ListDirRequest(VfsRequest):
123
121
 
124
122
    def do(self, relpath):
125
 
        if not relpath.endswith(b'/'):
126
 
            relpath += b'/'
 
123
        if not relpath.endswith('/'):
 
124
            relpath += '/'
127
125
        relpath = self.translate_client_path(relpath)
128
126
        filenames = self._backing_transport.list_dir(relpath)
129
 
        return request.SuccessfulSmartServerResponse((b'names',) + tuple(filenames))
 
127
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
130
128
 
131
129
 
132
130
class MkdirRequest(VfsRequest):
135
133
        relpath = self.translate_client_path(relpath)
136
134
        self._backing_transport.mkdir(relpath,
137
135
                                      _deserialise_optional_mode(mode))
138
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
136
        return request.SuccessfulSmartServerResponse(('ok',))
139
137
 
140
138
 
141
139
class MoveRequest(VfsRequest):
144
142
        rel_from = self.translate_client_path(rel_from)
145
143
        rel_to = self.translate_client_path(rel_to)
146
144
        self._backing_transport.move(rel_from, rel_to)
147
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
145
        return request.SuccessfulSmartServerResponse(('ok',))
148
146
 
149
147
 
150
148
class PutRequest(VfsRequest):
156
154
 
157
155
    def do_body(self, body_bytes):
158
156
        self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
159
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
157
        return request.SuccessfulSmartServerResponse(('ok',))
160
158
 
161
159
 
162
160
class PutNonAtomicRequest(VfsRequest):
167
165
        self._dir_mode = _deserialise_optional_mode(dir_mode)
168
166
        self._mode = _deserialise_optional_mode(mode)
169
167
        # a boolean would be nicer XXX
170
 
        self._create_parent = (create_parent == b'T')
 
168
        self._create_parent = (create_parent == 'T')
171
169
 
172
170
    def do_body(self, body_bytes):
173
171
        self._backing_transport.put_bytes_non_atomic(self._relpath,
175
173
                mode=self._mode,
176
174
                create_parent_dir=self._create_parent,
177
175
                dir_mode=self._dir_mode)
178
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
176
        return request.SuccessfulSmartServerResponse(('ok',))
179
177
 
180
178
 
181
179
class ReadvRequest(VfsRequest):
187
185
    def do_body(self, body_bytes):
188
186
        """accept offsets for a readv request."""
189
187
        offsets = self._deserialise_offsets(body_bytes)
190
 
        backing_bytes = b''.join(bytes for offset, bytes in
 
188
        backing_bytes = ''.join(bytes for offset, bytes in
191
189
            self._backing_transport.readv(self._relpath, offsets))
192
 
        return request.SuccessfulSmartServerResponse((b'readv',), backing_bytes)
 
190
        return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
193
191
 
194
192
    def _deserialise_offsets(self, text):
195
193
        # XXX: FIXME this should be on the protocol object.
196
194
        offsets = []
197
 
        for line in text.split(b'\n'):
 
195
        for line in text.split('\n'):
198
196
            if not line:
199
197
                continue
200
 
            start, length = line.split(b',')
 
198
            start, length = line.split(',')
201
199
            offsets.append((int(start), int(length)))
202
200
        return offsets
203
201
 
208
206
        rel_from = self.translate_client_path(rel_from)
209
207
        rel_to = self.translate_client_path(rel_to)
210
208
        self._backing_transport.rename(rel_from, rel_to)
211
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
209
        return request.SuccessfulSmartServerResponse(('ok', ))
212
210
 
213
211
 
214
212
class RmdirRequest(VfsRequest):
216
214
    def do(self, relpath):
217
215
        relpath = self.translate_client_path(relpath)
218
216
        self._backing_transport.rmdir(relpath)
219
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
217
        return request.SuccessfulSmartServerResponse(('ok', ))
220
218
 
221
219
 
222
220
class StatRequest(VfsRequest):
223
221
 
224
222
    def do(self, relpath):
225
 
        if not relpath.endswith(b'/'):
226
 
            relpath += b'/'
 
223
        if not relpath.endswith('/'):
 
224
            relpath += '/'
227
225
        relpath = self.translate_client_path(relpath)
228
226
        stat = self._backing_transport.stat(relpath)
229
227
        return request.SuccessfulSmartServerResponse(
230
 
            (b'stat', str(stat.st_size).encode('ascii'), oct(stat.st_mode).encode('ascii')))
 
228
            ('stat', str(stat.st_size), oct(stat.st_mode)))
231
229