Import first working version
[fuse-aexplorer.git] / AEpy / FuseCache.py
1 from stat import S_IFDIR
2
3
4 class FuseCache():
5     def __init__(self):
6         # Initialize virtual root
7         self.cache = {}
8
9     # Cache tuple:
10     # (dict of attrs, dict of children in folder)
11
12
13     def getattr(self, path):
14         paths = [p for p in path.split('/') if len(p) > 0]
15         #print(paths)
16
17         # Virtual root
18         st = dict(st_mode=(S_IFDIR | 0o755), st_nlink=2)
19
20         c = self.cache
21         for p in paths:
22             if c == None:
23                 return None
24
25             if p not in c:
26                 return None
27
28             (st, c) = c[p]
29
30         return st
31
32
33     def getkids(self, path):
34         paths = [p for p in path.split('/') if len(p) > 0]
35
36         c = self.cache
37         for p in paths:
38             if c == None:
39                 return None
40
41             if p not in c:
42                 return None
43
44             (_, c) = c[p]
45
46         return c
47
48
49     # Set a cache node's children and their attributes.
50     # This implicitly purges any prvious children from the cache.
51     # Thus, those directories will be re-scanned next time.
52     def setkids(self, path, children):
53         paths = [p for p in path.split('/') if len(p) > 0]
54
55         c = self.cache
56         for p in paths:
57             # We expect to be able to walk the path, because we can't get
58             # there without Linux' VFS stat'ing all directories leading up.
59             parent = c
60             (_, c) = c[p]
61
62         if (c == self.cache):
63             self.cache = children
64         else:
65             (st, _) = parent[p]
66             parent[p] = (st, children)