/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2402.1.2 by Andrew Bennetts
Deal with review comments.
1
# Copyright (C) 2006, 2007 Canonical Ltd
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
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
-- i.e. methods that operate directly on files and directories, rather than
21
higher-level concepts like branches and revisions.
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
22
23
These methods, plus 'hello' and 'get_bundle', are version 1 of the smart server
24
protocol, as implemented in bzr 0.11 and later.
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
25
"""
26
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
27
import os
28
29
from bzrlib import errors
2018.5.21 by Andrew Bennetts
Move bzrlib.transport.smart to bzrlib.smart
30
from bzrlib.smart import request
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
31
32
33
def _deserialise_optional_mode(mode):
34
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
35
    # might serialise modes differently.
36
    if mode == '':
37
        return None
38
    else:
39
        return int(mode)
40
41
2402.1.1 by Andrew Bennetts
Use the Command pattern for handling smart server commands.
42
def vfs_enabled():
43
    """Is the VFS enabled ?
44
2402.1.2 by Andrew Bennetts
Deal with review comments.
45
    the VFS is disabled when the BZR_NO_SMART_VFS environment variable is set.
2402.1.1 by Andrew Bennetts
Use the Command pattern for handling smart server commands.
46
47
    :return: True if it is enabled.
48
    """
2402.1.2 by Andrew Bennetts
Deal with review comments.
49
    return not 'BZR_NO_SMART_VFS' in os.environ
2402.1.1 by Andrew Bennetts
Use the Command pattern for handling smart server commands.
50
51
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
52
class VfsRequest(request.SmartServerRequest):
53
    """Base class for VFS requests.
54
    
