1
0
mirror of https://github.com/falk-werner/webfused synced 2026-03-02 04:09:19 +00:00

parse filesystem info

This commit is contained in:
Falk Werner
2020-03-17 21:51:04 +01:00
parent 16c5db6b2c
commit b62e9fc67b
12 changed files with 546 additions and 5 deletions

View File

@@ -48,4 +48,13 @@ wfd_config_builder_add_auth_provider(
return builder.vtable->add_auth_provider(builder.data, settings);
}
bool
wfd_config_builder_add_filesystem(
struct wfd_config_builder builder,
char const * name,
char const * mount_point)
{
return builder.vtable->add_filesystem(builder.data, name, mount_point);
}

View File

@@ -42,6 +42,12 @@ wfd_config_builder_add_auth_provider_fn(
void * data,
struct wfd_auth_settings * settings);
typedef bool
wfd_config_builder_add_filesystem_fn(
void * data,
char const * name,
char const * mount_point);
struct wfd_config_builder_vtable
{
wfd_config_builder_set_server_vhostname_fn * set_server_vhostname;
@@ -50,6 +56,7 @@ struct wfd_config_builder_vtable
wfd_config_builder_set_server_cert_fn * set_server_cert;
wfd_config_builder_set_server_document_root_fn * set_server_document_root;
wfd_config_builder_add_auth_provider_fn * add_auth_provider;
wfd_config_builder_add_filesystem_fn * add_filesystem;
};
struct wfd_config_builder
@@ -88,6 +95,11 @@ wfd_config_builder_add_auth_provider(
struct wfd_config_builder builder,
struct wfd_auth_settings * settings);
extern bool
wfd_config_builder_add_filesystem(
struct wfd_config_builder builder,
char const * name,
char const * mount_point);
#ifdef __cplusplus
}

View File

@@ -2,6 +2,7 @@
#include "webfuse/adapter/server_config.h"
#include "webfused/auth/factory.h"
#include "webfused/auth/authenticator.h"
#include "webfused/mountpoint_factory.h"
#include <stdlib.h>
@@ -13,6 +14,7 @@ struct wfd_config
struct wf_server_config * server;
bool has_authenticator;
struct wfd_authenticator authenticator;
struct wfd_mountpoint_factory * mountpoint_factory;
};
static void
@@ -86,6 +88,16 @@ wfd_config_add_auth_provider(
return result;
}
static bool
wfd_config_add_filesystem(
void * data,
char const * name,
char const * mount_point)
{
struct wfd_config * config = data;
return wfd_mountpoint_factory_add_filesystem(
config->mountpoint_factory, name, mount_point);
}
static const struct wfd_config_builder_vtable
wfd_config_vtable_config_builder =
@@ -95,7 +107,8 @@ wfd_config_vtable_config_builder =
.set_server_key = &wfd_config_set_server_key,
.set_server_cert = &wfd_config_set_server_cert,
.set_server_document_root = &wfd_config_set_server_document_root,
.add_auth_provider = &wfd_config_add_auth_provider
.add_auth_provider = &wfd_config_add_auth_provider,
.add_filesystem = &wfd_config_add_filesystem
};
struct wfd_config *
@@ -103,11 +116,14 @@ wfd_config_create(void)
{
struct wfd_config * config = malloc(sizeof(struct wfd_config));
config->mountpoint_factory = wfd_mountpoint_factory_create();
config->has_authenticator = false;
config->server = wf_server_config_create();
wf_server_config_set_vhostname(config->server, WFD_CONFIG_DEFAULT_VHOSTNAME);
wf_server_config_set_port(config->server, WFD_CONFIG_DEFAULT_PORT);
config->has_authenticator = false;
wf_server_config_set_mountpoint_factory(config->server,
wfd_mountpoint_factory_create_mountpoint,
config->mountpoint_factory);
return config;
}
@@ -121,6 +137,7 @@ wfd_config_dispose(
{
wfd_authenticator_dispose(config->authenticator);
}
wfd_mountpoint_factory_dispose(config->mountpoint_factory);
free(config);
}

View File

