2020-02-20 17:54:29 +00:00
|
|
|
#include "webfuse/utils/static_filesystem.h"
|
2019-04-26 18:49:09 +00:00
|
|
|
#include "webfuse/provider/client_config.h"
|
|
|
|
#include "webfuse/provider/dirbuffer.h"
|
|
|
|
#include "webfuse/provider/operation/error.h"
|
|
|
|
|
2020-02-20 17:54:29 +00:00
|
|
|
#include "webfuse/utils/path.h"
|
2019-04-26 18:49:09 +00:00
|
|
|
#include "webfuse/core/util.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#define WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY (16)
|
2019-05-26 18:46:41 +00:00
|
|
|
#define WFP_STATIC_FILSYSTEM_INODE_ROOT (1)
|
2019-04-26 18:49:09 +00:00
|
|
|
#define WFP_STATIC_FILESYSTEM_MAX_READ_SIZE (4 * 1024)
|
|
|
|
|
|
|
|
struct wfp_static_filesystem_entry
|
|
|
|
{
|
|
|
|
size_t inode;
|
|
|
|
size_t parent;
|
|
|
|
char * name;
|
|
|
|
bool is_file;
|
|
|
|
int mode;
|
|
|
|
size_t size;
|
|
|
|
char * content;
|
|
|
|
wfp_static_filesystem_read_fn * read;
|
|
|
|
wfp_static_filesystem_get_info_fn * get_info;
|
|
|
|
void * user_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct wfp_static_filesystem
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry * entries;
|
|
|
|
size_t size;
|
|
|
|
size_t capacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct wfp_static_filesystem_entry *
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_get_entry(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem * filesystem,
|
|
|
|
size_t inode)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry * entry = NULL;
|
|
|
|
|
|
|
|
if ((0 < inode) && (inode <= filesystem->size))
|
|
|
|
{
|
|
|
|
entry = &filesystem->entries[inode - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wfp_static_filesystem_entry *
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_get_entry_by_name(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem * filesystem,
|
|
|
|
size_t parent,
|
|
|
|
char const * name)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry * entry = NULL;
|
|
|
|
for(size_t i = 0; i < filesystem->size; i++)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry * current = &filesystem->entries[i];
|
|
|
|
if ((parent == current->parent) && (0 == strcmp(name, current->name)))
|
|
|
|
{
|
|
|
|
entry = current;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wfp_static_filesystem_entry *
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_add_entry(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem * filesystem)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry * entry = NULL;
|
|
|
|
|
|
|
|
if (filesystem->size >= filesystem->capacity)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry * entries;
|
|
|
|
|
|
|
|
size_t new_capacity = 2 * filesystem->capacity;
|
|
|
|
size_t new_size = new_capacity * sizeof(struct wfp_static_filesystem_entry);
|
|
|
|
entries = realloc(filesystem->entries, new_size);
|
|
|
|
|
|
|
|
if (NULL != entries)
|
|
|
|
{
|
|
|
|
filesystem->entries = entries;
|
|
|
|
filesystem->capacity = new_capacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filesystem->size < filesystem->capacity)
|
|
|
|
{
|
|
|
|
entry = &filesystem->entries[filesystem->size];
|
|
|
|
entry->inode = filesystem->size + 1;
|
|
|
|
filesystem->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_entry_read(
|
2019-04-26 18:49:09 +00:00
|
|
|
size_t offset,
|
|
|
|
char * buffer,
|
|
|
|
size_t buffer_size,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
size_t result = 0;
|
|
|
|
struct wfp_static_filesystem_entry * entry = user_data;
|
|
|
|
if (offset < entry->size)
|
|
|
|
{
|
|
|
|
size_t remaining = (entry->size - offset);
|
|
|
|
result = (buffer_size < remaining) ? buffer_size : remaining;
|
|
|
|
memcpy(buffer, entry->content, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_entry_get_info(
|
2019-04-26 18:49:09 +00:00
|
|
|
void * user_data,
|
|
|
|
int * result_mode,
|
|
|
|
size_t * result_size)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry * entry = user_data;
|
|
|
|
*result_mode = entry->mode;
|
|
|
|
*result_size = entry->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_add_dir(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem * filesystem,
|
|
|
|
size_t parent,
|
|
|
|
char const * name
|
|
|
|
)
|
|
|
|
{
|
2020-02-20 17:54:29 +00:00
|
|
|
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
|
2019-04-26 18:49:09 +00:00
|
|
|
if (NULL == entry)
|
|
|
|
{
|
2020-02-20 17:54:29 +00:00
|
|
|
entry = wfp_static_filesystem_add_entry(filesystem);
|
2019-04-26 18:49:09 +00:00
|
|
|
entry->parent = parent;
|
|
|
|
entry->is_file = false;
|
|
|
|
entry->mode = 0555;
|
|
|
|
entry->name = strdup(name);
|
|
|
|
entry->user_data = entry;
|
2020-02-20 17:54:29 +00:00
|
|
|
entry->read = &wfp_static_filesystem_entry_read;
|
|
|
|
entry->get_info = &wfp_static_filesystem_entry_get_info;
|
2019-04-26 18:49:09 +00:00
|
|
|
entry->size = 0;
|
|
|
|
entry->content = NULL;
|
|
|
|
}
|
2019-05-25 00:07:35 +00:00
|
|
|
|
|
|
|
return entry->inode;
|
2019-04-26 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_make_parent(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem * filesystem,
|
|
|
|
struct wf_path * path)
|
|
|
|
{
|
2019-05-26 18:46:41 +00:00
|
|
|
size_t result = WFP_STATIC_FILSYSTEM_INODE_ROOT;
|
2019-04-26 18:49:09 +00:00
|
|
|
|
|
|
|
size_t count = wf_path_element_count(path);
|
|
|
|
if (0 < count)
|
|
|
|
{
|
|
|
|
for(size_t i = 0; i < (count - 1); i++)
|
|
|
|
{
|
|
|
|
char const * name = wf_path_get_element(path, i);
|
2020-02-20 17:54:29 +00:00
|
|
|
result = wfp_static_filesystem_add_dir(filesystem, result, name);
|
2019-04-26 18:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_stat(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem_entry * entry,
|
|
|
|
struct stat * stat
|
|
|
|
)
|
|
|
|
{
|
|
|
|
memset(stat, 0, sizeof(struct stat));
|
|
|
|
|
|
|
|
int mode;
|
|
|
|
size_t size;
|
|
|
|
entry->get_info(entry->user_data, &mode, &size);
|
|
|
|
|
|
|
|
stat->st_ino = entry->inode;
|
|
|
|
stat->st_size = entry->size;
|
|
|
|
stat->st_mode = entry->mode & 0777;
|
|
|
|
stat->st_mode |= (entry->is_file) ? S_IFREG: S_IFDIR;
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:54:29 +00:00
|
|
|
static void wfp_static_filesystem_lookup(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_request * request,
|
|
|
|
ino_t parent,
|
|
|
|
char const * name,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem * filesystem = user_data;
|
2020-02-20 17:54:29 +00:00
|
|
|
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
|
2019-04-26 18:49:09 +00:00
|
|
|
|
|
|
|
if (NULL != entry)
|
|
|
|
{
|
|
|
|
struct stat stat;
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_stat(entry, &stat);
|
2019-04-26 18:49:09 +00:00
|
|
|
wfp_respond_lookup(request, &stat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wfp_respond_error(request, WF_BAD_NOENTRY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-20 17:54:29 +00:00
|
|
|
static void wfp_static_filesystem_getattr(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_request * request,
|
|
|
|
ino_t inode,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem * filesystem = user_data;
|
2020-02-20 17:54:29 +00:00
|
|
|
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
2019-04-26 18:49:09 +00:00
|
|
|
|
|
|
|
if (NULL != entry)
|
|
|
|
{
|
|
|
|
struct stat stat;
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_stat(entry, &stat);
|
2019-04-26 18:49:09 +00:00
|
|
|
wfp_respond_getattr(request, &stat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wfp_respond_error(request, WF_BAD_NOENTRY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:54:29 +00:00
|
|
|
static void wfp_static_filesystem_readdir(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_request * request,
|
|
|
|
ino_t directory,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem * filesystem = user_data;
|
2020-02-20 17:54:29 +00:00
|
|
|
struct wfp_static_filesystem_entry * dir = wfp_static_filesystem_get_entry(filesystem, directory);
|
2019-04-26 18:49:09 +00:00
|
|
|
|
|
|
|
if ((NULL != dir) && (!dir->is_file))
|
|
|
|
{
|
|
|
|
struct wfp_dirbuffer * buffer = wfp_dirbuffer_create();
|
|
|
|
wfp_dirbuffer_add(buffer, ".", dir->inode);
|
|
|
|
wfp_dirbuffer_add(buffer, "..", dir->inode);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < filesystem->size; i++)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry const * entry = &filesystem->entries[i];
|
|
|
|
if (directory == entry->parent)
|
|
|
|
{
|
|
|
|
wfp_dirbuffer_add(buffer, entry->name, entry->inode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wfp_respond_readdir(request, buffer);
|
|
|
|
wfp_dirbuffer_dispose(buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wfp_respond_error(request, WF_BAD_NOENTRY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:54:29 +00:00
|
|
|
static void wfp_static_filesystem_open(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_request * request,
|
|
|
|
ino_t inode,
|
|
|
|
int flags,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem * filesystem = user_data;
|
2020-02-20 17:54:29 +00:00
|
|
|
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
2019-04-26 18:49:09 +00:00
|
|
|
|
|
|
|
if ((NULL != entry) && (entry->is_file))
|
|
|
|
{
|
|
|
|
if (O_RDONLY == (flags & O_ACCMODE))
|
|
|
|
{
|
|
|
|
wfp_respond_open(request, 0U);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wfp_respond_error(request, WF_BAD_ACCESS_DENIED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wfp_respond_error(request, WF_BAD_NOENTRY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:54:29 +00:00
|
|
|
static void wfp_static_filesystem_read(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_request * request,
|
|
|
|
ino_t inode,
|
|
|
|
uint32_t WF_UNUSED_PARAM(handle),
|
|
|
|
size_t offset,
|
|
|
|
size_t length,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem * filesystem = user_data;
|
2020-02-20 17:54:29 +00:00
|
|
|
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
2019-04-26 18:49:09 +00:00
|
|
|
|
|
|
|
if ((NULL != entry) && (entry->is_file))
|
|
|
|
{
|
|
|
|
char buffer[WFP_STATIC_FILESYSTEM_MAX_READ_SIZE];
|
|
|
|
size_t max_size = (length < WFP_STATIC_FILESYSTEM_MAX_READ_SIZE) ? length : WFP_STATIC_FILESYSTEM_MAX_READ_SIZE;
|
|
|
|
|
|
|
|
size_t count = entry->read(offset, buffer, max_size, entry->user_data);
|
|
|
|
wfp_respond_read(request, buffer, count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wfp_respond_error(request, WF_BAD_NOENTRY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct wfp_static_filesystem *
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_create(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_client_config * config)
|
|
|
|
{
|
|
|
|
(void) config;
|
|
|
|
|
|
|
|
struct wfp_static_filesystem * filesystem = malloc(sizeof(struct wfp_static_filesystem));
|
|
|
|
if (NULL != filesystem)
|
|
|
|
{
|
|
|
|
filesystem->entries = malloc(sizeof(struct wfp_static_filesystem_entry) * WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY);
|
|
|
|
filesystem->size = 0;
|
|
|
|
filesystem->capacity = WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY;
|
|
|
|
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_add_dir(filesystem, 0, "<root>");
|
2019-04-26 18:49:09 +00:00
|
|
|
|
|
|
|
wfp_client_config_set_userdata(config, filesystem);
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_client_config_set_onlookup(config, &wfp_static_filesystem_lookup);
|
|
|
|
wfp_client_config_set_ongetattr(config, &wfp_static_filesystem_getattr);
|
|
|
|
wfp_client_config_set_onreaddir(config, &wfp_static_filesystem_readdir);
|
|
|
|
wfp_client_config_set_onopen(config, &wfp_static_filesystem_open);
|
|
|
|
wfp_client_config_set_onread(config, &wfp_static_filesystem_read);
|
2019-04-26 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return filesystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_dispose(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem * filesystem)
|
|
|
|
{
|
|
|
|
for(size_t i = 0; i < filesystem->size; i++)
|
|
|
|
{
|
|
|
|
struct wfp_static_filesystem_entry * entry = &filesystem->entries[i];
|
|
|
|
free(entry->name);
|
|
|
|
free(entry->content);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(filesystem->entries);
|
|
|
|
free(filesystem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_add(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem * filesystem,
|
|
|
|
char const * path,
|
|
|
|
int mode,
|
|
|
|
char const * content,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
struct wf_path * path_ = wf_path_create(path);
|
|
|
|
if (NULL != path_)
|
|
|
|
{
|
2020-02-20 17:54:29 +00:00
|
|
|
size_t parent = wfp_static_filesystem_make_parent(filesystem, path_);
|
|
|
|
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_add_entry(filesystem);
|
2019-04-26 18:49:09 +00:00
|
|
|
entry->parent = parent;
|
|
|
|
entry->is_file = true;
|
|
|
|
entry->name = strdup(wf_path_get_filename(path_));
|
|
|
|
entry->mode = mode;
|
|
|
|
entry->size = length;
|
2020-02-20 17:54:29 +00:00
|
|
|
entry->get_info = &wfp_static_filesystem_entry_get_info;
|
|
|
|
entry->read = &wfp_static_filesystem_entry_read;
|
2019-04-26 18:49:09 +00:00
|
|
|
entry->user_data = entry;
|
|
|
|
|
|
|
|
entry->content = malloc(length);
|
|
|
|
memcpy(entry->content, content, length);
|
|
|
|
|
|
|
|
wf_path_dispose(path_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_add_text(
|
2019-04-26 18:49:09 +00:00
|
|
|
struct wfp_static_filesystem * filesystem,
|
|
|
|
char const * path,
|
|
|
|
int mode,
|
|
|
|
char const * content)
|
|
|
|
{
|
|
|
|
size_t length = strlen(content);
|
2020-02-20 17:54:29 +00:00
|
|
|
wfp_static_filesystem_add(filesystem, path, mode, content, length);
|
2019-04-26 18:49:09 +00:00
|
|
|
}
|