25
var O_ACCMODE = 0x0003;
27
var STATUS_INVALID = -0x1;
29
var STATUS_ON_STORAGE = 0x2;
30
var STATUS_UNLINKED = 0x4;
31
var STATUS_FORWARDING = 0x5;
34
var JSONFS_VERSION = 3;
37
var JSONFS_IDX_NAME = 0;
38
var JSONFS_IDX_SIZE = 1;
39
var JSONFS_IDX_MTIME = 2;
40
var JSONFS_IDX_MODE = 3;
41
var JSONFS_IDX_UID = 4;
42
var JSONFS_IDX_GID = 5;
43
var JSONFS_IDX_TARGET = 6;
44
var JSONFS_IDX_SHA256 = 6;
52
function FS(storage, qidcounter) {
57
this.storage = storage;
59
this.qidcounter = qidcounter || { last_qidnumber: 0 };
65
this.total_size = 256 * 1024 * 1024 * 1024;
85
this.CreateDirectory("", -1);
88
FS.prototype.get_state = function()
92
state[0] = this.inodes;
93
state[1] = this.qidcounter.last_qidnumber;
95
for(const [id, data] of Object.entries(this.inodedata))
97
if((this.inodes[id].mode & S_IFDIR) === 0)
99
state[2].push([id, data]);
102
state[3] = this.total_size;
103
state[4] = this.used_size;
104
state = state.concat(this.mounts);
109
FS.prototype.set_state = function(state)
111
this.inodes = state[0].map(state => { const inode = new Inode(0); inode.set_state(state); return inode; });
112
this.qidcounter.last_qidnumber = state[1];
114
for(let [key, value] of state[2])
116
if(value.buffer.byteLength !== value.byteLength)
119
value = value.slice();
122
this.inodedata[key] = value;
124
this.total_size = state[3];
125
this.used_size = state[4];
126
this.mounts = state.slice(5);
132
FS.prototype.AddEvent = function(id, OnEvent) {
133
var inode = this.inodes[id];
134
if (inode.status == STATUS_OK || inode.status == STATUS_ON_STORAGE) {
137
else if(this.is_forwarder(inode))
139
this.follow_fs(inode).AddEvent(inode.foreign_id, OnEvent);
143
this.events.push({id: id, OnEvent: OnEvent});
147
FS.prototype.HandleEvent = function(id) {
148
const inode = this.inodes[id];
149
if(this.is_forwarder(inode))
151
this.follow_fs(inode).HandleEvent(inode.foreign_id);
155
for(var i=0; i<this.events.length; i++) {
156
if (this.events[i].id == id) {
157
this.events[i].OnEvent();
159
newevents.push(this.events[i]);
162
this.events = newevents;
165
FS.prototype.load_from_json = function(fs, done)
167
dbg_assert(fs, "Invalid fs passed to load_from_json");
169
if(fs["version"] !== JSONFS_VERSION)
171
throw "The filesystem JSON format has changed. " +
172
"Please update your fs2json (https://github.com/copy/fs2json) and recreate the filesystem JSON.";
175
var fsroot = fs["fsroot"];
176
this.used_size = fs["size"];
178
for(var i = 0; i < fsroot.length; i++) {
179
this.LoadRecursive(fsroot[i], 0);
190
FS.prototype.LoadRecursive = function(data, parentid)
192
var inode = this.CreateInode();
194
const name = data[JSONFS_IDX_NAME];
195
inode.size = data[JSONFS_IDX_SIZE];
196
inode.mtime = data[JSONFS_IDX_MTIME];
197
inode.ctime = inode.mtime;
198
inode.atime = inode.mtime;
199
inode.mode = data[JSONFS_IDX_MODE];
200
inode.uid = data[JSONFS_IDX_UID];
201
inode.gid = data[JSONFS_IDX_GID];
203
var ifmt = inode.mode & S_IFMT;
207
this.PushInode(inode, parentid, name);
208
this.LoadDir(this.inodes.length - 1, data[JSONFS_IDX_TARGET]);
210
else if(ifmt === S_IFREG)
212
inode.status = STATUS_ON_STORAGE;
213
inode.sha256sum = data[JSONFS_IDX_SHA256];
214
dbg_assert(inode.sha256sum);
215
this.PushInode(inode, parentid, name);
217
else if(ifmt === S_IFLNK)
219
inode.symlink = data[JSONFS_IDX_TARGET];
220
this.PushInode(inode, parentid, name);
222
else if(ifmt === S_IFSOCK)
228
dbg_log("Unexpected ifmt: " + h(ifmt) + " (" + name + ")");
232
FS.prototype.LoadDir = function(parentid, children)
234
for(var i = 0; i < children.length; i++) {
235
this.LoadRecursive(children[i], parentid);
247
FS.prototype.should_be_linked = function(inode)
251
return !this.is_forwarder(inode) || inode.foreign_id === 0;
260
FS.prototype.link_under_dir = function(parentid, idx, name)
262
const inode = this.inodes[idx];
263
const parent_inode = this.inodes[parentid];
265
dbg_assert(!this.is_forwarder(parent_inode),
266
"Filesystem: Shouldn't link under fowarder parents");
267
dbg_assert(this.IsDirectory(parentid),
268
"Filesystem: Can't link under non-directories");
269
dbg_assert(this.should_be_linked(inode),
270
"Filesystem: Can't link across filesystems apart from their root");
271
dbg_assert(inode.nlinks >= 0,
272
"Filesystem: Found negative nlinks value of " + inode.nlinks);
273
dbg_assert(!parent_inode.direntries.has(name),
274
"Filesystem: Name '" + name + "' is already taken");
276
parent_inode.direntries.set(name, idx);
279
if(this.IsDirectory(idx))
281
dbg_assert(!inode.direntries.has(".."),
282
"Filesystem: Cannot link a directory twice");
284
if(!inode.direntries.has(".")) inode.nlinks++;
285
inode.direntries.set(".", idx);
287
inode.direntries.set("..", parentid);
288
parent_inode.nlinks++;
297
FS.prototype.unlink_from_dir = function(parentid, name)
299
const idx = this.Search(parentid, name);
300
const inode = this.inodes[idx];
301
const parent_inode = this.inodes[parentid];
303
dbg_assert(!this.is_forwarder(parent_inode), "Filesystem: Can't unlink from forwarders");
304
dbg_assert(this.IsDirectory(parentid), "Filesystem: Can't unlink from non-directories");
306
const exists = parent_inode.direntries.delete(name);
309
dbg_assert(false, "Filesystem: Can't unlink non-existent file: " + name);
315
if(this.IsDirectory(idx))
317
dbg_assert(inode.direntries.get("..") === parentid,
318
"Filesystem: Found directory with bad parent id");
320
inode.direntries.delete("..");
321
parent_inode.nlinks--;
324
dbg_assert(inode.nlinks >= 0,
325
"Filesystem: Found negative nlinks value of " + inode.nlinks);
328
FS.prototype.PushInode = function(inode, parentid, name) {
329
if (parentid != -1) {
330
this.inodes.push(inode);
331
inode.fid = this.inodes.length - 1;
332
this.link_under_dir(parentid, inode.fid, name);
335
if (this.inodes.length == 0) {
336
this.inodes.push(inode);
337
inode.direntries.set(".", 0);
338
inode.direntries.set("..", 0);
344
message.Debug("Error in Filesystem: Pushed inode with name = "+ name + " has no parent");
350
function Inode(qidnumber)
352
this.direntries = new Map();
370
this.caps = undefined;
379
this.foreign_id = -1;
386
Inode.prototype.get_state = function()
389
state[0] = this.mode;
391
if((this.mode & S_IFMT) === S_IFDIR)
393
state[1] = [...this.direntries];
395
else if((this.mode & S_IFMT) === S_IFREG)
397
state[1] = this.sha256sum;
399
else if((this.mode & S_IFMT) === S_IFLNK)
401
state[1] = this.symlink;
403
else if((this.mode & S_IFMT) === S_IFSOCK)
405
state[1] = [this.minor, this.major];
412
state[2] = this.locks;
413
state[3] = this.status;
414
state[4] = this.size;
418
state[8] = this.ctime;
419
state[9] = this.atime;
420
state[10] = this.mtime;
421
state[11] = this.qid.version;
422
state[12] = this.qid.path;
423
state[13] = this.nlinks;
431
Inode.prototype.set_state = function(state)
433
this.mode = state[0];
435
if((this.mode & S_IFMT) === S_IFDIR)
437
this.direntries = new Map();
438
for(const [name, entry] of state[1])
440
this.direntries.set(name, entry);
443
else if((this.mode & S_IFMT) === S_IFREG)
445
this.sha256sum = state[1];
447
else if((this.mode & S_IFMT) === S_IFLNK)
449
this.symlink = state[1];
451
else if((this.mode & S_IFMT) === S_IFSOCK)
453
[this.minor, this.major] = state[1];
461
for(const lock_state of state[2])
463
const lock = new FSLockRegion();
464
lock.set_state(lock_state);
465
this.locks.push(lock);
467
this.status = state[3];
468
this.size = state[4];
472
this.ctime = state[8];
473
this.atime = state[9];
474
this.mtime = state[10];
475
this.qid.type = (this.mode & S_IFMT) >> 8;
476
this.qid.version = state[11];
477
this.qid.path = state[12];
478
this.nlinks = state[13];
493
FS.prototype.divert = function(parentid, filename)
495
const old_idx = this.Search(parentid, filename);
496
const old_inode = this.inodes[old_idx];
497
const new_inode = new Inode(-1);
499
dbg_assert(old_inode, "Filesystem divert: name (" + filename + ") not found");
500
dbg_assert(this.IsDirectory(old_idx) || old_inode.nlinks <= 1,
501
"Filesystem: can't divert hardlinked file '" + filename + "' with nlinks=" +
505
Object.assign(new_inode, old_inode);
507
const idx = this.inodes.length;
508
this.inodes.push(new_inode);
512
if(this.is_forwarder(old_inode))
514
this.mounts[old_inode.mount_id].backtrack.set(old_inode.foreign_id, idx);
516
if(this.should_be_linked(old_inode))
518
this.unlink_from_dir(parentid, filename);
519
this.link_under_dir(parentid, idx, filename);
523
if(this.IsDirectory(old_idx) && !this.is_forwarder(old_inode))
525
for(const [name, child_id] of new_inode.direntries)
527
if(name === "." || name === "..") continue;
528
if(this.IsDirectory(child_id))
530
this.inodes[child_id].direntries.set("..", idx);
536
this.inodedata[idx] = this.inodedata[old_idx];
537
delete this.inodedata[old_idx];
540
old_inode.direntries = new Map();
541
old_inode.nlinks = 0;
553
FS.prototype.copy_inode = function(src_inode, dest_inode)
555
Object.assign(dest_inode, src_inode, {
557
direntries: dest_inode.direntries,
558
nlinks: dest_inode.nlinks,
562
FS.prototype.CreateInode = function() {
564
const now = Math.round(Date.now() / 1000);
565
const inode = new Inode(++this.qidcounter.last_qidnumber);
566
inode.atime = inode.ctime = inode.mtime = now;
572
FS.prototype.CreateDirectory = function(name, parentid) {
573
const parent_inode = this.inodes[parentid];
574
if(parentid >= 0 && this.is_forwarder(parent_inode))
576
const foreign_parentid = parent_inode.foreign_id;
577
const foreign_id = this.follow_fs(parent_inode).CreateDirectory(name, foreign_parentid);
578
return this.create_forwarder(parent_inode.mount_id, foreign_id);
580
var x = this.CreateInode();
581
x.mode = 0x01FF | S_IFDIR;
583
x.uid = this.inodes[parentid].uid;
584
x.gid = this.inodes[parentid].gid;
585
x.mode = (this.inodes[parentid].mode & 0x1FF) | S_IFDIR;
587
x.qid.type = S_IFDIR >> 8;
588
this.PushInode(x, parentid, name);
589
this.NotifyListeners(this.inodes.length-1, 'newdir');
590
return this.inodes.length-1;
593
FS.prototype.CreateFile = function(filename, parentid) {
594
const parent_inode = this.inodes[parentid];
595
if(this.is_forwarder(parent_inode))
597
const foreign_parentid = parent_inode.foreign_id;
598
const foreign_id = this.follow_fs(parent_inode).CreateFile(filename, foreign_parentid);
599
return this.create_forwarder(parent_inode.mount_id, foreign_id);
601
var x = this.CreateInode();
602
x.uid = this.inodes[parentid].uid;
603
x.gid = this.inodes[parentid].gid;
604
x.qid.type = S_IFREG >> 8;
605
x.mode = (this.inodes[parentid].mode & 0x1B6) | S_IFREG;
606
this.PushInode(x, parentid, filename);
607
this.NotifyListeners(this.inodes.length-1, 'newfile');
608
return this.inodes.length-1;
612
FS.prototype.CreateNode = function(filename, parentid, major, minor) {
613
const parent_inode = this.inodes[parentid];
614
if(this.is_forwarder(parent_inode))
616
const foreign_parentid = parent_inode.foreign_id;
618
this.follow_fs(parent_inode).CreateNode(filename, foreign_parentid, major, minor);
619
return this.create_forwarder(parent_inode.mount_id, foreign_id);
621
var x = this.CreateInode();
624
x.uid = this.inodes[parentid].uid;
625
x.gid = this.inodes[parentid].gid;
626
x.qid.type = S_IFSOCK >> 8;
627
x.mode = (this.inodes[parentid].mode & 0x1B6);
628
this.PushInode(x, parentid, filename);
629
return this.inodes.length-1;
632
FS.prototype.CreateSymlink = function(filename, parentid, symlink) {
633
const parent_inode = this.inodes[parentid];
634
if(this.is_forwarder(parent_inode))
636
const foreign_parentid = parent_inode.foreign_id;
638
this.follow_fs(parent_inode).CreateSymlink(filename, foreign_parentid, symlink);
639
return this.create_forwarder(parent_inode.mount_id, foreign_id);
641
var x = this.CreateInode();
642
x.uid = this.inodes[parentid].uid;
643
x.gid = this.inodes[parentid].gid;
644
x.qid.type = S_IFLNK >> 8;
647
this.PushInode(x, parentid, filename);
648
return this.inodes.length-1;
651
FS.prototype.CreateTextFile = async function(filename, parentid, str) {
652
const parent_inode = this.inodes[parentid];
653
if(this.is_forwarder(parent_inode))
655
const foreign_parentid = parent_inode.foreign_id;
656
const foreign_id = await
657
this.follow_fs(parent_inode).CreateTextFile(filename, foreign_parentid, str);
658
return this.create_forwarder(parent_inode.mount_id, foreign_id);
660
var id = this.CreateFile(filename, parentid);
661
var x = this.inodes[id];
662
var data = new Uint8Array(str.length);
664
for (var j = 0; j < str.length; j++) {
665
data[j] = str.charCodeAt(j);
667
await this.set_data(id, data);
674
FS.prototype.CreateBinaryFile = async function(filename, parentid, buffer) {
675
const parent_inode = this.inodes[parentid];
676
if(this.is_forwarder(parent_inode))
678
const foreign_parentid = parent_inode.foreign_id;
679
const foreign_id = await
680
this.follow_fs(parent_inode).CreateBinaryFile(filename, foreign_parentid, buffer);
681
return this.create_forwarder(parent_inode.mount_id, foreign_id);
683
var id = this.CreateFile(filename, parentid);
684
var x = this.inodes[id];
685
var data = new Uint8Array(buffer.length);
687
await this.set_data(id, data);
688
x.size = buffer.length;
693
FS.prototype.OpenInode = function(id, mode) {
694
var inode = this.inodes[id];
695
if(this.is_forwarder(inode))
697
return this.follow_fs(inode).OpenInode(inode.foreign_id, mode);
699
if ((inode.mode&S_IFMT) == S_IFDIR) {
700
this.FillDirectory(id);
715
FS.prototype.CloseInode = async function(id) {
717
var inode = this.inodes[id];
718
if(this.is_forwarder(inode))
720
return await this.follow_fs(inode).CloseInode(inode.foreign_id);
722
if(inode.status === STATUS_ON_STORAGE)
724
this.storage.uncache(inode.sha256sum);
726
if (inode.status == STATUS_UNLINKED) {
728
inode.status = STATUS_INVALID;
729
await this.DeleteData(id);
736
FS.prototype.Rename = async function(olddirid, oldname, newdirid, newname) {
738
if ((olddirid == newdirid) && (oldname == newname)) {
741
var oldid = this.Search(olddirid, oldname);
748
var oldpath = this.GetFullPath(olddirid) + "/" + oldname;
750
var newid = this.Search(newdirid, newname);
752
const ret = this.Unlink(newdirid, newname);
753
if(ret < 0) return ret;
757
var inode = this.inodes[idx];
758
const olddir = this.inodes[olddirid];
759
const newdir = this.inodes[newdirid];
761
if(!this.is_forwarder(olddir) && !this.is_forwarder(newdir))
765
this.unlink_from_dir(olddirid, oldname);
766
this.link_under_dir(newdirid, idx, newname);
770
else if(this.is_forwarder(olddir) && olddir.mount_id === newdir.mount_id)
775
this.follow_fs(olddir).Rename(olddir.foreign_id, oldname, newdir.foreign_id, newname);
777
if(ret < 0) return ret;
779
else if(this.is_a_root(idx))
783
dbg_log("XXX: Attempted to move mountpoint (" + oldname + ") - skipped", LOG_9P);
786
else if(!this.IsDirectory(idx) && this.GetInode(idx).nlinks > 1)
789
dbg_log("XXX: Attempted to move hardlinked file (" + oldname + ") " +
790
"across filesystems - skipped", LOG_9P);
799
const diverted_old_idx = this.divert(olddirid, oldname);
800
const old_real_inode = this.GetInode(idx);
802
const data = await this.Read(diverted_old_idx, 0, old_real_inode.size);
804
if(this.is_forwarder(newdir))
807
const foreign_fs = this.follow_fs(newdir);
808
const foreign_id = this.IsDirectory(diverted_old_idx) ?
809
foreign_fs.CreateDirectory(newname, newdir.foreign_id) :
810
foreign_fs.CreateFile(newname, newdir.foreign_id);
812
const new_real_inode = foreign_fs.GetInode(foreign_id);
813
this.copy_inode(old_real_inode, new_real_inode);
816
this.set_forwarder(idx, newdir.mount_id, foreign_id);
821
this.delete_forwarder(inode);
822
this.copy_inode(old_real_inode, inode);
825
this.link_under_dir(newdirid, idx, newname);
829
await this.ChangeSize(idx, old_real_inode.size);
830
if(data && data.length)
832
await this.Write(idx, 0, data.length, data);
836
if(this.IsDirectory(idx))
838
for(const child_filename of this.GetChildren(diverted_old_idx))
840
const ret = await this.Rename(diverted_old_idx, child_filename, idx, child_filename);
841
if(ret < 0) return ret;
846
await this.DeleteData(diverted_old_idx);
847
const ret = this.Unlink(olddirid, oldname);
848
if(ret < 0) return ret;
851
this.NotifyListeners(idx, "rename", {oldpath: oldpath});
856
FS.prototype.Write = async function(id, offset, count, buffer) {
857
this.NotifyListeners(id, 'write');
858
var inode = this.inodes[id];
860
if(this.is_forwarder(inode))
862
const foreign_id = inode.foreign_id;
863
await this.follow_fs(inode).Write(foreign_id, offset, count, buffer);
867
var data = await this.get_buffer(id);
869
if (!data || data.length < (offset+count)) {
870
await this.ChangeSize(id, Math.floor(((offset+count)*3)/2));
871
inode.size = offset + count;
872
data = await this.get_buffer(id);
874
if (inode.size < (offset+count)) {
875
inode.size = offset + count;
879
data.set(buffer.subarray(0, count), offset);
881
await this.set_data(id, data);
884
FS.prototype.Read = async function(inodeid, offset, count)
886
const inode = this.inodes[inodeid];
887
if(this.is_forwarder(inode))
889
const foreign_id = inode.foreign_id;
890
return await this.follow_fs(inode).Read(foreign_id, offset, count);
893
return await this.get_data(inodeid, offset, count);
896
FS.prototype.Search = function(parentid, name) {
897
const parent_inode = this.inodes[parentid];
899
if(this.is_forwarder(parent_inode))
901
const foreign_parentid = parent_inode.foreign_id;
902
const foreign_id = this.follow_fs(parent_inode).Search(foreign_parentid, name);
903
if(foreign_id === -1) return -1;
904
return this.get_forwarder(parent_inode.mount_id, foreign_id);
907
const childid = parent_inode.direntries.get(name);
908
return childid === undefined ? -1 : childid;
911
FS.prototype.CountUsedInodes = function()
913
let count = this.inodes.length;
914
for(const { fs, backtrack } of this.mounts)
916
count += fs.CountUsedInodes();
919
count -= backtrack.size;
924
FS.prototype.CountFreeInodes = function()
926
let count = 1024 * 1024;
927
for(const { fs } of this.mounts)
929
count += fs.CountFreeInodes();
934
FS.prototype.GetTotalSize = function() {
935
let size = this.used_size;
936
for(const { fs } of this.mounts)
938
size += fs.GetTotalSize();
949
FS.prototype.GetSpace = function() {
950
let size = this.total_size;
951
for(const { fs } of this.mounts)
953
size += fs.GetSpace();
955
return this.total_size;
963
FS.prototype.GetDirectoryName = function(idx)
965
const parent_inode = this.inodes[this.GetParent(idx)];
967
if(this.is_forwarder(parent_inode))
969
return this.follow_fs(parent_inode).GetDirectoryName(this.inodes[idx].foreign_id);
973
if(!parent_inode) return "";
975
for(const [name, childid] of parent_inode.direntries)
977
if(childid === idx) return name;
980
dbg_assert(false, "Filesystem: Found directory inode whose parent doesn't link to it");
984
FS.prototype.GetFullPath = function(idx) {
985
dbg_assert(this.IsDirectory(idx), "Filesystem: Cannot get full path of non-directory inode");
990
path = "/" + this.GetDirectoryName(idx) + path;
991
idx = this.GetParent(idx);
993
return path.substring(1);
1002
FS.prototype.Link = function(parentid, targetid, name)
1004
if(this.IsDirectory(targetid))
1009
const parent_inode = this.inodes[parentid];
1010
const inode = this.inodes[targetid];
1012
if(this.is_forwarder(parent_inode))
1014
if(!this.is_forwarder(inode) || inode.mount_id !== parent_inode.mount_id)
1016
dbg_log("XXX: Attempted to hardlink a file into a child filesystem - skipped", LOG_9P);
1019
return this.follow_fs(parent_inode).Link(parent_inode.foreign_id, inode.foreign_id, name);
1022
if(this.is_forwarder(inode))
1024
dbg_log("XXX: Attempted to hardlink file across filesystems - skipped", LOG_9P);
1028
this.link_under_dir(parentid, targetid, name);
1032
FS.prototype.Unlink = function(parentid, name) {
1033
if(name === "." || name === "..")
1038
const idx = this.Search(parentid, name);
1039
const inode = this.inodes[idx];
1040
const parent_inode = this.inodes[parentid];
1044
if(this.is_forwarder(parent_inode))
1046
dbg_assert(this.is_forwarder(inode), "Children of forwarders should be forwarders");
1048
const foreign_parentid = parent_inode.foreign_id;
1049
return this.follow_fs(parent_inode).Unlink(foreign_parentid, name);
1054
if(this.IsDirectory(idx) && !this.IsEmpty(idx))
1059
this.unlink_from_dir(parentid, name);
1061
if(inode.nlinks === 0)
1064
inode.status = STATUS_UNLINKED;
1065
this.NotifyListeners(idx, 'delete');
1070
FS.prototype.DeleteData = async function(idx)
1072
const inode = this.inodes[idx];
1073
if(this.is_forwarder(inode))
1075
await this.follow_fs(inode).DeleteData(inode.foreign_id);
1079
delete this.inodedata[idx];
1089
FS.prototype.get_buffer = async function(idx)
1091
const inode = this.inodes[idx];
1092
dbg_assert(inode, `Filesystem get_buffer: idx ${idx} does not point to an inode`);
1094
if(this.inodedata[idx])
1096
return this.inodedata[idx];
1098
else if(inode.status === STATUS_ON_STORAGE)
1100
dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum");
1101
return await this.storage.read(inode.sha256sum, 0, inode.size);
1116
FS.prototype.get_data = async function(idx, offset, count)
1118
const inode = this.inodes[idx];
1119
dbg_assert(inode, `Filesystem get_data: idx ${idx} does not point to an inode`);
1121
if(this.inodedata[idx])
1123
return this.inodedata[idx].subarray(offset, offset + count);
1125
else if(inode.status === STATUS_ON_STORAGE)
1127
dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum");
1128
return await this.storage.read(inode.sha256sum, offset, count);
1141
FS.prototype.set_data = async function(idx, buffer)
1144
this.inodedata[idx] = buffer;
1145
if(this.inodes[idx].status === STATUS_ON_STORAGE)
1147
this.inodes[idx].status = STATUS_OK;
1148
this.storage.uncache(this.inodes[idx].sha256sum);
1156
FS.prototype.GetInode = function(idx)
1158
dbg_assert(!isNaN(idx), "Filesystem GetInode: NaN idx");
1159
dbg_assert(idx >= 0 && idx < this.inodes.length, "Filesystem GetInode: out of range idx:" + idx);
1161
const inode = this.inodes[idx];
1162
if(this.is_forwarder(inode))
1164
return this.follow_fs(inode).GetInode(inode.foreign_id);
1170
FS.prototype.ChangeSize = async function(idx, newsize)
1172
var inode = this.GetInode(idx);
1173
var temp = await this.get_data(idx, 0, inode.size);
1175
if (newsize == inode.size) return;
1176
var data = new Uint8Array(newsize);
1177
inode.size = newsize;
1180
var size = Math.min(temp.length, inode.size);
1181
data.set(temp.subarray(0, size), 0);
1183
await this.set_data(idx, data);
1186
FS.prototype.SearchPath = function(path) {
1188
path = path.replace("//", "/");
1189
var walk = path.split("/");
1190
if (walk.length > 0 && walk[walk.length - 1].length === 0) walk.pop();
1191
if (walk.length > 0 && walk[0].length === 0) walk.shift();
1192
const n = walk.length;
1196
let forward_path = null;
1197
for(var i=0; i<n; i++) {
1199
id = this.Search(parentid, walk[i]);
1200
if(!forward_path && this.is_forwarder(this.inodes[parentid]))
1202
forward_path = "/" + walk.slice(i).join("/");
1205
if (i < n-1) return {id: -1, parentid: -1, name: walk[i], forward_path };
1206
return {id: -1, parentid: parentid, name: walk[i], forward_path};
1209
return {id: id, parentid: parentid, name: walk[i], forward_path};
1217
FS.prototype.GetRecursiveList = function(dirid, list) {
1218
if(this.is_forwarder(this.inodes[dirid]))
1220
const foreign_fs = this.follow_fs(this.inodes[dirid]);
1221
const foreign_dirid = this.inodes[dirid].foreign_id;
1222
const mount_id = this.inodes[dirid].mount_id;
1224
const foreign_start = list.length;
1225
foreign_fs.GetRecursiveList(foreign_dirid, list);
1226
for(let i = foreign_start; i < list.length; i++)
1228
list[i].parentid = this.get_forwarder(mount_id, list[i].parentid);
1232
for(const [name, id] of this.inodes[dirid].direntries)
1234
if(name !== "." && name !== "..")
1236
list.push({ parentid: dirid, name });
1237
if(this.IsDirectory(id))
1239
this.GetRecursiveList(id, list);
1245
FS.prototype.RecursiveDelete = function(path) {
1247
var ids = this.SearchPath(path);
1248
if(ids.id === -1) return;
1250
this.GetRecursiveList(ids.id, toDelete);
1252
for(var i=toDelete.length-1; i>=0; i--)
1254
const ret = this.Unlink(toDelete[i].parentid, toDelete[i].name);
1255
dbg_assert(ret === 0, "Filesystem RecursiveDelete failed at parent=" + toDelete[i].parentid +
1256
", name='" + toDelete[i].name + "' with error code: " + (-ret));
1260
FS.prototype.DeleteNode = function(path) {
1261
var ids = this.SearchPath(path);
1262
if (ids.id == -1) return;
1264
if ((this.inodes[ids.id].mode&S_IFMT) == S_IFREG){
1265
const ret = this.Unlink(ids.parentid, ids.name);
1266
dbg_assert(ret === 0, "Filesystem DeleteNode failed with error code: " + (-ret));
1269
if ((this.inodes[ids.id].mode&S_IFMT) == S_IFDIR){
1270
this.RecursiveDelete(path);
1271
const ret = this.Unlink(ids.parentid, ids.name);
1272
dbg_assert(ret === 0, "Filesystem DeleteNode failed with error code: " + (-ret));
1278
FS.prototype.NotifyListeners = function(id, action, info) {
1296
FS.prototype.Check = function() {
1297
for(var i=1; i<this.inodes.length; i++)
1299
if (this.inodes[i].status == STATUS_INVALID) continue;
1301
var inode = this.GetInode(i);
1302
if (inode.nlinks < 0) {
1303
message.Debug("Error in filesystem: negative nlinks=" + inode.nlinks + " at id =" + i);
1306
if(this.IsDirectory(i))
1308
const inode = this.GetInode(i);
1309
if(this.IsDirectory(i) && this.GetParent(i) < 0) {
1310
message.Debug("Error in filesystem: negative parent id " + i);
1312
for(const [name, id] of inode.direntries)
1314
if(name.length === 0) {
1315
message.Debug("Error in filesystem: inode with no name and id " + id);
1318
for (const c of name) {
1320
message.Debug("Error in filesystem: Unallowed char in filename");
1330
FS.prototype.FillDirectory = function(dirid) {
1331
const inode = this.inodes[dirid];
1332
if(this.is_forwarder(inode))
1336
this.follow_fs(inode).FillDirectory(inode.foreign_id);
1341
for(const name of inode.direntries.keys())
1343
size += 13 + 8 + 1 + 2 + UTF8.UTF8Length(name);
1345
const data = this.inodedata[dirid] = new Uint8Array(size);
1349
for(const [name, id] of inode.direntries)
1351
const child = this.GetInode(id);
1352
offset += marshall.Marshall(
1353
["Q", "d", "b", "s"],
1355
offset+13+8+1+2+UTF8.UTF8Length(name),
1362
FS.prototype.RoundToDirentry = function(dirid, offset_target)
1364
const data = this.inodedata[dirid];
1365
dbg_assert(data, `FS directory data for dirid=${dirid} should be generated`);
1366
dbg_assert(data.length, "FS directory should have at least an entry");
1368
if(offset_target >= data.length)
1376
const next_offset = marshall.Unmarshall(["Q", "d"], data, { offset })[1];
1377
if(next_offset > offset_target) break;
1378
offset = next_offset;
1388
FS.prototype.IsDirectory = function(idx)
1390
const inode = this.inodes[idx];
1391
if(this.is_forwarder(inode))
1393
return this.follow_fs(inode).IsDirectory(inode.foreign_id);
1395
return (inode.mode & S_IFMT) === S_IFDIR;
1402
FS.prototype.IsEmpty = function(idx)
1404
const inode = this.inodes[idx];
1405
if(this.is_forwarder(inode))
1407
return this.follow_fs(inode).IsDirectory(inode.foreign_id);
1409
for(const name of inode.direntries.keys())
1411
if(name !== "." && name !== "..") return false;
1420
FS.prototype.GetChildren = function(idx)
1422
dbg_assert(this.IsDirectory(idx), "Filesystem: cannot get children of non-directory inode");
1423
const inode = this.inodes[idx];
1424
if(this.is_forwarder(inode))
1426
return this.follow_fs(inode).GetChildren(inode.foreign_id);
1428
const children = [];
1429
for(const name of inode.direntries.keys())
1431
if(name !== "." && name !== "..")
1433
children.push(name);
1443
FS.prototype.GetParent = function(idx)
1445
dbg_assert(this.IsDirectory(idx), "Filesystem: cannot get parent of non-directory inode");
1447
const inode = this.inodes[idx];
1449
if(this.should_be_linked(inode))
1451
return inode.direntries.get("..");
1455
const foreign_dirid = this.follow_fs(inode).GetParent(inode.foreign_id);
1456
dbg_assert(foreign_dirid !== -1, "Filesystem: should not have invalid parent ids");
1457
return this.get_forwarder(inode.mount_id, foreign_dirid);
1473
FS.prototype.PrepareCAPs = function(id) {
1474
var inode = this.GetInode(id);
1475
if (inode.caps) return inode.caps.length;
1476
inode.caps = new Uint8Array(20);
1481
inode.caps[0] = 0x00;
1482
inode.caps[1] = 0x00;
1483
inode.caps[2] = 0x00;
1484
inode.caps[3] = 0x02;
1488
inode.caps[4] = 0xFF;
1489
inode.caps[5] = 0xFF;
1490
inode.caps[6] = 0xFF;
1491
inode.caps[7] = 0xFF;
1493
inode.caps[8] = 0xFF;
1494
inode.caps[9] = 0xFF;
1495
inode.caps[10] = 0xFF;
1496
inode.caps[11] = 0xFF;
1500
inode.caps[12] = 0x3F;
1501
inode.caps[13] = 0x00;
1502
inode.caps[14] = 0x00;
1503
inode.caps[15] = 0x00;
1505
inode.caps[16] = 0x3F;
1506
inode.caps[17] = 0x00;
1507
inode.caps[18] = 0x00;
1508
inode.caps[19] = 0x00;
1510
return inode.caps.length;
1519
function FSMountInfo(filesystem)
1522
this.fs = filesystem;
1528
this.backtrack = new Map();
1531
FSMountInfo.prototype.get_state = function()
1536
state[1] = [...this.backtrack];
1541
FSMountInfo.prototype.set_state = function(state)
1544
this.backtrack = new Map(state[1]);
1553
FS.prototype.set_forwarder = function(idx, mount_id, foreign_id)
1555
const inode = this.inodes[idx];
1557
dbg_assert(inode.nlinks === 0,
1558
"Filesystem: attempted to convert an inode into forwarder before unlinking the inode");
1560
if(this.is_forwarder(inode))
1562
this.mounts[inode.mount_id].backtrack.delete(inode.foreign_id);
1565
inode.status = STATUS_FORWARDING;
1566
inode.mount_id = mount_id;
1567
inode.foreign_id = foreign_id;
1569
this.mounts[mount_id].backtrack.set(foreign_id, idx);
1578
FS.prototype.create_forwarder = function(mount_id, foreign_id)
1580
const inode = this.CreateInode();
1582
const idx = this.inodes.length;
1583
this.inodes.push(inode);
1586
this.set_forwarder(idx, mount_id, foreign_id);
1595
FS.prototype.is_forwarder = function(inode)
1597
return inode.status === STATUS_FORWARDING;
1606
FS.prototype.is_a_root = function(idx)
1608
return this.GetInode(idx).fid === 0;
1618
FS.prototype.get_forwarder = function(mount_id, foreign_id)
1620
const mount = this.mounts[mount_id];
1622
dbg_assert(foreign_id >= 0, "Filesystem get_forwarder: invalid foreign_id: " + foreign_id);
1623
dbg_assert(mount, "Filesystem get_forwarder: invalid mount number: " + mount_id);
1625
const result = mount.backtrack.get(foreign_id);
1627
if(result === undefined)
1630
return this.create_forwarder(mount_id, foreign_id);
1640
FS.prototype.delete_forwarder = function(inode)
1642
dbg_assert(this.is_forwarder(inode), "Filesystem delete_forwarder: expected forwarder");
1644
inode.status = STATUS_INVALID;
1645
this.mounts[inode.mount_id].backtrack.delete(inode.foreign_id);
1653
FS.prototype.follow_fs = function(inode)
1655
const mount = this.mounts[inode.mount_id];
1657
dbg_assert(this.is_forwarder(inode),
1658
"Filesystem follow_fs: inode should be a forwarding inode");
1659
dbg_assert(mount, "Filesystem follow_fs: inode<id=" + inode.fid +
1660
"> should point to valid mounted FS");
1671
FS.prototype.Mount = function(path, fs)
1673
dbg_assert(fs.qidcounter === this.qidcounter,
1674
"Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem.");
1676
const path_infos = this.SearchPath(path);
1678
if(path_infos.parentid === -1)
1680
dbg_log("Mount failed: parent for path not found: " + path, LOG_9P);
1683
if(path_infos.id !== -1)
1685
dbg_log("Mount failed: file already exists at path: " + path, LOG_9P);
1688
if(path_infos.forward_path)
1690
const parent = this.inodes[path_infos.parentid];
1691
const ret = this.follow_fs(parent).Mount(path_infos.forward_path, fs);
1692
if(ret < 0) return ret;
1693
return this.get_forwarder(parent.mount_id, ret);
1696
const mount_id = this.mounts.length;
1697
this.mounts.push(new FSMountInfo(fs));
1699
const idx = this.create_forwarder(mount_id, 0);
1700
this.link_under_dir(path_infos.parentid, idx, path_infos.name);
1708
function FSLockRegion()
1710
this.type = P9_LOCK_TYPE_UNLCK;
1712
this.length = Infinity;
1714
this.client_id = "";
1717
FSLockRegion.prototype.get_state = function()
1721
state[0] = this.type;
1722
state[1] = this.start;
1724
state[2] = this.length === Infinity ? 0 : this.length;
1725
state[3] = this.proc_id;
1726
state[4] = this.client_id;
1731
FSLockRegion.prototype.set_state = function(state)
1733
this.type = state[0];
1734
this.start = state[1];
1735
this.length = state[2] === 0 ? Infinity : state[2];
1736
this.proc_id = state[3];
1737
this.client_id = state[4];
1743
FSLockRegion.prototype.clone = function()
1745
const new_region = new FSLockRegion();
1746
new_region.set_state(this.get_state());
1754
FSLockRegion.prototype.conflicts_with = function(region)
1756
if(this.proc_id === region.proc_id && this.client_id === region.client_id) return false;
1757
if(this.type === P9_LOCK_TYPE_UNLCK || region.type === P9_LOCK_TYPE_UNLCK) return false;
1758
if(this.type !== P9_LOCK_TYPE_WRLCK && region.type !== P9_LOCK_TYPE_WRLCK) return false;
1759
if(this.start + this.length <= region.start) return false;
1760
if(region.start + region.length <= this.start) return false;
1768
FSLockRegion.prototype.is_alike = function(region)
1770
return region.proc_id === this.proc_id &&
1771
region.client_id === this.client_id &&
1772
region.type === this.type;
1779
FSLockRegion.prototype.may_merge_after = function(region)
1781
return this.is_alike(region) && region.start + region.length === this.start;
1792
FS.prototype.DescribeLock = function(type, start, length, proc_id, client_id)
1794
dbg_assert(type === P9_LOCK_TYPE_RDLCK ||
1795
type === P9_LOCK_TYPE_WRLCK ||
1796
type === P9_LOCK_TYPE_UNLCK,
1797
"Filesystem: Invalid lock type: " + type);
1798
dbg_assert(start >= 0, "Filesystem: Invalid negative lock starting offset: " + start);
1799
dbg_assert(length > 0, "Filesystem: Invalid non-positive lock length: " + length);
1801
const lock = new FSLockRegion();
1804
lock.length = length;
1805
lock.proc_id = proc_id;
1806
lock.client_id = client_id;
1816
FS.prototype.GetLock = function(id, request)
1818
const inode = this.inodes[id];
1820
if(this.is_forwarder(inode))
1822
const foreign_id = inode.foreign_id;
1823
return this.follow_fs(inode).GetLock(foreign_id, request);
1826
for(const region of inode.locks)
1828
if(request.conflicts_with(region))
1830
return region.clone();
1842
FS.prototype.Lock = function(id, request, flags)
1844
const inode = this.inodes[id];
1846
if(this.is_forwarder(inode))
1848
const foreign_id = inode.foreign_id;
1849
return this.follow_fs(inode).Lock(foreign_id, request, flags);
1852
request = request.clone();
1855
if(request.type !== P9_LOCK_TYPE_UNLCK && this.GetLock(id, request))
1857
return P9_LOCK_BLOCKED;
1861
for(let i = 0; i < inode.locks.length; i++)
1863
const region = inode.locks[i];
1865
dbg_assert(region.length > 0,
1866
"Filesystem: Found non-positive lock region length: " + region.length);
1867
dbg_assert(region.type === P9_LOCK_TYPE_RDLCK || region.type === P9_LOCK_TYPE_WRLCK,
1868
"Filesystem: Found invalid lock type: " + region.type);
1869
dbg_assert(!inode.locks[i-1] || inode.locks[i-1].start <= region.start,
1870
"Filesystem: Locks should be sorted by starting offset");
1873
if(region.start + region.length <= request.start) continue;
1876
if(request.start + request.length <= region.start) break;
1879
if(region.proc_id !== request.proc_id || region.client_id !== request.client_id)
1881
dbg_assert(!region.conflicts_with(request),
1882
"Filesytem: Found conflicting lock region, despite already checked for conflicts");
1887
const start1 = region.start;
1888
const start2 = request.start + request.length;
1889
const length1 = request.start - start1;
1890
const length2 = region.start + region.length - start2;
1892
if(length1 > 0 && length2 > 0 && region.type === request.type)
1896
return P9_LOCK_SUCCESS;
1902
region.length = length1;
1905
if(length1 <= 0 && length2 > 0)
1908
region.start = start2;
1909
region.length = length2;
1911
else if(length2 > 0)
1916
while(i < inode.locks.length && inode.locks[i].start < start2) i++;
1918
inode.locks.splice(i, 0,
1919
this.DescribeLock(region.type, start2, length2, region.proc_id, region.client_id));
1921
else if(length1 <= 0)
1924
inode.locks.splice(i, 1);
1932
if(request.type !== P9_LOCK_TYPE_UNLCK)
1934
let new_region = request;
1935
let has_merged = false;
1939
for(; i < inode.locks.length; i++)
1941
if(new_region.may_merge_after(inode.locks[i]))
1943
inode.locks[i].length += request.length;
1944
new_region = inode.locks[i];
1947
if(request.start <= inode.locks[i].start) break;
1952
inode.locks.splice(i, 0, new_region);
1957
for(; i < inode.locks.length; i++)
1959
if(!inode.locks[i].is_alike(new_region)) continue;
1961
if(inode.locks[i].may_merge_after(new_region))
1963
new_region.length += inode.locks[i].length;
1964
inode.locks.splice(i, 1);
1972
return P9_LOCK_SUCCESS;
1975
FS.prototype.read_dir = function(path)
1977
const p = this.SearchPath(path);
1984
const dir = this.GetInode(p.id);
1986
return Array.from(dir.direntries.keys()).filter(path => path !== "." && path !== "..");
1989
FS.prototype.read_file = function(file)
1991
const p = this.SearchPath(file);
1995
return Promise.resolve(null);
1998
const inode = this.GetInode(p.id);
2000
return this.Read(p.id, 0, inode.size);