/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 breezy/bzr/smart/vfs.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-29 19:54:32 UTC
  • mto: This revision was merged to the branch mainline in revision 7378.
  • Revision ID: jelmer@jelmer.uk-20190629195432-xuqzgxejnzq6gs2n
Use more ExitStacks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.
 
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.
 
25
"""
 
26
 
 
27
from __future__ import absolute_import
 
28
 
 
29
import os
 
30
 
 
31
from ... import urlutils
 
32
from . import request
 
33
 
 
34
 
 
35
def _deserialise_optional_mode(mode):
 
36
    # XXX: FIXME this should be on the protocol object.  Later protocol versions
 
37
    # might serialise modes differently.
 
38
    if mode == b'':
 
39
        return None
 
40
    else:
 
41
        return int(mode)
 
42
 
 
43
 
 
44
def vfs_enabled():
 
45
    """Is the VFS enabled ?
 
46
 
 
47
    the VFS is disabled when the BRZ_NO_SMART_VFS environment variable is set.
 
48
 
 
49
    :return: ``True`` if it is enabled.
 
50
    """
 
51
    return 'BRZ_NO_SMART_VFS' not in os.environ
 
52
 
 
53
 
 
54
class VfsRequest(request.SmartServerRequest):
 
55
    """Base class for VFS requests.
 
56
 
 
57
    VFS requests are disabled if vfs_enabled() returns False.
 
