/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: 2007-04-19 02:27:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2426.
  • Revision ID: robertc@robertcollins.net-20070419022744-pfdqz42kp1wizh43
``make docs`` now creates a man page at ``man1/bzr.1`` fixing bug 107388.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""VFS operations for the smart server.
18
18
 
26
26
 
27
27
import os
28
28
 
29
 
from ... import urlutils
30
 
from . import request
 
29
from bzrlib import errors
 
30
from bzrlib.smart import request
31
31
 
32
32
 
33
33
def _deserialise_optional_mode(mode):
34
34
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
35
35
    # might serialise modes differently.
36
 
    if mode == b'':
 
36
    if mode == '':
37
37
        return None
38
38
    else:
39
39
        return int(mode)
42
42
def vfs_enabled():
43
43
    """Is the VFS enabled ?
44
44
 
45
 
    the VFS is disabled when the BRZ_NO_SMART_VFS environment variable is set.
 
45
    the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
46
46
 
47
 
    :return: ``True`` if it is enabled.
 
47
    :return: True if it is enabled.
48
48
    """
49
 
    return 'BRZ_NO_SMART_VFS' not in os.environ
 
49
    return not 'BZR_NO_SMART_VFS' in os.environ
50
50
 
51
51
 
52
52
class VfsRequest(request.SmartServerRequest):
53
53
    """Base class for VFS requests.
54
 
 
 
54
    
55
55
    VFS requests are disabled if vfs_enabled() returns False.
56
56
    """
57
57
 
58
58
    def _check_enabled(self):
59
59
        if not vfs_enabled():
60
 
            raise request.DisabledMethod(self.__class__.__name__)
61
 
 
62
 
    def translate_client_path(self, relpath):
63
 
        # VFS requests are made with escaped paths so the escaping done in
64
 
        # SmartServerRequest.translate_client_path leads to double escaping.
65
 
        # Remove it here -- the fact that the result is still escaped means
66
 
        # that the str() will not fail on valid input.
67
 
        x = request.SmartServerRequest.translate_client_path(self, relpath)
68
 
        return str(urlutils.unescape(x))
 
60
            raise errors.DisabledMethod(self.__class__.__name__)
69
61
 
70
62
 
71
63
class HasRequest(VfsRequest):
72
64
 
73
65
    def do(self, relpath):
74
 
        relpath = self.translate_client_path(relpath)
75
 
        r = self._backing_transport.has(relpath) and b'yes' or b'no'
76
 
        return request.SuccessfulSmartServerResponse((r,))
 
66
        r = self._backing_transport.has(relpath) and 'yes' or 'no'
 
67
        return request.SmartServerResponse((r,))
77
68
 
78
69
 
79
70
class GetRequest(VfsRequest):
80
71
 
81
72
    def do(self, relpath):
82
 
        relpath = self.translate_client_path(relpath)
83
73
        backing_bytes = self._backing_transport.get_bytes(relpath)
84
 
        return request.SuccessfulSmartServerResponse((b'ok',), backing_bytes)
 
74
        return request.SmartServerResponse(('ok',), backing_bytes)
85
75
 
86
76
 
87
77
class AppendRequest(VfsRequest):
88
78
 
89
79
    def do(self, relpath, mode):
90
 
        relpath = self.translate_client_path(relpath)
91
80
        self._relpath = relpath
92
81
        self._mode = _deserialise_optional_mode(mode)
93
 
 
 
82
    
94
83
    def do_body(self, body_bytes):
95
84
        old_length = self._backing_transport.append_bytes(
96
85
            self._relpath, body_bytes, self._mode)
97
 
        return request.SuccessfulSmartServerResponse((b'appended', str(old_length).encode('ascii')))
 
86
        return request.SmartServerResponse(('appended', '%d' % old_length))
98
87
 
99
88
 
100
89
class DeleteRequest(VfsRequest):
101
90
 
102
91
    def do(self, relpath):
103
 
        relpath = self.translate_client_path(relpath)
104
92
        self._backing_transport.delete(relpath)
105
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
93
        return request.SmartServerResponse(('ok', ))
106
94
 
107
95
 
108
96
class IterFilesRecursiveRequest(VfsRequest):
109
97
 
110
98
    def do(self, relpath):
111
 
        if not relpath.endswith(b'/'):
112
 
            relpath += b'/'
113
 
        relpath = self.translate_client_path(relpath)
114
99
        transport = self._backing_transport.clone(relpath)
115
100
        filenames = transport.iter_files_recursive()
116
 
        return request.SuccessfulSmartServerResponse((b'names',) + tuple(filenames))
 
101
        return request.SmartServerResponse(('names',) + tuple(filenames))
117
102
 
118
103
 
119
104
class ListDirRequest(VfsRequest):
120
105
 
121
106
    def do(self, relpath):
122
 
        if not relpath.endswith(b'/'):
123
 
            relpath += b'/'
124
 
        relpath = self.translate_client_path(relpath)
125
107
        filenames = self._backing_transport.list_dir(relpath)
126
 
        return request.SuccessfulSmartServerResponse((b'names',) + tuple([filename.encode('utf-8') for filename in filenames]))
 
108
        return request.SmartServerResponse(('names',) + tuple(filenames))
127
109
 
