mirror of
https://codeberg.org/TicklishHoneyBee/CLIte.git
synced 2026-03-11 09:04:37 +00:00
add chown command
This commit is contained in:
parent
09579c26c5
commit
2983f4a1dd
2 changed files with 135 additions and 1 deletions
|
|
@ -2163,6 +2163,137 @@ Options:
|
|||
return main(args);
|
||||
});
|
||||
|
||||
clite.commands.load('chown',function(args,env,io) {
|
||||
var stdlib = io.include('stdlib');
|
||||
var stdio = io.include('stdio');
|
||||
var clite = io.include('clite');
|
||||
|
||||
let user = null;
|
||||
let group = null;
|
||||
|
||||
let uid = -1;
|
||||
let gid = -1;
|
||||
|
||||
function help() {
|
||||
stdio.printf(`
|
||||
chown - change the owner of files
|
||||
Usage: chown [OPTION] <USER>[:GROUP] <FILE>
|
||||
|
||||
Options:
|
||||
-? Print this help information
|
||||
`);
|
||||
}
|
||||
|
||||
function recurseDir(path) {
|
||||
let fd = stdio.open(path,stdio.flags.O_SEARCH|stdio.flags.O_DIRECTORY|stdio.flags.O_SYNC);
|
||||
if (!fd) {
|
||||
stdio.fprintf(io.stderr,'could not recurse into directory: %s\n',path);
|
||||
return;
|
||||
}
|
||||
let e = null;
|
||||
while ((e = stdio.read(fd)) != null) {
|
||||
let p = path+'/'+e;
|
||||
let r = stdio.chown(p,uid,gid);
|
||||
if (!r)
|
||||
stdio.fprintf(io.stderr,'could not change ownership of file: %s\n',path);
|
||||
}
|
||||
stdio.close(fd);
|
||||
}
|
||||
|
||||
function main(args) {
|
||||
let files = [];
|
||||
let recurse = false;
|
||||
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 '?':
|
||||
help();
|
||||
return 0;
|
||||
break;
|
||||
case 'R':
|
||||
recurse = true;
|
||||
break;
|
||||
default:
|
||||
stdio.fprintf(io.stderr,'unknown argument: -%c\n',args[i][j]);
|
||||
}
|
||||
}
|
||||
}else if (user == null) {
|
||||
user = args[i];
|
||||
}else{
|
||||
files.push(args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (files.length < 1) {
|
||||
stdio.write(io.stderr,'no file specified\n');
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
stdio.write(io.stderr,'no user specified\n');
|
||||
return 1;
|
||||
}
|
||||
|
||||
let ug = user.split(':');
|
||||
if (ug.length > 1) {
|
||||
if (ug.length > 2) {
|
||||
stdio.write(io.stderr,'no valid user specified\n');
|
||||
return 1;
|
||||
}
|
||||
|
||||
user = ug[0];
|
||||
group = ug[1];
|
||||
|
||||
if (user == '')
|
||||
user = null;
|
||||
if (group == '')
|
||||
group = null;
|
||||
}
|
||||
|
||||
if (user) {
|
||||
let pw = stdlib.getpwnam(user);
|
||||
if (!pw) {
|
||||
stdio.write(io.stderr,'invalid user specified\n');
|
||||
return 1;
|
||||
}
|
||||
uid = pw.pw_uid;
|
||||
}
|
||||
|
||||
if (group) {
|
||||
let gr = stdlib.getgrnam(group);
|
||||
if (!gr) {
|
||||
stdio.write(io.stderr,'invalid group specified\n');
|
||||
return 1;
|
||||
}
|
||||
gid = gr.gr_gid;
|
||||
}
|
||||
|
||||
if (uid < 0 && gid < 0) {
|
||||
stdio.write(io.stderr,'no valid user or group specified\n');
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (var i=0; i<files.length; i++) {
|
||||
let p = clite.resolvePath(files[i]);
|
||||
let r = stdio.chown(p,uid,gid);
|
||||
if (!r) {
|
||||
stdio.fprintf(io.stderr,'could not change ownership of file: %s\n',p);
|
||||
continue;
|
||||
}
|
||||
if (!recurse)
|
||||
continue;
|
||||
let st = stdio.stat(p);
|
||||
if (st && st.type == stdio.types.FT_DIR)
|
||||
recurseDir(p);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return main(args);
|
||||
});
|
||||
|
||||
if (window.location.protocol == 'file:')
|
||||
clite.commands.load('exp',function(args,env,io) {
|
||||
var stdio = io.include('stdio');
|
||||
|
|
|
|||
|
|
@ -1756,7 +1756,7 @@ clite.io = {
|
|||
|
||||
fd.node.mode = mode;
|
||||
fd.node.time.change = clite.time.sec();
|
||||
clite.vfs.saveFile(path);
|
||||
clite.vfs.saveFile(fd.path);
|
||||
return true;
|
||||
}
|
||||
clite.io.chmod = function(pid,path,mode) {
|
||||
|
|
@ -2611,6 +2611,7 @@ clite.vfs = {
|
|||
return true;
|
||||
}
|
||||
clite.vfs.restoreFile = function(path) {
|
||||
console.log('restore: '+path);
|
||||
var b = clite.state.bios.io.read(2);
|
||||
if (!b)
|
||||
return false;
|
||||
|
|
@ -3241,6 +3242,8 @@ clite.lib = {
|
|||
},
|
||||
checkNodeExecutable:function(node,pid) {
|
||||
var uid = clite.proc.getUID(pid);
|
||||
if (uid == 0)
|
||||
return true;
|
||||
if ((node.mode&clite.io.modes.S_IXOTH) == clite.io.modes.S_IXOTH)
|
||||
return true;
|
||||
if (clite.user.checkGID(uid,node.gid) && (node.mode&clite.io.modes.S_IXGRP) == clite.io.modes.S_IXGRP)
|
||||
|
|
|
|||
Loading…
Reference in a new issue