1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/example/daemon/www/js/filesystem.js

128 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-01-28 21:08:37 +00:00
class FileSystem {
static get GOOD() { return 0; }
static get BAD() { return 1; }
static get BAD_NOTIMPLEMENTED() { return 2; }
static get BAD_TIMEOUT() { return 3; }
static get BAD_FORMAT() { return 4; }
static get BAD_NOENTRY() { return 101; }
static get BAD_NOACCESS() { return 102; }
static get O_ACCMODE() { return 0x003; }
static get O_RDONLY() { return 0x000; }
static get O_WRONLY() { return 0x001; }
static get O_RDWR() { return 0x002; }
static get O_CREAT() { return 0x040; }
static get O_EXCL() { return 0x080; }
static get O_TRUNK() { return 0x200; }
static get O_APPEND() { return 0x400; }
constructor(root) {
this.root = root;
2019-02-02 13:25:57 +00:00
this._inodes = { };
this._walk(this.root, (entry) => { this._inodes[entry.inode] = entry; });
}
_walk(node, callback) {
callback(node);
const entries = node.entries;
if (entries) {
for(let entry of Object.entries(entries)) {
this._walk(entry[1], callback);
}
}
2019-01-28 21:08:37 +00:00
}
2019-02-03 18:10:05 +00:00
lookup(parent, name) {
2019-02-03 18:10:05 +00:00
const parentEntry = this._inodes[parent];
const entry = (parentEntry && parentEntry.entries && parentEntry.entries[name]) || null;
if (entry) {
return {
inode: entry.inode,
mode: entry.mode || parseInt("755", 8),
2019-02-13 20:09:43 +00:00
type: entry.type || "file",
size: entry.size || (entry.contents && entry.contents.length) || 0,
atime: entry.atime || 0,
mtime: entry.mtime || 0,
ctime: entry.ctime || 0
};
}
else {
return FileSystem.BAD_NOENTRY;
}
}
2019-02-03 18:10:05 +00:00
getattr(inode) {
let entry = this._inodes[inode];
2019-01-28 21:08:37 +00:00
if (entry) {
return {
mode: entry.mode || parseInt("755", 8),
2019-02-13 20:09:43 +00:00
type: entry.type || "file",
2019-01-28 21:08:37 +00:00
size: entry.size || (entry.contents && entry.contents.length) || 0,
atime: entry.atime || 0,
mtime: entry.mtime || 0,
ctime: entry.ctime || 0
};
}
else {
return FileSystem.BAD_NOENTRY;
2019-01-28 21:08:37 +00:00
}
}
2019-02-03 18:10:05 +00:00
readdir(inode) {
2019-01-28 21:08:37 +00:00
let result = FileSystem.BAD_NOENTRY;
2019-02-03 18:10:05 +00:00
let entry = this._inodes[inode];
2019-01-28 21:08:37 +00:00
if ((entry) && ("dir" === entry.type)) {
result = [
2019-02-13 20:09:43 +00:00
{name: ".", inode: entry.inode},
{name: "..", inode: entry.inode}
];
for(let subdir of Object.entries(entry.entries)) {
const name = subdir[0];
const inode = subdir[1].inode;
2019-02-13 20:09:43 +00:00
result.push({name, inode});
2019-01-28 21:08:37 +00:00
}
}
return result;
}
2019-02-03 18:10:05 +00:00
open(inode, mode) {
2019-01-28 21:08:37 +00:00
let result = FileSystem.BAD_NOENTRY;
2019-02-03 18:10:05 +00:00
let entry = this._inodes[inode];
2019-01-28 21:08:37 +00:00
2019-02-13 20:09:43 +00:00
if (entry.type === "file") {
result = ((mode & FileSystem.O_ACCMODE) === FileSystem.O_RDONLY) ? {handle: 1337} : FileSystem.BAD_NOACCESS;
2019-01-28 21:08:37 +00:00
}
return result;
}
2019-02-03 18:10:05 +00:00
close(inode, handle, mode) {
2019-01-29 22:47:08 +00:00
// do nothing
return true;
}
2019-02-03 18:10:05 +00:00
read(inode, handle, offset, length) {
2019-01-28 21:08:37 +00:00
let result = FileSystem.BAD_NOENTRY;
2019-02-03 18:10:05 +00:00
let entry = this._inodes[inode];
2019-01-28 21:08:37 +00:00
2019-02-13 20:09:43 +00:00
if (entry.type === "file") {
2019-01-28 21:08:37 +00:00
let end = Math.min(offset + length, entry.contents.length);
let data = (offset < entry.contents.length) ? entry.contents.substring(offset, end) : "";
result = {
2019-02-10 21:18:22 +00:00
data: btoa(data),
format: "base64",
2019-01-28 21:08:37 +00:00
count: data.length
};
}
return result;
}
}