~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/vfs.py

  • Committer: Martin Pool
  • Date: 2006-04-20 02:37:21 UTC
  • mto: This revision was merged to the branch mainline in revision 1675.
  • Revision ID: mbp@sourcefrog.net-20060420023721-04d8a3b015987240
Add .hg to default ignore list

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
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
16
 
 
17
 
"""VFS operations for the smart server.
18
 
 
19
 
This module defines the smart server methods that are low-level file operations
20
 
higher-level concepts like branches and revisions.
21
 
 
22
 
These methods, plus 'hello' and 'get_bundle', are version 1 of the smart server
23
 
protocol, as implemented in bzr 0.11 and later.
24
 
"""
25
 
 
26
 
import os
27
 
 
28
 
from bzrlib import errors
29
 
from bzrlib.smart import request
30
 
 
31
 
 
32
 
def _deserialise_optional_mode(mode):
33
 
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
34
 
    # might serialise modes differently.
35
 
    if mode == '':
36
 
        return None
37
 
    else:
38
 
        return int(mode)
39
 
 
40
 
 
41
 
def vfs_enabled():
42
 
    """Is the VFS enabled ?
43
 
 
44
 
    the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
45
 
 
46
 
    :return: True if it is enabled.
47
 
    """
48
 
    return not 'BZR_NO_SMART_VFS' in os.environ
49
 
 
50
 
 
51
 
class VfsRequest(request.SmartServerRequest):
52
 
    """Base class for VFS requests.
53
 
    
54
 
    VFS requests are disabled if vfs_enabled() returns False.
55
 
    """
56
 
 
57
 
    def _check_enabled(self):
58
 
        if not vfs_enabled():
59
 
            raise errors.DisabledMethod(self.__class__.__name__)
60
 
 
61
 
 
62
 
class HasRequest(VfsRequest):
63
 
 
64
 
    def do(self, relpath):
65
 
        relpath = self.translate_client_path(relpath)
66
 
        r = self._backing_transport.has(relpath) and 'yes' or 'no'
67
 
        return request.SuccessfulSmartServerResponse((r,))
68
 
 
69
 
 
70
 
class GetRequest(VfsRequest):
71
 
 
72
 
    def do(self, relpath):
73
 
        relpath = self.translate_client_path(relpath)
74
 
        try:
75
 
            backing_bytes = self._backing_transport.get_bytes(relpath)
76
 
        except errors.ReadError:
77
 
            # cannot read the file
78
 
            return request.FailedSmartServerResponse(('ReadError', ))
79
 
        except errors.PermissionDenied:
80
 
            return request.FailedSmartServerResponse(('PermissionDenied',))
81
 
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
82
 
 
83
 
 
84
 
class AppendRequest(VfsRequest):
85
 
 
86
 
    def do(self, relpath, mode):
87
 
        relpath = self.translate_client_path(relpath)
88
 
        self._relpath = relpath
89
 
        self._mode = _deserialise_optional_mode(mode)
90
 
    
91
 
    def do_body(self, body_bytes):
92
 
        old_length = self._backing_transport.append_bytes(
93
 
            self._relpath, body_bytes, self._mode)
94
 
        return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
95
 
 
96
 
 
97
 
class DeleteRequest(VfsRequest):
98
 
 
99
 
    def do(self, relpath):
100
 
        relpath = self.translate_client_path(relpath)
101
 
        self._backing_transport.delete(relpath)
102
 
        return request.SuccessfulSmartServerResponse(('ok', ))
103
 
 
104
 
 
105
 
class IterFilesRecursiveRequest(VfsRequest):
106
 
 
107
 
    def do(self, relpath):
108
 
        if not relpath.endswith('/'):
109
 
            relpath += '/'
110
 
        relpath = self.translate_client_path(relpath)
111
 
        transport = self._backing_transport.clone(relpath)
112
 
        filenames = transport.iter_files_recursive()
113
 
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
114
 
 
115
 
 
116
 
class ListDirRequest(VfsRequest):
117
 
 
118
 
    def do(self, relpath):
119
 
        if not relpath.endswith('/'):
120
 
            relpath += '/'
121
 
        relpath = self.translate_client_path(relpath)
122
 
        filenames = self._backing_transport.list_dir(relpath)
123
 
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
124
 
 
125
 
 
126
 
