umka/kofuse.c

96 lines
2.4 KiB
C
Raw Normal View History

#define FUSE_USE_VERSION 31
2017-10-18 02:08:32 +02:00
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
2017-11-02 21:41:11 +01:00
#include <stdlib.h>
#include <assert.h>
#include "kocdecl.h"
2017-10-18 02:08:32 +02:00
2017-11-02 21:41:11 +01:00
static void bdfe_to_stat(struct bdfe *kf, struct stat *st) {
if (kf->attr & KF_FOLDER) {
st->st_mode = S_IFDIR | 0755;
st->st_nlink = 2;
} else {
2017-11-16 02:16:20 +01:00
st->st_mode = S_IFREG | 0644;
2017-11-02 21:41:11 +01:00
st->st_nlink = 1;
st->st_size = kf->size;
}
}
static void *hello_init(struct fuse_conn_info *conn,
2017-11-02 21:41:11 +01:00
struct fuse_config *cfg) {
(void) conn;
cfg->kernel_cache = 1;
return NULL;
}
2017-10-18 02:08:32 +02:00
static int hello_getattr(const char *path, struct stat *stbuf,
2017-11-02 21:41:11 +01:00
struct fuse_file_info *fi) {
(void) fi;
int res = 0;
2017-11-16 02:16:20 +01:00
struct bdfe *kf = kos_fuse_getattr(path);
bdfe_to_stat(kf, stbuf);
// res = -ENOENT;
2017-11-02 21:41:11 +01:00
return res;
2017-10-18 02:08:32 +02:00
}
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
2017-11-02 21:41:11 +01:00
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags) {
(void) offset;
(void) fi;
(void) flags;
void *header = kos_fuse_readdir(path, offset);
uint32_t i = *(uint32_t*)(header + 4);
struct bdfe *kf = header + 0x20;
2017-10-18 02:08:32 +02:00
for(; i>0; i--) {
2017-11-02 21:41:11 +01:00
filler(buf, kf->name, NULL, 0, 0);
kf++;
2017-10-18 02:08:32 +02:00
}
2017-11-02 21:41:11 +01:00
return 0;
2017-10-18 02:08:32 +02:00
}
2017-11-02 21:41:11 +01:00
static int hello_open(const char *path, struct fuse_file_info *fi) {
2017-11-16 02:16:20 +01:00
// if (strcmp(path+1, "blah") != 0)
// return -ENOENT;
2017-10-18 02:08:32 +02:00
2017-11-02 21:41:11 +01:00
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES;
2017-10-18 02:08:32 +02:00
2017-11-02 21:41:11 +01:00
return 0;
2017-10-18 02:08:32 +02:00
}
static int hello_read(const char *path, char *buf, size_t size, off_t offset,
2017-11-02 21:41:11 +01:00
struct fuse_file_info *fi) {
(void) fi;
2017-11-16 02:16:20 +01:00
kos_fuse_read(path, buf, size, offset);
2017-11-02 21:41:11 +01:00
return size;
2017-10-18 02:08:32 +02:00
}
static struct fuse_operations hello_oper = {
2017-11-02 21:41:11 +01:00
.init = hello_init,
.getattr = hello_getattr,
.readdir = hello_readdir,
.open = hello_open,
.read = hello_read,
2017-10-18 02:08:32 +02:00
};
2017-11-02 21:41:11 +01:00
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("usage: kofuse dir img\n");
exit(1);
}
2017-10-18 02:08:32 +02:00
int fd = open(argv[2], O_RDONLY);
kos_fuse_init(fd);
2017-11-02 21:41:11 +01:00
return fuse_main(argc-1, argv, &hello_oper, NULL);
2017-10-18 02:08:32 +02:00
}