summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/chroot_file_system.py
blob: af521d8ebd45cc7fcdd2e3e9e0e5021f65a7c07e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import posixpath

from docs_server_utils import StringIdentity
from file_system import FileSystem
from future import Future


class ChrootFileSystem(FileSystem):
  '''ChrootFileSystem(fs, path) exposes a FileSystem whose root is |path| inside
  |fs|, so ChrootFileSystem(fs, 'hello').Read(['world']) is equivalent to
  fs.Read(['hello/world']) with the 'hello' prefix stripped from the result.
  '''

  def __init__(self, file_system, root):
    '''Parameters:
    |file_system| The FileSystem instance to transpose paths of.
    |root|        The path to transpose all Read/Stat calls by.
    '''
    self._file_system = file_system
    self._root = root.strip('/')

  def Read(self, paths, skip_not_found=False):
    # Maintain reverse mapping so the result can be mapped to the original
    # paths given (the result from |file_system| will include |root| in the
    # result, which would be wrong).
    prefixed_paths = {}
    def prefix(path):
      prefixed = posixpath.join(self._root, path)
      prefixed_paths[prefixed] = path
      return prefixed
    future_result = self._file_system.Read(
        tuple(prefix(path) for path in paths),
        skip_not_found=skip_not_found)
    def resolve():
      return dict((prefixed_paths[path], content)
                  for path, content in future_result.Get().iteritems())
    return Future(callback=resolve)

  def Refresh(self):
    return self._file_system.Refresh()

  def Stat(self, path):
    return self._file_system.Stat(posixpath.join(self._root, path))

  def GetIdentity(self):
    return StringIdentity(
        '%s/%s' % (self._file_system.GetIdentity(), self._root))

  def __repr__(self):
    return 'ChrootFileSystem(%s, %s)' % (
            self._root, repr(self._file_system))