persistant filesystem part 2

This commit is contained in:
Lisa Milne 2023-12-18 10:55:19 +10:00
parent e852f31781
commit 2d8214a6c8
3 changed files with 147 additions and 10 deletions

View file

@ -4,6 +4,7 @@ var clite = {
isinit:false,
runlevel:1,
bios:null,
cookiesAccepted:null
},
includes:{
// a list of program/command files to load
@ -511,12 +512,7 @@ license.txt:/etc/license:0:0:-rw-r--r--`;
if (b) {
let list = b.getList();
list.forEach(function(fn) {
let d = clite.lib.dirname(fn);
vfsapi.mkPath(0,d);
vfsapi.mkFile(0,fn);
let n = vfsapi.getNode(0,fn);
if (n)
n.data.content = b.getFile(fn);
clite.vfs.restoreFile(fn);
});
}
// populate /dev (data/filesys.txt is /dev/wfs already)
@ -923,7 +919,6 @@ clite.user = {
return null;
}
function saveUsers() {
var b = clite.state.bios.io.read(2)
var n = vfsapi.getNode(0,'/etc/passwd');
if (!n)
return false;
@ -935,7 +930,7 @@ clite.user = {
pw = '$'+u.pass;
n.data.content += u.name+':'+pw+':'+u.uid+':'+u.gid+'::'+u.env.HOME+':'+u.env.SHELL+'\n';
});
b.addFile('/etc/passwd',n.data.content);
clite.vfs.saveFile('/etc/passwd');
n = vfsapi.getNode(0,'/etc/group');
if (!n)
return false;
@ -944,7 +939,7 @@ clite.user = {
data.groups.forEach(function(g) {
n.data.content += g.name+':x:'+g.gid+':'+g.users.join(',')+'\n'
});
b.addFile('/etc/group',n.data.content);
clite.vfs.saveFile('/etc/group');
}
function loadUsers() {
// load in user data from /etc/passwd
@ -2469,6 +2464,131 @@ clite.vfs = {
parent.data.content = c;
return true;
}
clite.vfs.getSavedFile = function(path) {
var fsnode = {
name: '',
uid: 0,
gid: 0,
mode:clite.io.modes.S_IRUSR|clite.io.modes.S_IWUSR|clite.io.modes.S_IRGRP|clite.io.modes.S_IROTH,
time:{
access:clite.time.sec(),
modify:clite.time.sec(),
change:clite.time.sec()
},
data:{
remote:null,
isdir:false,
islink:false,
isdev:false,
istty:false,
parent:null,
content:null
}
};
let n = vfsdata.api.getNode(0,path);
if (!n)
return null;
var str = n.uid.toString(10)+':'+n.gid.toString(10)+':';
str += n.mode.toString(10)+':'+n.time.access.toString(10)+':';
str += n.time.modify.toString(10)+':'+n.time.change.toString(10)+':';
let t = clite.lib.getFileType({node:n});
str += t.toString(10)+':';
if (typeof n.data.content === 'string' && (t == clite.io.types.FT_LINK || t == clite.io.types.FT_SCRIPT || t == clite.io.types.FT_TEXT))
str += n.data.content.length.toString(10)+':'+n.data.content;
return str
}
clite.vfs.setSavedFile = function(path,data) {
let ind = data.indexOf(':');
if (ind < 0)
return false;
let v = data.substring(0,ind);
let str = data.substring(ind+1);
let uid = parseInt(v,10);
ind = str.indexOf(':');
if (ind < 0)
return false;
v = str.substring(0,ind);
str = str.substring(ind+1);
let gid = parseInt(v,10);
ind = str.indexOf(':');
if (ind < 0)
return false;
v = str.substring(0,ind);
str = str.substring(ind+1);
let mode = parseInt(v,10)
ind = str.indexOf(':');
if (ind < 0)
return false;
v = str.substring(0,ind);
str = str.substring(ind+1);
let access = parseInt(v,10);
ind = str.indexOf(':');
if (ind < 0)
return false;
v = str.substring(0,ind);
str = str.substring(ind+1);
let modify = parseInt(v,10);
ind = str.indexOf(':');
if (ind < 0)
return false;
v = str.substring(0,ind);
str = str.substring(ind+1);
let change = parseInt(v,10);
ind = str.indexOf(':');
if (ind < 0)
return false;
v = str.substring(0,ind);
str = str.substring(ind+1);
let type = parseInt(v,10);
let size = 0;
let content = null;
ind = str.indexOf(':');
if (ind > -1) {
v = str.substring(0,ind);
content = str.substring(ind+1);
size = parseInt(v,10);
}
let n = vfsdata.api.getNode(0,path);
if (!n) {
return false;
}
if (clite.lib.getFileType({node:n}) != type)
return false;
n.uid = uid;
n.gid = gid;
n.mode = mode;
n.time.access = access;
n.time.modify = modify;
n.time.change = change;
if (content != null)
n.data.content = content;
return true;
}
clite.vfs.saveFile = function(path) {
if (!clite.state.cookiesAccepted)
return true;
var str = clite.vfs.getSavedFile(path);
if (!str)
return false;
var b = clite.state.bios.io.read(2);
if (!b)
return false;
b.addFile(path,str);
return true;
}
clite.vfs.restoreFile = function(path) {
var b = clite.state.bios.io.read(2);
if (!b)
return false;
var d = b.getFile(path);
if (!d)
return false;
return clite.vfs.setSavedFile(path,d);
}
// make some directories
vfsdata.api.mkDir(0,'/bin');
@ -2502,6 +2622,12 @@ clite.vfs = {
clite.core.execSafeAsync(function() {clite.vfs.init = null;});
},
getSavedFile:function(path) {
return null;
},
setSavedFile:function(path,data) {
return false;
},
getApi:null
};

View file

@ -146,6 +146,14 @@ This way all of your personal data is kept on your own computer, rather
than being uploaded to a random server somewhere.
`);
},
getcookiestate:function() {
return clite.state.cookiesAccepted;
},
setcookiestate:function(v) {
if (typeof v == 'boolean')
clite.state.cookiesAccepted = v;
}
});

View file

@ -78,6 +78,7 @@ Options:
function preLogin(str) {
if (str == 'no') {
clite.setcookiestate(false);
io.exit(0);
return;
}
@ -86,6 +87,8 @@ Options:
return;
}
clite.setcookiestate(true);
if (stdlib.getuid() != 0) {
stdio.write(io.stdout,'Password: ');
term.ttyctrl('echo',false);
@ -169,7 +172,7 @@ Options:
return 1;
}
if (accept || user == 'root' || user == 'guest') {
if (accept || user == 'root' || user == 'guest' || clite.getcookiestate() == true) {
preLogin('yes');
return null;
}