add stdio.mkdir() and mkdir command

This commit is contained in:
Lisa Milne 2023-12-03 13:16:18 +10:00
parent 14fd1f7d69
commit cc03c96982
3 changed files with 106 additions and 1 deletions

View file

@ -495,7 +495,7 @@ clite.commands.load('rm',function(args,env,io) {
function help() {
stdio.printf(`
rm - remove files or directories
Usage: file [OPTION] <FILE>
Usage: rm [OPTION] <FILE>
Options:
-r Remove directories and their contents recusively
@ -588,6 +588,86 @@ Options:
return removeFile(file);
});
clite.commands.load('mkdir',function(args,env,io) {
var short = null;
var file = null;
var stdio = io.include('stdio');
var stdlib = io.include('stdlib');
var clite = io.include('clite');
var recurse = false;
function help() {
stdio.printf(`
mkdir - remove files or directories
Usage: mkdir [OPTION] <DIRECTORY>
Options:
-p Make the full path, not just the final directory
-? Print this help information
`);
}
for (var i=1; i<args.length; i++) {
if (args[i][0] == '-') {
for (var j=1; j<args[i].length; j++) {
switch (args[i][j]) {
case 'p':
recurse = true;
break;
case '?':
help();
return 0;
break;
default:
stdio.fprintf(io.stderr,'unknown argument: -%c\n',args[i][j]);
}
}
}else if (file == null) {
short = args[i];
file = clite.resolvePath(args[i],false);
}else{
stdio.fprintf(io.stderr,'unknown argument: %s\n',args[i]);
}
}
if (file == null) {
stdio.fprintf(io.stderr,'no path specified');
return 1;
}
var st = stdio.stat(file);
if (st) {
stdio.fprintf(io.stderr,'already exists: %s\n',short);
return 1;
}
if (!stdio.mkdir(file)) {
if (!recurse) {
stdio.fprintf(io.stderr,'persmission denied: %s\n',short);
return 1;
}
var parts = file.split('/');
var path = '';
for (var i=0; i<parts.length; i++) {
path += '/'+parts[i];
st = stdio.stat(path);
if (st) {
if (st.type != stdio.types.FT_DIR) {
stdio.fprintf(io.stderr,'permission denied: %s',path);
return 1;
}
continue;
}
if (!stdio.mkdir(path)) {
stdio.fprintf(io.stderr,'permission denied: %s',path);
return 1;
}
}
}
return 0;
});
clite.commands.load('file',function(args,env,io) {
var short = '';
var file = null;
@ -683,6 +763,7 @@ clite.commands.load('reboot',function(args,env,io) {
function help() {
stdio.printf(`
reboot - reboot the system, forcing reload of all code
Usage: reboot [OPTION]
Options:
-? Print this help information
@ -727,6 +808,7 @@ clite.commands.load('uname',function(args,env,io) {
function help() {
stdio.printf(`
uname - print system information
Usage: uname [OPTION]
Options:
-a All, equivalent to -mnrsv

View file

@ -1184,6 +1184,24 @@ clite.io = {
clite.io.isatty = function(pid,fd) {
return fd.node.data.istty;
}
clite.io.mkdir = function(pid,path,mode) {
if (typeof mode !== 'string' || mode.length < 9 || mode.length > 10)
mode = 'drwxr-xr-x';
if (mode.length == 9)
mode = 'd'+mode;
if (mode[0] != 'd')
mode = 'd'+mode.substring(0,9);
var uid = clite.proc.getUID(pid);
var r = vfsapi.mkDir(uid,path);
if (!r)
return false;
var n = vfsapi.getNode(uid,path);
if (!n)
return false;
n.perms = mode;
return true;
}
clite.io.vfprintf = function(pid,fd,fmt,args) {
var str = '';
var parts = fmt.replace(/%%/g,'&percnt;').split('%');
@ -1576,6 +1594,7 @@ clite.io = {
chmod:null,
fchmod:null,
isatty:null,
mkdir:null,
vfprintf:null
};

View file

@ -74,6 +74,10 @@ return Object.create({
return clite.io.isatty(io.pid,fd);
},
mkdir:function(path,mode) {
return clite.io.mkdir(io.pid,path,mode);
},
vfprintf:function(fd,fmt,args) {
return clite.io.vfprintf(io.pid,fd,fmt,args);
},