128
110
 
129
111
class MkdirRequest(VfsRequest):
130
112
 
131
113
    def do(self, relpath, mode):
132
 
        relpath = self.translate_client_path(relpath)
133
114
        self._backing_transport.mkdir(relpath,
134
115
                                      _deserialise_optional_mode(mode))
135
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
116
        return request.SmartServerResponse(('ok',))
136
117
 
137
118
 
138
119
class MoveRequest(VfsRequest):
139
120
 
140
121
    def do(self, rel_from, rel_to):
141
 
        rel_from = self.translate_client_path(rel_from)
142
 
        rel_to = self.translate_client_path(rel_to)
143
122
        self._backing_transport.move(rel_from, rel_to)
144
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
123
        return request.SmartServerResponse(('ok',))
145
124
 
146
125
 
147
126
class PutRequest(VfsRequest):
148
127
 
149
128
    def do(self, relpath, mode):
150
 
        relpath = self.translate_client_path(relpath)
151
129
        self._relpath = relpath
152
130
        self._mode = _deserialise_optional_mode(mode)
153
131
 
154
132
    def do_body(self, body_bytes):
155
 
        self._backing_transport.put_bytes(
156
 
            self._relpath, body_bytes, self._mode)
157
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
133
        self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
 
134
        return request.SmartServerResponse(('ok',))
158
135
 
159
136
 
160
137
class PutNonAtomicRequest(VfsRequest):
161
138
 
162
139
    def do(self, relpath, mode, create_parent, dir_mode):
163
 
        relpath = self.translate_client_path(relpath)
164
140
        self._relpath = relpath
165
141
        self._dir_mode = _deserialise_optional_mode(dir_mode)
166
142
        self._mode = _deserialise_optional_mode(mode)
167
143
        # a boolean would be nicer XXX
168
 
        self._create_parent = (create_parent == b'T')
 
144
        self._create_parent = (create_parent == 'T')
169
145
 
170
146
    def do_body(self, body_bytes):
171
147
        self._backing_transport.put_bytes_non_atomic(self._relpath,
172
 
                                                     body_bytes,
173
 
                                                     mode=self._mode,
174
 
                                                     create_parent_dir=self._create_parent,
175
 
                                                     dir_mode=self._dir_mode)
176
 
        return request.SuccessfulSmartServerResponse((b'ok',))
 
148
                body_bytes,
 
149
                mode=self._mode,
 
150
                create_parent_dir=self._create_parent,
 
151
                dir_mode=self._dir_mode)
 
152
        return request.SmartServerResponse(('ok',))
177
153
 
178
154
 
179
155
class ReadvRequest(VfsRequest):
180
156
 
181
157
    def do(self, relpath):
182
 
        relpath = self.translate_client_path(relpath)
183
158
        self._relpath = relpath
184
159
 
185
160
    def do_body(self, body_bytes):
186
161
        """accept offsets for a readv request."""
187
162
        offsets = self._deserialise_offsets(body_bytes)
188
 
        backing_bytes = b''.join(bytes for offset, bytes in
189
 
                                 self._backing_transport.readv(self._relpath, offsets))
190
 
        return request.SuccessfulSmartServerResponse((b'readv',), backing_bytes)
 
163
        backing_bytes = ''.join(bytes for offset, bytes in
 
164
            self._backing_transport.readv(self._relpath, offsets))
 
165
        return request.SmartServerResponse(('readv',), backing_bytes)
191
166
 
192
167
    def _deserialise_offsets(self, text):
193
168
        # XXX: FIXME this should be on the protocol object.
194
169
        offsets = []
195
 
        for line in text.split(b'\n'):
 
170
        for line in text.split('\n'):
196
171
            if not line:
197
172
                continue
198
 
            start, length = line.split(b',')
 
173
            start, length = line.split(',')
199
174
            offsets.append((int(start), int(length)))
200
175
        return offsets
201
176
 
203
178
class RenameRequest(VfsRequest):
204
179
 
205
180
    def do(self, rel_from, rel_to):
206
 
        rel_from = self.translate_client_path(rel_from)
207
 
        rel_to = self.translate_client_path(rel_to)
208
181
        self._backing_transport.rename(rel_from, rel_to)
209
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
182
        return request.SmartServerResponse(('ok', ))
210
183
 
211
184
 
212
185
class RmdirRequest(VfsRequest):
213
186
 
214
187
    def do(self, relpath):
215
 
        relpath = self.translate_client_path(relpath)
216
188
        self._backing_transport.rmdir(relpath)
217
 
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
189
        return request.SmartServerResponse(('ok', ))
218
190
 
219
191
 
220
192
class StatRequest(VfsRequest):
221
193
 
222
194
    def do(self, relpath):
223
 
        if not relpath.endswith(b'/'):
224
 
            relpath += b'/'
225
 
        relpath = self.translate_client_path(relpath)
226
195
        stat = self._backing_transport.stat(relpath)
227
 
        return request.SuccessfulSmartServerResponse(
228
 
            (b'stat', str(stat.st_size).encode('ascii'), oct(stat.st_mode).encode('ascii')))
 
196
        return request.SmartServerResponse(
 
197
            ('stat', str(stat.st_size), oct(stat.st_mode)))
 
198