~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/vfs.py

Merge bzr.dev

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
 
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
29
from bzrlib import errors
32
 
from bzrlib import urlutils
33
30
from bzrlib.smart import request
34
31
 
35
32
 
54
51
 
55
52
class VfsRequest(request.SmartServerRequest):
56
53
    """Base class for VFS requests.
57
 
 
 
54
    
58
55
    VFS requests are disabled if vfs_enabled() returns False.
59
56
    """
60
57
 
62
59
        if not vfs_enabled():
63
60
            raise errors.DisabledMethod(self.__class__.__name__)
64
61
 
65
 
    def translate_client_path(self, relpath):
66
 
        # VFS requests are made with escaped paths so the escaping done in
67
 
        # SmartServerRequest.translate_client_path leads to double escaping.
68
 
        # Remove it here -- the fact that the result is still escaped means
69
 
        # that the str() will not fail on valid input.
70
 
        x = request.SmartServerRequest.translate_client_path(self, relpath)
71
 
        return str(urlutils.unescape(x))
72
 
 
73
62
 
74
63
class HasRequest(VfsRequest):
75
64
 
76
65
    def do(self, relpath):
77
 
        relpath = self.translate_client_path(relpath)
78
66
        r = self._backing_transport.has(relpath) and 'yes' or 'no'
79
67
        return request.SuccessfulSmartServerResponse((r,))
80
68
 
82
70
class GetRequest(VfsRequest):
83
71
 
84
72
    def do(self, relpath):
85
 
        relpath = self.translate_client_path(relpath)
86
 
        backing_bytes = self._backing_transport.get_bytes(relpath)
 
73
        try:
 
74
            backing_bytes = self._backing_transport.get_bytes(relpath)
 
75
        except errors.ReadError:
 
76
            # cannot read the file
 
77
            return request.FailedSmartServerResponse(('ReadError', ))
87
78
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
88
79
 
89
80
 
90
81
class AppendRequest(VfsRequest):
91
82
 
92
83
    def do(self, relpath, mode):
93
 
        relpath = self.translate_client_path(relpath)
94
84
        self._relpath = relpath
95
85
        self._mode = _deserialise_optional_mode(mode)
96
 
 
 
86
    
97
87
    def do_body(self, body_bytes):
98
88
        old_length = self._backing_transport.append_bytes(
99
89
            self._relpath, body_bytes, self._mode)
103
93
class DeleteRequest(VfsRequest):
104
94
 
105
95
    def do(self, relpath):
106
 
        relpath = self.translate_client_path(relpath)
107
96
        self._backing_transport.delete(relpath)
108
97
        return request.SuccessfulSmartServerResponse(('ok', ))
109
98
 
111
100
class IterFilesRecursiveRequest(VfsRequest):
112
101
 
113
102
    def do(self, relpath):
114
 
        if not relpath.endswith('/'):
115
 
            relpath += '/'
116
 
        relpath = self.translate_client_path(relpath)
117
103
        transport = self._backing_transport.clone(relpath)
118
104
        filenames = transport.iter_files_recursive()
119
105
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
122
108
class ListDirRequest(VfsRequest):
123
109
 
124
110
    def do(self, relpath):
125
 
        if not relpath.endswith('/'):
126
 
            relpath += '/'
127
 
        relpath = self.translate_client_path(relpath)
128
111
        filenames = self._backing_transport.list_dir(relpath)
129
112
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
130
113
 
132
115
class MkdirRequest(VfsRequest):
133
116
 
134
117
    def do(self, relpath, mode):
135
 
        relpath = self.translate_client_path(relpath)
136
118
        self._backing_transport.mkdir(relpath,
137
119
                                      _deserialise_optional_mode(mode))
138
120
        return request.SuccessfulSmartServerResponse(('ok',))
141
123
class MoveRequest(VfsRequest):
142
124
 
143
125
    def do(self, rel_from, rel_to):
144
 
        rel_from = self.translate_client_path(rel_from)
145
 
        rel_to = self.translate_client_path(rel_to)
146
126
        self._backing_transport.move(rel_from, rel_to)
147
127
        return request.SuccessfulSmartServerResponse(('ok',))
148
128
 
150
130
class PutRequest(VfsRequest):
151
131
 
152
132
    def do(self, relpath, mode):
153
 
        relpath = self.translate_client_path(relpath)
154
133
        self._relpath = relpath
155
134
        self._mode = _deserialise_optional_mode(mode)
156
135
 
162
141
class PutNonAtomicRequest(VfsRequest):
163
142
 
164
143
    def do(self, relpath, mode, create_parent, dir_mode):
165
 
        relpath = self.translate_client_path(relpath)
166
144
        self._relpath = relpath
167
145
        self._dir_mode = _deserialise_optional_mode(dir_mode)
168
146
        self._mode = _deserialise_optional_mode(mode)
181
159
class ReadvRequest(VfsRequest):
182
160
 
183
161
    def do(self, relpath):
184
 
        relpath = self.translate_client_path(relpath)
185
162
        self._relpath = relpath
186
163
 
187
164
    def do_body(self, body_bytes):
205
182
class RenameRequest(VfsRequest):
206
183
 
207
184
    def do(self, rel_from, rel_to):
208
 
        rel_from = self.translate_client_path(rel_from)
209
 
        rel_to = self.translate_client_path(rel_to)
210
185
        self._backing_transport.rename(rel_from, rel_to)
211
186
        return request.SuccessfulSmartServerResponse(('ok', ))
212
187
 
214
189
class RmdirRequest(VfsRequest):
215
190
 
216
191
    def do(self, relpath):
217
 
        relpath = self.translate_client_path(relpath)
218
192
        self._backing_transport.rmdir(relpath)
219
193
        return request.SuccessfulSmartServerResponse(('ok', ))
220
194
 
222
196
class StatRequest(VfsRequest):
223
197
 
224
198
    def do(self, relpath):
225
 
        if not relpath.endswith('/'):
226
 
            relpath += '/'
227
 
        relpath = self.translate_client_path(relpath)
228
199
        stat = self._backing_transport.stat(relpath)
229
200
        return request.SuccessfulSmartServerResponse(
230
201
            ('stat', str(stat.st_size), oct(stat.st_mode)))