class MkdirRequest(VfsRequest):
127
 
 
128
 
    def do(self, relpath, mode):
129
 
        relpath = self.translate_client_path(relpath)
130
 
        self._backing_transport.mkdir(relpath,
131
 
                                      _deserialise_optional_mode(mode))
132
 
        return request.SuccessfulSmartServerResponse(('ok',))
133
 
 
134
 
 
135
 
class MoveRequest(VfsRequest):
136
 
 
137
 
    def do(self, rel_from, rel_to):
138
 
        rel_from = self.translate_client_path(rel_from)
139
 
        rel_to = self.translate_client_path(rel_to)
140
 
        self._backing_transport.move(rel_from, rel_to)
141
 
        return request.SuccessfulSmartServerResponse(('ok',))
142
 
 
143
 
 
144
 
class PutRequest(VfsRequest):
145
 
 
146
 
    def do(self, relpath, mode):
147
 
        relpath = self.translate_client_path(relpath)
148
 
        self._relpath = relpath
149
 
        self._mode = _deserialise_optional_mode(mode)
150
 
 
151
 
    def do_body(self, body_bytes):
152
 
        self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
153
 
        return request.SuccessfulSmartServerResponse(('ok',))
154
 
 
155
 
 
156
 
class PutNonAtomicRequest(VfsRequest):
157
 
 
158
 
    def do(self, relpath, mode, create_parent, dir_mode):
159
 
        relpath = self.translate_client_path(relpath)
160
 
        self._relpath = relpath
161
 
        self._dir_mode = _deserialise_optional_mode(dir_mode)
162
 
        self._mode = _deserialise_optional_mode(mode)
163
 
        # a boolean would be nicer XXX
164
 
        self._create_parent = (create_parent == 'T')
165
 
 
166
 
    def do_body(self, body_bytes):
167
 
        self._backing_transport.put_bytes_non_atomic(self._relpath,
168
 
                body_bytes,
169
 
                mode=self._mode,
170
 
                create_parent_dir=self._create_parent,
171
 
                dir_mode=self._dir_mode)
172
 
        return request.SuccessfulSmartServerResponse(('ok',))
173
 
 
174
 
 
175
 
class ReadvRequest(VfsRequest):
176
 
 
177
 
    def do(self, relpath):
178
 
        relpath = self.translate_client_path(relpath)
179
 
        self._relpath = relpath
180
 
 
181
 
    def do_body(self, body_bytes):
182
 
        """accept offsets for a readv request."""
183
 
        offsets = self._deserialise_offsets(body_bytes)
184
 
        backing_bytes = ''.join(bytes for offset, bytes in
185
 
            self._backing_transport.readv(self._relpath, offsets))
186
 
        return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
187
 
 
188
 
    def _deserialise_offsets(self, text):
189
 
        # XXX: FIXME this should be on the protocol object.
190
 
        offsets = []
191
 
        for line in text.split('\n'):
192
 
            if not line:
193
 
                continue
194
 
            start, length = line.split(',')
195
 
            offsets.append((int(start), int(length)))
196
 
        return offsets
197
 
 
198
 
 
199
 
class RenameRequest(VfsRequest):
200
 
 
201
 
    def do(self, rel_from, rel_to):
202
 
        rel_from = self.translate_client_path(rel_from)
203
 
        rel_to = self.translate_client_path(rel_to)
204
 
        self._backing_transport.rename(rel_from, rel_to)
205
 
        return request.SuccessfulSmartServerResponse(('ok', ))
206
 
 
207
 
 
208
 
class RmdirRequest(VfsRequest):
209
 
 
210
 
    def do(self, relpath):
211
 
        relpath = self.translate_client_path(relpath)
212
 
        self._backing_transport.rmdir(relpath)
213
 
        return request.SuccessfulSmartServerResponse(('ok', ))
214
 
 
215
 
 
216
 
class StatRequest(VfsRequest):
217
 
 
218
 
    def do(self, relpath):
219
 
        if not relpath.endswith('/'):
220
 
            relpath += '/'
221
 
        relpath = self.translate_client_path(relpath)
222
 
        stat = self._backing_transport.stat(relpath)
223
 
        return request.SuccessfulSmartServerResponse(
224
 
            ('stat', str(stat.st_size), oct(stat.st_mode)))
225