#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <fuse.h>
static const char *file_path = "/hello.txt";
static const char file_content[] = "Hello World!\n";
static const size_t file_size = sizeof(file_content)/sizeof(char) - 1;
static int
hello_getattr(const char *path, struct stat *stbuf)
{
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 3;
} else if (strcmp(path, file_path) == 0) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = file_size;
} else
return -ENOENT;
return 0;
}
static int
hello_open(const char *path, struct fuse_file_info *fi)
{
if (strcmp(path, file_path) != 0)
return -ENOENT;
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES;
return 0;
}
static int
hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, file_path + 1, NULL, 0);
return 0;
}
static int
hello_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
if (strcmp(path, file_path) != 0)
return -ENOENT;
if (offset >= file_size)
return 0;
if (offset + size > file_size)
size = file_size - offset;
memcpy(buf, file_content + offset, size);
return size;
}
static struct fuse_operations hello_filesystem_operations = {
.getattr = hello_getattr,
.open = hello_open,
.read = hello_read,
.readdir = hello_readdir,
};
int
main(int argc, char **argv)
{
return fuse_main(argc, argv, &hello_filesystem_operations, NULL);
}