~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/vfs.py

  • Committer: John Arbash Meinel
  • Date: 2008-03-20 15:10:05 UTC
  • mto: This revision was merged to the branch mainline in revision 3299.
  • Revision ID: john@arbash-meinel.com-20080320151005-z9lajjy69m20of17
uncommit --local in an unbound branch raises the same exception as commit --local

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