commands should not access fd.node on file descriptors

This commit is contained in:
Lisa Milne 2023-11-13 20:16:35 +10:00
parent a15f693f93
commit 80543aef12
2 changed files with 47 additions and 14 deletions

View file

@ -48,15 +48,18 @@ Options:
`);
}
function writeNodeData(node) {
function writeNodeData(st,fd) {
if (long) {
if (node.data.islink) {
io.write(node.perms+'\t'+node.uid+'\t'+node.gid+'\t'+node.name+' -> '+node.data.content);
if (st.type == 4) {
var d = stdio.readAll(fd);
if (!d)
d = '?';
io.write(st.perms+'\t'+st.uid+'\t'+st.gid+'\t'+st.name+' -> '+d);
}else{
io.write(node.perms+'\t'+node.uid+'\t'+node.gid+'\t'+node.name);
io.write(st.perms+'\t'+st.uid+'\t'+st.gid+'\t'+st.name);
}
}else{
io.write(node.name);
io.write(st.name);
}
}
@ -91,14 +94,27 @@ Options:
io.error('cannot open directory: '+dir);
return;
}
if (!fd.node.data.isdir) {
writeNodeData(fd.node);
var st = stdio.fstat(fd);
if (!st || st.type != 3) {
writeNodeData(st,fd);
stdio.close(fd);
return;
}
var e;
while ((e = stdio.read(fd)) != null) {
writeNodeData(e);
var efd = stdio.open(dir+'/'+e,false,true);
if (!efd) {
io.error('cannot read contents: '+dir);
break;
}
var est = stdio.fstat(efd);
if (!est) {
stdio.close(efd);
io.error('cannot read contents: '+dir);
break;
}
writeNodeData(est,efd);
stdio.close(efd);
}
stdio.close(fd);
});
@ -149,15 +165,21 @@ Options:
pending = files.length;
function fcb(fd) {
if (fd.node.data.isdir || fd.node.data.islink) {
io.error('not a normal file: '+fd.node.name);
var st = stdio.fstat(fd);
// check if it's a directory or link
if (!st || st.type == 3 || st.type == 4) {
if (st) {
io.error('not a normal file: '+st.name);
}else{
io.error('not a normal file');
}
}else{
var d = stdio.readAll(fd);
if (d != null) {
if (download) {
var ld = stdio.open('/dev/local',false);
if (ld) {
stdio.write(ld,{name:fd.node.name,data:d});
stdio.write(ld,{name:st.name,data:d});
stdio.close(ld);
}else{
io.error('can not write to local device');
@ -180,7 +202,9 @@ Options:
pending--;
return;
}
if (fd.node.data.isdir || fd.node.data.islink) {
var st = stdio.fstat(fd);
// check if it's a directory or link
if (!st || st.type == 3 || st.type == 4) {
io.error('not a normal file: '+file);
pending--;
return;

View file

@ -519,8 +519,12 @@ clite.io = {
return vfsapi.mkFile(path);
}
}
clite.io.open = function(path,cb) {
return getFileDes(path,false,cb);
clite.io.open = function(path,cb,open_link) {
if (typeof cb === 'undefined')
cb = false;
if (typeof open_link === 'undefined')
open_link = false;
return getFileDes(path,open_link,cb);
}
clite.io.close = function(fd) {
try{
@ -542,6 +546,10 @@ clite.io = {
}
if (fd.pos >= fd.node.data.content.length)
return null;
if (fd.node.data.isdir) {
var e = fd.node.data.content[fd.pos++];
return e.name;
}
return fd.node.data.content[fd.pos++];
}
clite.io.readLine = function(fd) {
@ -662,6 +670,7 @@ clite.io = {
return null;
var stat = Object.create({
name:fd.node.name,
type:clite.lib.getFileType(fd),
uid:fd.node.uid,
gid:fd.node.gid,