Reduce size of filesystem state

This commit is contained in:
Fabian 2020-12-31 19:14:33 -06:00
parent d5c49ea662
commit 6b38af941e

View file

@ -92,9 +92,12 @@ FS.prototype.get_state = function()
state[0] = this.inodes;
state[1] = this.qidcounter.last_qidnumber;
state[2] = [];
for(let entry of Object.entries(this.inodedata))
for(const [id, data] of Object.entries(this.inodedata))
{
state[2].push(entry);
if((this.inodes[id].mode & S_IFDIR) === 0)
{
state[2].push([id, data]);
}
}
state[3] = this.total_size;
state[4] = this.used_size;
@ -201,7 +204,6 @@ FS.prototype.LoadRecursive = function(data, parentid)
if(ifmt === S_IFDIR)
{
inode.updatedir = true;
this.PushInode(inode, parentid, name);
this.LoadDir(this.inodes.length - 1, data[JSONFS_IDX_TARGET]);
}
@ -272,7 +274,6 @@ FS.prototype.link_under_dir = function(parentid, idx, name)
"Filesystem: Name '" + name + "' is already taken");
parent_inode.direntries.set(name, idx);
parent_inode.updatedir = true;
inode.nlinks++;
if(this.IsDirectory(idx))
@ -285,8 +286,6 @@ FS.prototype.link_under_dir = function(parentid, idx, name)
inode.direntries.set("..", parentid);
parent_inode.nlinks++;
inode.updatedir = true;
}
};
@ -312,7 +311,6 @@ FS.prototype.unlink_from_dir = function(parentid, name)
}
inode.nlinks--;
parent_inode.updatedir = true;
if(this.IsDirectory(idx))
{
@ -321,8 +319,6 @@ FS.prototype.unlink_from_dir = function(parentid, name)
inode.direntries.delete("..");
parent_inode.nlinks--;
inode.updatedir = true;
}
dbg_assert(inode.nlinks >= 0,
@ -353,7 +349,6 @@ FS.prototype.PushInode = function(inode, parentid, name) {
/** @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;
@ -374,7 +369,6 @@ function Inode(qidnumber)
};
this.caps = undefined;
this.nlinks = 0;
this.dirty = false; // has this file changed?
this.sha256sum = "";
/** @type{!Array<!FSLockRegion>} */
@ -392,43 +386,77 @@ function Inode(qidnumber)
Inode.prototype.get_state = function()
{
const state = [];
state[0] = this.updatedir;
state[1] = [...this.direntries];
state[0] = this.mode;
if((this.mode & S_IFMT) === S_IFDIR)
{
state[1] = [...this.direntries];
}
else if((this.mode & S_IFMT) === S_IFREG)
{
state[1] = this.sha256sum;
}
else if((this.mode & S_IFMT) === S_IFLNK)
{
state[1] = this.symlink;
}
else if((this.mode & S_IFMT) === S_IFSOCK)
{
state[1] = [this.minor, this.major];
}
else
{
state[1] = null;
}
state[2] = this.locks;
//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;
state[3] = this.status;
state[4] = this.size;
state[5] = this.uid;
state[6] = this.gid;
state[7] = this.fid;
state[8] = this.ctime;
state[9] = this.atime;
state[10] = this.mtime;
state[11] = this.qid.version;
state[12] = this.qid.path;
state[13] = this.nlinks;
//state[23] = this.mount_id;
//state[24] = this.foreign_id;
//state[25] = this.caps; // currently not writable
return state;
};
Inode.prototype.set_state = function(state)
{
this.updatedir = state[0];
this.direntries = new Map();
for(const [name, entry] of state[1])
this.mode = state[0];
if((this.mode & S_IFMT) === S_IFDIR)
{
this.direntries.set(name, entry);
this.direntries = new Map();
for(const [name, entry] of state[1])
{
this.direntries.set(name, entry);
}
}
else if((this.mode & S_IFMT) === S_IFREG)
{
this.sha256sum = state[1];
}
else if((this.mode & S_IFMT) === S_IFLNK)
{
this.symlink = state[1];
}
else if((this.mode & S_IFMT) === S_IFSOCK)
{
[this.minor, this.major] = state[1];
}
else
{
// Nothing
}
this.locks = [];
for(const lock_state of state[2])
{
@ -436,29 +464,22 @@ Inode.prototype.set_state = function(state)
lock.set_state(lock_state);
this.locks.push(lock);
}
//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];
this.status = state[3];
this.size = state[4];
this.uid = state[5];
this.gid = state[6];
this.fid = state[7];
this.ctime = state[8];
this.atime = state[9];
this.mtime = state[10];
this.qid.type = (this.mode & S_IFMT) >> 8;
this.qid.version = state[11];
this.qid.path = state[12];
this.nlinks = state[13];
//this.mount_id = state[23];
//this.foreign_id = state[24];
//this.caps = state[20];
};
/**
@ -558,7 +579,6 @@ FS.prototype.CreateDirectory = function(name, parentid) {
}
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;
@ -579,7 +599,6 @@ FS.prototype.CreateFile = function(filename, 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;
@ -641,7 +660,6 @@ FS.prototype.CreateTextFile = async function(filename, parentid, str) {
var id = this.CreateFile(filename, parentid);
var x = this.inodes[id];
var data = new Uint8Array(str.length);
x.dirty = true;
x.size = str.length;
for (var j = 0; j < str.length; j++) {
data[j] = str.charCodeAt(j);
@ -665,7 +683,6 @@ FS.prototype.CreateBinaryFile = async function(filename, parentid, buffer) {
var id = this.CreateFile(filename, parentid);
var x = this.inodes[id];
var data = new Uint8Array(buffer.length);
x.dirty = true;
data.set(buffer);
await this.set_data(id, data);
x.size = buffer.length;
@ -847,7 +864,6 @@ FS.prototype.Write = async function(id, offset, count, buffer) {
return;
}
inode.dirty = true;
var data = await this.get_buffer(id);
if (!data || data.length < (offset+count)) {
@ -1155,7 +1171,6 @@ FS.prototype.ChangeSize = async function(idx, newsize)
{
var inode = this.GetInode(idx);
var temp = await this.get_data(idx, 0, inode.size);
inode.dirty = true;
//message.Debug("change size to: " + newsize);
if (newsize == inode.size) return;
var data = new Uint8Array(newsize);
@ -1321,8 +1336,6 @@ FS.prototype.FillDirectory = function(dirid) {
this.follow_fs(inode).FillDirectory(inode.foreign_id);
return;
}
if (!inode.updatedir) return;
inode.updatedir = false;
let size = 0;
for(const name of inode.direntries.keys())