@@ -143,6 +143,55 @@ wfd_config_read_authentication(
return result;
}
static bool
wfd_config_read_filesystems(
config_t * config,
struct wfd_config_builder builder)
{
bool result = true;
config_setting_t * filesystems = config_lookup(config, "filesystems");
if (NULL != filesystems)
{
int length = config_setting_length(filesystems);
for (int i = 0; i < length; i++)
{
config_setting_t * fs = config_setting_get_elem(filesystems, i);
if (NULL == fs)
{
WFD_ERROR("failed to load config: invalid filesystem section");
result = false;
break;
}
char const * name;
int rc = config_setting_lookup_string(fs, "name", &name);
if (rc != CONFIG_TRUE)
{
WFD_ERROR("failed to load config: missing required filesystem property \'name\'");
result = false;
break;
}
char const * mount_point;
rc = config_setting_lookup_string(fs, "mount_point", &mount_point);
if (rc != CONFIG_TRUE)
{
WFD_ERROR("failed to load config: missing required filesystem property \'mount_point\'");
result = false;
break;
}
result = wfd_config_builder_add_filesystem(builder, name, mount_point);
if (!result)
{
break;
}
}
}
return result;
}
static bool
wfd_config_load(
struct wfd_config_builder builder,
@@ -152,6 +201,7 @@ wfd_config_load(
bool result = wfd_config_check_version(config)
&& wfd_config_read_server(config, builder)
&& wfd_config_read_authentication(config, builder)
&& wfd_config_read_filesystems(config, builder)
;
return result;

View File

@@ -0,0 +1,145 @@
#include "webfused/mountpoint_factory.h"
#include "webfused/log/log.h"
#include <webfuse/adapter/mountpoint.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#define WFD_FILESYSTEM_DEFAULT_CAPACITY 16
struct wfd_filesystem
{
char * name;
char * mount_point;
bool in_use;
};
struct wfd_mountpoint_factory
{
struct wfd_filesystem * filesystems;
size_t capacity;
size_t count;
};
static struct wfd_filesystem *
wfd_mountpoint_factory_find(
struct wfd_mountpoint_factory * factory,
char const * name)
{
for (size_t i = 0; i < factory->count; i++)
{
struct wfd_filesystem * filesystem = &(factory->filesystems[i]);
if (0 == strcmp(name, filesystem->name))
{
return filesystem;
}
}
return NULL;
}
static void
wfd_mountpoint_factory_release_mountpoint(
void * user_data)
{
bool * in_use = user_data;
*in_use = false;
}
struct wfd_mountpoint_factory *
wfd_mountpoint_factory_create(void)
{
struct wfd_mountpoint_factory * factory = malloc(sizeof(struct wfd_mountpoint_factory));
factory->filesystems = malloc(sizeof(struct wfd_filesystem) * WFD_FILESYSTEM_DEFAULT_CAPACITY);
factory->count = 0;
factory->capacity = WFD_FILESYSTEM_DEFAULT_CAPACITY;
return factory;
}
void
wfd_mountpoint_factory_dispose(
struct wfd_mountpoint_factory * factory)
{
for(size_t i = 0; i < factory->count; i++)
{
struct wfd_filesystem * filesystem = &(factory->filesystems[i]);
free(filesystem->name);
free(filesystem->mount_point);
}
free(factory->filesystems);
free(factory);
}
bool
wfd_mountpoint_factory_add_filesystem(
struct wfd_mountpoint_factory * factory,
char const * name,
char const * mount_point)
{
bool result = (NULL == wfd_mountpoint_factory_find(factory, name));
if (!result)
{
WFD_ERROR("mount_point already defined: \'%s\'", mount_point);
}
char * path = NULL;
if (result)
{
mkdir(mount_point, 0755);
path = realpath(mount_point, NULL);
if (NULL == path)
{
WFD_ERROR("invalid mount_point: \'%s\'", mount_point);
result = false;
}
}
if (result)
{
if (factory->count >= factory->capacity)
{
factory->capacity *= 2;
factory->filesystems = realloc(factory->filesystems,
sizeof(struct wfd_filesystem) * factory->capacity);
}
struct wfd_filesystem * actual = &(factory->filesystems[factory->count]);
actual->name = strdup(name);
actual->mount_point = path;
actual->in_use = false;
factory->count++;
}
return result;
}
extern struct wf_mountpoint *
wfd_mountpoint_factory_create_mountpoint(
char const * filesystem,
void * user_data)
{
struct wfd_mountpoint_factory * factory = user_data;
struct wfd_filesystem * fs = wfd_mountpoint_factory_find(factory, filesystem);
if (NULL == fs)
{
WFD_INFO("failed to create mountpoint: filesystem \'%s\' not found", filesystem);
return NULL;
}
if (fs->in_use)
{
WFD_INFO("failed to create mountpoint: filesystem \'%s\' already in use", filesystem);
return NULL;
}
fs->in_use = true;
struct wf_mountpoint * result = wf_mountpoint_create(fs->mount_point);
wf_mountpoint_set_userdata(result,
&fs->in_use, &wfd_mountpoint_factory_release_mountpoint);
return result;
}

View File

@@ -0,0 +1,39 @@
#ifndef WFD_MOUNTPOINT_FACTORY_H
#define WFD_MOUNTPOINT_FACTORY_H
#include "webfuse/adapter/mountpoint_factory.h"
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfd_mountpoint_factory;
extern struct wfd_mountpoint_factory *
wfd_mountpoint_factory_create(void);
extern void
wfd_mountpoint_factory_dispose(
struct wfd_mountpoint_factory * factory);
extern bool
wfd_mountpoint_factory_add_filesystem(
struct wfd_mountpoint_factory * factory,
char const * name,
char const * mount_point);
extern struct wf_mountpoint *
wfd_mountpoint_factory_create_mountpoint(
char const * filesystem,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif