diff options
author | Marvin Borner | 2021-12-31 16:37:33 +0100 |
---|---|---|
committer | Marvin Borner | 2021-12-31 16:40:54 +0100 |
commit | 96797653950948165863d9dd016c82af7468636f (patch) | |
tree | 722b6006fb32e6c5c0fe516e289b94d6c4a2d5a7 | |
parent | 028755c57007c3241e287b9927c6810814eecde1 (diff) |
FUSE!!
-rw-r--r-- | makefile | 31 | ||||
-rw-r--r-- | src/fuse.c | 166 |
2 files changed, 191 insertions, 6 deletions
@@ -7,13 +7,15 @@ SU = sudo TG = ctags BUILD = $(PWD)/build +MNT = $(PWD)/mnt SRC = ./src INC = ./inc SRCS = $(shell find $(SRC) -type f -name '*.c') OBJS = $(SRCS:%=$(BUILD)/%.o) -CFLAGS_WARNINGS = -Wall -Wextra -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors -CFLAGS = $(CFLAGS_WARNINGS) -std=c99 -fno-profile-generate -fno-omit-frame-pointer -fno-common -fno-asynchronous-unwind-tables -mno-red-zone -Ofast -I$(INC) +CFLAGS_DEBUG = -Wno-error -Wno-unused -fsanitize=address -fsanitize=undefined -fstack-protector-all +CFLAGS_WARNINGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors +CFLAGS = $(CFLAGS_WARNINGS) -std=c11 -fno-profile-generate -fno-omit-frame-pointer -fno-common -fno-asynchronous-unwind-tables -mno-red-zone -Ofast -D_DEFAULT_SOURCE -I$(INC) $(shell pkg-config fuse3 --cflags --libs) $(CFLAGS_DEBUG) all: $(BUILD) $(OBJS) @@ -25,7 +27,7 @@ clean: clean_disk: @rm -rf $(BUILD)/*.img -run: compile $(BUILD) clean_disk $(BUILD)/disk.img +run: mount sync: # Ugly hack @$(MAKE) --always-make --dry-run | grep -wE 'gcc' | jq -nR '[inputs|{directory: ".", command: ., file: match(" [^ ]+$$").string[1:]}]' >compile_commands.json & @@ -34,15 +36,32 @@ sync: # Ugly hack $(BUILD)/%.c.o: %.c @$(CC) $(CFLAGS) $< -o $(patsubst $(BUILD)/$(SRC)/%.c.o,$(BUILD)/marfs_%,$@) -.PHONY: all run compile clean clean_disk sync +.PHONY: all run compile clean clean_disk sync mount umount $(BUILD): @mkdir -p $@ -%.img: $(BUILD) +$(MNT): + @mkdir -p $@ + +mount: disk $(MNT) + @DEV=$$(losetup -a | grep $(BUILD)/disk.img | grep -m 1 -o '/dev/loop[[:digit:]]*') && \ + PART="p1" && \ + $(BUILD)/marfs_fuse "$$DEV$$PART" $(MNT) && \ + echo "Mounted $$DEV$$PART" + +umount: + @DEV=$$(losetup -a | grep $(BUILD)/disk.img | grep -m 1 -o '/dev/loop[[:digit:]]*') && \ + umount $(MNT) && \ + $(SU) losetup -d "$$DEV" && \ + echo "Unmounted $$DEV$$PART" || \ + echo "No disk to unmount" + +disk: $(BUILD)/disk.img +%.img: compile clean_disk $(BUILD) @dd if=/dev/zero of=$@ bs=1k count=32k status=none @DEV=$$($(SU) losetup --find --partscan --show $@) && \ PART="p1" && \ $(SU) parted -s "$$DEV" mklabel msdos mkpart primary 32k 100% -a minimal set 1 boot on && \ $(SU) ./build/marfs_mkfs "$$DEV$$PART" && \ - $(SU) losetup -d "$$DEV" + echo "Created $$DEV$$PART. Please run '$(MAKE) umount' afterwards" diff --git a/src/fuse.c b/src/fuse.c new file mode 100644 index 0000000..262317a --- /dev/null +++ b/src/fuse.c @@ -0,0 +1,166 @@ +/** + * Copyright (c) 2021, Marvin Borner <melvix@marvinborner.de> + * SPDX-License-Identifier: MIT + */ + +#define FUSE_USE_VERSION 30 + +#include <spec.h> + +#include <fuse.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <time.h> + +static char *image = 0; + +static void marfs_debug(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); +} + +static void *marfs_init(struct fuse_conn_info *conn, struct fuse_config *cfg) +{ + UNUSED(conn); + cfg->kernel_cache = 1; + return NULL; +} + +static void marfs_destroy(void *data) +{ + if (image) + free(image); +} + +static int marfs_open(const char *file_path, struct fuse_file_info *file_info) +{ + marfs_debug("opening file %s\n", file_path); + return 0; +} + +static int marfs_opendir(const char *dir_path, struct fuse_file_info *file_info) +{ + marfs_debug("opening dir %s\n", dir_path); + return 0; +} + +static int marfs_getattr(const char *path, struct stat *stat, struct fuse_file_info *info) +{ + marfs_debug("getattr() on %s\n", path); + return 0; +} + +static int marfs_readdir(const char *path, void *buf, fuse_fill_dir_t fill, off_t offset, + struct fuse_file_info *file_info, enum fuse_readdir_flags flags) +{ + marfs_debug("readdir() on %s and offset %lu\n", path, offset); + return 0; +} + +static int marfs_release(const char *path, struct fuse_file_info *file_info) +{ + return 0; +} + +static int marfs_releasedir(const char *path, struct fuse_file_info *file_info) +{ + return 0; +} + +static int marfs_read(const char *path, char *buf, size_t to_read, off_t offset, + struct fuse_file_info *file_info) +{ + marfs_debug("marfs_read() on %s, %lu\n", path, to_read); + return to_read; +} + +static int marfs_write(const char *path, const char *buf, size_t to_write, off_t offset, + struct fuse_file_info *file_info) +{ + marfs_debug("marfs_write() on %s\n", path); + return to_write; +} + +static int marfs_create(const char *path, mode_t mode, struct fuse_file_info *file_info) +{ + marfs_debug("marfs_create() on %s\n", path); + return 0; +} + +static int marfs_mkdir(const char *path, mode_t mode) +{ + marfs_debug("marfs_mkdir() on %s\n", path); + return 0; +} + +static int marfs_unlink(const char *path) +{ + return 0; +} + +static int marfs_rmdir(const char *path) +{ + return 0; +} + +static int marfs_utimens(const char *path, const struct timespec tv[2], + struct fuse_file_info *file_info) +{ + marfs_debug("marfs_utimens() on %s\n", path); + return 0; +} + +static int marfs_truncate(const char *path, off_t size, struct fuse_file_info *file_info) +{ + marfs_debug("marfs_truncate() on %s, size %lu\n", path, size); + return 0; +} + +static int marfs_rename(const char *path, const char *new, unsigned int flags) +{ + marfs_debug("marfs_rename() on %s, %s\n", path, new); + return 0; +} + +static struct fuse_operations operations = { + .init = marfs_init, + .destroy = marfs_destroy, + .open = marfs_open, + .opendir = marfs_opendir, + .getattr = marfs_getattr, + .readdir = marfs_readdir, + .release = marfs_release, + .releasedir = marfs_releasedir, + .read = marfs_read, + .write = marfs_write, + .create = marfs_create, + .unlink = marfs_unlink, + .utimens = marfs_utimens, + .truncate = marfs_truncate, + .mkdir = marfs_mkdir, + .rmdir = marfs_rmdir, + .rename = marfs_rename, +}; + +int main(int argc, char **argv) +{ + if (argc < 2) { + fprintf(stderr, "error: no image specified\n"); + exit(1); + } + + // I don't think that's how fuse is supposed to work but idc + image = strdup(argv[1]); + argv++; + argc--; + + fuse_main(argc, argv, &operations, NULL); + + return 0; +} |