36
39
directories of the backing transport are not visible. The chroot url will
37
40
not allow '..' sequences to result in requests to the chroot affecting
38
41
directories outside the backing transport.
40
PathFilteringServer does all the path sanitation needed to enforce a
41
chroot, so this is a simple subclass of PathFilteringServer that ignores
45
44
def __init__(self, backing_transport):
46
pathfilter.PathFilteringServer.__init__(self, backing_transport, None)
45
self.backing_transport = backing_transport
48
47
def _factory(self, url):
49
48
return ChrootTransport(self, url)
51
def start_server(self):
52
54
self.scheme = 'chroot-%d:///' % id(self)
53
55
register_transport(self.scheme, self._factory)
56
class ChrootTransport(pathfilter.PathFilteringTransport):
58
unregister_transport(self.scheme, self._factory)
61
class ChrootTransport(Transport):
57
62
"""A ChrootTransport.
59
64
Please see ChrootServer for details.
62
def _filter(self, relpath):
63
# A simplified version of PathFilteringTransport's _filter that omits
64
# the call to self.server.filter_func.
65
return self._relpath_from_server_root(relpath)
67
def __init__(self, server, base):
69
if not base.endswith('/'):
71
Transport.__init__(self, base)
72
self.base_path = self.base[len(self.server.scheme)-1:]
73
self.scheme = self.server.scheme
75
def _call(self, methodname, relpath, *args):
76
method = getattr(self.server.backing_transport, methodname)
77
return method(self._safe_relpath(relpath), *args)
79
def _safe_relpath(self, relpath):
80
safe_relpath = self._combine_paths(self.base_path, relpath)
81
if not safe_relpath.startswith('/'):
82
raise ValueError(safe_relpath)
83
return safe_relpath[1:]
86
def abspath(self, relpath):
87
return self.scheme + self._safe_relpath(relpath)
89
def append_file(self, relpath, f, mode=None):
90
return self._call('append_file', relpath, f, mode)
92
def _can_roundtrip_unix_modebits(self):
93
return self.server.backing_transport._can_roundtrip_unix_modebits()
95
def clone(self, relpath):
96
return ChrootTransport(self.server, self.abspath(relpath))
98
def delete(self, relpath):
99
return self._call('delete', relpath)
101
def delete_tree(self, relpath):
102
return self._call('delete_tree', relpath)
104
def external_url(self):
105
"""See bzrlib.transport.Transport.external_url."""
106
# Chroots, like MemoryTransport depend on in-process
107
# state and thus the base cannot simply be handed out.
108
# See the base class docstring for more details and
109
# possible directions. For now we return the chrooted
111
return self.server.backing_transport.external_url()
113
def get(self, relpath):
114
return self._call('get', relpath)
116
def has(self, relpath):
117
return self._call('has', relpath)
119
def is_readonly(self):
120
return self.server.backing_transport.is_readonly()
122
def iter_files_recursive(self):
123
backing_transport = self.server.backing_transport.clone(
124
self._safe_relpath('.'))
125
return backing_transport.iter_files_recursive()
128
return self.server.backing_transport.listable()
130
def list_dir(self, relpath):
131
return self._call('list_dir', relpath)
133
def lock_read(self, relpath):
134
return self._call('lock_read', relpath)
136
def lock_write(self, relpath):
137
return self._call('lock_write', relpath)
139
def mkdir(self, relpath, mode=None):
140
return self._call('mkdir', relpath, mode)
142
def open_write_stream(self, relpath, mode=None):
143
return self._call('open_write_stream', relpath, mode)
145
def put_file(self, relpath, f, mode=None):
146
return self._call('put_file', relpath, f, mode)
148
def rename(self, rel_from, rel_to):
149
return self._call('rename', rel_from, self._safe_relpath(rel_to))
151
def rmdir(self, relpath):
152
return self._call('rmdir', relpath)
154
def stat(self, relpath):
155
return self._call('stat', relpath)
158
class TestingChrootServer(ChrootServer):
161
"""TestingChrootServer is not usable until setUp is called."""
163
def setUp(self, backing_server=None):
164
"""Setup the Chroot on backing_server."""
165
if backing_server is not None:
166
self.backing_transport = get_transport(backing_server.get_url())
168
self.backing_transport = get_transport('.')
169
ChrootServer.setUp(self)
68
172
def get_test_permutations():
69
173
"""Return the permutations to be used in testing."""
70
from bzrlib.tests import test_server
71
return [(ChrootTransport, test_server.TestingChrootServer)]
174
return [(ChrootTransport, TestingChrootServer),