add rm command, fix bug in vfs.remove()

This commit is contained in:
Lisa Milne 2023-12-03 12:18:24 +10:00
parent 87d2a7ebc6
commit 14fd1f7d69
2 changed files with 105 additions and 6 deletions

View file

@ -485,9 +485,107 @@ clite.commands.load('view',function(args,env,io) {
return 0;
});
clite.commands.load('edit',function(args,env,io) {
stdio.printf('edit is under development');
return 0;
clite.commands.load('rm',function(args,env,io) {
var short = null;
var file = null;
var stdio = io.include('stdio');
var clite = io.include('clite');
var recurse = false;
var force = false;
function help() {
stdio.printf(`
rm - remove files or directories
Usage: file [OPTION] <FILE>
Options:
-r Remove directories and their contents recusively
-f Ignore nonexistant files and arguments
-? 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 'r':
recurse = true;
break;
case 'f':
force = 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 file specified');
return 1;
}
function removeFile(path) {
var st = stdio.stat(path);
if (!st) {
if (force)
return 0;
stdio.fprintf(io.stderr,'no such file or directory: %s',path);
return 1;
}
var fd = stdio.open(path,false,true);
if (!fd) {
stdio.fprintf(io.stderr,'permission denied: %s',path);
return 1;
}
if (st.type == stdio.types.FT_DIR) {
if (!recurse) {
var i = stdio.read(fd);
if (i) {
stdio.close(fd);
stdio.fprintf(io.stderr,'cannot remove non-empty directory: %s',path);
return 1;
}
}else{
var f;
while ((f = stdio.read(fd)) != null) {
if (removeFile(path+'/'+f)) {
stdio.close(fd);
return 1;
}
}
}
}
stdio.close(fd);
if (st.type == stdio.types.FT_DEV) {
stdio.fprintf(io.stderr,'cannot remove device files: %s',path);
return 1;
}
if (!stdio.remove(path)) {
stdio.fprintf(io.stderr,'permission denied: %s',path);
return 1;
}
return 0;
}
return removeFile(file);
});
clite.commands.load('file',function(args,env,io) {
@ -498,8 +596,8 @@ clite.commands.load('file',function(args,env,io) {
var clite = io.include('clite');
function help() {
stdio.printf(`
file - determine type of FILE
Usage: touch [OPTION] [FILE]
file - determine the file type
Usage: file [OPTION] <FILE>
Options:
-? Print this help information

View file

@ -1116,7 +1116,8 @@ clite.io = {
var fd = getFileDes(pid,path,true,null);
if (!fd || !fd.canwrite || fd.node.data.isdev)
return false;
return vfsapi.remove(path);
var uid = clite.proc.getUID(pid);
return vfsapi.remove(uid,path);
}
clite.io.link = function(pid,path,target) {
if (!getFileDes(pid,target,false,null))