~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/chroot.py

Completely rework chrooted transports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
from urlparse import urlparse
21
21
 
22
22
from bzrlib import errors, urlutils
 
23
from bzrlib.transport import (
 
24
    get_transport,
 
25
    register_transport,
 
26
    Server,
 
27
    Transport,
 
28
    unregister_transport,
 
29
    )
23
30
from bzrlib.transport.decorator import TransportDecorator, DecoratorServer
24
 
 
25
 
 
26
 
class ChrootTransportDecorator(TransportDecorator):
27
 
    """A decorator that can convert any transport to be chrooted.
28
 
 
29
 
    This is requested via the 'chroot+' prefix to get_transport().
30
 
 
31
 
    :ivar chroot_url: the root of this chroot
32
 
    :ivar chroot_relative: this transport's location relative to the chroot
33
 
        root.  e.g. A chroot_relative of '/' means this location is the same as
34
 
        chroot_url.
35
 
    """
36
 
 
37
 
    def __init__(self, url, _decorated=None, chroot=None):
38
 
        super(ChrootTransportDecorator, self).__init__(url,
39
 
                _decorated=_decorated)
40
 
        if chroot is None:
41
 
            self.chroot_url = self._decorated.base
42
 
        else:
43
 
            self.chroot_url = chroot
44
 
        self.chroot_relative = '/' + self._decorated.base[len(self.chroot_url):]
45
 
 
46
 
    @classmethod
47
 
    def _get_url_prefix(self):
48
 
        """Chroot transport decorators are invoked via 'chroot+'"""
49
 
        return 'chroot+'
50
 
 
51
 
    def _ensure_relpath_is_child(self, relpath):
52
 
        abspath = self.abspath(relpath)
53
 
        chroot_base = self._get_url_prefix() + self.chroot_url
54
 
        real_relpath = urlutils.relative_url(chroot_base, abspath)
55
 
        if real_relpath == '..' or real_relpath.startswith('../'):
56
 
            raise errors.PathNotChild(relpath, self.chroot_url)
57
 
 
58
 
    # decorated methods
 
31
from bzrlib.transport.memory import MemoryTransport
 
32
 
 
33
 
 
34
class ChrootServer(Server):
 
35
    """Server for chroot transports."""
 
36
 
 
37
    def __init__(self, backing_transport):
 
38
        self.backing_transport = backing_transport
 
39
 
 
40
    def _factory(self, url):
 
41
        assert url.startswith(self.scheme)
 
42
        return ChrootTransport(self, url)
 
43
 
 
44
    def get_url(self):
 
45
        return self.scheme
 
46
 
 
47
    def setUp(self):
 
48
        self.scheme = 'chroot-%d:///' % id(self)
 
49
        register_transport(self.scheme, self._factory)
 
50
 
 
51
    def tearDown(self):
 
52
        unregister_transport(self.scheme, self._factory)
 
53
 
 
54
 
 
55
class ChrootTransport(Transport):
 
56
 
 
57
    def __init__(self, server, base):
 
58
        self.server = server
 
59
        if not base.endswith('/'):
 
60
            base += '/'
 
61
        Transport.__init__(self, base)
 
62
        self.base_path = self.base[len(self.server.scheme)-1:]
 
63
        self.scheme = self.server.scheme
 
64
 
 
65
    def _call(self, methodname, relpath, *args):
 
66
        method = getattr(self.server.backing_transport, methodname)
 
67
        return method(self._safe_relpath(relpath), *args)
 
68
 
 
69
    def _safe_relpath(self, relpath):
 
70
        safe_relpath = self._combine_paths(self.base_path, relpath)
 
71
        assert safe_relpath.startswith('/')
 
72
        return safe_relpath[1:]
 
73
 
 
74
    # Transport methods
59
75
    def abspath(self, relpath):
60
 
        try:
61
 
            url = urlutils.join('fake:///', relpath)
62
 
        except errors.InvalidURLJoin:
63
 
            raise errors.PathNotChild(relpath, self.chroot_url)
64
 
        normalised_abspath = url[len('fake:///'):]
65
 
        return self._get_url_prefix() + self.chroot_url + normalised_abspath[1:]
 
76
        return self.scheme + self._safe_relpath(relpath)
66
77
 
67
78
    def append_file(self, relpath, f, mode=None):
68
 
        self._ensure_relpath_is_child(relpath)
69
 
        return TransportDecorator.append_file(self, relpath, f, mode=mode)
70
 
 
71
 
    def append_bytes(self, relpath, bytes, mode=None):
72
 
        self._ensure_relpath_is_child(relpath)
73
 
        return TransportDecorator.append_bytes(self, relpath, bytes, mode=mode)
74
 
 
75
 
    def clone(self, offset=None):
76
 
        if offset is None: return self
77
 
        # the new URL we want to clone to is
78
 
        # self.chroot_url + an adjusted self.chroot_relative, with the leading
79
 
        # / removed.
80
 
        new_relpath = urlutils.joinpath(self.chroot_relative, offset)
81
 
        assert new_relpath.startswith('/')
82
 
        new_url = self.chroot_url + new_relpath[1:]
