~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/vfs.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-13 23:45:11 UTC
  • mfrom: (3272.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080313234511-fkj5oa8gm3nrfcro
(Neil Martinsen-Burrell) Explain version-info --custom in the User
        Guide

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