fs Add tree subcommand
This commit is contained in:
59
user/fs/tree.c
Normal file
59
user/fs/tree.c
Normal file
@ -0,0 +1,59 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <ulib.h>
|
||||
|
||||
int showtree(char *root, int indent) {
|
||||
#define INDENT() for (size_t i = 0; i < indent; i++) uprintf(" ")
|
||||
|
||||
IoctlStat statbuf; ZERO(&statbuf);
|
||||
if (ioctl(IOCTL_NOHANDLE, IOCTL_STAT, (uint64_t)&statbuf, (uint64_t)root, 0) != E_OK) {
|
||||
uprintf("fs: could not stat %s\n", root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *bn = path_basename(root);
|
||||
if (string_len(bn) > 0) {
|
||||
INDENT();
|
||||
uprintf("%s\n", bn);
|
||||
}
|
||||
|
||||
|
||||
if (statbuf.type == IOCTLSTAT_FILE) {
|
||||
return 0;
|
||||
}
|
||||
if (statbuf.type == IOCTLSTAT_DIR) {
|
||||
IoctlDirent *dirents = (IoctlDirent *)umalloc(statbuf.size * sizeof(*dirents));
|
||||
|
||||
for (size_t i = 0; i < statbuf.size; i++) {
|
||||
ioctl(IOCTL_NOHANDLE, IOCTL_FETCHDIRENT, (uint64_t)root, (uint64_t)&dirents[i], i);
|
||||
|
||||
IoctlDirent *dirent = &dirents[i];
|
||||
|
||||
if (string_strcmp(dirent->name, ".") == 0 || string_strcmp(dirent->name, "..") == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
char tmpbuf[1024]; ZERO(tmpbuf);
|
||||
string_strcpy(tmpbuf, root);
|
||||
if (root[string_len(root) - 1] != '/') {
|
||||
string_combine(tmpbuf, "/");
|
||||
}
|
||||
string_combine(tmpbuf, dirent->name);
|
||||
showtree(tmpbuf, indent+1);
|
||||
}
|
||||
|
||||
ufree(dirents);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fs_tree(void) {
|
||||
if (argslen() < 2) {
|
||||
uprintf("fs: Not enough arguments\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *path = *(args()+1);
|
||||
|
||||
showtree(path, 0);
|
||||
}
|
Reference in New Issue
Block a user