fs Fetch file contents

This commit is contained in:
2025-10-02 22:29:20 +02:00
parent 91d648ade4
commit bc2b115cb3
9 changed files with 115 additions and 32 deletions

2
user/fs/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
fs

24
user/fs/Makefile Normal file
View File

@ -0,0 +1,24 @@
include $(ROOT)/mk/grabsrc.mk
include ../Makefile.inc
.PHONY: all clean
TARGET := fs
LDFLAGS += -L$(ROOT)/ulib -l:libulib.a
SRCFILES := $(call GRABSRC, .)
CFILES := $(call GET_CFILES, $(SRCFILES))
OBJ := $(call GET_OBJ, $(SRCFILES))
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
all: $(TARGET)
$(TARGET): $(OBJ)
$(LD) $^ $(LDFLAGS) -o $@
echo $$(realpath $(TARGET)) >> $(FILES)
clean:
rm -f $(OBJ) $(TARGET)

35
user/fs/fetch.c Normal file
View File

@ -0,0 +1,35 @@
#include <stddef.h>
#include <stdint.h>
#include <ulib.h>
void fs_fetch(void) {
if (argslen() < 2) {
uprintf("fs: Not enough arguments\n");
return;
}
char *path = *(args()+1);
IOH ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)path, IOCTL_F_READ, 0);
if (ioh < 0) {
uprintf("fs: could not open %s\n", path);
return;
}
IoctlStat statbuf; ZERO(&statbuf);
ioctl(ioh, IOCTL_STAT, (uint64_t)&statbuf, 0, 0);
if (statbuf.type == IOCTLSTAT_FILE) {
uint8_t *buf = umalloc(statbuf.size+1);
string_memset(buf, 0, statbuf.size+1);
if (ioctl(ioh, IOCTL_READ, (uint64_t)buf, statbuf.size, 0) < 0) {
uprintf("fs: coult not read %s\n", path);
ufree(buf);
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
return;
}
uprintf("%s", buf);
}
}

19
user/fs/main.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdint.h>
#include <stddef.h>
#include <ulib.h>
extern void fs_fetch(void);
void main(void) {
if (argslen() == 0) {
return;
}
char *cmd = args()[0];
if (string_strcmp(cmd, "fetch") == 0) {
fs_fetch();
} else {
uprintf("fs: unknown command %s\n", cmd);
}
}