~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/vfs.py

  • Committer: Martin Packman
  • Date: 2011-12-23 19:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 6405.
  • Revision ID: martin.packman@canonical.com-20111223193822-hesheea4o8aqwexv
Accept and document passing the medium rather than transport for smart connections

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
30
31
from bzrlib.smart import request
31
32
 
32
33
 
51
52
 
52
53
class VfsRequest(request.SmartServerRequest):
53
54
    """Base class for VFS requests.
54
 
    
 
55
 
55
56
    VFS requests are disabled if vfs_enabled() returns False.
56
57
    """
57
58
 
59
60
        if not vfs_enabled():
60
61
            raise errors.DisabledMethod(self.__class__.__name__)
61
62
 
 
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
 
62
71
 
63
72
class HasRequest(VfsRequest):
64
73
 
65
74
    def do(self, relpath):
 
75
        relpath = self.translate_client_path(relpath)
66
76
        r = self._backing_transport.has(relpath) and 'yes' or 'no'
67
77
        return request.SuccessfulSmartServerResponse((r,))
68
78
 
70
80
class GetRequest(VfsRequest):
71
81
 
72
82
    def do(self, relpath):
 
83
        relpath = self.translate_client_path(relpath)
73
84
        backing_bytes = self._backing_transport.get_bytes(relpath)
74
85
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
75
86
 
77
88
class AppendRequest(VfsRequest):
78
89
 
79
90
    def do(self, relpath, mode):
 
91
        relpath = self.translate_client_path(relpath)
80
92
        self._relpath = relpath
81
93
        self._mode = _deserialise_optional_mode(mode)
82
 
    
 
94
 
83
95
    def do_body(self, body_bytes):
84
96
        old_length = self._backing_transport.append_bytes(
85
97
            self._relpath, body_bytes, self._mode)
89
101
class DeleteRequest(VfsRequest):
90
102
 
91
103
    def do(self, relpath):
 
104
        relpath = self.translate_client_path(relpath)
92
105
        self._backing_transport.delete(relpath)
93
106
        return request.SuccessfulSmartServerResponse(('ok', ))
94
107
 
96
109
class IterFilesRecursiveRequest(VfsRequest):
97
110
 
98
111
    def do(self, relpath):
 
112
        if not relpath.endswith('/'):
 
113
            relpath += '/'
 
114
        relpath = self.translate_client_path(relpath)
99
115
        transport = self._backing_transport.clone(relpath)
100
116
        filenames = transport.iter_files_recursive()
101
117
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
104
120
class ListDirRequest(VfsRequest):
105
121
 
106
122
    def do(self, relpath):
 
123
        if not relpath.endswith('/'):
 
124
            relpath += '/'
 
125
        relpath = self.translate_client_path(relpath)
107
126
        filenames = self._backing_transport.list_dir(relpath)
108
127
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
109
128
 
111
130
class MkdirRequest(VfsRequest):
112
131
 
113
132
    def do(self, relpath, mode):
 
133
        relpath = self.translate_client_path(relpath)
114
134
        self._backing_transport.mkdir(relpath,
115
135
                                      _deserialise_optional_mode(mode))
116
136
        return request.SuccessfulSmartServerResponse(('ok',))
119
139
class MoveRequest(VfsRequest):
120
140
 
121
141
    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)
122
144
        self._backing_transport.move(rel_from, rel_to)
123
145
        return request.SuccessfulSmartServerResponse(('ok',))
124
146
 
126
148
class PutRequest(VfsRequest):
127
149
 
128
150
    def do(self, relpath, mode):
 
151
        relpath = self.translate_client_path(relpath)
129
152
        self._relpath = relpath
130
153
        self._mode = _deserialise_optional_mode(mode)
131
154
 
137
160
class PutNonAtomicRequest(VfsRequest):
138
161
 
139
162
    def do(self, relpath, mode, create_parent, dir_mode):
 
163
        relpath = self.translate_client_path(relpath)
140
164
        self._relpath = relpath
141
165
        self._dir_mode = _deserialise_optional_mode(dir_mode)
142
166
        self._mode = _deserialise_optional_mode(mode)
155
179
class ReadvRequest(VfsRequest):
156
180
 
157
181
    def do(self, relpath):
 
182
        relpath = self.translate_client_path(relpath)
158
183
        self._relpath = relpath
159
184
 
160
185
    def do_body(self, body_bytes):
178
203
class RenameRequest(VfsRequest):
179
204
 
180
205
    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)
181
208
        self._backing_transport.rename(rel_from, rel_to)
182
209
        return request.SuccessfulSmartServerResponse(('ok', ))
183
210
 
185
212
class RmdirRequest(VfsRequest):
186
213
 
187
214
    def do(self, relpath):
 
215
        relpath = self.translate_client_path(relpath)
188
216
        self._backing_transport.rmdir(relpath)
189
217
        return request.SuccessfulSmartServerResponse(('ok', ))
190
218
 
192
220
class StatRequest(VfsRequest):
193
221
 
194
222
    def do(self, relpath):
 
223
        if not relpath.endswith('/'):
 
224
            relpath += '/'
 
225
        relpath = self.translate_client_path(relpath)
195
226
        stat = self._backing_transport.stat(relpath)
196
227
        return request.SuccessfulSmartServerResponse(
197
228
            ('stat', str(stat.st_size), oct(stat.st_mode)))