-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposix_backend.py
121 lines (112 loc) · 4.28 KB
/
posix_backend.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
#!/usr/bin/env python
import os
import sys
import errno
import stat
from abstract_backend import AbstractBackend
from abstract_backend import FileNotFoundError
class PosixBackend(AbstractBackend):
def __init__(self, root):
print("PosixBackend: init, root=", root)
super(PosixBackend, self).__init__()
self.root = root
self.inodes = {1: self.root}
self.saveInode(1, 1)
def getPathByInode(self, inode):
if not inode in self.inodes:
raise(FileNotFoundError)
return self.inodes[inode]
def lookup(self, inode_p, name):
print("PosixBackend: lookup inode=", inode_p, ", name=", name)
if name == '.':
inode = inode_p
else:
path = os.path.join(self.getPathByInode(inode_p), name)
print("PosixBackend: lookup path=%s", path)
st = os.stat(path)
inode = st.st_ino
self.inodes[inode] = path
return inode
def getattr(self, inode):
path = self.getPathByInode(inode)
print("PosixBackend: getattr inode=", inode, ", path=", path)
try:
return os.stat(path)
except OSError, e:
if e.errno == errno.ENOENT:
print('PosixBackend: path %s does not exist or is a broken symlink', path)
raise(llfuse.FUSEError(errno.ENOENT))
def setattr(self, inode, attr):
print("PosixBackend: setattr=", attr.st_mode)
path = self.getPathByInode(inode)
if attr.st_mode is not None:
os.chmod(path, attr.st_mode)
if attr.st_uid is not None and attr.st_gid is not None:
os.chown(path, attr.st_uid, attr.st_gid)
if attr.st_atime is not None and attr.st_mtime is not None:
os.utime(path, (attr.st_atime, attr.st_mtime))
def readdir(self, inode, off):
path = self.getPathByInode(inode)
print("PosixBackend: readdir inode=", inode, ", path=", path, ", off=", off)
return os.listdir(path)
def opendir(self, inode):
print("PosixBackend: opendir ", inode)
return inode
def open(self, inode, flags):
print("PosixBackend: open")
path = self.getPathByInode(inode)
fh = os.open(path, flags)
return fh
def read(self, fh, offset, length):
print("PosixBackend: read")
os.lseek(fh, offset, os.SEEK_SET)
data = os.read(fh, length)
if data is None:
data = ''
return data
def write(self, fh, offset, buf):
print("PosixBackend: write")
os.lseek(fh, offset, os.SEEK_SET)
return os.write(fh, buf)
def release(self, fh):
print("PosixBackend: release")
os.close(fh)
def mkdir(self, inode_p, name, mode):
print("PosixBackend: mkdir")
path = os.path.join(self.getPathByInode(inode_p), name)
os.mkdir(path, mode)
def rmdir(self, inode_p, name):
print("PosixBackend: rmdir")
path = os.path.join(self.getPathByInode(inode_p), name)
return os.rmdir(path)
def create(self, inode_parent, name, mode, flags):
print("PosixBackend: create")
path = os.path.join(self.getPathByInode(inode_parent), name)
return os.open(path, flags)
def unlink(self, inode):
print("PosixBackend: unlink")
path = self.getPathByInode(inode)
os.unlink(path)
def rename(self, inode_p_old, name_old, inode_p_new, name_new):
print("PosixBackend: rename")
oldpath = os.path.join(self.getPathByInode(inode_p_old), name_old)
newpath = os.path.join(self.getPathByInode(inode_p_new), name_new)
os.rename(oldpath, newpath)
inode = self.lookup(inode_p_new, name_new)
self.inodes[inode] = newpath
def statfs(self):
print("PosixBackend: statfs")
stat = os.statvfs(self.root)
return stat
def readlink(self, inode):
print("PosixBackend: readlink")
return inode
def symlink(self, inode_p, name, target, ctx):
print("PosixBackend: symlink")
def link(self, inode, new_inode_p, new_name):
print("PosixBackend: link")
def mknod(self, inode_p, name, mode, rdev, ctx):
print("PosixBackend: mknod")
def access(self, inode, mode, ctx):
print("PosixBackend: access")
return True