// ------------------------------------------------- // ----------------- FILESYSTEM--------------------- // ------------------------------------------------- // Implementation of a unix filesystem in memory. "use strict"; var S_IRWXUGO = 0x1FF; var S_IFMT = 0xF000; var S_IFSOCK = 0xC000; var S_IFLNK = 0xA000; var S_IFREG = 0x8000; var S_IFBLK = 0x6000; var S_IFDIR = 0x4000; var S_IFCHR = 0x2000; //var S_IFIFO 0010000 //var S_ISUID 0004000 //var S_ISGID 0002000 //var S_ISVTX 0001000 var O_RDONLY = 0x0000; // open for reading only var O_WRONLY = 0x0001; // open for writing only var O_RDWR = 0x0002; // open for reading and writing var O_ACCMODE = 0x0003; // mask for above modes var STATUS_INVALID = -0x1; var STATUS_OK = 0x0; var STATUS_ON_SERVER = 0x2; var STATUS_LOADING = 0x3; var STATUS_UNLINKED = 0x4; var STATUS_FORWARDING = 0x5; /** @const */ var JSONFS_VERSION = 3; /** @const */ var JSONFS_IDX_NAME = 0; /** @const */ var JSONFS_IDX_SIZE = 1; /** @const */ var JSONFS_IDX_MTIME = 2; /** @const */ var JSONFS_IDX_MODE = 3; /** @const */ var JSONFS_IDX_UID = 4; /** @const */ var JSONFS_IDX_GID = 5; /** @const */ var JSONFS_IDX_TARGET = 6; /** @const */ var JSONFS_IDX_SHA256 = 6; /** * @constructor * @param {string=} baseurl * @param {{ last_qidnumber: number }=} qidcounter Another fs's qidcounter to synchronise with. */ function FS(baseurl, qidcounter) { /** @type {Array.} */ this.inodes = []; this.events = []; this.baseurl = baseurl; this.qidcounter = qidcounter || { last_qidnumber: 0 }; this.filesinloadingqueue = 0; this.OnLoaded = function() {}; //this.tar = new TAR(this); this.inodedata = {}; this.total_size = 256 * 1024 * 1024 * 1024; this.used_size = 0; /** @type {!Array} */ this.mounts = []; //RegisterMessage("LoadFilesystem", this.LoadFilesystem.bind(this) ); //RegisterMessage("MergeFile", this.MergeFile.bind(this) ); //RegisterMessage("tar", // function(data) { // SendToMaster("tar", this.tar.Pack(data)); // }.bind(this) //); //RegisterMessage("sync", // function(data) { // SendToMaster("sync", this.tar.Pack(data)); // }.bind(this) //); // root entry this.CreateDirectory("", -1); } FS.prototype.get_state = function() { let state = []; state[0] = this.inodes; state[1] = this.qidcounter.last_qidnumber; state[2] = []; for(let entry of Object.entries(this.inodedata)) { state[2].push(entry); } state[3] = this.total_size; state[4] = this.used_size; state = state.concat(this.mounts); return state; }; FS.prototype.set_state = function(state) { this.inodes = state[0].map(state => { const inode = new Inode(0); inode.set_state(state); return inode; }); this.qidcounter.last_qidnumber = state[1]; this.inodedata = {}; for(let [key, value] of state[2]) { if(value.buffer.byteLength !== value.byteLength) { // make a copy if we didn't get one value = value.slice(); } this.inodedata[key] = value; } this.total_size = state[3]; this.used_size = state[4]; this.mounts = state.slice(5); }; // ----------------------------------------------------- FS.prototype.IsLoading = function() { if(this.filesinloadingqueue > 0) return true; for(const mount of this.mounts) { if(mount.fs.IsLoading()) return true; } return false; }; FS.prototype.AddEvent = function(id, OnEvent) { var inode = this.inodes[id]; if (inode.status == STATUS_OK) { OnEvent(); } else if(this.is_forwarder(inode)) { this.follow_fs(inode).AddEvent(inode.foreign_id, OnEvent); } else { this.events.push({id: id, OnEvent: OnEvent}); } }; FS.prototype.HandleEvent = function(id) { const inode = this.inodes[id]; if(this.is_forwarder(inode)) { this.follow_fs(inode).HandleEvent(inode.foreign_id); } if (!this.IsLoading()) { this.OnLoaded(); this.OnLoaded = function() {}; } //message.Debug("number of events: " + this.events.length); var newevents = []; for(var i=0; i= 0, "Filesystem: Found negative nlinks value of " + inode.nlinks); dbg_assert(!parent_inode.direntries.has(name), "Filesystem: Name '" + name + "' is already taken"); parent_inode.direntries.set(name, idx); parent_inode.updatedir = true; inode.nlinks++; if(this.IsDirectory(idx)) { dbg_assert(!inode.direntries.has(".."), "Filesystem: Cannot link a directory twice"); if(!inode.direntries.has(".")) inode.nlinks++; inode.direntries.set(".", idx); inode.direntries.set("..", parentid); parent_inode.nlinks++; inode.updatedir = true; } }; /** * @private * @param {number} parentid * @param {string} name */ FS.prototype.unlink_from_dir = function(parentid, name) { const idx = this.Search(parentid, name); const inode = this.inodes[idx]; const parent_inode = this.inodes[parentid]; dbg_assert(!this.is_forwarder(parent_inode), "Filesystem: Can't unlink from fowarders"); dbg_assert(this.IsDirectory(parentid), "Filesystem: Can't unlink from non-directories"); const exists = parent_inode.direntries.delete(name); if(!exists) { dbg_assert(false, "Filesystem: Can't unlink non-existent file: " + name); return; } inode.nlinks--; parent_inode.updatedir = true; if(this.IsDirectory(idx)) { dbg_assert(inode.direntries.get("..") === parentid, "Filesystem: Found directory with bad parent id"); inode.direntries.delete(".."); parent_inode.nlinks--; inode.updatedir = true; } dbg_assert(inode.nlinks >= 0, "Filesystem: Found negative nlinks value of " + inode.nlinks); }; FS.prototype.PushInode = function(inode, parentid, name) { if (parentid != -1) { this.inodes.push(inode); inode.fid = this.inodes.length - 1; this.link_under_dir(parentid, inode.fid, name); return; } else { if (this.inodes.length == 0) { // if root directory this.inodes.push(inode); inode.direntries.set(".", 0); inode.direntries.set("..", 0); inode.nlinks = 2; return; } } message.Debug("Error in Filesystem: Pushed inode with name = "+ name + " has no parent"); message.Abort(); }; /** @constructor */ function Inode(qidnumber) { this.updatedir = false; // did the directory listing changed? this.direntries = new Map(); // maps filename to inode id this.status = 0; this.size = 0x0; this.uid = 0x0; this.gid = 0x0; this.fid = 0; this.ctime = 0; this.atime = 0; this.mtime = 0; this.major = 0x0; this.minor = 0x0; this.symlink = ""; this.mode = 0x01ED; this.qid = { type: 0, version: 0, path: qidnumber, }; this.caps = undefined; this.nlinks = 0; this.dirty = false; // has this file changed? this.sha256sum = ""; // For forwarders: this.mount_id = -1; // which fs in this.mounts does this inode forward to? this.foreign_id = -1; // which foreign inode id does it represent? //this.qid_type = 0; //this.qid_version = 0; //this.qid_path = qidnumber; } Inode.prototype.get_state = function() { const state = []; state[0] = this.updatedir; state[1] = [...this.direntries]; //state[2] //state[3] state[4] = this.status; //state[5] state[6] = this.size; state[7] = this.uid; state[8] = this.gid; state[9] = this.fid; state[10] = this.ctime; state[11] = this.atime; state[12] = this.mtime; state[13] = this.major; state[14] = this.minor; state[15] = this.symlink; state[16] = this.mode; state[17] = this.qid.type; state[18] = this.qid.version; state[19] = this.qid.path; state[20] = this.caps; state[21] = this.nlinks; state[22] = this.dirty; state[23] = this.mount_id; state[24] = this.foreign_id; state[25] = this.sha256sum; return state; }; Inode.prototype.set_state = function(state) { this.updatedir = state[0]; this.direntries = new Map(state[1]); //state[2]; //state[3]; this.status = state[4]; //state[5]; this.size = state[6]; this.uid = state[7]; this.gid = state[8]; this.fid = state[9]; this.ctime = state[10]; this.atime = state[11]; this.mtime = state[12]; this.major = state[13]; this.minor = state[14]; this.symlink = state[15]; this.mode = state[16]; this.qid.type = state[17]; this.qid.version = state[18]; this.qid.path = state[19]; this.caps = state[20]; this.nlinks = state[21]; this.dirty = state[22]; this.mount_id = state[23]; this.foreign_id = state[24]; this.sha256sum = state[25]; }; /** * Clones given inode to new idx, effectively diverting the inode to new idx value. * Hence, original idx value is now free to use without losing the original information. * @private * @param {number} parentid Parent of target to divert. * @param {string} filename Name of target to divert. * @return {number} New idx of diversion. */ FS.prototype.divert = function(parentid, filename) { const old_idx = this.Search(parentid, filename); const old_inode = this.inodes[old_idx]; const new_inode = new Inode(-1); dbg_assert(old_inode, "Filesystem divert: name (" + filename + ") not found"); dbg_assert(this.IsDirectory(old_idx) || old_inode.nlinks <= 1, "Filesystem: can't divert hardlinked file '" + filename + "' with nlinks=" + old_inode.nlinks); // Shallow copy is alright. Object.assign(new_inode, old_inode); const idx = this.inodes.length; this.inodes.push(new_inode); new_inode.fid = idx; // Relink references if(this.is_forwarder(old_inode)) { this.mounts[old_inode.mount_id].backtrack.set(old_inode.foreign_id, idx); } if(this.should_be_linked(old_inode)) { this.unlink_from_dir(parentid, filename); this.link_under_dir(parentid, idx, filename); } // Update children if(this.IsDirectory(old_idx) && !this.is_forwarder(old_inode)) { for(const [name, child_id] of new_inode.direntries) { if(name === "." || name === "..") continue; if(this.IsDirectory(child_id)) { this.inodes[child_id].direntries.set("..", idx); } } } // Relocate local data if any. this.inodedata[idx] = this.inodedata[old_idx]; delete this.inodedata[old_idx]; // Retire old reference information. old_inode.direntries = new Map(); old_inode.nlinks = 0; return idx; }; /** * Copy all non-redundant info. * References left untouched: local idx value and links * @private * @param {!Inode} src_inode * @param {!Inode} dest_inode */ FS.prototype.copy_inode = function(src_inode, dest_inode) { Object.assign(dest_inode, src_inode, { fid: dest_inode.fid, direntries: dest_inode.direntries, nlinks: dest_inode.nlinks, }); }; FS.prototype.CreateInode = function() { //console.log("CreateInode", Error().stack); const now = Math.round(Date.now() / 1000); const inode = new Inode(++this.qidcounter.last_qidnumber); inode.atime = inode.ctime = inode.mtime = now; return inode; }; // Note: parentid = -1 for initial root directory. FS.prototype.CreateDirectory = function(name, parentid) { const parent_inode = this.inodes[parentid]; if(parentid >= 0 && this.is_forwarder(parent_inode)) { const foreign_parentid = parent_inode.foreign_id; const foreign_id = this.follow_fs(parent_inode).CreateDirectory(name, foreign_parentid); return this.create_forwarder(parent_inode.mount_id, foreign_id); } var x = this.CreateInode(); x.mode = 0x01FF | S_IFDIR; x.updatedir = true; if (parentid >= 0) { x.uid = this.inodes[parentid].uid; x.gid = this.inodes[parentid].gid; x.mode = (this.inodes[parentid].mode & 0x1FF) | S_IFDIR; } x.qid.type = S_IFDIR >> 8; this.PushInode(x, parentid, name); this.NotifyListeners(this.inodes.length-1, 'newdir'); return this.inodes.length-1; }; FS.prototype.CreateFile = function(filename, parentid) { const parent_inode = this.inodes[parentid]; if(this.is_forwarder(parent_inode)) { const foreign_parentid = parent_inode.foreign_id; const foreign_id = this.follow_fs(parent_inode).CreateFile(filename, foreign_parentid); return this.create_forwarder(parent_inode.mount_id, foreign_id); } var x = this.CreateInode(); x.dirty = true; x.uid = this.inodes[parentid].uid; x.gid = this.inodes[parentid].gid; x.qid.type = S_IFREG >> 8; x.mode = (this.inodes[parentid].mode & 0x1B6) | S_IFREG; this.PushInode(x, parentid, filename); this.NotifyListeners(this.inodes.length-1, 'newfile'); return this.inodes.length-1; }; FS.prototype.CreateNode = function(filename, parentid, major, minor) { const parent_inode = this.inodes[parentid]; if(this.is_forwarder(parent_inode)) { const foreign_parentid = parent_inode.foreign_id; const foreign_id = this.follow_fs(parent_inode).CreateNode(filename, foreign_parentid, major, minor); return this.create_forwarder(parent_inode.mount_id, foreign_id); } var x = this.CreateInode(); x.major = major; x.minor = minor; x.uid = this.inodes[parentid].uid; x.gid = this.inodes[parentid].gid; x.qid.type = S_IFSOCK >> 8; x.mode = (this.inodes[parentid].mode & 0x1B6); this.PushInode(x, parentid, filename); return this.inodes.length-1; }; FS.prototype.CreateSymlink = function(filename, parentid, symlink) { const parent_inode = this.inodes[parentid]; if(this.is_forwarder(parent_inode)) { const foreign_parentid = parent_inode.foreign_id; const foreign_id = this.follow_fs(parent_inode).CreateSymlink(filename, foreign_parentid, symlink); return this.create_forwarder(parent_inode.mount_id, foreign_id); } var x = this.CreateInode(); x.uid = this.inodes[parentid].uid; x.gid = this.inodes[parentid].gid; x.qid.type = S_IFLNK >> 8; x.symlink = symlink; x.mode = S_IFLNK; this.PushInode(x, parentid, filename); return this.inodes.length-1; }; FS.prototype.CreateTextFile = function(filename, parentid, str) { const parent_inode = this.inodes[parentid]; if(this.is_forwarder(parent_inode)) { const foreign_parentid = parent_inode.foreign_id; const foreign_id = this.follow_fs(parent_inode).CreateTextFile(filename, foreign_parentid, str); return this.create_forwarder(parent_inode.mount_id, foreign_id); } var id = this.CreateFile(filename, parentid); var x = this.inodes[id]; var data = this.inodedata[id] = new Uint8Array(str.length); x.dirty = true; x.size = str.length; for (var j = 0; j < str.length; j++) { data[j] = str.charCodeAt(j); } return id; }; /** * @param {Uint8Array} buffer */ FS.prototype.CreateBinaryFile = function(filename, parentid, buffer) { const parent_inode = this.inodes[parentid]; if(this.is_forwarder(parent_inode)) { const foreign_parentid = parent_inode.foreign_id; const foreign_id = this.follow_fs(parent_inode).CreateBinaryFile(filename, foreign_parentid, buffer); return this.create_forwarder(parent_inode.mount_id, foreign_id); } var id = this.CreateFile(filename, parentid); var x = this.inodes[id]; var data = this.inodedata[id] = new Uint8Array(buffer.length); x.dirty = true; data.set(buffer); x.size = buffer.length; return id; }; FS.prototype.OpenInode = function(id, mode) { var inode = this.inodes[id]; if(this.is_forwarder(inode)) { return this.follow_fs(inode).OpenInode(inode.foreign_id, mode); } if ((inode.mode&S_IFMT) == S_IFDIR) { this.FillDirectory(id); } /* var type = ""; switch(inode.mode&S_IFMT) { case S_IFREG: type = "File"; break; case S_IFBLK: type = "Block Device"; break; case S_IFDIR: type = "Directory"; break; case S_IFCHR: type = "Character Device"; break; } */ //message.Debug("open:" + this.GetFullPath(id) + " type: " + inode.mode + " status:" + inode.status); if (inode.status == STATUS_ON_SERVER) { this.LoadFile(id); return false; } return true; }; FS.prototype.CloseInode = function(id) { //message.Debug("close: " + this.GetFullPath(id)); var inode = this.inodes[id]; if(this.is_forwarder(inode)) { return this.follow_fs(inode).CloseInode(inode.foreign_id); } if (inode.status == STATUS_UNLINKED) { //message.Debug("Filesystem: Delete unlinked file"); inode.status = STATUS_INVALID; delete this.inodedata[id]; inode.size = 0; } }; /** * @return {number} 0 if success, or -errno if failured. */ FS.prototype.Rename = function(olddirid, oldname, newdirid, newname) { // message.Debug("Rename " + oldname + " to " + newname); if ((olddirid == newdirid) && (oldname == newname)) { return 0; } var oldid = this.Search(olddirid, oldname); if(oldid === -1) { return -ENOENT; } // For event notification near end of method. var oldpath = this.GetFullPath(olddirid) + "/" + oldname; var newid = this.Search(newdirid, newname); if (newid != -1) { const ret = this.Unlink(newdirid, newname); if(ret < 0) return ret; } var idx = oldid; // idx contains the id which we want to rename var inode = this.inodes[idx]; const olddir = this.inodes[olddirid]; const newdir = this.inodes[newdirid]; if(!this.is_forwarder(olddir) && !this.is_forwarder(newdir)) { // Move inode within current filesystem. this.unlink_from_dir(olddirid, oldname); this.link_under_dir(newdirid, idx, newname); inode.qid.version++; } else if(this.is_forwarder(olddir) && olddir.mount_id === newdir.mount_id) { // Move inode within the same child filesystem. const ret = this.follow_fs(olddir) .Rename(olddir.foreign_id, oldname, newdir.foreign_id, newname); if(ret < 0) return ret; } else if(this.is_a_root(idx)) { // The actual inode is a root of some descendant filesystem. // Moving mountpoint across fs not supported - needs to update all corresponding forwarders. dbg_log("XXX: Attempted to move mountpoint (" + oldname + ") - skipped", LOG_9P); return -EPERM; } else if(!this.IsDirectory(idx) && this.GetInode(idx).nlinks > 1) { // Move hardlinked inode vertically in mount tree. dbg_log("XXX: Attempted to move hardlinked file (" + oldname + ") " + "across filesystems - skipped", LOG_9P); return -EPERM; } else { // Jump between filesystems. // Can't work with both old and new inode information without first diverting the old // information into a new idx value. const diverted_old_idx = this.divert(olddirid, oldname); const old_real_inode = this.GetInode(idx); const data = this.Read(diverted_old_idx, 0, old_real_inode.size); if(this.is_forwarder(newdir)) { // Create new inode. const foreign_fs = this.follow_fs(newdir); const foreign_id = this.IsDirectory(diverted_old_idx) ? foreign_fs.CreateDirectory(newname, newdir.foreign_id) : foreign_fs.CreateFile(newname, newdir.foreign_id); const new_real_inode = foreign_fs.GetInode(foreign_id); this.copy_inode(old_real_inode, new_real_inode); // Point to this new location. this.set_forwarder(idx, newdir.mount_id, foreign_id); } else { // Replace current forwarder with real inode. this.delete_forwarder(inode); this.copy_inode(old_real_inode, inode); // Link into new location in this filesystem. this.link_under_dir(newdirid, idx, newname); } // Rewrite data to newly created destination. this.ChangeSize(idx, old_real_inode.size); if(data && data.length) { this.Write(idx, 0, data.length, data); } // Move children to newly created destination. if(this.IsDirectory(idx)) { for(const child_filename of this.GetChildren(diverted_old_idx)) { const ret = this.Rename(diverted_old_idx, child_filename, idx, child_filename); if(ret < 0) return ret; } } // Perform destructive changes only after migration succeeded. this.DeleteData(diverted_old_idx); const ret = this.Unlink(olddirid, oldname); if(ret < 0) return ret; } this.NotifyListeners(idx, "rename", {oldpath: oldpath}); return 0; }; FS.prototype.Write = function(id, offset, count, buffer) { this.NotifyListeners(id, 'write'); var inode = this.inodes[id]; if(this.is_forwarder(inode)) { const foreign_id = inode.foreign_id; this.follow_fs(inode).Write(foreign_id, offset, count, buffer); return; } inode.dirty = true; var data = this.inodedata[id]; if (!data || data.length < (offset+count)) { this.ChangeSize(id, Math.floor(((offset+count)*3)/2) ); inode.size = offset + count; data = this.inodedata[id]; } else if (inode.size < (offset+count)) { inode.size = offset + count; } if(buffer) { data.set(buffer.subarray(0, count), offset); } }; FS.prototype.Read = function(inodeid, offset, count) { const inode = this.inodes[inodeid]; if(this.is_forwarder(inode)) { const foreign_id = inode.foreign_id; return this.follow_fs(inode).Read(foreign_id, offset, count); } else if(!this.inodedata[inodeid]) { return null; } else { return this.inodedata[inodeid].subarray(offset, offset + count); } }; FS.prototype.Search = function(parentid, name) { const parent_inode = this.inodes[parentid]; if(this.is_forwarder(parent_inode)) { const foreign_parentid = parent_inode.foreign_id; const foreign_id = this.follow_fs(parent_inode).Search(foreign_parentid, name); if(foreign_id === -1) return -1; return this.get_forwarder(parent_inode.mount_id, foreign_id); } const childid = parent_inode.direntries.get(name); return childid === undefined ? -1 : childid; }; FS.prototype.CountUsedInodes = function() { let count = this.inodes.length; for(const { fs, backtrack } of this.mounts) { count += fs.CountUsedInodes(); // Forwarder inodes don't count. count -= backtrack.size; } return count; }; FS.prototype.CountFreeInodes = function() { let count = 1024 * 1024; for(const { fs } of this.mounts) { count += fs.CountFreeInodes(); } return count; }; FS.prototype.GetTotalSize = function() { let size = this.used_size; for(const { fs } of this.mounts) { size += fs.GetTotalSize(); } return size; //var size = 0; //for(var i=0; i= 0 && idx < this.inodes.length, "Filesystem GetInode: out of range idx:" + idx); const inode = this.inodes[idx]; if(this.is_forwarder(inode)) { return this.follow_fs(inode).GetInode(inode.foreign_id); } return inode; }; FS.prototype.ChangeSize = function(idx, newsize) { var inode = this.GetInode(idx); var temp = this.inodedata[idx]; inode.dirty = true; //message.Debug("change size to: " + newsize); if (newsize == inode.size) return; var data = this.inodedata[idx] = new Uint8Array(newsize); inode.size = newsize; if(!temp) return; var size = Math.min(temp.length, inode.size); data.set(temp.subarray(0, size), 0); }; FS.prototype.SearchPath = function(path) { //path = path.replace(/\/\//g, "/"); path = path.replace("//", "/"); var walk = path.split("/"); if (walk.length > 0 && walk[walk.length - 1].length === 0) walk.pop(); if (walk.length > 0 && walk[0].length === 0) walk.shift(); const n = walk.length; var parentid = -1; var id = 0; let forward_path = null; for(var i=0; i} list */ FS.prototype.GetRecursiveList = function(dirid, list) { if(this.is_forwarder(this.inodes[dirid])) { const foreign_fs = this.follow_fs(this.inodes[dirid]); const foreign_dirid = this.inodes[dirid].foreign_id; const mount_id = this.inodes[dirid].mount_id; const foreign_start = list.length; foreign_fs.GetRecursiveList(foreign_dirid, list); for(let i = foreign_start; i < list.length; i++) { list[i].parentid = this.get_forwarder(mount_id, list[i].parentid); } return; } for(const [name, id] of this.inodes[dirid].direntries) { if(name !== "." && name !== "..") { list.push({ parentid: dirid, name }); if(this.IsDirectory(id)) { this.GetRecursiveList(id, list); } } } }; FS.prototype.RecursiveDelete = function(path) { var toDelete = []; var ids = this.SearchPath(path); if(ids.id === -1) return; this.GetRecursiveList(ids.id, toDelete); for(var i=toDelete.length-1; i>=0; i--) { const ret = this.Unlink(toDelete[i].parentid, toDelete[i].name); dbg_assert(ret === 0, "Filesystem RecursiveDelete failed at parent=" + toDelete[i].parentid + ", name='" + toDelete[i].name + "' with error code: " + (-ret)); } }; FS.prototype.DeleteNode = function(path) { var ids = this.SearchPath(path); if (ids.id == -1) return; if ((this.inodes[ids.id].mode&S_IFMT) == S_IFREG){ const ret = this.Unlink(ids.parentid, ids.name); dbg_assert(ret === 0, "Filesystem DeleteNode failed with error code: " + (-ret)); return; } if ((this.inodes[ids.id].mode&S_IFMT) == S_IFDIR){ this.RecursiveDelete(path); const ret = this.Unlink(ids.parentid, ids.name); dbg_assert(ret === 0, "Filesystem DeleteNode failed with error code: " + (-ret)); return; } }; /** @param {*=} info */ FS.prototype.NotifyListeners = function(id, action, info) { //if(info==undefined) // info = {}; //var path = this.GetFullPath(id); //if (this.watchFiles[path] == true && action=='write') { // message.Send("WatchFileEvent", path); //} //for (var directory in this.watchDirectories) { // if (this.watchDirectories.hasOwnProperty(directory)) { // var indexOf = path.indexOf(directory) // if(indexOf == 0 || indexOf == 1) // message.Send("WatchDirectoryEvent", {path: path, event: action, info: info}); // } //} }; FS.prototype.Check = function() { for(var i=1; i> 12, name], data, offset); } }; FS.prototype.RoundToDirentry = function(dirid, offset_target) { const data = this.inodedata[dirid]; dbg_assert(data, `FS directory data for dirid=${dirid} should be generated`); dbg_assert(data.length, "FS directory should have at least an entry"); if(offset_target >= data.length) { return data.length; } let offset = 0; while(true) { const next_offset = marshall.Unmarshall(["Q", "d"], data, { offset })[1]; if(next_offset > offset_target) break; offset = next_offset; } return offset; }; /** * @param {number} idx * @return {boolean} */ FS.prototype.IsDirectory = function(idx) { const inode = this.inodes[idx]; if(this.is_forwarder(inode)) { return this.follow_fs(inode).IsDirectory(inode.foreign_id); } return (inode.mode & S_IFMT) === S_IFDIR; }; /** * @param {number} idx * @return {boolean} */ FS.prototype.IsEmpty = function(idx) { const inode = this.inodes[idx]; if(this.is_forwarder(inode)) { return this.follow_fs(inode).IsDirectory(inode.foreign_id); } for(const name of inode.direntries.keys()) { if(name !== "." && name !== "..") return false; } return true; }; /** * @param {number} idx * @return {!Array} List of children names */ FS.prototype.GetChildren = function(idx) { dbg_assert(this.IsDirectory(idx), "Filesystem: cannot get children of non-directory inode"); const inode = this.inodes[idx]; if(this.is_forwarder(inode)) { return this.follow_fs(inode).GetChildren(inode.foreign_id); } const children = []; for(const name of inode.direntries.keys()) { if(name !== "." && name !== "..") { children.push(name); } } return children; }; /** * @param {number} idx * @return {number} Local idx of parent */ FS.prototype.GetParent = function(idx) { dbg_assert(this.IsDirectory(idx), "Filesystem: cannot get parent of non-directory inode"); const inode = this.inodes[idx]; if(this.should_be_linked(inode)) { return inode.direntries.get(".."); } else { const foreign_dirid = this.follow_fs(inode).GetParent(inode.foreign_id); dbg_assert(foreign_dirid !== -1, "Filesystem: should not have invalid parent ids"); return this.get_forwarder(inode.mount_id, foreign_dirid); } }; // ----------------------------------------------------- // only support for security.capabilities // should return a "struct vfs_cap_data" defined in // linux/capability for format // check also: // sys/capability.h // http://lxr.free-electrons.com/source/security/commoncap.c#L376 // http://man7.org/linux/man-pages/man7/capabilities.7.html // http://man7.org/linux/man-pages/man8/getcap.8.html // http://man7.org/linux/man-pages/man3/libcap.3.html FS.prototype.PrepareCAPs = function(id) { var inode = this.GetInode(id); if (inode.caps) return inode.caps.length; inode.caps = new Uint8Array(20); // format is little endian // note: getxattr returns -EINVAL if using revision 1 format. // note: getxattr presents revision 3 as revision 2 when revision 3 is not needed. // magic_etc (revision=0x02: 20 bytes) inode.caps[0] = 0x00; inode.caps[1] = 0x00; inode.caps[2] = 0x00; inode.caps[3] = 0x02; // lower // permitted (first 32 capabilities) inode.caps[4] = 0xFF; inode.caps[5] = 0xFF; inode.caps[6] = 0xFF; inode.caps[7] = 0xFF; // inheritable (first 32 capabilities) inode.caps[8] = 0xFF; inode.caps[9] = 0xFF; inode.caps[10] = 0xFF; inode.caps[11] = 0xFF; // higher // permitted (last 6 capabilities) inode.caps[12] = 0x3F; inode.caps[13] = 0x00; inode.caps[14] = 0x00; inode.caps[15] = 0x00; // inheritable (last 6 capabilities) inode.caps[16] = 0x3F; inode.caps[17] = 0x00; inode.caps[18] = 0x00; inode.caps[19] = 0x00; return inode.caps.length; }; // ----------------------------------------------------- /** * @constructor * @param {FS} filesystem */ function FSMountInfo(filesystem) { /** @type {FS}*/ this.fs = filesystem; /** * Maps foreign inode id back to local inode id. * @type {!Map} */ this.backtrack = new Map(); } FSMountInfo.prototype.get_state = function() { const state = []; state[0] = this.fs; state[1] = [...this.backtrack]; return state; }; FSMountInfo.prototype.set_state = function(state) { this.fs = state[0]; this.backtrack = new Map(state[1]); }; /** * @private * @param {number} idx Local idx of inode. * @param {number} mount_id Mount number of the destination fs. * @param {number} foreign_id Foreign idx of destination inode. */ FS.prototype.set_forwarder = function(idx, mount_id, foreign_id) { const inode = this.inodes[idx]; dbg_assert(inode.nlinks === 0, "Filesystem: attempted to convert an inode into forwarder before unlinking the inode"); if(this.is_forwarder(inode)) { this.mounts[inode.mount_id].backtrack.delete(inode.foreign_id); } inode.status = STATUS_FORWARDING; inode.mount_id = mount_id; inode.foreign_id = foreign_id; this.mounts[mount_id].backtrack.set(foreign_id, idx); }; /** * @private * @param {number} mount_id Mount number of the destination fs. * @param {number} foreign_id Foreign idx of destination inode. * @return {number} Local idx of newly created forwarder. */ FS.prototype.create_forwarder = function(mount_id, foreign_id) { const inode = this.CreateInode(); const idx = this.inodes.length; this.inodes.push(inode); inode.fid = idx; this.set_forwarder(idx, mount_id, foreign_id); return idx; }; /** * @private * @param {Inode} inode * @return {boolean} */ FS.prototype.is_forwarder = function(inode) { return inode.status === STATUS_FORWARDING; }; /** * Whether the inode it points to is a root of some filesystem. * @private * @param {number} idx * @return {boolean} */ FS.prototype.is_a_root = function(idx) { return this.GetInode(idx).fid === 0; }; /** * Ensures forwarder exists, and returns such forwarder, for the described foreign inode. * @private * @param {number} mount_id * @param {number} foreign_id * @return {number} Local idx of a forwarder to described inode. */ FS.prototype.get_forwarder = function(mount_id, foreign_id) { const mount = this.mounts[mount_id]; dbg_assert(foreign_id >= 0, "Filesystem get_forwarder: invalid foreign_id: " + foreign_id); dbg_assert(mount, "Filesystem get_forwarder: invalid mount number: " + mount_id); const result = mount.backtrack.get(foreign_id); if(result === undefined) { // Create if not already exists. return this.create_forwarder(mount_id, foreign_id); } return result; }; /** * @private * @param {Inode} inode */ FS.prototype.delete_forwarder = function(inode) { dbg_assert(this.is_forwarder(inode), "Filesystem delete_forwarder: expected forwarder"); inode.status = STATUS_INVALID; this.mounts[inode.mount_id].backtrack.delete(inode.foreign_id); }; /** * @private * @param {Inode} inode * @return {FS} */ FS.prototype.follow_fs = function(inode) { const mount = this.mounts[inode.mount_id]; dbg_assert(this.is_forwarder(inode), "Filesystem follow_fs: inode should be a forwarding inode"); dbg_assert(mount, "Filesystem follow_fs: inode should point to valid mounted FS"); return mount.fs; }; /** * Mount another filesystem to given path. * @param {string} path * @param {FS} fs * @return {number} inode id of mount point if successful, or -errno if mounting failed. */ FS.prototype.Mount = function(path, fs) { dbg_assert(fs.qidcounter === this.qidcounter, "Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem."); const path_infos = this.SearchPath(path); if(path_infos.parentid === -1) { dbg_log("Mount failed: parent for path not found: " + path, LOG_9P); return -ENOENT; } if(path_infos.id !== -1) { dbg_log("Mount failed: file already exists at path: " + path, LOG_9P); return -EEXIST; } if(path_infos.forward_path) { const parent = this.inodes[path_infos.parentid]; const ret = this.follow_fs(parent).Mount(path_infos.forward_path, fs); if(ret < 0) return ret; return this.get_forwarder(parent.mount_id, ret); } const mount_id = this.mounts.length; this.mounts.push(new FSMountInfo(fs)); const idx = this.create_forwarder(mount_id, 0); this.link_under_dir(path_infos.parentid, idx, path_infos.name); return idx; };