1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
7 from docs_server_utils import StringIdentity
8 from file_system import FileSystem
9 from future import Gettable, Future
12 class ChrootFileSystem(FileSystem):
13 '''ChrootFileSystem(fs, path) exposes a FileSystem whose root is |path| inside
14 |fs|, so ChrootFileSystem(fs, '/hello').Read(['/world']) is equivalent to
15 fs.Read(['/hello/world']) with the '/hello' prefix stripped from the result.
18 def __init__(self, file_system, root):
20 |file_system| The FileSystem instance to transpose paths of.
21 |root| The path to transpose all Read/Stat calls by.
23 self._file_system = file_system
24 self._root = root.strip('/')
26 def Read(self, paths, binary=False):
27 # Maintain reverse mapping so the result can be mapped to the original
28 # paths given (the result from |file_system| will include |root| in the
29 # result, which would be wrong).
32 prefixed = posixpath.join(self._root, path)
33 prefixed_paths[prefixed] = path
35 future_result = self._file_system.Read(
36 tuple(prefix(path) for path in paths), binary=binary)
38 return dict((prefixed_paths[path], content)
39 for path, content in future_result.Get().iteritems())
40 return Future(delegate=Gettable(resolve))
43 return self._file_system.Stat(posixpath.join(self._root, path))
45 def GetIdentity(self):
46 return StringIdentity(
47 '%s/%s' % (self._file_system.GetIdentity(), self._root))