make file types human readable-ish, FT_TEXT rather than 1

This commit is contained in:
Lisa Milne 2023-11-13 20:43:09 +10:00
parent 80543aef12
commit 3dbefb93da
2 changed files with 42 additions and 22 deletions

View file

@ -50,7 +50,7 @@ Options:
function writeNodeData(st,fd) { function writeNodeData(st,fd) {
if (long) { if (long) {
if (st.type == 4) { if (st.type == stdio.types.FT_LINK) {
var d = stdio.readAll(fd); var d = stdio.readAll(fd);
if (!d) if (!d)
d = '?'; d = '?';
@ -95,7 +95,7 @@ Options:
return; return;
} }
var st = stdio.fstat(fd); var st = stdio.fstat(fd);
if (!st || st.type != 3) { if (!st || st.type != stdio.types.FT_DIR) {
writeNodeData(st,fd); writeNodeData(st,fd);
stdio.close(fd); stdio.close(fd);
return; return;
@ -167,7 +167,7 @@ Options:
function fcb(fd) { function fcb(fd) {
var st = stdio.fstat(fd); var st = stdio.fstat(fd);
// check if it's a directory or link // check if it's a directory or link
if (!st || st.type == 3 || st.type == 4) { if (!st || st.type == stdio.types.FT_DIR || st.type == stdio.types.FT_LINK) {
if (st) { if (st) {
io.error('not a normal file: '+st.name); io.error('not a normal file: '+st.name);
}else{ }else{
@ -196,16 +196,16 @@ Options:
} }
files.forEach(function(file,index) { files.forEach(function(file,index) {
var fd = stdio.open(file,fcb); var st = stdio.stat(file);
if (!fd) { // check if it's a directory or link
io.error('cannot open file: '+file); if (!st || st.type == stdio.types.FT_DIR || st.type == stdio.types.FT_LINK) {
io.error('not a normal file: '+file);
pending--; pending--;
return; return;
} }
var st = stdio.fstat(fd); var fd = stdio.open(file,fcb);
// check if it's a directory or link if (!fd) {
if (!st || st.type == 3 || st.type == 4) { io.error('cannot open file: '+file);
io.error('not a normal file: '+file);
pending--; pending--;
return; return;
} }

View file

@ -714,6 +714,26 @@ clite.io = {
return clite.io.fchmod(fd,mode); return clite.io.fchmod(fd,mode);
} }
}, },
// 0: unknown
// 1: text file
// 2: executable/binary (function)
// 3: directory
// 4: link
// 5: device
// 6: unloaded remote file
// 7: shell script
// 8: image
types:{
FT_UNKOWN:0,
FT_TEXT:1,
FT_BINARY:2,
FT_DIR:3,
FT_LINK:4,
FT_DEV:5,
FT_REMOTE:6,
FT_SCRIPT:7,
FT_IMAGE:8
},
creat:null, creat:null,
open:null, open:null,
close:null, close:null,
@ -1633,39 +1653,39 @@ clite.lib = {
// 8: image // 8: image
if (fd.node.data.content == null) { if (fd.node.data.content == null) {
if (fd.node.data.remote != null) if (fd.node.data.remote != null)
return 6; return clite.io.types.FT_REMOTE;
return 0; return clite.io.types.FT_UNKOWN;
} }
if (fd.node.data.islink) if (fd.node.data.islink)
return 4; return clite.io.types.FT_LINK;
if (fd.node.data.isdir) if (fd.node.data.isdir)
return 3; return clite.io.types.FT_DIR;
if (fd.node.data.isdev) if (fd.node.data.isdev)
return 5; return clite.io.types.FT_DEV;
const type = typeof fd.node.data.content; const type = typeof fd.node.data.content;
switch (type) { switch (type) {
case 'string': // text file case 'string': // text file
if (fd.node.data.content.substring(0,2) == '#!') if (fd.node.data.content.substring(0,2) == '#!')
return 7; return clite.io.types.FT_SCRIPT;
return 1; return clite.io.types.FT_TEXT;
break; break;
case 'function': // executable case 'function': // executable
return 2; return clite.io.types.FT_BINARY;
break; break;
case 'object': case 'object':
if (Array.isArray(fd.node.data.content)) // directory if (Array.isArray(fd.node.data.content)) // directory
return 3; return clite.io.types.FT_DIR;
if (fd.node.data.content instanceof HTMLImageElement) if (fd.node.data.content instanceof HTMLImageElement)
return 8; return clite.io.types.FT_IMAGE;
case 'symbol': case 'symbol':
case 'boolean': case 'boolean':
case 'number': case 'number':
case 'bigint': case 'bigint':
case 'undefined': case 'undefined':
default: default:
return 0; return clite.io.types.FT_UNKOWN;
} }
return 0; return clite.io.types.FT_UNKOWN;
}, },
getBuffer:function() { getBuffer:function() {
return Object.create({ return Object.create({