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 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
, skip_not_found
=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
36 return dict((prefixed_paths
[path
], content
)
37 for path
, content
in results
.iteritems())
38 return self
._file
_system
.Read(tuple(prefix(path
) for path
in paths
),
39 skip_not_found
-skip_not_found
).Then(next
)
42 return self
._file
_system
.Refresh()
45 return self
._file
_system
.Stat(posixpath
.join(self
._root
, path
))
47 def GetIdentity(self
):
48 return StringIdentity(
49 '%s/%s' % (self
._file
_system
.GetIdentity(), self
._root
))
52 return 'ChrootFileSystem(%s, %s)' % (
53 self
._root
, repr(self
._file
_system
))