58
    """
 
59
 
 
60
    def _check_enabled(self):
 
61
        if not vfs_enabled():
 
62
            raise request.DisabledMethod(self.__class__.__name__)
 
63
 
 
64
    def translate_client_path(self, relpath):
 
65
        # VFS requests are made with escaped paths so the escaping done in
 
66
        # SmartServerRequest.translate_client_path leads to double escaping.
 
67
        # Remove it here -- the fact that the result is still escaped means
 
68
        # that the str() will not fail on valid input.
 
69
        x = request.SmartServerRequest.translate_client_path(self, relpath)
 
70
        return str(urlutils.unescape(x))
 
71
 
 
72
 
 
73
class HasRequest(VfsRequest):
 
74
 
 
75
    def do(self, relpath):
 
76
        relpath = self.translate_client_path(relpath)
 
77
        r = self._backing_transport.has(relpath) and b'yes' or b'no'
 
78
        return request.SuccessfulSmartServerResponse((r,))
 
79
 
 
80
 
 
81
class GetRequest(VfsRequest):
 
82
 
 
83
    def do(self, relpath):
 
84
        relpath = self.translate_client_path(relpath)
 
85
        backing_bytes = self._backing_transport.get_bytes(relpath)
 
86
        return request.SuccessfulSmartServerResponse((b'ok',), backing_bytes)
 
87
 
 
88
 
 
89
class AppendRequest(VfsRequest):
 
90
 
 
91
    def do(self, relpath, mode):
 
92
        relpath = self.translate_client_path(relpath)
 
93
        self._relpath = relpath
 
94
        self._mode = _deserialise_optional_mode(mode)
 
95
 
 
96
    def do_body(self, body_bytes):
 
97
        old_length = self._backing_transport.append_bytes(
 
98
            self._relpath, body_bytes, self._mode)
 
99
        return request.SuccessfulSmartServerResponse((b'appended', str(old_length).encode('ascii')))
 
100
 
 
101
 
 
102
class DeleteRequest(VfsRequest):
 
103
 
 
104
    def do(self, relpath):
 
105
        relpath = self.translate_client_path(relpath)
 
106
        self._backing_transport.delete(relpath)
 
107
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
108
 
 
109
 
 
110
class IterFilesRecursiveRequest(VfsRequest):
 
111
 
 
112
    def do(self, relpath):
 
113
        if not relpath.endswith(b'/'):
 
114
            relpath += b'/'
 
115
        relpath = self.translate_client_path(relpath)
 
116
        transport = self._backing_transport.clone(relpath)
 
117
        filenames = transport.iter_files_recursive()
 
118
        return request.SuccessfulSmartServerResponse((b'names',) + tuple(filenames))
 
119
 
 
120
 
 
121
class ListDirRequest(VfsRequest):
 
122
 
 
123
    def do(self, relpath):
 
124
        if not relpath.endswith(b'/'):
 
125
            relpath += b'/'
 
126
        relpath = self.translate_client_path(relpath)
 
127
        filenames = self._backing_transport.list_dir(relpath)
 
128
        return request.SuccessfulSmartServerResponse((b'names',) + tuple([filename.encode('utf-8') for filename in filenames]))
 
129
 
 
130
 
 
131
class MkdirRequest(VfsRequest):
 
132
 
 
133
    def do(self, relpath, mode):
 
134
        relpath = self.translate_client_path(relpath)
 
135
        self._backing_transport.mkdir(relpath,
 
136
                                      _deserialise_optional_mode(mode))
 
137
        return request.SuccessfulSmartServerResponse((b'ok',))
 
138
 
 
139
 
 
140
class MoveRequest(VfsRequest):
 
141
 
 
142
    def do(self, rel_from, rel_to):
 
143
        rel_from = self.translate_client_path(rel_from)
 
144
        rel_to = self.translate_client_path(rel_to)
 
145
        self._backing_transport.move(rel_from, rel_to)
 
146
        return request.SuccessfulSmartServerResponse((b'ok',))
 
147
 
 
148
 
 
149
class PutRequest(VfsRequest):
 
150
 
 
151
    def do(self, relpath, mode):
 
152
        relpath = self.translate_client_path(relpath)
 
153
        self._relpath = relpath
 
154
        self._mode = _deserialise_optional_mode(mode)
 
155
 
 
156
    def do_body(self, body_bytes):
 
157
        self._backing_transport.put_bytes(
 
158
            self._relpath, body_bytes, self._mode)
 
159
        return request.SuccessfulSmartServerResponse((b'ok',))
 
160
 
 
161
 
 
162
class PutNonAtomicRequest(VfsRequest):
 
163
 
 
164
    def do(self, relpath, mode, create_parent, dir_mode):
 
165
        relpath = self.translate_client_path(relpath)
 
166
        self._relpath = relpath
 
167
        self._dir_mode = _deserialise_optional_mode(dir_mode)
 
168
        self._mode = _deserialise_optional_mode(mode)
 
169
        # a boolean would be nicer XXX
 
170
        self._create_parent = (create_parent == b'T')
 
171
 
 
172
    def do_body(self, body_bytes):
 
173
        self._backing_transport.put_bytes_non_atomic(self._relpath,
 
174
                                                     body_bytes,
 
175
                                                     mode=self._mode,
 
176
                                                     create_parent_dir=self._create_parent,
 
177
                                                     dir_mode=self._dir_mode)
 
178
        return request.SuccessfulSmartServerResponse((b'ok',))
 
179
 
 
180
 
 
181
class ReadvRequest(VfsRequest):
 
182
 
 
183
    def do(self, relpath):
 
184
        relpath = self.translate_client_path(relpath)
 
185
        self._relpath = relpath
 
186
 
 
187
    def do_body(self, body_bytes):
 
188
        """accept offsets for a readv request."""
 
189
        offsets = self._deserialise_offsets(body_bytes)
 
190
        backing_bytes = b''.join(bytes for offset, bytes in
 
191
                                 self._backing_transport.readv(self._relpath, offsets))
 
192
        return request.SuccessfulSmartServerResponse((b'readv',), backing_bytes)
 
193
 
 
194
    def _deserialise_offsets(self, text):
 
195
        # XXX: FIXME this should be on the protocol object.
 
196
        offsets = []
 
197
        for line in text.split(b'\n'):
 
198
            if not line:
 
199
                continue
 
200
            start, length = line.split(b',')
 
201
            offsets.append((int(start), int(length)))
 
202
        return offsets
 
203
 
 
204
 
 
205
class RenameRequest(VfsRequest):
 
206
 
 
207
    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
        self._backing_transport.rename(rel_from, rel_to)
 
211
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
212
 
 
213
 
 
214
class RmdirRequest(VfsRequest):
 
215
 
 
216
    def do(self, relpath):
 
217
        relpath = self.translate_client_path(relpath)
 
218
        self._backing_transport.rmdir(relpath)
 
219
        return request.SuccessfulSmartServerResponse((b'ok', ))
 
220
 
 
221
 
 
222
class StatRequest(VfsRequest):
 
223
 
 
224
    def do(self, relpath):
 
225
        if not relpath.endswith(b'/'):
 
226
            relpath += b'/'
 
227
        relpath = self.translate_client_path(relpath)
 
228
        stat = self._backing_transport.stat(relpath)
 
229
        return request.SuccessfulSmartServerResponse(
 
230
            (b'stat', str(stat.st_size).encode('ascii'), oct(stat.st_mode).encode('ascii')))