-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathloopback.py
More file actions
108 lines (83 loc) · 2.52 KB
/
loopback.py
File metadata and controls
108 lines (83 loc) · 2.52 KB
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
#!/usr/bin/env python
"""A simple FUSE filesystem which just mirrors a given directory."""
import errno
import os
import os.path
import threading
import fuse
class Loopback(fuse.Operations):
"""A simple FUSE-py class for doing a loopback FS."""
def __init__(self, root):
self.root = os.path.realpath(root)
self.rwlock = threading.Lock()
def __call__(self, op, path, *args):
return super(Loopback, self).__call__(op, self.root + path, *args)
def access(self, path, mode):
if not os.access(path, mode):
raise fuse.FuseOSError(errno.EACCES)
chmod = os.chmod
chown = os.chown
def create(self, path, mode):
return os.open(path, os.O_WRONLY | os.O_CREAT, mode)
def flush(self, path, fh):
return os.fsync(fh)
def fsync(self, path, datasync, fh):
return os.fsync(fh)
def getattr(self, path, fh=None):
st = os.lstat(path)
keys = (
'st_atime',
'st_ctime',
'st_gid',
'st_mode',
'st_mtime',
'st_nlink',
'st_size',
'st_uid',
)
return dict((key, getattr(st, key)) for key in keys)
getxattr = None
def link(self, target, source):
return os.link(source, target)
listxattr = None
mkdir = os.mkdir
mknod = os.mknod
open = os.open
def read(self, path, size, offset, fh):
with self.rwlock:
os.lseek(fh, offset, 0)
return os.read(fh, size)
def readdir(self, path, fh):
return ['.', '..'] + os.listdir(path)
readlink = os.readlink
def release(self, path, fh):
return os.close(fh)
def rename(self, old, new):
return os.rename(old, self.root + new)
rmdir = os.rmdir
def statfs(self, path):
stv = os.statvfs(path)
keys = (
'f_bavail',
'f_bfree',
'f_blocks',
'f_bsize',
'f_favail',
'f_ffree',
'f_files',
'f_flag',
'f_frsize',
'f_namemax',
)
return dict((key, getattr(stv, key)) for key in keys)
def symlink(self, target, source):
return os.symlink(source, target)
def truncate(self, path, length, fh=None):
with open(path, 'r+') as f:
f.truncate(length)
unlink = os.unlink
utimens = os.utime
def write(self, path, data, offset, fh):
with self.rwlock:
os.lseek(fh, offset, 0)
return os.write(fh, data)