/
a2fuse2.py
261 lines (177 loc) · 7.87 KB
/
a2fuse2.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
# Name: Jack Chamberlain
# UPI (Login): jcha928
from __future__ import print_function, absolute_import, division
import logging
import os
import sys
import errno
from errno import ENOENT
import re
from collections import defaultdict
from stat import S_IFDIR, S_IFLNK, S_IFREG
from sys import argv, exit
from time import time
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn, ENOTSUP
from passthrough import Passthrough
from memory import Memory
class A2Fuse2(LoggingMixIn, Operations):
# Note: when initialising fields and performing operations using the memory file system, code from memory.py has been copied
def __init__(self, root1, root2):
self.files = {}
self.data = defaultdict(bytes)
self.fd = 0
now = time()
self.files['/'] = dict(st_mode=(S_IFDIR | 0o755), st_ctime=now,
st_mtime=now, st_atime=now, st_nlink=2)
self.passthrough1 = Passthrough(root1)
self.passthrough2 = Passthrough(root2)
def dirCheck(self, path):
return os.access(path, os.F_OK)
def getattr(self, path, fh=None):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.getattr(path)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.getattr(path)
else:
if path not in self.files:
raise FuseOSError(ENOENT)
return self.files[path]
def readdir(self, path, fh):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
pathMemExists = path in self.files
dirContent = list()
if self.dirCheck(fullPathRoot1):
for x in self.passthrough1.readdir(path, fh):
dirContent.append(x)
if self.dirCheck(fullPathRoot2):
for y in self.passthrough2.readdir(path, fh):
dirContent.append(y)
if pathMemExists:
for z in (['.', '..'] + [x[1:] for x in self.files if x != '/']):
dirContent.append(z)
return dirContent
def open(self, path, flags):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.open(path, flags)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.open(path, flags)
else:
self.fd += 1
return self.fd
def create(self, path, flags):
# modified existing memory implementation to include UID and GID
self.files[path] = dict(st_mode=(S_IFREG | flags),
st_nlink=1, st_uid=os.getuid(), st_gid=os.getgid(), st_size=0, st_ctime=time(),
st_mtime=time(), st_atime=time())
self.fd += 1
return self.fd
def unlink(self, path):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.unlink(path)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.unlink(path)
else:
self.files.pop(path)
def write(self, path, data, offset, fh):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.write(path, data, offset, fh)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.write(path, data, offset, fh)
else:
self.data[path] = self.data[path][:offset] + data
self.files[path]['st_size'] = len(self.data[path])
return len(data)
def read(self, path, size, offset, fh):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.read(path, size, offset, fh)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.read(path, size, offset, fh)
else:
return self.data[path][offset:offset + size]
def chmod(self, path, mode):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.chmod(path, mode)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.chmod(path, mode)
else:
self.files[path]['st_mode'] &= 0o770000
self.files[path]['st_mode'] |= mode
return 0
def chown(self, path, uid, gid):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.chown(path, uid, gid)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.chown(path, uid, gid)
else:
self.files[path]['st_uid'] = uid
self.files[path]['st_gid'] = gid
def getxattr(self, path, name, position=0):
# This method is not supported
raise FuseOSError(ENOTSUP)
def truncate(self, path, length, fh=None):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.truncate(path, length)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.truncate(path, length)
else:
self.data[path] = self.data[path][:length]
self.files[path]['st_size'] = length
def utimens(self, path, times=None):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
pathRoot1Exists = os.path.isdir(fullPathRoot1)
pathRoot2Exists = os.path.isdir(fullPathRoot2)
if pathRoot1Exists:
return self.passthrough1.utimens(path)
elif pathRoot2Exists:
return self.passthrough2.utimens(path)
else:
now = time()
atime, mtime = times if times else (now, now)
self.files[path]['st_atime'] = atime
self.files[path]['st_mtime'] = mtime
def release(self, path, fh):
# get hypothetical path for both source folders
fullPathRoot1 = self.passthrough1._full_path(path)
fullPathRoot2 = self.passthrough2._full_path(path)
if self.dirCheck(fullPathRoot1):
return self.passthrough1.release(path, fh)
elif self.dirCheck(fullPathRoot2):
return self.passthrough2.release(path, fh)
else:
# there is no release method for the memory implementation
pass
def main(mountpoint, root1, root2):
FUSE(A2Fuse2(root1, root2), mountpoint, nothreads=True, foreground=True)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
# passes command line arguments into main (mount point is 3rd arg)
main(sys.argv[3], sys.argv[1], sys.argv[2])