fs Add tree subcommand
This commit is contained in:
@ -15,6 +15,7 @@ SRCFILES := $(call GRABSRC, \
|
|||||||
util \
|
util \
|
||||||
ubsan \
|
ubsan \
|
||||||
umalloc \
|
umalloc \
|
||||||
|
fs \
|
||||||
)
|
)
|
||||||
|
|
||||||
CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \
|
CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \
|
||||||
|
60
ulib/fs/path.c
Normal file
60
ulib/fs/path.c
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include <fs/path.h>
|
||||||
|
#include <string/string.h>
|
||||||
|
|
||||||
|
void path_parse(const char *in, char *mp, char *path) {
|
||||||
|
if (in == 0 || *in == 0) {
|
||||||
|
mp[0] = 0;
|
||||||
|
path[0] = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0, j = 0, colonfound = 0;
|
||||||
|
|
||||||
|
while (in[i]) {
|
||||||
|
if (in[i] == ':') {
|
||||||
|
if (colonfound) {
|
||||||
|
mp[0] = 0;
|
||||||
|
path[0] = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
colonfound = 1;
|
||||||
|
mp[i] = 0;
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!colonfound) {
|
||||||
|
mp[i] = in[i];
|
||||||
|
} else {
|
||||||
|
path[j++] = in[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!colonfound) {
|
||||||
|
mp[i] = 0;
|
||||||
|
path[0] = 0;
|
||||||
|
} else {
|
||||||
|
path[j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *path_basename(const char *path) {
|
||||||
|
if (!path) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *aftercolon = string_strchr(path, ':');
|
||||||
|
if (aftercolon) {
|
||||||
|
path = aftercolon + 1;
|
||||||
|
}
|
||||||
|
const char *lastslash = NULL;
|
||||||
|
for (const char *p = path; *p; p++) {
|
||||||
|
if (*p == '/') {
|
||||||
|
lastslash = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lastslash ? lastslash + 1 : path;
|
||||||
|
}
|
||||||
|
|
7
ulib/fs/path.h
Normal file
7
ulib/fs/path.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef ULIB_FS_PATH_H_
|
||||||
|
#define ULIB_FS_PATH_H_
|
||||||
|
|
||||||
|
void path_parse(const char *in, char *mp, char *path);
|
||||||
|
const char *path_basename(const char *path);
|
||||||
|
|
||||||
|
#endif // ULIB_FS_PATH_H_
|
@ -172,3 +172,14 @@ char *string_tokenizealloc(char *s, char *delim) {
|
|||||||
|
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://stackoverflow.com/questions/2488563/strcat-implementation
|
||||||
|
char *string_combine(char *dest, const char *src) {
|
||||||
|
size_t i, j;
|
||||||
|
for(i = 0; dest[i] != '\0'; i++);
|
||||||
|
for(j = 0; src[j] != '\0'; j++) {
|
||||||
|
dest[i+j] = src[j];
|
||||||
|
}
|
||||||
|
dest[i+j] = '\0';
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
@ -17,6 +17,7 @@ char *string_strcpy(char *dest, const char *src);
|
|||||||
char *string_strchr(const char *s, int c);
|
char *string_strchr(const char *s, int c);
|
||||||
int string_strncmp(const char * s1, const char * s2, size_t n);
|
int string_strncmp(const char * s1, const char * s2, size_t n);
|
||||||
char *string_tokenizealloc(char *s, char *delim);
|
char *string_tokenizealloc(char *s, char *delim);
|
||||||
|
char *string_combine(char *dest, const char *src);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <umalloc/umalloc.h>
|
#include <umalloc/umalloc.h>
|
||||||
#include <dev.h>
|
#include <dev.h>
|
||||||
|
#include <fs/path.h>
|
||||||
|
|
||||||
#include <errors.h>
|
#include <errors.h>
|
||||||
#include <sysdefs/ioctl.h>
|
#include <sysdefs/ioctl.h>
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
#include <ulib.h>
|
#include <ulib.h>
|
||||||
|
|
||||||
#define CMDS(X) \
|
#define CMDS(X) \
|
||||||
X(fetch) X(mkf) X(mkd)
|
X(fetch) X(mkf) X(mkd) \
|
||||||
|
X(tree)
|
||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
if (argslen() == 0) {
|
if (argslen() == 0) {
|
||||||
|
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