Files
umka/kofuse.c
T

110 lines
2.2 KiB
C
Raw Normal View History

2017-10-18 23:19:53 +03:00
#define FUSE_USE_VERSION 31
2017-10-18 03:08:32 +03:00
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
2017-10-18 23:19:53 +03:00
#include <assert.h>
#include "kocdecl.h"
2017-10-18 03:08:32 +03:00
2017-10-18 23:19:53 +03:00
static void *hello_init(struct fuse_conn_info *conn,
struct fuse_config *cfg)
2017-10-18 03:08:32 +03:00
{
2017-10-18 23:19:53 +03:00
(void) conn;
cfg->kernel_cache = 1;
return NULL;
}
static int hello_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
(void) fi;
2017-10-18 03:08:32 +03:00
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
2017-10-18 23:19:53 +03:00
} else if (strcmp(path+1, "blah") == 0) {
2017-10-18 03:08:32 +03:00
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
2017-10-18 23:19:53 +03:00
stbuf->st_size = strlen("blah");
2017-10-18 03:08:32 +03:00
} else
res = -ENOENT;
return res;
}
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
2017-10-18 23:19:53 +03:00
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags)
2017-10-18 03:08:32 +03:00
{
(void) offset;
(void) fi;
2017-10-18 23:19:53 +03:00
(void) flags;
2017-10-18 03:08:32 +03:00
char *bdfe = kos_fuse_readdir(path, offset);
// if (strcmp(path, "/") != 0)
// return -ENOENT;
uint32_t i = *(uint32_t*)(bdfe + 4);
// int f = open("/tmp/t", O_RDWR | O_CREAT);
// write(f, bdfe, 1000);
// write(f, &i, 4);
// close(f);
bdfe += 0x20;
for(; i>0; i--) {
2017-10-18 23:19:53 +03:00
filler(buf, bdfe + 0x28, NULL, 0, 0);
2017-10-18 03:08:32 +03:00
bdfe += 304;
}
return 0;
}
static int hello_open(const char *path, struct fuse_file_info *fi)
{
2017-10-18 23:19:53 +03:00
if (strcmp(path+1, "blah") != 0)
2017-10-18 03:08:32 +03:00
return -ENOENT;
2017-10-18 23:19:53 +03:00
if ((fi->flags & O_ACCMODE) != O_RDONLY)
2017-10-18 03:08:32 +03:00
return -EACCES;
return 0;
}
static int hello_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
(void) fi;
2017-10-18 23:19:53 +03:00
if(strcmp(path+1, "blah") != 0)
2017-10-18 03:08:32 +03:00
return -ENOENT;
2017-10-18 23:19:53 +03:00
len = strlen("blah");
2017-10-18 03:08:32 +03:00
if (offset < len) {
if (offset + size > len)
size = len - offset;
2017-10-18 23:19:53 +03:00
memcpy(buf, "blah" + offset, size);
2017-10-18 03:08:32 +03:00
} else
size = 0;
return size;
}
static struct fuse_operations hello_oper = {
2017-10-18 23:19:53 +03:00
.init = hello_init,
2017-10-18 03:08:32 +03:00
.getattr = hello_getattr,
.readdir = hello_readdir,
.open = hello_open,
.read = hello_read,
};
int main(int argc, char *argv[])
{
int fd = open(argv[2], O_RDONLY);
kos_fuse_init(fd);
return fuse_main(argc-1, argv, &hello_oper, NULL);
}