-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotofs_main.py
138 lines (103 loc) · 3.48 KB
/
photofs_main.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
'''photofs is a virtual file-system show photos in a convenient way.'''
__author__ = '[email protected] (Sergey Pisarenko)'
import errno
import logging
import os
import sys
import threading
import fuse
from fuse import Fuse
from photofs.storage import PhotoDb
from photofs.views import FsStat, GetViews
from photofs.walker import PhotoWalker
from photofs.watcher import PhotoWatcher
fuse.fuse_python_api = (0, 2)
class PhotoFS(Fuse):
def main(self, *a, **kw):
if not self.fuse_args.getmod('showhelp'):
self._validate_args()
self.db = PhotoDb(self.root)
self.views = GetViews(self.db)
if self.db.TryLock():
logging.info('Acquired database lock, will write/update it')
else:
logging.error(('Failed to acquire database lock, '
'another instance is already running'))
sys.exit(0)
return Fuse.main(self, *a, **kw)
def fsinit(self):
self.walker = PhotoWalker(self.root, self.db)
self.watcher = PhotoWatcher(self.db, self.walker, self.root)
if self.db.IsEmptyDb():
self.walker.Walk()
else:
self.walker.Sync()
self.watcher.Watch()
def fsdestroy(self):
sys.exit(0)
def RouteView(func):
def inner(*args, **kwargs):
path_split = args[1].split('/')
if not path_split[1]:
kwargs['is_root'] = True
return func(*args, **kwargs)
view = path_split[1]
self = args[0]
if view not in self.views.keys():
return -errno.ENOENT
kwargs['view'] = self.views[view]
kwargs['path_split'] = path_split
return func(*args, **kwargs)
return inner
@RouteView
def getattr(self, path, is_root=False, view=None, path_split=None):
if is_root or len(path_split) == 2:
return FsStat()
return view.getattr(path_split[2:])
@RouteView
def readdir(self, path, offset, is_root=False, view=None, path_split=None):
entries = ['.', '..']
if is_root:
entries.extend(self.views.keys())
else:
entries.extend(view.readdir(path_split[2:], offset))
for e in entries:
yield fuse.Direntry(e)
@RouteView
def open(self, path, flags, view=None, path_split=None):
return view.open(path_split[2:], flags)
@RouteView
def read(self, path, length, offset, view=None, path_split=None):
return view.read(path_split[2:], length, offset)
@RouteView
def write(self, path, buf, offset, view=None, path_split=None):
return view.write(path_split[2:], buf, offset)
@RouteView
def release(self, path, flags, view=None, path_split=None):
return view.release(path_split[2:], flags)
@RouteView
def unlink(self, path, view=None, path_split=None):
return view.unlink(path_split[2:])
@RouteView
def rename(self, oldPath, newPath, view=None, path_split=None):
return view.rename(oldPath.split('/')[2:], newPath.split('/')[2:])
@RouteView
def truncate(self, path, length, view=None, path_split=None):
return view.truncate(path_split[2:], length)
def _validate_args(self):
if not self.cmdline[0].root:
print '"root" parameter must be specified'
sys.exit(0)
if not os.path.isdir(self.cmdline[0].root):
print 'Source path does not exist'
sys.exit(0)
self.root = self.cmdline[0].root
def main():
photo_fs = PhotoFS()
photo_fs.parser.add_option(mountopt='root', metavar='PATH',
help='Path to folder containing your photos.')
photo_fs.parse(errex=1)
photo_fs.main()
if __name__ == "__main__":
main()