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

moved code to examples

This commit is contained in:
Falk Werner
2020-06-15 19:23:31 +02:00
parent a4aa1fd08c
commit ad4330cfd7
14 changed files with 0 additions and 0 deletions

383
examples/src/main.c Normal file
View File

@@ -0,0 +1,383 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include "webfuse_provider.h"
struct config
{
char * url;
struct wfp_client_config * client_config;
bool show_help;
};
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
{
struct fs_entry const * entries;
};
static void show_help()
{
printf(
"webfuse-provider, Copyright (c) 2019, webfuse authors <https://github.com/falk-werner/webfuse>\n"
"Example for websocket file system provider\n"
"\n"
"Usage: webfuse-provider -u <url> [-k <key_path>] [-c <cert_path>]\n"
"\n"
"Options:\n"
"\t-u, --url URL of webfuse server (required)\n"
"\t-k, --key_path Path to private key of provider (default: not set, TLS disabled)\n"
"\t-c, --cert_path Path to certificate of provider (defautl: not set, TLS disabled)\n"
"\t-h, --help prints this message\n"
"\n"
"Example:\n"
"\twebfuse-provider -u ws://localhost:8080/\n"
"\n"
);
}
static int parse_arguments(
int argc,
char* argv[],
struct config * config)
{
static struct option const options[] =
{
{"url", required_argument, NULL, 'u'},
{"key_path", required_argument, NULL, 'k'},
{"cert_path", required_argument, NULL, 'c'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
int result = EXIT_SUCCESS;
bool finished = false;
while (!finished)
{
int option_index = 0;
int const c = getopt_long(argc, argv, "u:k:c:h", options, &option_index);
switch (c)
{
case -1:
finished = true;
break;
case 'h':
config->show_help = true;
finished = true;
break;
case 'u':
free(config->url);
config->url = strdup(optarg);
break;
case 'k':
wfp_client_config_set_keypath(config->client_config, optarg);
break;
case 'c':
wfp_client_config_set_certpath(config->client_config, optarg);
break;
default:
fprintf(stderr, "error: unknown argument\n");
finished = true;
result = EXIT_FAILURE;
break;
}
if (NULL == config->url)
{
fprintf(stderr, "error: missing required argument \"-u\"\n");
result = EXIT_FAILURE;
}
if (result != EXIT_SUCCESS)
{
config->show_help = true;
}
}
return result;
}
static struct fs_entry const * fs_getentry(
struct fs * fs,
ino_t inode)
{
for (size_t i = 0; 0 != fs->entries[i].inode; i++)
{
struct fs_entry const * entry = &fs->entries[i];
if (inode == entry->inode)
{
return entry;
}
}
return NULL;
}
static struct fs_entry const * fs_getentry_byname(
struct fs * fs,
ino_t parent,
char const * name)
{
for( size_t i = 0; 0 != fs->entries[i].inode; i++)
{
struct fs_entry const * entry = &fs->entries[i];
if ((parent == entry->parent) && (0 == strcmp(name, entry->name)))
{
return entry;
}
}
return NULL;
}
static void fs_stat(
struct fs_entry const * entry,
struct stat * stat)
{
memset(stat, 0, sizeof(struct stat));
stat->st_ino = entry->inode;
stat->st_mode = entry->mode;
if (FS_DIR == entry->type)
{
stat->st_mode |= S_IFDIR;
}
if (FS_FILE == entry->type)
{
stat->st_mode |= S_IFREG;
stat->st_size = entry->content_length;
}
}
static void fs_lookup(
struct wfp_request * request,
ino_t parent,
char const * name,
void * user_data)
{
struct fs * fs = (struct fs*) user_data;
struct fs_entry const * entry = fs_getentry_byname(fs, parent, name);
if (NULL != entry)
{
struct stat stat;
fs_stat(entry, &stat);
wfp_respond_lookup(request, &stat);
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
}
}
static void fs_getattr(
struct wfp_request * request,
ino_t inode,
void * user_data)
{
struct fs * fs = (struct fs*) user_data;
struct fs_entry const * entry = fs_getentry(fs, inode);
if (NULL != entry)
{
struct stat stat;
fs_stat(entry, &stat);
wfp_respond_getattr(request, &stat);
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
}
}
static void fs_readdir(
struct wfp_request * request,
ino_t directory,
void * user_data)
{
struct fs * fs = (struct fs*) user_data;
struct fs_entry const * dir = fs_getentry(fs, directory);
if ((NULL != dir) && (FS_DIR == dir->type))
{
struct wfp_dirbuffer * buffer = wfp_dirbuffer_create();
wfp_dirbuffer_add(buffer, ".", dir->inode);
wfp_dirbuffer_add(buffer, "..", dir->inode);
for(size_t i = 0; 0 != fs->entries[i].inode; i++)
{
struct fs_entry const * entry = &fs->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 fs_open(
struct wfp_request * request,
ino_t inode,
int flags,
void * user_data)
{
struct fs * fs = (struct fs*) user_data;
struct fs_entry const * entry = fs_getentry(fs, inode);
if ((NULL != entry) && (FS_FILE == entry->type))
{
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 size_t min(size_t const a, size_t const b)
{
return (a < b) ? a : b;
}
static void fs_read(
struct wfp_request * request,
ino_t inode,
uint32_t handle,
size_t offset,
size_t length,
void * user_data)
{
(void) handle;
struct fs * fs = (struct fs*) user_data;
struct fs_entry const * entry = fs_getentry(fs, inode);
if ((NULL != entry) && (FS_FILE == entry->type))
{
if (entry->content_length > offset)
{
size_t const remaining = entry->content_length - offset;
size_t const count = min(remaining, length);
wfp_respond_read(request, &entry->content[offset], count);
}
else
{
wfp_respond_error(request, WF_BAD);
}
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
}
}
static volatile bool shutdown_requested = false;
static void on_interrupt(int signal_id)
{
(void) signal_id;
shutdown_requested = true;
}
int main(int argc, char* argv[])
{
struct config config;
config.url = NULL;
config.show_help = false;
config.client_config = wfp_client_config_create();
int result = parse_arguments(argc, argv, &config);
if (EXIT_SUCCESS == result)
{
static struct fs_entry const entries[]=
{
{.parent = 0, .inode = 1, .name = "<root>", .mode = 0555, .type = FS_DIR},
{
.parent = 1,
.inode = 2,
.name = "hello.txt",
.mode = 0444,
.type = FS_FILE,
.content="hello, world!",
.content_length = 13,
},
{.parent = 0, .inode = 0, .name = NULL}
};
struct fs fs =
{
.entries = entries
};
signal(SIGINT, &on_interrupt);
wfp_client_config_set_userdata(config.client_config, &fs);
wfp_client_config_set_onlookup(config.client_config, &fs_lookup);
wfp_client_config_set_ongetattr(config.client_config, &fs_getattr);
wfp_client_config_set_onreaddir(config.client_config, &fs_readdir);
wfp_client_config_set_onopen(config.client_config, &fs_open);
wfp_client_config_set_onread(config.client_config, &fs_read);
struct wfp_client * client = wfp_client_create(config.client_config);
wfp_client_connect(client, config.url);
while (!shutdown_requested)
{
wfp_client_service(client);
}
wfp_client_dispose(client);
}
if (config.show_help)
{
show_help();
}
free(config.url);
wfp_client_config_dispose(config.client_config);
return result;
}

View File

@@ -0,0 +1,108 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <signal.h>
#include "webfuse_provider.h"
#include "static_filesystem.h"
struct args
{
char const * url;
bool show_help;
};
static int
parse_args(
struct args * args,
int argc,
char * argv[])
{
int result = EXIT_FAILURE;
args->show_help = true;
args->url = NULL;
if (2 == argc)
{
result = EXIT_SUCCESS;
char const * url = argv[1];
if ((0 != strcmp(url, "-h")) && (0 != strcmp(url, "--help")))
{
args->show_help = false;
args->url = url;
}
}
else
{
fprintf(stderr, "error: missing argument\n");
}
return result;
}
static volatile bool shutdown_requested = false;
static void on_interrupt(int signal_id)
{
(void) signal_id;
shutdown_requested = true;
}
static void print_usage()
{
printf(
"static-filesystem-provider Copyright (c) 2019, webfuse authors <https://github.com/falk-werner/webfuse>\n"
"Example of webfuse static filesystem provider\n"
"\n"
"Usage: static-filesystem-provider <url>\n"
"\n"
"Arguments:\n"
"\t<url> URL of webfuse server (required)\n"
"\t-h, --help prints this message\n"
"\n"
"Example:\n"
"\tstatic-filesystem-provider ws://localhost:8080/\n"
"\n"
);
}
int main(int argc, char* argv[])
{
signal(SIGINT, &on_interrupt);
struct args args;
int result = parse_args(&args, argc, argv);
if ((EXIT_SUCCESS == result) && (!args.show_help))
{
struct wfp_client_config * config = wfp_client_config_create();
struct static_filesystem * fs = static_filesystem_create(config);
static_filesystem_add_text(fs, "brummni/hello_world.txt", 0444, "Hello, World!\n");
static_filesystem_add_text(fs, "brummni/hello_bob.txt", 0444, "Hello, Bob!\n");
static_filesystem_add_text(fs, "brummni/hello_bob.txt", 0444, "Hello, Alice!\n");
static_filesystem_add_text(fs, "bla/hello_world.txt", 0444, "Hello, World!\n");
static_filesystem_add_text(fs, "foo.txt", 0444, "foo\n");
static_filesystem_add_text(fs, "bar.txt", 0444, "bar\n");
struct wfp_client * client = wfp_client_create(config);
wfp_client_connect(client, args.url);
while (!shutdown_requested)
{
wfp_client_service(client);
}
wfp_client_dispose(client);
static_filesystem_dispose(fs);
wfp_client_config_dispose(config);
}
if (args.show_help)
{
print_usage();
}
return result;
}

View File

@@ -0,0 +1,116 @@
#include "path.h"
#include <stdlib.h>
#include <string.h>
#define PATH_DEFAULT_CAPACITY (8)
struct path
{
char * * elements;
size_t count;
size_t capacity;
};
static void
path_add(
struct 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 path *
path_create(
char const * value)
{
struct path * path = malloc(sizeof(struct path));
if (NULL != path)
{
path->elements = malloc(sizeof(char*) * PATH_DEFAULT_CAPACITY);
path->capacity = PATH_DEFAULT_CAPACITY;
path->count = 0;
char const * remainder = value;
char const * pos = strchr(remainder, '/');
while (NULL != pos)
{
path_add(path, remainder, (pos - remainder));
remainder = pos + 1;
pos = strchr(remainder, '/');
}
path_add(path, remainder, strlen(remainder));
}
return path;
}
void
path_dispose(
struct path * path)
{
for(size_t i = 0; i < path->count; i++)
{
free(path->elements[i]);
}
free(path->elements);
free(path);
(void) path;
}
size_t
path_element_count(
struct path * path)
{
return path->count;
}
char const *
path_get_element(
struct path * path,
size_t i)
{
char const * result = NULL;
if (i < path->count)
{
result = path->elements[i];
}
return result;
}
char const *
path_get_filename(
struct path * path)
{
char const * result = NULL;
if (0 < path->count)
{
result = path->elements[path->count - 1];
}
return result;
}

View File

@@ -0,0 +1,43 @@
#ifndef PATH_H
#define PATH_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
using ::std::size_t;
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct path;
extern struct path *
path_create(
char const * value);
extern void
path_dispose(
struct path * path);
extern size_t
path_element_count(
struct path * path);
extern char const *
path_get_element(
struct path * path,
size_t i);
extern char const *
path_get_filename(
struct path * path);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,500 @@
#include "static_filesystem.h"
#include "webfuse/provider/client_config.h"
#include "webfuse/provider/dirbuffer.h"
#include "webfuse/provider/operation/error.h"
#include "path.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define STATIC_FILESYSTEM_DEFAULT_CAPACITY (16)
#define STATIC_FILSYSTEM_INODE_ROOT (1)
#define STATIC_FILESYSTEM_MAX_READ_SIZE (4 * 1024)
struct static_filesystem_entry
{
size_t inode;
size_t parent;
char * name;
bool is_file;
int mode;
size_t size;
char * content;
static_filesystem_read_fn * read;
static_filesystem_get_info_fn * get_info;
void * user_data;
};
struct static_filesystem
{
struct static_filesystem_entry * entries;
size_t size;
size_t capacity;
};
static struct static_filesystem_entry *
static_filesystem_get_entry(
struct static_filesystem * filesystem,
size_t inode)
{
struct static_filesystem_entry * entry = NULL;
if ((0 < inode) && (inode <= filesystem->size))
{
entry = &filesystem->entries[inode - 1];
}
return entry;
}
static struct static_filesystem_entry *
static_filesystem_get_entry_by_name(
struct static_filesystem * filesystem,
size_t parent,
char const * name)
{
struct static_filesystem_entry * entry = NULL;
for(size_t i = 0; i < filesystem->size; i++)
{
struct static_filesystem_entry * current = &filesystem->entries[i];
if ((parent == current->parent) && (0 == strcmp(name, current->name)))
{
entry = current;
break;
}
}
return entry;
}
static struct static_filesystem_entry *
static_filesystem_add_entry(
struct static_filesystem * filesystem)
{
struct static_filesystem_entry * entry = NULL;
if (filesystem->size >= filesystem->capacity)
{
struct static_filesystem_entry * entries;
size_t new_capacity = 2 * filesystem->capacity;
size_t new_size = new_capacity * sizeof(struct 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
static_filesystem_entry_read(
size_t offset,
char * buffer,
size_t buffer_size,
void * user_data)
{
size_t result = 0;
struct 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
static_filesystem_entry_get_info(
void * user_data,
int * result_mode,
size_t * result_size)
{
struct static_filesystem_entry * entry = user_data;
*result_mode = entry->mode;
*result_size = entry->size;
}
static size_t
static_filesystem_file_read(
size_t offset,
char * buffer,
size_t buffer_size,
void * user_data)
{
size_t result = 0;
struct 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
static_filesystem_file_get_info(
void * user_data,
int * result_mode,
size_t * result_size)
{
struct 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
static_filesystem_add_dir(
struct static_filesystem * filesystem,
size_t parent,
char const * name
)
{
struct static_filesystem_entry * entry = static_filesystem_get_entry_by_name(filesystem, parent, name);
if (NULL == entry)
{
entry = 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 = &static_filesystem_entry_read;
entry->get_info = &static_filesystem_entry_get_info;
entry->size = 0;
entry->content = NULL;
}
return entry->inode;
}
static size_t
static_filesystem_make_parent(
struct static_filesystem * filesystem,
struct path * path)
{
size_t result = STATIC_FILSYSTEM_INODE_ROOT;
size_t count = path_element_count(path);
if (0 < count)
{
for(size_t i = 0; i < (count - 1); i++)
{
char const * name = path_get_element(path, i);
result = static_filesystem_add_dir(filesystem, result, name);
}
}
return result;
}
static void
static_filesystem_stat(
struct 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 static_filesystem_lookup(
struct wfp_request * request,
ino_t parent,
char const * name,
void * user_data)
{
struct static_filesystem * filesystem = user_data;
struct static_filesystem_entry * entry = static_filesystem_get_entry_by_name(filesystem, parent, name);
if (NULL != entry)
{
struct stat stat;
static_filesystem_stat(entry, &stat);
wfp_respond_lookup(request, &stat);
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
}
}
static void static_filesystem_getattr(
struct wfp_request * request,
ino_t inode,
void * user_data)
{
struct static_filesystem * filesystem = user_data;
struct static_filesystem_entry * entry = static_filesystem_get_entry(filesystem, inode);
if (NULL != entry)
{
struct stat stat;
static_filesystem_stat(entry, &stat);
wfp_respond_getattr(request, &stat);
}
else
{
wfp_respond_error(request, WF_BAD_NOENTRY);
}
}
static void static_filesystem_readdir(
struct wfp_request * request,
ino_t directory,
void * user_data)
{
struct static_filesystem * filesystem = user_data;
struct static_filesystem_entry * dir = 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 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 static_filesystem_open(
struct wfp_request * request,
ino_t inode,
int flags,
void * user_data)
{
struct static_filesystem * filesystem = user_data;
struct static_filesystem_entry * entry = 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 static_filesystem_read(
struct wfp_request * request,
ino_t inode,
uint32_t handle,
size_t offset,
size_t length,
void * user_data)
{
(void) handle;
struct static_filesystem * filesystem = user_data;
struct static_filesystem_entry * entry = static_filesystem_get_entry(filesystem, inode);
if ((NULL != entry) && (entry->is_file))
{
char buffer[STATIC_FILESYSTEM_MAX_READ_SIZE];
size_t max_size = (length < STATIC_FILESYSTEM_MAX_READ_SIZE) ? length : 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 static_filesystem *
static_filesystem_create(
struct wfp_client_config * config)
{
(void) config;
struct static_filesystem * filesystem = malloc(sizeof(struct static_filesystem));
if (NULL != filesystem)
{
filesystem->entries = malloc(sizeof(struct static_filesystem_entry) * STATIC_FILESYSTEM_DEFAULT_CAPACITY);
filesystem->size = 0;
filesystem->capacity = STATIC_FILESYSTEM_DEFAULT_CAPACITY;
static_filesystem_add_dir(filesystem, 0, "<root>");
wfp_client_config_set_userdata(config, filesystem);
wfp_client_config_set_onlookup(config, &static_filesystem_lookup);
wfp_client_config_set_ongetattr(config, &static_filesystem_getattr);
wfp_client_config_set_onreaddir(config, &static_filesystem_readdir);
wfp_client_config_set_onopen(config, &static_filesystem_open);
wfp_client_config_set_onread(config, &static_filesystem_read);
}
return filesystem;
}
void
static_filesystem_dispose(
struct static_filesystem * filesystem)
{
for(size_t i = 0; i < filesystem->size; i++)
{
struct static_filesystem_entry * entry = &filesystem->entries[i];
free(entry->name);
free(entry->content);
}
free(filesystem->entries);
free(filesystem);
}
void
static_filesystem_add(
struct static_filesystem * filesystem,
char const * path,
int mode,
char const * content,
size_t length)
{
struct path * path_ = path_create(path);
if (NULL != path_)
{
size_t parent = static_filesystem_make_parent(filesystem, path_);
struct static_filesystem_entry * entry = static_filesystem_add_entry(filesystem);
entry->parent = parent;
entry->is_file = true;
entry->name = strdup(path_get_filename(path_));
entry->mode = mode;
entry->size = length;
entry->get_info = &static_filesystem_entry_get_info;
entry->read = &static_filesystem_entry_read;
entry->user_data = entry;
entry->content = malloc(length);
memcpy(entry->content, content, length);
path_dispose(path_);
}
}
void
static_filesystem_add_text(
struct static_filesystem * filesystem,
char const * path,
int mode,
char const * content)
{
size_t length = strlen(content);
static_filesystem_add(filesystem, path, mode, content, length);
}
void
static_filesystem_add_file(
struct static_filesystem * filesystem,
char const * path,
char const * filename)
{
struct path * path_ = path_create(path);
if (NULL != path_)
{
size_t parent = static_filesystem_make_parent(filesystem, path_);
struct static_filesystem_entry * entry = 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(path_get_filename(path_));
entry->get_info = &static_filesystem_file_get_info;
entry->read = &static_filesystem_file_read;
entry->user_data = entry;
path_dispose(path_);
}
}
void
static_filesystem_add_generic(
struct static_filesystem * filesystem,
char const * path,
static_filesystem_read_fn * read,
static_filesystem_get_info_fn * get_info,
void * user_data)
{
struct path * path_ = path_create(path);
if (NULL != path_)
{
size_t parent = static_filesystem_make_parent(filesystem, path_);
struct static_filesystem_entry * entry = static_filesystem_add_entry(filesystem);
entry->parent = parent;
entry->is_file = true;
entry->mode = 0;
entry->content = NULL;
entry->size = 0;
entry->name = strdup(path_get_filename(path_));
entry->get_info = get_info;
entry->read = read;
entry->user_data = user_data;
path_dispose(path_);
}
}

View File

@@ -0,0 +1,73 @@
#ifndef STATIC_FILESYSTEM_H
#define STATIC_FILESYSTEM_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
using ::std::size_t;
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_client_config;
struct wfp_static_filesystem;
typedef size_t
static_filesystem_read_fn(
size_t offset,
char * buffer,
size_t buffer_size,
void * user_data);
typedef void
static_filesystem_get_info_fn(
void * user_data,
int * result_mode,
size_t * result_size);
extern struct static_filesystem *
static_filesystem_create(
struct wfp_client_config * config);
extern void
static_filesystem_dispose(
struct static_filesystem * filesystem);
extern void
static_filesystem_add(
struct static_filesystem * filesystem,
char const * path,
int mode,
char const * content,
size_t length);
extern void
static_filesystem_add_text(
struct static_filesystem * filesystem,
char const * path,
int mode,
char const * content);
extern void
static_filesystem_add_file(
struct static_filesystem * filesystem,
char const * path,
char const * filename);
extern void
static_filesystem_add_generic(
struct static_filesystem * filesystem,
char const * path,
static_filesystem_read_fn * read,
static_filesystem_get_info_fn * get_info,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif