1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

reorganized filesystem abstraction: files and directories are almost everywhere handled the same way, so there is no need for separate structs

This commit is contained in:
Falk Werner 2019-03-03 15:29:39 +01:00
parent 58e65c4a31
commit 119d08c42e

View File

@ -6,6 +6,22 @@
#include "wsfs_provider.h" #include "wsfs_provider.h"
enum fs_entry_type
{
FS_FILE,
FS_DIR
};
struct fs_entry
{
ino_t parent;
ino_t inode;
char const * name;
int mode;
enum fs_entry_type type;
size_t content_length;
char const * content;
};
struct fs_dir struct fs_dir
{ {
@ -26,41 +42,25 @@ struct fs_file
struct fs struct fs
{ {
struct fs_dir const * directories; struct fs_entry const * entries;
struct fs_file const * files;
}; };
static struct fs_dir const * fs_getdir( static struct fs_entry const * fs_getentry(
struct fs * fs, struct fs * fs,
ino_t inode) ino_t inode)
{ {
for (size_t i = 0; 0 != fs->directories[i].inode; i++) for (size_t i = 0; 0 != fs->entries[i].inode; i++)
{ {
struct fs_dir const * dir = &fs->directories[i]; struct fs_entry const * entry = &fs->entries[i];
if (inode == dir->inode) if (inode == entry->inode)
{ {
return dir; return entry;
} }
} }
return NULL; return NULL;
} }
static struct fs_file const * fs_getfile(
struct fs * fs,
ino_t inode)
{
for (size_t i = 0; 0 != fs->files[i].inode; i++)
{
struct fs_file const * f = &fs->files[i];
if (inode == f->inode)
{
return f;
}
}
return NULL;
}
static void fs_lookup( static void fs_lookup(
struct wsfsp_request * request, struct wsfsp_request * request,
@ -68,7 +68,14 @@ static void fs_lookup(
char const * name, char const * name,
void * user_data) void * user_data)
{ {
(void) parent; struct fs * fs = (struct fs*) user_data;
struct fs_entry const * dir = fs_getentry(fs, parent);
if ((NULL != dir) && (FS_DIR == dir->type))
{
}
(void) name; (void) name;
(void) user_data; (void) user_data;
@ -83,26 +90,27 @@ static void fs_getattr(
void * user_data) void * user_data)
{ {
struct fs * fs = (struct fs*) user_data; struct fs * fs = (struct fs*) user_data;
struct fs_dir const * dir = fs_getdir(fs, inode); struct fs_entry const * entry = fs_getentry(fs, inode);
struct fs_file const * f = fs_getfile(fs, inode);
if (NULL != dir) if (NULL != entry)
{ {
struct stat stat; struct stat stat;
memset(&stat, 0, sizeof(stat)); memset(&stat, 0, sizeof(stat));
stat.st_ino = inode; stat.st_ino = entry->inode;
stat.st_mode = S_IFDIR | 0555; stat.st_mode = entry->mode;
wsfsp_respond_getattr(request, &stat);
} if (FS_DIR == entry->type)
else if (NULL != f) {
{ stat.st_mode |= S_IFDIR;
struct stat stat; }
memset(&stat, 0, sizeof(stat));
if (FS_FILE == entry->type)
{
stat.st_mode |= S_IFREG;
stat.st_size = entry->content_length;
}
stat.st_ino = inode;
stat.st_mode = S_IFREG | 0555;
stat.st_size = f->content_length;
wsfsp_respond_getattr(request, &stat); wsfsp_respond_getattr(request, &stat);
} }
else else
@ -118,28 +126,19 @@ static void fs_readdir(
{ {
struct fs * fs = (struct fs*) user_data; struct fs * fs = (struct fs*) user_data;
struct fs_dir const * dir = fs_getdir(fs, directory); struct fs_entry const * dir = fs_getentry(fs, directory);
if (NULL != dir) if ((NULL != dir) && (FS_DIR == dir->type))
{ {
struct wsfsp_dirbuffer * buffer = wsfsp_dirbuffer_create(); struct wsfsp_dirbuffer * buffer = wsfsp_dirbuffer_create();
wsfsp_dirbuffer_add(buffer, ".", dir->inode); wsfsp_dirbuffer_add(buffer, ".", dir->inode);
wsfsp_dirbuffer_add(buffer, "..", dir->parent); wsfsp_dirbuffer_add(buffer, "..", dir->parent);
for(size_t i = 0; 0 != fs->directories[i].inode; i++) for(size_t i = 0; 0 != fs->entries[i].inode; i++)
{ {
struct fs_dir const * subdir = &fs->directories[i]; struct fs_entry const * entry = &fs->entries[i];
if (directory == subdir->parent) if (directory == entry->parent)
{ {
wsfsp_dirbuffer_add(buffer, subdir->name, subdir->inode); wsfsp_dirbuffer_add(buffer, entry->name, entry->inode);
}
}
for(size_t i = 0; 0 != fs->files[i].inode; i++)
{
struct fs_file const * f = &fs->files[i];
if (directory == f->parent)
{
wsfsp_dirbuffer_add(buffer, f->name, f->inode);
} }
} }
@ -206,29 +205,24 @@ int main(int argc, char* argv[])
(void) argc; (void) argc;
(void) argv; (void) argv;
static struct fs_dir const directories[]= static struct fs_entry const entries[]=
{
{.parent = 0, .inode = 1, .name = "<root>"},
{.parent = 0, .inode = 0, .name = NULL}
};
static struct fs_file const files[] =
{ {
{.parent = 0, .inode = 1, .name = "<root>", .mode = 0555, .type = FS_DIR},
{ {
.parent = 1, .parent = 1,
.inode = 2, .inode = 2,
.name = "hello.txt", .name = "hello.txt",
.mode = 0555,
.type = FS_FILE,
.content="hello, world!", .content="hello, world!",
.content_length = 13, .content_length = 13,
.is_executable = false
}, },
{.parent = 0, .inode = 0} {.parent = 0, .inode = 0, .name = NULL}
}; };
struct fs fs = struct fs fs =
{ {
.directories = directories, .entries = entries
.files = files
}; };
signal(SIGINT, &on_interrupt); signal(SIGINT, &on_interrupt);