/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:
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):
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',))
 
83
        relpath = self.translate_client_path(relpath)
 
84
        backing_bytes = self._backing_transport.get_bytes(relpath)
80
85
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
81
86
 
82
87
 
83
88
class AppendRequest(VfsRequest):
84
89
 
85
90
    def do(self, relpath, mode):
 
91
        relpath = self.translate_client_path(relpath)
86
92
        self._relpath = relpath
87
93
        self._mode = _deserialise_optional_mode(mode)
88
 
    
 
94
 
89
95
    def do_body(self, body_bytes):
90
96
        old_length = self._backing_transport.append_bytes(
91
97
            self._relpath, body_bytes, self._mode)
95
101
class DeleteRequest(VfsRequest):
96
102
 
97
103
    def do(self, relpath):
 
104
        relpath = self.translate_client_path(relpath)
98
105
        self._backing_transport.delete(relpath)
99
106
        return request.SuccessfulSmartServerResponse(('ok', ))
100
107
 
102
109
class IterFilesRecursiveRequest(VfsRequest):
103
110
 
104
111
    def do(self, relpath):
 
112
        if not relpath.endswith('/'):
 
113
            relpath += '/'
 
114
        relpath = self.translate_client_path(relpath)
105
115
        transport = self._backing_transport.clone(relpath)
106
116
        filenames = transport.iter_files_recursive()
107
117
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
110
120
class ListDirRequest(VfsRequest):
111
121
 
112
122
    def do(self, relpath):
 
123
        if not relpath.endswith('/'):
 
124
            relpath += '/'
 
125
        relpath = self.translate_client_path(relpath)
113
126
        filenames = self._backing_transport.list_dir(relpath)
114
127
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
115
128
 
117
130
class MkdirRequest(VfsRequest):
118
131
 
119
132
    def do(self, relpath, mode):
 
133
        relpath = self.translate_client_path(relpath)
120
134
        self._backing_transport.mkdir(relpath,
121
135
                                      _deserialise_optional_mode(mode))
122
136
        return request.SuccessfulSmartServerResponse(('ok',))
125
139
class MoveRequest(VfsRequest):
126
140
 
127
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)
128
144
        self._backing_transport.move(rel_from, rel_to)
129
145
        return request.SuccessfulSmartServerResponse(('ok',))
130
146
 
132
148
class PutRequest(VfsRequest):
133
149
 
134
150
    def do(self, relpath, mode):
 
151
        relpath = self.translate_client_path(relpath)
135
152
        self._relpath = relpath
136
153
        self._mode = _deserialise_optional_mode(mode)
137
154
 
143
160
class PutNonAtomicRequest(VfsRequest):
144
161
 
145
162
    def do(self, relpath, mode, create_parent, dir_mode):
 
163
        relpath = self.translate_client_path(relpath)
146
164
        self._relpath = relpath
147
165
        self._dir_mode = _deserialise_optional_mode(dir_mode)
148
166
        self._mode = _deserialise_optional_mode(mode)
161
179
class ReadvRequest(VfsRequest):
162
180
 
163
181
    def do(self, relpath):
 
182
        relpath = self.translate_client_path(relpath)
164
183
        self._relpath = relpath
165
184
 
166
185
    def do_body(self, body_bytes):
184
203
class RenameRequest(VfsRequest):
185
204
 
186
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)
187
208
        self._backing_transport.rename(rel_from, rel_to)
188
209
        return request.SuccessfulSmartServerResponse(('ok', ))
189
210
 
191
212
class RmdirRequest(VfsRequest):
192
213
 
193
214
    def do(self, relpath):
 
215
        relpath = self.translate_client_path(relpath)
194
216
        self._backing_transport.rmdir(relpath)
195
217
        return request.SuccessfulSmartServerResponse(('ok', ))
196
218
 
198
220
class StatRequest(VfsRequest):
199
221
 
200
222
    def do(self, relpath):
 
223
        if not relpath.endswith('/'):
 
224
            relpath += '/'
 
225
        relpath = self.translate_client_path(relpath)
201
226
        stat = self._backing_transport.stat(relpath)
202
227
        return request.SuccessfulSmartServerResponse(
203
228
            ('stat', str(stat.st_size), oct(stat.st_mode)))