-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFoundationLayer.java
More file actions
319 lines (272 loc) · 10.9 KB
/
FoundationLayer.java
File metadata and controls
319 lines (272 loc) · 10.9 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package foundationdb_fslayer.fdb;
import com.apple.foundationdb.*;
import com.apple.foundationdb.directory.DirectoryLayer;
import com.apple.foundationdb.directory.DirectorySubspace;
import foundationdb_fslayer.Util;
import foundationdb_fslayer.cache.DirectoryCacheEntry;
import foundationdb_fslayer.cache.FsCacheSingleton;
import foundationdb_fslayer.fdb.object.Attr;
import foundationdb_fslayer.fdb.object.DirectorySchema;
import foundationdb_fslayer.fdb.object.FileSchema;
import foundationdb_fslayer.fdb.object.ObjectType;
import foundationdb_fslayer.permissions.PermissionManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import static foundationdb_fslayer.Util.parsePath;
public class FoundationLayer implements FoundationFileOperations {
private final FDB fdb;
private final DirectoryLayer directoryLayer;
private final Database db;
public FoundationLayer(Integer apiVersion) {
this.fdb = FDB.selectAPIVersion(apiVersion);
this.directoryLayer = new DirectoryLayer();
this.db = fdb.open();
}
private <T> T dbRead(Function<ReadTransaction, T> op){
return db.read(op);
}
private <T> T dbWrite(Function<Transaction, T> op){
return db.run(op);
}
@Override
public byte[] read(String path, long offset, long size, long userId) {
FileSchema file = new FileSchema(path);
return dbRead(transaction -> file.read(directoryLayer, transaction, offset, size, userId));
}
@Override
public boolean rmdir(String path, long uid) {
DirectorySchema dir = new DirectorySchema(path);
return dbWrite(transaction ->
canNodeBeCreatedOrRemoved(transaction, path, uid)
&& dir.delete(directoryLayer, transaction));
}
@Override
public DirectorySubspace mkdir(String path, long mode, long uid) {
DirectorySchema dir = new DirectorySchema(path);
return dbWrite(transaction -> {
if (!canNodeBeCreatedOrRemoved(transaction, path, uid)) {
return null;
}
return dir.create(directoryLayer, transaction, mode, uid);
});
}
@Override
public boolean write(String path, byte[] data, long offset, long userId) {
FileSchema file = new FileSchema(path);
return dbWrite(transaction -> file.write(directoryLayer, transaction, data, offset, userId));
}
@Override
public List<String> ls(String path, long userId) {
// Check if the user is allowed to read this directory
if (!path.equals("/")
&& !dbRead(tr->checkDirectoryPermission(path, tr, userId, 0400, 0004))) {
return null;
}
Optional<List<String>> cacheValue = dbRead(rt ->
FsCacheSingleton.getDir(path).flatMap(entry ->
entry.isCurrent(directoryLayer, rt)
? Optional.of(entry.getChildren())
: Optional.empty()));
return cacheValue.orElseGet(() -> {
try {
List<String> children = directoryLayer.list(db, parsePath(path)).get();
if (!FsCacheSingleton.dirInCache(path)) {
dbRead(rt -> {
FsCacheSingleton.loadDirToCache(path, directoryLayer, rt, children);
return null;
});
} else {
DirectoryCacheEntry entry = FsCacheSingleton.getDir(path)
.orElseThrow(() -> new IllegalStateException("Dir not present after check"));
dbRead(rt -> entry.reload(directoryLayer, rt, children));
}
return children;
} catch (Exception e) {
return null;
}
});
}
private boolean checkDirectoryPermission(String dirPath, ReadTransaction rt, long userId, long userMask, long otherMask) {
Attr attr = getDirectoryMetadata(dirPath, rt);
return Util.checkPermission(attr.getMode(), attr.getUid(), userId, userMask, otherMask);
}
public boolean clearFileContent(String filepath, long userId) {
FileSchema file = new FileSchema(filepath);
return dbWrite(transaction ->
canNodeBeCreatedOrRemoved(transaction, filepath, userId)
&& file.delete(directoryLayer, transaction));
}
@Override
public boolean createFile(String path, long userId) {
FileSchema file = new FileSchema(path);
return dbWrite(transaction ->
canNodeBeCreatedOrRemoved(transaction, path, userId)
&& file.create(directoryLayer, transaction));
}
@Override
public Attr getAttr(String path) {
return dbRead(rt -> {
Optional<Boolean> isDir = isDirectory(path, rt);
if (isDir.isPresent()) {
if (isDir.get()) {
return getDirectoryMetadata(path, rt);
} else {
return new FileSchema(path).getMetadata(directoryLayer, rt);
}
} else {
return new Attr().setObjectType(ObjectType.NOT_FOUND);
}
});
}
private Optional<Boolean> isDirectory(String path, ReadTransaction rt) {
List<String> paths = parsePath(path);
List<String> listDotPath = new ArrayList<>(paths);
listDotPath.add(DirectorySchema.Metadata.META_ROOT);
try {
directoryLayer.open(rt, paths).get();
return Optional.of(directoryLayer.exists(rt, listDotPath).get());
} catch (Exception e) {
return Optional.empty();
}
}
private Optional<List<String>> loadDirectoryContents(String path) {
try {
return Optional.of(directoryLayer.list(db, parsePath(path)).get());
} catch (Exception e) {
return Optional.empty();
}
}
private Attr getDirectoryMetadata(String path, ReadTransaction rt) {
DirectoryCacheEntry entry = FsCacheSingleton.getDir(path)
.flatMap(storedEntry -> {
if (storedEntry.isCurrent(directoryLayer, rt)) {
return Optional.of(storedEntry);
} else {
return loadDirectoryContents(path)
.map(children -> storedEntry.reload(directoryLayer, rt, children));
}
})
.orElseGet(() -> {
try {
return FsCacheSingleton.loadDirToCache(
path,
directoryLayer,
rt,
directoryLayer.list(rt, parsePath(path)).get());
} catch (Exception e) {
return null;
}
});
return entry != null
? entry.getMetadata()
: new Attr().setObjectType(ObjectType.NOT_FOUND);
}
@Override
public boolean setFileTime(Long timestamp, String path) {
return dbWrite(tr ->
new FileSchema(path).setTimestamp(directoryLayer, tr, timestamp));
}
@Override
public int getFileSize(String path) {
return dbRead(rt -> new FileSchema(path).size(directoryLayer, rt));
}
@Override
public boolean truncate(String path, long size, long userId) {
return dbWrite(tr -> new FileSchema(path).truncate(directoryLayer, tr, size, userId));
}
@Override
public boolean chmod(String path, long mode, long userId) {
return dbWrite(tr -> isDirectory(path, tr).map(isDir -> {
if (isDir) {
return getDirectoryMetadata(path, tr).getUid() == userId
&& new DirectorySchema(path).setMode(directoryLayer, tr, mode);
} else {
return new FileSchema(path).setMode(directoryLayer, tr, mode, userId);
}
}).orElse(false));
}
@Override
public boolean chown(String path, long uid, long gid) {
return dbWrite(tr -> new FileSchema(path).setOwnership(directoryLayer, tr, uid, gid));
}
@Override
public int open(String path, int flags) {
return dbWrite(tr -> new FileSchema(path).open(directoryLayer, tr, flags));
}
@Override
public boolean move(String oldPath, String newPath, long userId) {
return dbWrite(tr-> {
String path = newPath;
if (isDirectory(newPath, tr).orElse(false)) {
path += oldPath.substring(oldPath.lastIndexOf("/") + 1);
}
return recursiveMove(oldPath, path, tr, userId)
&& new DirectorySchema(oldPath).delete(directoryLayer, tr);
});
}
public boolean recursiveMove(String oldPath, String newPath, Transaction transaction, long userId) {
System.out.printf("Renaming %s to %s\n", oldPath, newPath);
return isDirectory(oldPath, transaction).map(isDir -> {
if (isDir) {
// Grab the metadata from the old directory
Attr oldMetadata = getDirectoryMetadata(oldPath, transaction);
// Create the new directory
DirectorySchema newDir = new DirectorySchema(newPath);
newDir.create(directoryLayer, transaction, oldMetadata.getMode(), oldMetadata.getUid());
// Recur on all subdirectories, then on child files
List<String> childPaths = ls(oldPath, oldMetadata.getUid());
boolean subDirCopySuccess = childPaths.stream()
.filter(child -> isDirectory(oldPath + "/" + child, transaction).orElse(false))
.allMatch(subDir -> recursiveMove(
oldPath + "/" + subDir,
newPath + "/" + subDir,
transaction, userId));
return subDirCopySuccess &&
childPaths.stream()
.filter(child -> !isDirectory(oldPath + "/" + child, transaction).orElse(true))
.filter(child -> !child.equals("."))
.allMatch(file -> new FileSchema(oldPath + "/" + file)
.move(directoryLayer, transaction, newPath + "/" + file) != null);
} else {
new FileSchema(oldPath).move(directoryLayer, transaction, newPath);
return true;
}
}).orElse(false);
}
@Override
public void initRootIfNeeded() {
db.run(tr -> {
try {
if (!directoryLayer.exists(tr, Arrays.asList(DirectorySchema.Metadata.META_ROOT)).get()) {
new DirectorySchema("/").initMetadata(directoryLayer, tr);
System.out.println("Root directory created!");
} else {
System.out.println("Root directory exists!");
}
} catch (Exception e) {
System.err.println("Error checking if meta root exists to initialize");
e.printStackTrace();
}
return null;
});
}
@Override
public Optional<PermissionManager> login(String username, String password) {
return dbWrite(tr -> PermissionManager.login(username, password, directoryLayer, tr));
}
private boolean canNodeBeCreatedOrRemoved(ReadTransaction rt, String targetPath, long userId) {
// A node can be created or removed if the user has permissions to write to the parent directory
// So, let's first grab the parent directory.
String parentPath = targetPath.substring(0, targetPath.lastIndexOf("/"));
// Everyone has access to the root directory
if (parentPath.equals("")) {
return true;
}
// Return true if the user can both read and write to that directory
return checkDirectoryPermission(parentPath, rt, userId, 0200, 0002)
&& checkDirectoryPermission(parentPath, rt, userId, 0400, 0004);
}
}