83
 
        # Clone the decorated transport according to this new path.
84
 
        assert new_url.startswith(self.chroot_url), (
85
 
            'new_url (%r) does not start with %r'
86
 
            % (new_url, self._decorated.base))
87
 
        path = urlutils.relative_url(self._decorated.base, new_url)
88
 
        decorated_clone = self._decorated.clone(path)
89
 
        return ChrootTransportDecorator(self._get_url_prefix() + new_url,
90
 
            decorated_clone, self.chroot_url)
 
79
        return self._call('append_file', relpath, f, mode)
 
80
 
 
81
    def clone(self, relpath):
 
82
        return ChrootTransport(self.server, self.abspath(relpath))
91
83
 
92
84
    def delete(self, relpath):
93
 
        self._ensure_relpath_is_child(relpath)
94
 
        return TransportDecorator.delete(self, relpath)
 
85
        return self._call('delete', relpath)
95
86
 
96
87
    def delete_tree(self, relpath):
97
 
        self._ensure_relpath_is_child(relpath)
98
 
        return TransportDecorator.delete_tree(self, relpath)
 
88
        return self._call('delete_tree', relpath)
99
89
 
100
90
    def get(self, relpath):
101
 
        self._ensure_relpath_is_child(relpath)
102
 
        return TransportDecorator.get(self, relpath)
103
 
 
104
 
    def get_bytes(self, relpath):
105
 
        self._ensure_relpath_is_child(relpath)
106
 
        return TransportDecorator.get_bytes(self, relpath)
 
91
        return self._call('get', relpath)
107
92
 
108
93
    def has(self, relpath):
109
 
        self._ensure_relpath_is_child(relpath)
110
 
        return TransportDecorator.has(self, relpath)
 
94
        return self._call('has', relpath)
 
95
 
 
96
    def iter_files_recursive(self):
 
97
        backing_transport = self.server.backing_transport.clone(
 
98
            self._safe_relpath('.'))
 
99
        return backing_transport.iter_files_recursive()
 
100
 
 
101
    def listable(self):
 
102
        return self.server.backing_transport.listable()
111
103
 
112
104
    def list_dir(self, relpath):
113
 
        self._ensure_relpath_is_child(relpath)
114
 
        return TransportDecorator.list_dir(self, relpath)
 
105
        return self._call('list_dir', relpath)
115
106
 
116
107
    def lock_read(self, relpath):
117
 
        self._ensure_relpath_is_child(relpath)
118
 
        return TransportDecorator.lock_read(self, relpath)
 
108
        return self._call('lock_read', relpath)
119
109
 
120
110
    def lock_write(self, relpath):
121
 
        self._ensure_relpath_is_child(relpath)
122
 
        return TransportDecorator.lock_write(self, relpath)
 
111
        return self._call('lock_write', relpath)
123
112
 
124
113
    def mkdir(self, relpath, mode=None):
125
 
        self._ensure_relpath_is_child(relpath)
126
 
        return TransportDecorator.mkdir(self, relpath, mode=mode)
127
 
 
128
 
    def put_bytes(self, relpath, bytes, mode=None):
129
 
        self._ensure_relpath_is_child(relpath)
130
 
        return TransportDecorator.put_bytes(self, relpath, bytes, mode=mode)
 
114
        return self._call('mkdir', relpath, mode)
131
115
 
132
116
    def put_file(self, relpath, f, mode=None):
133
 
        self._ensure_relpath_is_child(relpath)
134
 
        return TransportDecorator.put_file(self, relpath, f, mode=mode)
 
117
        return self._call('put_file', relpath, f, mode)
135
118
 
136
119
    def rename(self, rel_from, rel_to):
137
 
        self._ensure_relpath_is_child(rel_from)
138
 
        self._ensure_relpath_is_child(rel_to)
139
 
        return TransportDecorator.rename(self, rel_from, rel_to)
 
120
        return self._call('rename', rel_from, self._safe_relpath(rel_to))
140
121
 
141
122
    def rmdir(self, relpath):
142
 
        self._ensure_relpath_is_child(relpath)
143
 
        return TransportDecorator.rmdir(self, relpath)
 
123
        return self._call('rmdir', relpath)
144
124
 
145
125
    def stat(self, relpath):
146
 
        self._ensure_relpath_is_child(relpath)
147
 
        return TransportDecorator.stat(self, relpath)
148
 
 
149
 
 
150
 
class ChrootServer(DecoratorServer):
151
 
    """Server for the ReadonlyTransportDecorator for testing with."""
152
 
 
153
 
    def get_decorator_class(self):
154
 
        return ChrootTransportDecorator
 
126
        return self._call('stat', relpath)
 
127
 
 
128
 
 
129
class TestingChrootServer(ChrootServer):
 
130
 
 
131
    def __init__(self):
 
132
        ChrootServer.__init__(self, get_transport('.'))
155
133
 
156
134
 
157
135
def get_test_permutations():
158
136
    """Return the permutations to be used in testing."""
159
 
    return [(ChrootTransportDecorator, ChrootServer),
 
137
    return [(ChrootTransport, TestingChrootServer),
160
138
            ]