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

feat(webfuse): static file provider (#30)

* added API stub of static_filesystem

* added callback functions

* added basic directory listing

* resize filesystem if necessary

* added path stub

* adds imlementation and tests of path

* adds mock of wpf_request

* adds test implementation and some matchers

* added matcher of readdir results

* fixes default directory test

* adds implementation of static_filesystem_add and add_text

* added implementation of read

* adds implementation of filesystem_read

* corrects naming of some functions

* removes Flawfinder comments, since Flawfinder is disabled
This commit is contained in:
Falk Werner
2019-04-26 20:49:09 +02:00
committed by GitHub
parent a717248e80
commit fa78e23533
19 changed files with 1301 additions and 9 deletions

116
lib/webfuse/core/path.c Normal file
View File

@@ -0,0 +1,116 @@
#include "webfuse/core/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
lib/webfuse/core/path.h Normal file
View 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

View File

@@ -10,7 +10,7 @@ char * wf_create_string(char const * format, ...)
va_list measure_args;
va_start(measure_args, format);
char buffer;
int needed = vsnprintf(&buffer, 1, format, measure_args); /* Flawfinder: ignore */
int needed = vsnprintf(&buffer, 1, format, measure_args);
va_end(measure_args);
if (0 <= needed)
@@ -20,7 +20,7 @@ char * wf_create_string(char const * format, ...)
{
va_list args;
va_start(args, format);
int count = vsnprintf(result, needed + 1, format, args); /* Flawfinder: ignore */
int count = vsnprintf(result, needed + 1, format, args);
va_end(args);
if ((count < 0) || (needed < count))