2402.1.1 by Andrew Bennetts
Use the Command pattern for handling smart server commands.
55
    VFS requests are disabled if vfs_enabled() returns False.
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
56
    """
57
58
    def _check_enabled(self):
2402.1.1 by Andrew Bennetts
Use the Command pattern for handling smart server commands.
59
        if not vfs_enabled():
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
60
            raise errors.DisabledMethod(self.__class__.__name__)
61
62
63
class HasRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
64
65
    def do(self, relpath):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
66
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
67
        r = self._backing_transport.has(relpath) and 'yes' or 'no'
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
68
        return request.SuccessfulSmartServerResponse((r,))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
69
70
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
71
class GetRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
72
73
    def do(self, relpath):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
74
        relpath = self.translate_client_path(relpath)
2052.6.2 by Robert Collins
Merge bzr.dev.
75
        try:
76
            backing_bytes = self._backing_transport.get_bytes(relpath)
77
        except errors.ReadError:
78
            # cannot read the file
79
            return request.FailedSmartServerResponse(('ReadError', ))
3146.4.10 by Aaron Bentley
Add handling of PermissionDenied errors to smart protocol VFS layer
80
        except errors.PermissionDenied:
81
            return request.FailedSmartServerResponse(('PermissionDenied',))
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
82
        return request.SuccessfulSmartServerResponse(('ok',), backing_bytes)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
83
84
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
85
class AppendRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
86
87
    def do(self, relpath, mode):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
88
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
89
        self._relpath = relpath
90
        self._mode = _deserialise_optional_mode(mode)
91
    
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
92
    def do_body(self, body_bytes):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
93
        old_length = self._backing_transport.append_bytes(
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
94
            self._relpath, body_bytes, self._mode)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
95
        return request.SuccessfulSmartServerResponse(('appended', '%d' % old_length))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
96
97
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
98
class DeleteRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
99
100
    def do(self, relpath):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
101
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
102
        self._backing_transport.delete(relpath)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
103
        return request.SuccessfulSmartServerResponse(('ok', ))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
104
105
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
106
class IterFilesRecursiveRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
107
108
    def do(self, relpath):
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
109
        if not relpath.endswith('/'):
110
            relpath += '/'
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
111
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
112
        transport = self._backing_transport.clone(relpath)
113
        filenames = transport.iter_files_recursive()
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
114
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
115
116
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
117
class ListDirRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
118
119
    def do(self, relpath):
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
120
        if not relpath.endswith('/'):
121
            relpath += '/'
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
122
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
123
        filenames = self._backing_transport.list_dir(relpath)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
124
        return request.SuccessfulSmartServerResponse(('names',) + tuple(filenames))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
125
126
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
127
class MkdirRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
128
129
    def do(self, relpath, mode):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
130
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
131
        self._backing_transport.mkdir(relpath,
132
                                      _deserialise_optional_mode(mode))
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
133
        return request.SuccessfulSmartServerResponse(('ok',))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
134
135
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
136
class MoveRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
137
138
    def do(self, rel_from, rel_to):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
139
        rel_from = self.translate_client_path(rel_from)
140
        rel_to = self.translate_client_path(rel_to)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
141
        self._backing_transport.move(rel_from, rel_to)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
142
        return request.SuccessfulSmartServerResponse(('ok',))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
143
144
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
145
class PutRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
146
147
    def do(self, relpath, mode):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
148
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
149
        self._relpath = relpath
150
        self._mode = _deserialise_optional_mode(mode)
151
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
152
    def do_body(self, body_bytes):
153
        self._backing_transport.put_bytes(self._relpath, body_bytes, self._mode)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
154
        return request.SuccessfulSmartServerResponse(('ok',))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
155
156
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
157
class PutNonAtomicRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
158
159
    def do(self, relpath, mode, create_parent, dir_mode):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
160
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
161
        self._relpath = relpath
162
        self._dir_mode = _deserialise_optional_mode(dir_mode)
163
        self._mode = _deserialise_optional_mode(mode)
164
        # a boolean would be nicer XXX
165
        self._create_parent = (create_parent == 'T')
166
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
167
    def do_body(self, body_bytes):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
168
        self._backing_transport.put_bytes_non_atomic(self._relpath,
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
169
                body_bytes,
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
170
                mode=self._mode,
171
                create_parent_dir=self._create_parent,
172
                dir_mode=self._dir_mode)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
173
        return request.SuccessfulSmartServerResponse(('ok',))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
174
175
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
176
class ReadvRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
177
178
    def do(self, relpath):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
179
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
180
        self._relpath = relpath
181
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
182
    def do_body(self, body_bytes):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
183
        """accept offsets for a readv request."""
2018.5.5 by Andrew Bennetts
Pass body_bytes directly to SmartServerRequest.do_body
184
        offsets = self._deserialise_offsets(body_bytes)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
185
        backing_bytes = ''.join(bytes for offset, bytes in
186
            self._backing_transport.readv(self._relpath, offsets))
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
187
        return request.SuccessfulSmartServerResponse(('readv',), backing_bytes)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
188
189
    def _deserialise_offsets(self, text):
190
        # XXX: FIXME this should be on the protocol object.
191
        offsets = []
192
        for line in text.split('\n'):
193
            if not line:
194
                continue
195
            start, length = line.split(',')
196
            offsets.append((int(start), int(length)))
197
        return offsets
198
199
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
200
class RenameRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
201
202
    def do(self, rel_from, rel_to):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
203
        rel_from = self.translate_client_path(rel_from)
204
        rel_to = self.translate_client_path(rel_to)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
205
        self._backing_transport.rename(rel_from, rel_to)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
206
        return request.SuccessfulSmartServerResponse(('ok', ))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
207
208
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
209
class RmdirRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
210
211
    def do(self, relpath):
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
212
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
213
        self._backing_transport.rmdir(relpath)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
214
        return request.SuccessfulSmartServerResponse(('ok', ))
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
215
216
2018.5.37 by Andrew Bennetts
Make sure all the request handlers in bzrlib/smart/vfs.py have consistent names.
217
class StatRequest(VfsRequest):
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
218
219
    def do(self, relpath):
2692.1.11 by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured.
220
        if not relpath.endswith('/'):
221
            relpath += '/'
2692.1.5 by Andrew Bennetts
Add translate_client_path calls to bzrlib/smart/vfs.py
222
        relpath = self.translate_client_path(relpath)
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
223
        stat = self._backing_transport.stat(relpath)
2432.4.5 by Robert Collins
Make using SuccessfulSmartServerResponse and FailedSmartServerResponse mandatory rather than optional in smart server logic.
224
        return request.SuccessfulSmartServerResponse(
2018.5.4 by Andrew Bennetts
Split smart server VFS logic out into a new file, and start using the command pattern in the SmartServerRequestHandler.
225
            ('stat', str(stat.st_size), oct(stat.st_mode)))
226