fs Make fetch subcmd more configurable
This commit is contained in:
@ -2,6 +2,29 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <ulib.h>
|
#include <ulib.h>
|
||||||
|
|
||||||
|
struct {
|
||||||
|
bool plain;
|
||||||
|
bool names;
|
||||||
|
bool sizes;
|
||||||
|
bool types;
|
||||||
|
bool humsz;
|
||||||
|
} FS_FETCH_CONFIG = {
|
||||||
|
.plain = false,
|
||||||
|
.names = true,
|
||||||
|
.sizes = true,
|
||||||
|
.types = true,
|
||||||
|
.humsz = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
static Arg ARGS[] = {
|
||||||
|
ARG("-p", ARG_BOOL, &FS_FETCH_CONFIG.plain),
|
||||||
|
ARG("-n", ARG_BOOL, &FS_FETCH_CONFIG.names),
|
||||||
|
ARG("-s", ARG_BOOL, &FS_FETCH_CONFIG.sizes),
|
||||||
|
ARG("-t", ARG_BOOL, &FS_FETCH_CONFIG.types),
|
||||||
|
ARG("-h", ARG_BOOL, &FS_FETCH_CONFIG.humsz),
|
||||||
|
ARG_END(),
|
||||||
|
};
|
||||||
|
|
||||||
void fs_fetch(void) {
|
void fs_fetch(void) {
|
||||||
if (argslen() < 2) {
|
if (argslen() < 2) {
|
||||||
uprintf("fs: Not enough arguments\n");
|
uprintf("fs: Not enough arguments\n");
|
||||||
@ -10,6 +33,17 @@ void fs_fetch(void) {
|
|||||||
|
|
||||||
char *path = *(args()+1);
|
char *path = *(args()+1);
|
||||||
|
|
||||||
|
int32_t ret;
|
||||||
|
if ((ret = parse_args(args()+2, argslen()-2, ARGS)) < 0) {
|
||||||
|
uprintf("fs: could not parse args: %d\n", ret);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (FS_FETCH_CONFIG.plain) {
|
||||||
|
FS_FETCH_CONFIG.sizes = false;
|
||||||
|
FS_FETCH_CONFIG.types = false;
|
||||||
|
}
|
||||||
|
|
||||||
IoctlStat statbuf; ZERO(&statbuf);
|
IoctlStat statbuf; ZERO(&statbuf);
|
||||||
if (ioctl(IOCTL_NOHANDLE, IOCTL_STAT, (uint64_t)&statbuf, (uint64_t)path, 0) != E_OK) {
|
if (ioctl(IOCTL_NOHANDLE, IOCTL_STAT, (uint64_t)&statbuf, (uint64_t)path, 0) != E_OK) {
|
||||||
uprintf("fs: could not stat %s\n", path);
|
uprintf("fs: could not stat %s\n", path);
|
||||||
@ -43,23 +77,52 @@ void fs_fetch(void) {
|
|||||||
ioctl(IOCTL_NOHANDLE, IOCTL_FETCHDIRENT, (uint64_t)path, (uint64_t)&dirents[i], i);
|
ioctl(IOCTL_NOHANDLE, IOCTL_FETCHDIRENT, (uint64_t)path, (uint64_t)&dirents[i], i);
|
||||||
|
|
||||||
IoctlDirent *dirent = &dirents[i];
|
IoctlDirent *dirent = &dirents[i];
|
||||||
|
|
||||||
|
if (FS_FETCH_CONFIG.names) {
|
||||||
if (dirent->stat.type == IOCTLSTAT_FILE) {
|
if (FS_FETCH_CONFIG.plain) {
|
||||||
char *membuf = umalloc(20);
|
uprintf("%s ", dirent->name);
|
||||||
uprintf("%-30s %-15s %-1s\n",
|
} else {
|
||||||
dirent->name,
|
uprintf("%-30s ", dirent->name);
|
||||||
human_size(dirent->stat.size, membuf, 20),
|
}
|
||||||
"F"
|
|
||||||
);
|
|
||||||
ufree(membuf);
|
|
||||||
} else if (dirent->stat.type == IOCTLSTAT_DIR) {
|
|
||||||
uprintf("%-30s %-15s %-1s\n",
|
|
||||||
dirent->name,
|
|
||||||
"-",
|
|
||||||
"D"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (FS_FETCH_CONFIG.sizes) {
|
||||||
|
if (dirent->stat.type == IOCTLSTAT_FILE) {
|
||||||
|
if (FS_FETCH_CONFIG.humsz) {
|
||||||
|
char *membuf = umalloc(20);
|
||||||
|
|
||||||
|
if (FS_FETCH_CONFIG.plain) {
|
||||||
|
uprintf("%s ", human_size(dirent->stat.size, membuf, 20));
|
||||||
|
} else {
|
||||||
|
uprintf("%-15s ", human_size(dirent->stat.size, membuf, 20));
|
||||||
|
}
|
||||||
|
|
||||||
|
ufree(membuf);
|
||||||
|
} else {
|
||||||
|
if (FS_FETCH_CONFIG.plain) {
|
||||||
|
uprintf("%zu ", dirent->stat.size);
|
||||||
|
} else {
|
||||||
|
uprintf("%-15zu ", dirent->stat.size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (dirent->stat.type == IOCTLSTAT_DIR) {
|
||||||
|
if (FS_FETCH_CONFIG.plain) {
|
||||||
|
uprintf("- ");
|
||||||
|
} else {
|
||||||
|
uprintf("%-15c ", '-');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (FS_FETCH_CONFIG.types) {
|
||||||
|
if (dirent->stat.type == IOCTLSTAT_FILE) {
|
||||||
|
uprintf("%c ", 'F');
|
||||||
|
} else if (dirent->stat.type == IOCTLSTAT_DIR) {
|
||||||
|
uprintf("%c ", 'D');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uprintf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
ufree(dirents);
|
ufree(dirents);
|
||||||
|
|||||||
Reference in New Issue
Block a user