mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
made static_filesystem private (used for test purposes only)
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "webfuse/core/path.h"
|
||||
|
||||
TEST(wf_path, empty)
|
||||
{
|
||||
struct wf_path * path = wf_path_create("");
|
||||
ASSERT_EQ(0, wf_path_element_count(path));
|
||||
ASSERT_EQ(nullptr, wf_path_get_element(path, 0));
|
||||
|
||||
wf_path_dispose(path);
|
||||
}
|
||||
|
||||
TEST(wf_path, relative_file)
|
||||
{
|
||||
struct wf_path * path = wf_path_create("some.file");
|
||||
ASSERT_EQ(1, wf_path_element_count(path));
|
||||
ASSERT_STREQ("some.file", wf_path_get_element(path, 0));
|
||||
|
||||
wf_path_dispose(path);
|
||||
}
|
||||
|
||||
TEST(wf_path, absolute_file)
|
||||
{
|
||||
struct wf_path * path = wf_path_create("/absolute.file");
|
||||
ASSERT_EQ(1, wf_path_element_count(path));
|
||||
ASSERT_STREQ("absolute.file", wf_path_get_element(path, 0));
|
||||
|
||||
wf_path_dispose(path);
|
||||
}
|
||||
|
||||
TEST(wf_path, nested_path)
|
||||
{
|
||||
struct wf_path * path = wf_path_create("/a/nested/path");
|
||||
ASSERT_EQ(3, wf_path_element_count(path));
|
||||
ASSERT_STREQ("a", wf_path_get_element(path, 0));
|
||||
ASSERT_STREQ("nested", wf_path_get_element(path, 1));
|
||||
ASSERT_STREQ("path", wf_path_get_element(path, 2));
|
||||
|
||||
wf_path_dispose(path);
|
||||
}
|
||||
|
||||
TEST(wf_path, deep_nested_path)
|
||||
{
|
||||
struct wf_path * path = wf_path_create("/this/is/a/very/deep/nested/path/to/some/file");
|
||||
ASSERT_EQ(10, wf_path_element_count(path));
|
||||
ASSERT_STREQ("this", wf_path_get_element(path, 0));
|
||||
ASSERT_STREQ("is", wf_path_get_element(path, 1));
|
||||
ASSERT_STREQ("a", wf_path_get_element(path, 2));
|
||||
ASSERT_STREQ("very", wf_path_get_element(path, 3));
|
||||
ASSERT_STREQ("deep", wf_path_get_element(path, 4));
|
||||
ASSERT_STREQ("nested", wf_path_get_element(path, 5));
|
||||
ASSERT_STREQ("path", wf_path_get_element(path, 6));
|
||||
ASSERT_STREQ("to", wf_path_get_element(path, 7));
|
||||
ASSERT_STREQ("some", wf_path_get_element(path, 8));
|
||||
ASSERT_STREQ("file", wf_path_get_element(path, 9));
|
||||
|
||||
wf_path_dispose(path);
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "webfuse/utils/static_filesystem.h"
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/provider/impl/static_filesystem.h"
|
||||
#include "webfuse/provider/client_config.h"
|
||||
#include "webfuse/provider/impl/client_config.h"
|
||||
|
||||
#include "webfuse/mocks/mock_request.hpp"
|
||||
|
||||
using webfuse_test::request_create;
|
||||
using webfuse_test::MockRequest;
|
||||
using webfuse_test::GetAttrMatcher;
|
||||
using webfuse_test::ReaddirMatcher;
|
||||
using testing::_;
|
||||
|
||||
TEST(wfp_static_filesystem, has_root_dir)
|
||||
{
|
||||
struct wfp_client_config * config = wfp_client_config_create();
|
||||
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
|
||||
|
||||
MockRequest mock;
|
||||
struct wfp_request * request = request_create(&mock, 42);
|
||||
EXPECT_CALL(mock, respond(GetAttrMatcher(1, 0555, "dir"), 42)).Times(1);
|
||||
|
||||
config->provider.getattr(request, 1, config->user_data);
|
||||
|
||||
wfp_impl_static_filesystem_dispose(filesystem);
|
||||
wfp_client_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(wfp_static_filesystem, contains_default_dirs)
|
||||
{
|
||||
struct wfp_client_config * config = wfp_client_config_create();
|
||||
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
|
||||
|
||||
MockRequest mock;
|
||||
struct wfp_request * request = request_create(&mock, 23);
|
||||
char const * default_dirs[] = {".", "..", nullptr};
|
||||
EXPECT_CALL(mock, respond(ReaddirMatcher(default_dirs), 23)).Times(1);
|
||||
|
||||
config->provider.readdir(request, 1, config->user_data);
|
||||
|
||||
wfp_impl_static_filesystem_dispose(filesystem);
|
||||
wfp_client_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(wfp_static_filesystem, add_text)
|
||||
{
|
||||
struct wfp_client_config * config = wfp_client_config_create();
|
||||
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
|
||||
wfp_impl_static_filesystem_add_text(filesystem, "text.file", 666, "some text");
|
||||
|
||||
MockRequest mock;
|
||||
struct wfp_request * request = request_create(&mock, 23);
|
||||
char const * contained_elements[] = {"text.file", nullptr};
|
||||
EXPECT_CALL(mock, respond(ReaddirMatcher(contained_elements), 23)).Times(1);
|
||||
|
||||
config->provider.readdir(request, 1, config->user_data);
|
||||
|
||||
wfp_impl_static_filesystem_dispose(filesystem);
|
||||
wfp_client_config_dispose(config);
|
||||
}
|
||||
116
test/webfuse/utils/path.c
Normal file
116
test/webfuse/utils/path.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "webfuse/utils/path.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WF_PATH_DEFAULT_CAPACITY (8)
|
||||
|
||||
struct wf_path
|
||||
{
|
||||
char * * elements;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
static void
|
||||
wf_path_add(
|
||||
struct wf_path * path,
|
||||
char const * element,
|
||||
size_t element_size)
|
||||
{
|
||||
if (0 < element_size)
|
||||
{
|
||||
if (path->count >= path->capacity)
|
||||
{
|
||||
size_t new_capacity = 2 * path->capacity;
|
||||
size_t new_size = sizeof(char*) * new_capacity;
|
||||
|
||||
char * * elements = realloc(path->elements, new_size);
|
||||
if (NULL != elements)
|
||||
{
|
||||
path->elements = elements;
|
||||
path->capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
if (path->count < path->capacity)
|
||||
{
|
||||
path->elements[path->count] = strndup(element, element_size);
|
||||
path->count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct wf_path *
|
||||
wf_path_create(
|
||||
char const * value)
|
||||
{
|
||||
struct wf_path * path = malloc(sizeof(struct wf_path));
|
||||
if (NULL != path)
|
||||
{
|
||||
path->elements = malloc(sizeof(char*) * WF_PATH_DEFAULT_CAPACITY);
|
||||
path->capacity = WF_PATH_DEFAULT_CAPACITY;
|
||||
path->count = 0;
|
||||
|
||||
char const * remainder = value;
|
||||
char const * pos = strchr(remainder, '/');
|
||||
while (NULL != pos)
|
||||
{
|
||||
wf_path_add(path, remainder, (pos - remainder));
|
||||
remainder = pos + 1;
|
||||
pos = strchr(remainder, '/');
|
||||
}
|
||||
|
||||
wf_path_add(path, remainder, strlen(remainder));
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
void
|
||||
wf_path_dispose(
|
||||
struct wf_path * path)
|
||||
{
|
||||
for(size_t i = 0; i < path->count; i++)
|
||||
{
|
||||
free(path->elements[i]);
|
||||
}
|
||||
|
||||
free(path->elements);
|
||||
free(path);
|
||||
(void) path;
|
||||
}
|
||||
|
||||
size_t
|
||||
wf_path_element_count(
|
||||
struct wf_path * path)
|
||||
{
|
||||
return path->count;
|
||||
}
|
||||
|
||||
char const *
|
||||
wf_path_get_element(
|
||||
struct wf_path * path,
|
||||
size_t i)
|
||||
{
|
||||
char const * result = NULL;
|
||||
if (i < path->count)
|
||||
{
|
||||
result = path->elements[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char const *
|
||||
wf_path_get_filename(
|
||||
struct wf_path * path)
|
||||
{
|
||||
char const * result = NULL;
|
||||
|
||||
if (0 < path->count)
|
||||
{
|
||||
result = path->elements[path->count - 1];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
43
test/webfuse/utils/path.h
Normal file
43
test/webfuse/utils/path.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef WF_PATH_H
|
||||
#define WF_PATH_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using ::std::size_t;
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_path;
|
||||
|
||||
extern struct wf_path *
|
||||
wf_path_create(
|
||||
char const * value);
|
||||
|
||||
extern void
|
||||
wf_path_dispose(
|
||||
struct wf_path * path);
|
||||
|
||||
extern size_t
|
||||
wf_path_element_count(
|
||||
struct wf_path * path);
|
||||
|
||||
extern char const *
|
||||
wf_path_get_element(
|
||||
struct wf_path * path,
|
||||
size_t i);
|
||||
|
||||
extern char const *
|
||||
wf_path_get_filename(
|
||||
struct wf_path * path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
498
test/webfuse/utils/static_filesystem.c
Normal file
498
test/webfuse/utils/static_filesystem.c
Normal file
@@ -0,0 +1,498 @@
|
||||
#include "webfuse/utils/static_filesystem.h"
|
||||
#include "webfuse/provider/client_config.h"
|
||||
#include "webfuse/provider/dirbuffer.h"
|
||||
#include "webfuse/provider/operation/error.h"
|
||||
|
||||
#include "webfuse/utils/path.h"
|
||||
#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)
|
||||
#define WFP_STATIC_FILSYSTEM_INODE_ROOT (1)
|
||||
#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 *
|
||||
wfp_static_filesystem_get_entry(
|
||||
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 *
|
||||
wfp_static_filesystem_get_entry_by_name(
|
||||
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 *
|
||||
wfp_static_filesystem_add_entry(
|
||||
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
|
||||
wfp_static_filesystem_entry_read(
|
||||
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
|
||||
wfp_static_filesystem_entry_get_info(
|
||||
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
|
||||
wfp_static_filesystem_file_read(
|
||||
size_t offset,
|
||||
char * buffer,
|
||||
size_t buffer_size,
|
||||
void * user_data)
|
||||
{
|
||||
size_t result = 0;
|
||||
struct wfp_static_filesystem_entry * entry = user_data;
|
||||
char const * filename = entry->content;
|
||||
|
||||
FILE * file = fopen(filename, "rb");
|
||||
if (NULL != file)
|
||||
{
|
||||
fseek(file, offset, SEEK_SET);
|
||||
result = fread(buffer, buffer_size, 1, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
wfp_static_filesystem_file_get_info(
|
||||
void * user_data,
|
||||
int * result_mode,
|
||||
size_t * result_size)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = user_data;
|
||||
char const * filename = entry->content;
|
||||
|
||||
struct stat buffer;
|
||||
stat(filename, &buffer);
|
||||
|
||||
*result_mode = (int) (buffer.st_mode & 0777);
|
||||
*result_size = (size_t) buffer.st_size;
|
||||
}
|
||||
|
||||
|
||||
static size_t
|
||||
wfp_static_filesystem_add_dir(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
size_t parent,
|
||||
char const * name
|
||||
)
|
||||
{
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
|
||||
if (NULL == entry)
|
||||
{
|
||||
entry = wfp_static_filesystem_add_entry(filesystem);
|
||||
entry->parent = parent;
|
||||
entry->is_file = false;
|
||||
entry->mode = 0555;
|
||||
entry->name = strdup(name);
|
||||
entry->user_data = entry;
|
||||
entry->read = &wfp_static_filesystem_entry_read;
|
||||
entry->get_info = &wfp_static_filesystem_entry_get_info;
|
||||
entry->size = 0;
|
||||
entry->content = NULL;
|
||||
}
|
||||
|
||||
return entry->inode;
|
||||
}
|
||||
|
||||
static size_t
|
||||
wfp_static_filesystem_make_parent(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
struct wf_path * path)
|
||||
{
|
||||
size_t result = WFP_STATIC_FILSYSTEM_INODE_ROOT;
|
||||
|
||||
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);
|
||||
result = wfp_static_filesystem_add_dir(filesystem, result, name);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
wfp_static_filesystem_stat(
|
||||
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;
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_lookup(
|
||||
struct wfp_request * request,
|
||||
ino_t parent,
|
||||
char const * name,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name);
|
||||
|
||||
if (NULL != entry)
|
||||
{
|
||||
struct stat stat;
|
||||
wfp_static_filesystem_stat(entry, &stat);
|
||||
wfp_respond_lookup(request, &stat);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void wfp_static_filesystem_getattr(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
if (NULL != entry)
|
||||
{
|
||||
struct stat stat;
|
||||
wfp_static_filesystem_stat(entry, &stat);
|
||||
wfp_respond_getattr(request, &stat);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_readdir(
|
||||
struct wfp_request * request,
|
||||
ino_t directory,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * dir = wfp_static_filesystem_get_entry(filesystem, directory);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_open(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_static_filesystem * filesystem = user_data;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_static_filesystem_read(
|
||||
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;
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry(filesystem, inode);
|
||||
|
||||
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 *
|
||||
wfp_static_filesystem_create(
|
||||
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;
|
||||
|
||||
wfp_static_filesystem_add_dir(filesystem, 0, "<root>");
|
||||
|
||||
wfp_client_config_set_userdata(config, filesystem);
|
||||
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);
|
||||
}
|
||||
|
||||
return filesystem;
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_dispose(
|
||||
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
|
||||
wfp_static_filesystem_add(
|
||||
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_)
|
||||
{
|
||||
size_t parent = wfp_static_filesystem_make_parent(filesystem, path_);
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_add_entry(filesystem);
|
||||
entry->parent = parent;
|
||||
entry->is_file = true;
|
||||
entry->name = strdup(wf_path_get_filename(path_));
|
||||
entry->mode = mode;
|
||||
entry->size = length;
|
||||
entry->get_info = &wfp_static_filesystem_entry_get_info;
|
||||
entry->read = &wfp_static_filesystem_entry_read;
|
||||
entry->user_data = entry;
|
||||
|
||||
entry->content = malloc(length);
|
||||
memcpy(entry->content, content, length);
|
||||
|
||||
wf_path_dispose(path_);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_add_text(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content)
|
||||
{
|
||||
size_t length = strlen(content);
|
||||
wfp_static_filesystem_add(filesystem, path, mode, content, length);
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_add_file(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
char const * filename)
|
||||
{
|
||||
struct wf_path * path_ = wf_path_create(path);
|
||||
if (NULL != path_)
|
||||
{
|
||||
size_t parent = wfp_static_filesystem_make_parent(filesystem, path_);
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_add_entry(filesystem);
|
||||
entry->parent = parent;
|
||||
entry->is_file = true;
|
||||
entry->mode = 0;
|
||||
entry->content = strdup(filename);
|
||||
entry->size = 0;
|
||||
entry->name = strdup(wf_path_get_filename(path_));
|
||||
entry->get_info = &wfp_static_filesystem_file_get_info;
|
||||
entry->read = &wfp_static_filesystem_file_read;
|
||||
entry->user_data = entry;
|
||||
|
||||
wf_path_dispose(path_);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wfp_static_filesystem_add_generic(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
wfp_static_filesystem_read_fn * read,
|
||||
wfp_static_filesystem_get_info_fn * get_info,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_path * path_ = wf_path_create(path);
|
||||
if (NULL != path_)
|
||||
{
|
||||
size_t parent = wfp_static_filesystem_make_parent(filesystem, path_);
|
||||
struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_add_entry(filesystem);
|
||||
entry->parent = parent;
|
||||
entry->is_file = true;
|
||||
entry->mode = 0;
|
||||
entry->content = NULL;
|
||||
entry->size = 0;
|
||||
entry->name = strdup(wf_path_get_filename(path_));
|
||||
entry->get_info = get_info;
|
||||
entry->read = read;
|
||||
entry->user_data = user_data;
|
||||
|
||||
wf_path_dispose(path_);
|
||||
}
|
||||
}
|
||||
77
test/webfuse/utils/static_filesystem.h
Normal file
77
test/webfuse/utils/static_filesystem.h
Normal file
@@ -0,0 +1,77 @@
|
||||
#ifndef WFP_STATIC_FILESYSTEM_H
|
||||
#define WFP_STATIC_FILESYSTEM_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using ::std::size_t;
|
||||
#endif
|
||||
|
||||
#include <webfuse/provider/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_client_config;
|
||||
|
||||
struct wfp_static_filesystem;
|
||||
|
||||
typedef size_t
|
||||
wfp_static_filesystem_read_fn(
|
||||
size_t offset,
|
||||
char * buffer,
|
||||
size_t buffer_size,
|
||||
void * user_data);
|
||||
|
||||
typedef void
|
||||
wfp_static_filesystem_get_info_fn(
|
||||
void * user_data,
|
||||
int * result_mode,
|
||||
size_t * result_size);
|
||||
|
||||
|
||||
extern WFP_API struct wfp_static_filesystem *
|
||||
wfp_static_filesystem_create(
|
||||
struct wfp_client_config * config);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_dispose(
|
||||
struct wfp_static_filesystem * filesystem);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_add(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content,
|
||||
size_t length);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_add_text(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
int mode,
|
||||
char const * content);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_add_file(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
char const * filename);
|
||||
|
||||
extern WFP_API void
|
||||
wfp_static_filesystem_add_generic(
|
||||
struct wfp_static_filesystem * filesystem,
|
||||
char const * path,
|
||||
wfp_static_filesystem_read_fn * read,
|
||||
wfp_static_filesystem_get_info_fn * get_info,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user