-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathputiofs.py
executable file
·168 lines (142 loc) · 4.99 KB
/
putiofs.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import errno
import fuse
import putioapi
from cachefs import CacheManager, CacheFS
from error import AuthenticationFailed
from node import Dir
fuse.fuse_python_api = (0, 2)
PATH_SEP = '/'
class PutIOFS(fuse.Fuse):
def __init__(self, *args, **kwargs):
super(PutIOFS, self).__init__(*args, **kwargs)
self.api = None
self.key = None
self.secret = None
self.root_fs = None
self.cache_fd = None
@staticmethod
def _dirname(p):
"""
Roughly equivalent of os.path.dirname with specific separator.
"""
i = p.rfind(PATH_SEP) + 1
head = p[:i]
if head and head != PATH_SEP*len(head):
head = head.rstrip(PATH_SEP)
return head
def _geturl(self, path):
return self.find_inode(path).item.download_url
def _genitem(self, **config):
config.setdefault('id', -1)
config.setdefault('parent_id', -1)
return putioapi.Item(self.api, config)
def initialize(self):
self.api = putioapi.Api(self.key, self.secret)
if not self.api.access_token:
raise AuthenticationFailed
item = self._genitem(type='folder', name='.', id=0, parent_id=0)
self.root_fs = CacheFS(item.id, Dir(item.name).stat, item)
self.cache_fd = CacheManager((self.key, self.secret))
def get_inode(self, path, item):
try:
return self.find_inode(path)
except KeyError:
return self.register_inode(path, item)
def find_inode(self, path):
dir_node = self.root_fs
for name in path.split(PATH_SEP):
if name:
dir_node = dir_node.find_inode(name)
return dir_node
def register_inode(self, path, item):
dirname = self._dirname(path)
return self.find_inode(dirname).register_inode(item)
def getattr(self, path):
"""
Given a path string, returns the Dir or File object corresponding to
the path, or None.
"""
if not path.startswith(PATH_SEP):
return None
first_try = True
while True:
try:
return self.find_inode(path).stat
except KeyError:
if first_try:
list(self.readdir(self._dirname(path)))
first_try = False
else:
break
def readdir(self, path, offset=0, dh=None):
"""
Generator function. Produces a directory listing.
Yields individual fuse.Direntry objects, one per file in the
directory. Should always yield at least "." and "..".
Should yield nothing if the file is not a directory or does not exist.
(Does not need to raise an error).
offset: I don't know what this does, but I think it allows the OS to
request starting the listing partway through (which I clearly don't
yet support). Seems to always be 0 anyway.
"""
yield fuse.Direntry('.')
yield fuse.Direntry('..')
inode = self.find_inode(path)
items = {_.item for _ in inode.get_entries()}
if not items:
while True:
try:
newitems = set(self.api.get_items(20, len(items), inode.id))
except putioapi.PutioError:
pass
if items & newitems:
break
items |= newitems
if len(items) % 20:
break
for it in items:
name = it.name.encode('utf-8')
it_path = PATH_SEP.join([path, name])
inode = self.get_inode(it_path, it)
yield fuse.Direntry(name)
def read(self, path, size, offset):
return self.cache_fd.read(self._geturl(path), size, offset)
def write(self, path, buf, offset):
return -errno.ENOSYS
def release(self, path, flags):
self.cache_fd.remove(self._geturl(path))
return 0
def open(self, path, flags):
self.cache_fd.append(self._geturl(path))
return 0
def truncate(self, path, size):
return -errno.ENOSYS
def utime(self, path, times):
return -errno.ENOSYS
def mkdir(self, path, mode):
return -errno.ENOSYS
def rmdir(self, path):
return -errno.ENOSYS
def rename(self, pathfrom, pathto):
return -errno.ENOSYS
def fsync(self, path, isfsyncfile):
return -errno.ENOSYS
def main():
server = PutIOFS(
version="%prog " + fuse.__version__,
dash_s_do='setsingle')
server.parser.add_option(mountopt="key", metavar="KEY",
help="put.io API key")
server.parser.add_option(mountopt="secret", metavar="SECRET",
help="put.io API secret")
server.parse(values=server, errex=1)
server.initialize()
server.main()
if __name__ == '__main__':
try:
main()
except Exception, ex:
print "ERROR: %s" % ex
raise