-
Notifications
You must be signed in to change notification settings - Fork 87
/
MemoryFS.java
executable file
·410 lines (359 loc) · 12.9 KB
/
MemoryFS.java
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package ru.serce.jnrfuse.examples;
import jnr.ffi.Platform;
import jnr.ffi.Pointer;
import jnr.ffi.types.mode_t;
import jnr.ffi.types.off_t;
import jnr.ffi.types.size_t;
import ru.serce.jnrfuse.ErrorCodes;
import ru.serce.jnrfuse.FuseFillDir;
import ru.serce.jnrfuse.FuseStubFS;
import ru.serce.jnrfuse.struct.FileStat;
import ru.serce.jnrfuse.struct.FuseFileInfo;
import ru.serce.jnrfuse.struct.Statvfs;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import static jnr.ffi.Platform.OS.WINDOWS;
public class MemoryFS extends FuseStubFS {
private class MemoryDirectory extends MemoryPath {
private List<MemoryPath> contents = new ArrayList<>();
private MemoryDirectory(String name) {
super(name);
}
private MemoryDirectory(String name, MemoryDirectory parent) {
super(name, parent);
}
public synchronized void add(MemoryPath p) {
contents.add(p);
p.parent = this;
}
private synchronized void deleteChild(MemoryPath child) {
contents.remove(child);
}
@Override
protected MemoryPath find(String path) {
if (super.find(path) != null) {
return super.find(path);
}
while (path.startsWith("/")) {
path = path.substring(1);
}
synchronized (this) {
if (!path.contains("/")) {
for (MemoryPath p : contents) {
if (p.name.equals(path)) {
return p;
}
}
return null;
}
String nextName = path.substring(0, path.indexOf("/"));
String rest = path.substring(path.indexOf("/"));
for (MemoryPath p : contents) {
if (p.name.equals(nextName)) {
return p.find(rest);
}
}
}
return null;
}
@Override
protected void getattr(FileStat stat) {
stat.st_mode.set(FileStat.S_IFDIR | 0777);
stat.st_uid.set(getContext().uid.get());
stat.st_gid.set(getContext().gid.get());
}
private synchronized void mkdir(String lastComponent) {
contents.add(new MemoryDirectory(lastComponent, this));
}
public synchronized void mkfile(String lastComponent) {
contents.add(new MemoryFile(lastComponent, this));
}
public synchronized void read(Pointer buf, FuseFillDir filler) {
for (MemoryPath p : contents) {
filler.apply(buf, p.name, null, 0);
}
}
}
private class MemoryFile extends MemoryPath {
private ByteBuffer contents = ByteBuffer.allocate(0);
private MemoryFile(String name) {
super(name);
}
private MemoryFile(String name, MemoryDirectory parent) {
super(name, parent);
}
public MemoryFile(String name, String text) {
super(name);
try {
byte[] contentBytes = text.getBytes("UTF-8");
contents = ByteBuffer.wrap(contentBytes);
} catch (UnsupportedEncodingException e) {
// Not going to happen
}
}
@Override
protected void getattr(FileStat stat) {
stat.st_mode.set(FileStat.S_IFREG | 0777);
stat.st_size.set(contents.capacity());
stat.st_uid.set(getContext().uid.get());
stat.st_gid.set(getContext().gid.get());
}
private int read(Pointer buffer, long size, long offset) {
int bytesToRead = (int) Math.min(contents.capacity() - offset, size);
byte[] bytesRead = new byte[bytesToRead];
synchronized (this) {
contents.position((int) offset);
contents.get(bytesRead, 0, bytesToRead);
buffer.put(0, bytesRead, 0, bytesToRead);
contents.position(0); // Rewind
}
return bytesToRead;
}
private synchronized void truncate(long size) {
if (size < contents.capacity()) {
// Need to create a new, smaller buffer
ByteBuffer newContents = ByteBuffer.allocate((int) size);
byte[] bytesRead = new byte[(int) size];
contents.get(bytesRead);
newContents.put(bytesRead);
contents = newContents;
}
}
private int write(Pointer buffer, long bufSize, long writeOffset) {
int maxWriteIndex = (int) (writeOffset + bufSize);
byte[] bytesToWrite = new byte[(int) bufSize];
synchronized (this) {
if (maxWriteIndex > contents.capacity()) {
// Need to create a new, larger buffer
ByteBuffer newContents = ByteBuffer.allocate(maxWriteIndex);
newContents.put(contents);
contents = newContents;
}
buffer.get(0, bytesToWrite, 0, (int) bufSize);
contents.position((int) writeOffset);
contents.put(bytesToWrite);
contents.position(0); // Rewind
}
return (int) bufSize;
}
}
private abstract class MemoryPath {
private String name;
private MemoryDirectory parent;
private MemoryPath(String name) {
this(name, null);
}
private MemoryPath(String name, MemoryDirectory parent) {
this.name = name;
this.parent = parent;
}
private synchronized void delete() {
if (parent != null) {
parent.deleteChild(this);
parent = null;
}
}
protected MemoryPath find(String path) {
while (path.startsWith("/")) {
path = path.substring(1);
}
if (path.equals(name) || path.isEmpty()) {
return this;
}
return null;
}
protected abstract void getattr(FileStat stat);
private void rename(String newName) {
while (newName.startsWith("/")) {
newName = newName.substring(1);
}
name = newName;
}
}
public static void main(String[] args) {
MemoryFS memfs = new MemoryFS();
try {
String path;
switch (Platform.getNativePlatform().getOS()) {
case WINDOWS:
path = "J:\\";
break;
default:
path = "/tmp/mntm";
}
memfs.mount(Paths.get(path), true, true);
} finally {
memfs.umount();
}
}
private MemoryDirectory rootDirectory = new MemoryDirectory("");
public MemoryFS() {
// Sprinkle some files around
rootDirectory.add(new MemoryFile("Sample file.txt", "Hello there, feel free to look around.\n"));
rootDirectory.add(new MemoryDirectory("Sample directory"));
MemoryDirectory dirWithFiles = new MemoryDirectory("Directory with files");
rootDirectory.add(dirWithFiles);
dirWithFiles.add(new MemoryFile("hello.txt", "This is some sample text.\n"));
dirWithFiles.add(new MemoryFile("hello again.txt", "This another file with text in it! Oh my!\n"));
MemoryDirectory nestedDirectory = new MemoryDirectory("Sample nested directory");
dirWithFiles.add(nestedDirectory);
nestedDirectory.add(new MemoryFile("So deep.txt", "Man, I'm like, so deep in this here file structure.\n"));
}
@Override
public int create(String path, @mode_t long mode, FuseFileInfo fi) {
if (getPath(path) != null) {
return -ErrorCodes.EEXIST();
}
MemoryPath parent = getParentPath(path);
if (parent instanceof MemoryDirectory) {
((MemoryDirectory) parent).mkfile(getLastComponent(path));
return 0;
}
return -ErrorCodes.ENOENT();
}
@Override
public int getattr(String path, FileStat stat) {
MemoryPath p = getPath(path);
if (p != null) {
p.getattr(stat);
return 0;
}
return -ErrorCodes.ENOENT();
}
private String getLastComponent(String path) {
while (path.substring(path.length() - 1).equals("/")) {
path = path.substring(0, path.length() - 1);
}
if (path.isEmpty()) {
return "";
}
return path.substring(path.lastIndexOf("/") + 1);
}
private MemoryPath getParentPath(String path) {
return rootDirectory.find(path.substring(0, path.lastIndexOf("/")));
}
private MemoryPath getPath(String path) {
return rootDirectory.find(path);
}
@Override
public int mkdir(String path, @mode_t long mode) {
if (getPath(path) != null) {
return -ErrorCodes.EEXIST();
}
MemoryPath parent = getParentPath(path);
if (parent instanceof MemoryDirectory) {
((MemoryDirectory) parent).mkdir(getLastComponent(path));
return 0;
}
return -ErrorCodes.ENOENT();
}
@Override
public int read(String path, Pointer buf, @size_t long size, @off_t long offset, FuseFileInfo fi) {
MemoryPath p = getPath(path);
if (p == null) {
return -ErrorCodes.ENOENT();
}
if (!(p instanceof MemoryFile)) {
return -ErrorCodes.EISDIR();
}
return ((MemoryFile) p).read(buf, size, offset);
}
@Override
public int readdir(String path, Pointer buf, FuseFillDir filter, @off_t long offset, FuseFileInfo fi) {
MemoryPath p = getPath(path);
if (p == null) {
return -ErrorCodes.ENOENT();
}
if (!(p instanceof MemoryDirectory)) {
return -ErrorCodes.ENOTDIR();
}
filter.apply(buf, ".", null, 0);
filter.apply(buf, "..", null, 0);
((MemoryDirectory) p).read(buf, filter);
return 0;
}
@Override
public int statfs(String path, Statvfs stbuf) {
if (Platform.getNativePlatform().getOS() == WINDOWS) {
// statfs needs to be implemented on Windows in order to allow for copying
// data from other devices because winfsp calculates the volume size based
// on the statvfs call.
// see https://github.com/billziss-gh/winfsp/blob/14e6b402fe3360fdebcc78868de8df27622b565f/src/dll/fuse/fuse_intf.c#L654
if ("/".equals(path)) {
stbuf.f_blocks.set(1024 * 1024); // total data blocks in file system
stbuf.f_frsize.set(1024); // fs block size
stbuf.f_bfree.set(1024 * 1024); // free blocks in fs
}
}
return super.statfs(path, stbuf);
}
@Override
public int rename(String path, String newName) {
MemoryPath p = getPath(path);
if (p == null) {
return -ErrorCodes.ENOENT();
}
MemoryPath newParent = getParentPath(newName);
if (newParent == null) {
return -ErrorCodes.ENOENT();
}
if (!(newParent instanceof MemoryDirectory)) {
return -ErrorCodes.ENOTDIR();
}
p.delete();
p.rename(newName.substring(newName.lastIndexOf("/")));
((MemoryDirectory) newParent).add(p);
return 0;
}
@Override
public int rmdir(String path) {
MemoryPath p = getPath(path);
if (p == null) {
return -ErrorCodes.ENOENT();
}
if (!(p instanceof MemoryDirectory)) {
return -ErrorCodes.ENOTDIR();
}
p.delete();
return 0;
}
@Override
public int truncate(String path, long offset) {
MemoryPath p = getPath(path);
if (p == null) {
return -ErrorCodes.ENOENT();
}
if (!(p instanceof MemoryFile)) {
return -ErrorCodes.EISDIR();
}
((MemoryFile) p).truncate(offset);
return 0;
}
@Override
public int unlink(String path) {
MemoryPath p = getPath(path);
if (p == null) {
return -ErrorCodes.ENOENT();
}
p.delete();
return 0;
}
@Override
public int open(String path, FuseFileInfo fi) {
return 0;
}
@Override
public int write(String path, Pointer buf, @size_t long size, @off_t long offset, FuseFileInfo fi) {
MemoryPath p = getPath(path);
if (p == null) {
return -ErrorCodes.ENOENT();
}
if (!(p instanceof MemoryFile)) {
return -ErrorCodes.EISDIR();
}
return ((MemoryFile) p).write(buf, size, offset);
}
}