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

renamed to webfuse

This commit is contained in:
Falk Werner
2019-03-26 23:04:53 +01:00
parent 1c9d1c8420
commit 7447fb5dff
203 changed files with 4639 additions and 4639 deletions

View File

@@ -0,0 +1,49 @@
#include "webfuse/adapter/impl/authenticator.h"
#include <stdlib.h>
#include <string.h>
#include "webfuse/adapter/impl/credentials.h"
struct wf_impl_authenticator * wf_impl_authenticator_create(
char const * type,
wf_authenticate_fn * authenticate,
void * user_data)
{
struct wf_impl_authenticator * authenticator = malloc(sizeof(struct wf_impl_authenticator));
if (NULL != authenticator)
{
authenticator->type = strdup(type);
authenticator->authenticate = authenticate;
authenticator->user_data = user_data;
authenticator->next = NULL;
}
return authenticator;
}
void wf_impl_authenticator_dispose(
struct wf_impl_authenticator * authenticator)
{
free(authenticator->type);
free(authenticator);
}
bool wf_impl_authenticator_autenticate(
struct wf_impl_authenticator * authenticator,
struct wf_credentials * credentials)
{
bool result;
if (0 == strcmp(authenticator->type, credentials->type))
{
result = authenticator->authenticate(credentials, authenticator->user_data);
}
else
{
result = false;
}
return result;
}

View File

@@ -0,0 +1,42 @@
#ifndef WF_ADAPTER_IMPL_AUTHENTICATOR_H
#define WF_ADAPTER_IMPL_AUTHENTICATOR_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include "webfuse/adapter/authenticate.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_credentials;
struct wf_impl_authenticator
{
char * type;
wf_authenticate_fn * authenticate;
void * user_data;
struct wf_impl_authenticator * next;
};
extern struct wf_impl_authenticator * wf_impl_authenticator_create(
char const * type,
wf_authenticate_fn * authenticate,
void * user_data);
extern void wf_impl_authenticator_dispose(
struct wf_impl_authenticator * authenticator);
extern bool wf_impl_authenticator_autenticate(
struct wf_impl_authenticator * authenticator,
struct wf_credentials * credentials);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,101 @@
#include "webfuse/adapter/impl/authenticators.h"
#include <stddef.h>
#include <string.h>
#include "webfuse/adapter/impl/authenticator.h"
#include "webfuse/adapter/impl/credentials.h"
static struct wf_impl_authenticator * wf_impl_authenticators_find(
struct wf_impl_authenticators * authenticators,
char const * type)
{
struct wf_impl_authenticator * result = NULL;
struct wf_impl_authenticator * actual = authenticators->first;
while ((NULL == result) && (NULL != actual))
{
struct wf_impl_authenticator * next = actual->next;
if (0 == strcmp(type, actual->type))
{
result = actual;
}
actual = next;
}
return result;
}
void wf_impl_authenticators_init(
struct wf_impl_authenticators * authenticators)
{
authenticators->first = NULL;
}
void wf_impl_authenticators_cleanup(
struct wf_impl_authenticators * authenticators)
{
struct wf_impl_authenticator * actual = authenticators->first;
while (NULL != actual)
{
struct wf_impl_authenticator * next = actual->next;
wf_impl_authenticator_dispose(actual);
actual = next;
}
authenticators->first = NULL;
}
void wf_impl_authenticators_clone(
struct wf_impl_authenticators * authenticators,
struct wf_impl_authenticators * other)
{
wf_impl_authenticators_init(other);
struct wf_impl_authenticator * actual = authenticators->first;
while (NULL != actual)
{
struct wf_impl_authenticator * next = actual->next;
wf_impl_authenticators_add(other,
actual->type, actual->authenticate, actual->user_data);
actual = next;
}
}
extern void wf_impl_authenticators_move(
struct wf_impl_authenticators * authenticators,
struct wf_impl_authenticators * other)
{
other->first = authenticators->first;
authenticators->first = NULL;
}
void wf_impl_authenticators_add(
struct wf_impl_authenticators * authenticators,
char const * type,
wf_authenticate_fn * authenticate,
void * user_data)
{
struct wf_impl_authenticator * authenticator = wf_impl_authenticator_create(type, authenticate, user_data);
authenticator->next = authenticators->first;
authenticators->first = authenticator;
}
bool wf_impl_authenticators_authenticate(
struct wf_impl_authenticators * authenticators,
struct wf_credentials * credentials)
{
bool result = (NULL == authenticators->first);
if (NULL != credentials)
{
struct wf_impl_authenticator * authenticator = wf_impl_authenticators_find(authenticators, credentials->type);
if (NULL != authenticator)
{
result = wf_impl_authenticator_autenticate(authenticator, credentials);
}
}
return result;
}

View File

@@ -0,0 +1,51 @@
#ifndef WF_ADAPTER_IMPL_AUTHENTICATORS_H
#define WF_ADAPTER_IMPL_AUTHENTICATORS_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include "webfuse/adapter/authenticate.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_impl_authenticator;
struct wf_credentials;
struct wf_impl_authenticators
{
struct wf_impl_authenticator * first;
};
extern void wf_impl_authenticators_init(
struct wf_impl_authenticators * authenticators);
extern void wf_impl_authenticators_cleanup(
struct wf_impl_authenticators * authenticators);
extern void wf_impl_authenticators_clone(
struct wf_impl_authenticators * authenticators,
struct wf_impl_authenticators * other);
extern void wf_impl_authenticators_move(
struct wf_impl_authenticators * authenticators,
struct wf_impl_authenticators * other);
extern void wf_impl_authenticators_add(
struct wf_impl_authenticators * authenticators,
char const * type,
wf_authenticate_fn * authenticate,
void * user_data);
extern bool wf_impl_authenticators_authenticate(
struct wf_impl_authenticators * authenticators,
struct wf_credentials * credentials);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,40 @@
#include "webfuse/adapter/impl/credentials.h"
#include <string.h>
void wf_impl_credentials_init(
struct wf_credentials * credentials,
char const * type,
json_t * data)
{
credentials->type = strdup(type);
credentials->data = data;
json_incref(credentials->data);
}
void wf_impl_credentials_cleanup(
struct wf_credentials * credentials)
{
free(credentials->type);
json_decref(credentials->data);
}
char const * wf_impl_credentials_type(
struct wf_credentials const * credentials)
{
return credentials->type;
}
char const * wf_impl_credentials_get(
struct wf_credentials const * credentials,
char const * key)
{
char const * result = NULL;
json_t * value_holder = json_object_get(credentials->data, key);
if (json_is_string(value_holder))
{
result = json_string_value(value_holder);
}
return result;
}

View File

@@ -0,0 +1,36 @@
#ifndef WF_ADAPTER_IMPL_CREDENTIALS_H
#define WF_ADAPTER_IMPL_CREDENTIALS_H
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_credentials
{
char * type;
json_t * data;
};
extern void wf_impl_credentials_init(
struct wf_credentials * credentials,
char const * type,
json_t * data);
extern void wf_impl_credentials_cleanup(
struct wf_credentials * credentials);
extern char const * wf_impl_credentials_type(
struct wf_credentials const * credentials);
extern char const * wf_impl_credentials_get(
struct wf_credentials const * credentials,
char const * key);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,83 @@
#include "webfuse/adapter/impl/filesystem.h"
#include "webfuse/adapter/impl/operations.h"
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include <stddef.h>
#include <string.h>
#include <errno.h>
static struct fuse_lowlevel_ops const filesystem_operations =
{
.lookup = &wf_impl_operation_lookup,
.getattr = &wf_impl_operation_getattr,
.readdir = &wf_impl_operation_readdir,
.open = &wf_impl_operation_open,
.release = &wf_impl_operation_close,
.read = &wf_impl_operation_read
};
bool wf_impl_filesystem_init(
struct wf_impl_filesystem * filesystem,
struct wf_impl_jsonrpc_server * rpc,
char * mount_point)
{
bool result = false;
char * argv[] = {"", NULL};
filesystem->args.argc = 1;
filesystem->args.argv = argv;
filesystem->args.allocated = 0;
filesystem->user_data.rpc = rpc;
filesystem->user_data.timeout = 1.0;
memset(&filesystem->buffer, 0, sizeof(struct fuse_buf));
filesystem->session = fuse_session_new(
&filesystem->args,
&filesystem_operations,
sizeof(filesystem_operations),
&filesystem->user_data);
if (NULL != filesystem->session)
{
result = (0 == fuse_session_mount(filesystem->session, mount_point));
}
return result;
}
void wf_impl_filesystem_cleanup(
struct wf_impl_filesystem * filesystem)
{
if (NULL != filesystem->session)
{
fuse_session_reset(filesystem->session);
fuse_session_unmount(filesystem->session);
fuse_session_destroy(filesystem->session);
filesystem->session = NULL;
}
free(filesystem->buffer.mem);
fuse_opt_free_args(&filesystem->args);
}
int wf_impl_filesystem_get_fd(
struct wf_impl_filesystem * filesystem)
{
return fuse_session_fd(filesystem->session);
}
void wf_impl_filesystem_process_request(
struct wf_impl_filesystem * filesystem)
{
int const result = fuse_session_receive_buf(filesystem->session, &filesystem->buffer);
if (0 < result)
{
fuse_session_process_buf(filesystem->session, &filesystem->buffer);
}
else if (-EINTR != result)
{
// ToDo
}
}

View File

@@ -0,0 +1,45 @@
#ifndef WF_ADAPTER_IMPL_FILESYSTEM_H
#define WF_ADAPTER_IMPL_FILESYSTEM_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include "webfuse/adapter/impl/fuse_wrapper.h"
#include "webfuse/adapter/impl/operations.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_impl_jsonrpc_server;
struct wf_impl_filesystem
{
struct fuse_args args;
struct fuse_session * session;
struct fuse_buf buffer;
struct wf_impl_operations_context user_data;
};
extern bool wf_impl_filesystem_init(
struct wf_impl_filesystem * filesystem,
struct wf_impl_jsonrpc_server * rpc,
char * mount_point);
extern void wf_impl_filesystem_cleanup(
struct wf_impl_filesystem * filesystem);
extern int wf_impl_filesystem_get_fd(
struct wf_impl_filesystem * filesystem);
extern void wf_impl_filesystem_process_request(
struct wf_impl_filesystem * filesystem);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,16 @@
#ifndef WF_ADAPTER_IMPL_FUSE_H
#define WF_ADAPTER_IMPL_FUSE_H
#ifdef __cplusplus
extern "C" {
#endif
#define FUSE_USE_VERSION 31
#include <fuse_lowlevel.h>
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,28 @@
#include "webfuse/adapter/impl/jsonrpc/method_intern.h"
#include <stdlib.h>
#include <string.h>
struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create(
char const * name,
wf_impl_jsonrpc_method_invoke_fn * invoke,
void * user_data)
{
struct wf_impl_jsonrpc_method * method = malloc(sizeof(struct wf_impl_jsonrpc_method));
if (NULL != method)
{
method->next = NULL;
method->name = strdup(name);
method->invoke = invoke;
method->user_data = user_data;
}
return method;
}
void wf_impl_jsonrpc_method_dispose(
struct wf_impl_jsonrpc_method * method)
{
free(method->name);
free(method);
}

View File

@@ -0,0 +1,31 @@
#ifndef WF_ADAPTER_IMPL_JSONRPC_METHOD_H
#define WF_ADAPTER_IMPL_JSONRPC_METHOD_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <jansson.h>
#include "webfuse/core/status.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef bool wf_impl_jsonrpc_method_invoke_fn(
void * user_data,
struct json_t const * method_call);
typedef void wf_impl_jsonrpc_method_finished_fn(
void * user_data,
wf_status status,
struct json_t const * result);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,31 @@
#ifndef WF_ADAPTER_IMPL_JSONRPC_METHOD_INTERN_H
#define WF_ADAPTER_IMPL_JSONRPC_METHOD_INTERN_H
#include "webfuse/adapter/impl/jsonrpc/method.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_impl_jsonrpc_method
{
struct wf_impl_jsonrpc_method * next;
char * name;
wf_impl_jsonrpc_method_invoke_fn * invoke;
void * user_data;
};
extern struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create(
char const * name,
wf_impl_jsonrpc_method_invoke_fn * invoke,
void * user_data);
extern void wf_impl_jsonrpc_method_dispose(
struct wf_impl_jsonrpc_method * method);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,44 @@
#include "webfuse/adapter/impl/jsonrpc/request.h"
json_t * wf_impl_jsonrpc_request_create(
char const * method,
int id,
char const * param_info,
va_list args)
{
json_t * request = json_object();
json_object_set_new(request, "method", json_string(method));
json_t * params = json_array();
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
{
switch(*param_type)
{
case 's':
{
char const * const value = va_arg(args, char const *);
json_array_append_new(params, json_string(value));
}
break;
case 'i':
{
int const value = va_arg(args, int);
json_array_append_new(params, json_integer(value));
}
break;
default:
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
exit(EXIT_FAILURE);
break;
}
}
json_object_set_new(request, "params", params);
if (0 != id)
{
json_object_set_new(request, "id", json_integer(id));
}
return request;
}

View File

@@ -0,0 +1,31 @@
#ifndef WF_ADAPTER_IMPL_JSONRPC_REQUEST_H
#define WF_ADAPTER_IMPL_JSONRPC_REQUEST_H
#ifndef __cplusplus
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#else
#include <cstdarg>
#include <cstddef>
using std::size_t;
#endif
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
extern json_t * wf_impl_jsonrpc_request_create(
char const * method,
int id,
char const * param_info,
va_list args);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,59 @@
#include "webfuse/adapter/impl/jsonrpc/response.h"
void wf_impl_jsonrpc_response_init(
struct wf_impl_jsonrpc_response * result,
char const * buffer,
size_t length)
{
result->status = WF_BAD;
result->id = -1;
result->result = NULL;
json_t * response = json_loadb(buffer, length, 0, NULL);
if (NULL == response)
{
result->status = WF_BAD_FORMAT;
return;
}
json_t * id_holder = json_object_get(response, "id");
if ((NULL == id_holder) || (!json_is_integer(id_holder)))
{
result->status = WF_BAD_FORMAT;
json_decref(response);
return;
}
result->status = WF_GOOD;
result->id = json_integer_value(id_holder);
result->result = json_object_get(response, "result");
if (NULL != result->result)
{
json_incref(result->result);
}
else
{
result->status = WF_BAD_FORMAT;
json_t * error = json_object_get(response, "error");
if (NULL != error)
{
json_t * error_code = json_object_get(error, "code");
if ((NULL != error_code) && (json_is_integer(error_code)))
{
result->status = json_integer_value(error_code);
}
}
}
json_decref(response);
}
void wf_impl_jsonrpc_response_cleanup(
struct wf_impl_jsonrpc_response * response)
{
if (NULL != response->result)
{
json_decref(response->result);
}
}

View File

@@ -0,0 +1,38 @@
#ifndef WF_ADAPTER_IMPL_JSONRPC_RESPONSE_H
#define WF_ADAPTER_IMPL_JSONRPC_RESPONSE_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
using std::size_t;
#endif
#include <jansson.h>
#include "webfuse/core/status.h"
#ifdef __cplusplus
extern "C" {
#endif
struct wf_impl_jsonrpc_response
{
wf_status status;
int id;
json_t * result;
};
extern void wf_impl_jsonrpc_response_init(
struct wf_impl_jsonrpc_response * response,
char const * buffer,
size_t buffer_length);
extern void wf_impl_jsonrpc_response_cleanup(
struct wf_impl_jsonrpc_response * response);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,186 @@
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include <string.h>
#include "webfuse/adapter/impl/jsonrpc/method_intern.h"
#include "webfuse/adapter/impl/jsonrpc/request.h"
#include "webfuse/adapter/impl/jsonrpc/response.h"
#define WF_DEFAULT_TIMEOUT (10 * 1000)
static struct wf_impl_jsonrpc_method const * wf_impl_jsonrpc_server_getmethod(
struct wf_impl_jsonrpc_server * server,
char const * name)
{
struct wf_impl_jsonrpc_method * method = server->methods;
while ((NULL != method) && (0 == strcmp(name, method->name)))
{
method = method->next;
}
return method;
}
static void wf_impl_jsonrpc_server_timeout(
struct wf_impl_timer * timer)
{
struct wf_impl_jsonrpc_server * server = timer->user_data;
if (server->request.is_pending)
{
wf_impl_jsonrpc_method_finished_fn * finished = server->request.finished;
void * user_data = server->request.user_data;
server->request.is_pending = false;
server->request.id = 0;
server->request.user_data = NULL;
server->request.finished = NULL;
wf_impl_timer_cancel(&server->request.timer);
finished(user_data, WF_BAD_TIMEOUT, NULL);
}
}
void wf_impl_jsonrpc_server_init(
struct wf_impl_jsonrpc_server * server,
struct wf_impl_timeout_manager * timeout_manager)
{
server->methods = NULL;
server->request.is_pending = false;
wf_impl_timer_init(&server->request.timer, timeout_manager);
}
void wf_impl_jsonrpc_server_cleanup(
struct wf_impl_jsonrpc_server * server)
{
wf_impl_timer_cleanup(&server->request.timer);
if (server->request.is_pending)
{
server->request.finished(server->request.user_data, WF_BAD, NULL);
server->request.is_pending = false;
}
struct wf_impl_jsonrpc_method * method = server->methods;
while (NULL != method)
{
struct wf_impl_jsonrpc_method * next = method->next;
method->next = NULL;
wf_impl_jsonrpc_method_dispose(method);
method = next;
}
server->methods = NULL;
}
void wf_impl_jsonrpc_server_add(
struct wf_impl_jsonrpc_server * server,
char const * name,
wf_impl_jsonrpc_method_invoke_fn * invoke,
void * user_data)
{
struct wf_impl_jsonrpc_method * method = wf_impl_jsonrpc_method_create(name, invoke, user_data);
method->next = server->methods;
server->methods = method;
}
void wf_impl_jsonrpc_server_invoke(
struct wf_impl_jsonrpc_server * server,
wf_impl_jsonrpc_method_finished_fn * finished,
void * user_data,
char const * method_name,
char const * param_info,
...
)
{
if (!server->request.is_pending)
{
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_getmethod(server, method_name);
if (NULL != method)
{
server->request.is_pending = true;
server->request.finished = finished;
server->request.user_data = user_data;
server->request.id = 42;
wf_impl_timer_start(&server->request.timer, wf_impl_timepoint_in_msec(WF_DEFAULT_TIMEOUT),
&wf_impl_jsonrpc_server_timeout, server);
va_list args;
va_start(args, param_info);
json_t * request = wf_impl_jsonrpc_request_create(method_name, server->request.id, param_info, args);
va_end(args);
if (NULL != request)
{
if (!method->invoke(method->user_data, request))
{
server->request.is_pending = false;
server->request.finished = NULL;
server->request.user_data = NULL;
server->request.id = 0;
wf_impl_timer_cancel(&server->request.timer);
finished(user_data, WF_BAD, NULL);
}
json_decref(request);
}
}
else
{
finished(user_data, WF_BAD_NOTIMPLEMENTED, NULL);
}
}
else
{
finished(user_data, WF_BAD_BUSY, NULL);
}
}
extern void wf_impl_jsonrpc_server_notify(
struct wf_impl_jsonrpc_server * server,
char const * method_name,
char const * param_info,
...
)
{
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_getmethod(server, method_name);
if (NULL != method)
{
va_list args;
va_start(args, param_info);
json_t * request = wf_impl_jsonrpc_request_create(method_name, 0, param_info, args);
va_end(args);
if (NULL != request)
{
method->invoke(method->user_data, request);
json_decref(request);
}
}
}
void wf_impl_jsonrpc_server_onresult(
struct wf_impl_jsonrpc_server * server,
char const * message,
size_t length)
{
struct wf_impl_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message, length);
if ((server->request.is_pending) && (response.id == server->request.id))
{
wf_impl_jsonrpc_method_finished_fn * finished = server->request.finished;
void * user_data = server->request.user_data;
server->request.is_pending = false;
server->request.id = 0;
server->request.user_data = NULL;
server->request.finished = NULL;
wf_impl_timer_cancel(&server->request.timer);
finished(user_data, response.status, response.result);
}
wf_impl_jsonrpc_response_cleanup(&response);
}

View File

@@ -0,0 +1,77 @@
#ifndef WF_ADAPTER_IMPL_JSONRPC_SERVER_H
#define WF_ADAPTER_IMPL_JSONRPC_SERVER_H
#ifndef __cplusplus
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#else
#include <cstdarg>
#include <cstddef>
using std::size_t;
#endif
#include <jansson.h>
#include "webfuse/adapter/impl/jsonrpc/method.h"
#include "webfuse/adapter/impl/time/timeout_manager.h"
#include "webfuse/adapter/impl/time/timer.h"
#ifdef __cplusplus
extern "C" {
#endif
struct wf_impl_jsonrpc_request
{
bool is_pending;
wf_impl_jsonrpc_method_finished_fn * finished;
void * user_data;
int id;
struct wf_impl_timer timer;
};
struct wf_impl_jsonrpc_server
{
struct wf_impl_jsonrpc_method * methods;
struct wf_impl_jsonrpc_request request;
};
extern void wf_impl_jsonrpc_server_init(
struct wf_impl_jsonrpc_server * server,
struct wf_impl_timeout_manager * manager);
extern void wf_impl_jsonrpc_server_cleanup(
struct wf_impl_jsonrpc_server * server);
extern void wf_impl_jsonrpc_server_add(
struct wf_impl_jsonrpc_server * server,
char const * name,
wf_impl_jsonrpc_method_invoke_fn * invoke,
void * user_data );
extern void wf_impl_jsonrpc_server_invoke(
struct wf_impl_jsonrpc_server * server,
wf_impl_jsonrpc_method_finished_fn * finished,
void * user_data,
char const * method_name,
char const * param_info,
...
);
extern void wf_impl_jsonrpc_server_notify(
struct wf_impl_jsonrpc_server * server,
char const * method_name,
char const * param_info,
...
);
extern void wf_impl_jsonrpc_server_onresult(
struct wf_impl_jsonrpc_server * server,
char const * message,
size_t length);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,14 @@
#include "webfuse/adapter/impl/jsonrpc/util.h"
int wf_impl_json_get_int(json_t const * object, char const * key, int default_value)
{
int result = default_value;
json_t * holder = json_object_get(object, key);
if ((NULL != holder) && (json_is_integer(holder)))
{
result = json_integer_value(holder);
}
return result;
}

View File

@@ -0,0 +1,17 @@
#ifndef WF_ADAPTER_IMPL_JSON_UTIL_H
#define WF_ADAPTER_IMPL_JSON_UTIL_H
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
extern int wf_impl_json_get_int(json_t const * object, char const * key, int default_value);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,21 @@
#include "webfuse/adapter/impl/operations.h"
#include <limits.h>
#include <errno.h>
#include <jansson.h>
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/core/util.h"
void wf_impl_operation_close(
fuse_req_t request,
fuse_ino_t inode,
struct fuse_file_info * file_info)
{
struct wf_impl_operations_context * user_data = fuse_req_userdata(request);
struct wf_impl_jsonrpc_server * rpc = user_data->rpc;
int handle = (int) (file_info->fh & INT_MAX);
wf_impl_jsonrpc_server_notify(rpc, "close", "iii", inode, handle, file_info->flags);
fuse_reply_err(request, 0);
}

View File

@@ -0,0 +1,93 @@
#include "webfuse/adapter/impl/operations.h"
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/adapter/impl/jsonrpc/util.h"
#include "webfuse/core/util.h"
struct wf_impl_operation_getattr_context
{
fuse_req_t request;
double timeout;
uid_t uid;
gid_t gid;
};
static void wf_impl_operation_getattr_finished(
void * user_data,
wf_status status,
json_t const * data)
{
struct wf_impl_operation_getattr_context * context = user_data;
struct stat buffer;
if (NULL != data)
{
json_t * mode_holder = json_object_get(data, "mode");
json_t * type_holder = json_object_get(data, "type");
if ((NULL != mode_holder) && (json_is_integer(mode_holder)) &&
(NULL != type_holder) && (json_is_string(type_holder)))
{
memset(&buffer, 0, sizeof(struct stat));
buffer.st_mode = json_integer_value(mode_holder) & 0555;
char const * type = json_string_value(type_holder);
if (0 == strcmp("file", type))
{
buffer.st_mode |= S_IFREG;
}
else if (0 == strcmp("dir", type))
{
buffer.st_mode |= S_IFDIR;
}
buffer.st_uid = context->uid;
buffer.st_gid = context->gid;
buffer.st_nlink = 1;
buffer.st_size = wf_impl_json_get_int(data, "size", 0);
buffer.st_atime = wf_impl_json_get_int(data, "atime", 0);
buffer.st_mtime = wf_impl_json_get_int(data, "mtime", 0);
buffer.st_ctime = wf_impl_json_get_int(data, "ctime", 0);
}
else
{
status = WF_BAD_FORMAT;
}
}
if (WF_GOOD == status)
{
fuse_reply_attr(context->request, &buffer, context->timeout);
}
else
{
fuse_reply_err(context->request, ENOENT);
}
free(context);
}
void wf_impl_operation_getattr (
fuse_req_t request,
fuse_ino_t inode,
struct fuse_file_info * WF_UNUSED_PARAM(file_info))
{
struct fuse_ctx const * context = fuse_req_ctx(request);
struct wf_impl_operations_context * user_data = fuse_req_userdata(request);
struct wf_impl_jsonrpc_server * rpc = user_data->rpc;
struct wf_impl_operation_getattr_context * getattr_context = malloc(sizeof(struct wf_impl_operation_getattr_context));
getattr_context->request = request;
getattr_context->uid = context->uid;
getattr_context->gid = context->gid;
getattr_context->timeout = user_data->timeout;
wf_impl_jsonrpc_server_invoke(rpc, &wf_impl_operation_getattr_finished, getattr_context, "getattr", "i", inode);
}

View File

@@ -0,0 +1,102 @@
#include "webfuse/adapter/impl/operations.h"
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/adapter/impl/jsonrpc/util.h"
#include "webfuse/core/util.h"
struct wf_impl_operation_lookup_context
{
fuse_req_t request;
double timeout;
uid_t uid;
gid_t gid;
};
static void wf_impl_operation_lookup_finished(
void * user_data,
wf_status status,
json_t const * data
)
{
struct wf_impl_operation_lookup_context * context = user_data;
struct fuse_entry_param buffer;
if (NULL != data)
{
json_t * inode_holder = json_object_get(data, "inode");
json_t * mode_holder = json_object_get(data, "mode");
json_t * type_holder = json_object_get(data, "type");
if ((NULL != inode_holder) && (json_is_integer(inode_holder)) &&
(NULL != mode_holder) && (json_is_integer(mode_holder)) &&
(NULL != type_holder) && (json_is_string(type_holder)))
{
memset(&buffer, 0, sizeof(struct stat));
buffer.ino = json_integer_value(inode_holder);
buffer.attr.st_mode = json_integer_value(mode_holder) & 0555;
char const * type = json_string_value(type_holder);
if (0 == strcmp("file", type))
{
buffer.attr.st_mode |= S_IFREG;
}
else if (0 == strcmp("dir", type))
{
buffer.attr.st_mode |= S_IFDIR;
}
buffer.attr_timeout = context->timeout;
buffer.entry_timeout = context->timeout;
buffer.attr.st_uid = context->uid;
buffer.attr.st_gid = context->gid;
buffer.attr.st_nlink = 1;
buffer.attr.st_size = wf_impl_json_get_int(data, "size", 0);
buffer.attr.st_atime = wf_impl_json_get_int(data, "atime", 0);
buffer.attr.st_mtime = wf_impl_json_get_int(data, "mtime", 0);
buffer.attr.st_ctime = wf_impl_json_get_int(data, "ctime", 0);
}
else
{
status = WF_BAD_FORMAT;
}
}
if (WF_GOOD == status)
{
fuse_reply_entry(context->request, &buffer);
}
else
{
fuse_reply_err(context->request, ENOENT);
}
free(context);
}
void wf_impl_operation_lookup (
fuse_req_t request,
fuse_ino_t parent,
char const * name)
{
struct fuse_ctx const * context = fuse_req_ctx(request);
struct wf_impl_operations_context * user_data = fuse_req_userdata(request);
struct wf_impl_jsonrpc_server * rpc = user_data->rpc;
struct wf_impl_operation_lookup_context * lookup_context = malloc(sizeof(struct wf_impl_operation_lookup_context));
lookup_context->request = request;
lookup_context->uid = context->uid;
lookup_context->gid = context->gid;
lookup_context->timeout = user_data->timeout;
wf_impl_jsonrpc_server_invoke(rpc, &wf_impl_operation_lookup_finished, lookup_context, "lookup", "is", (int) (parent & INT_MAX), name);
}

View File

@@ -0,0 +1,53 @@
#include "webfuse/adapter/impl/operations.h"
#include <string.h>
#include <errno.h>
#include <jansson.h>
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/core/util.h"
#include "webfuse/core/status.h"
static void wf_impl_operation_open_finished(
void * user_data,
wf_status status,
json_t const * result)
{
fuse_req_t request = user_data;
struct fuse_file_info file_info;
memset(&file_info, 0, sizeof(struct fuse_file_info));
if (NULL != result)
{
json_t * handle_holder = json_object_get(result, "handle");
if ((NULL != handle_holder) && (json_is_integer(handle_holder)))
{
file_info.fh = json_integer_value(handle_holder);
}
else
{
status = WF_BAD_FORMAT;
}
}
if (WF_GOOD == status)
{
fuse_reply_open(request, &file_info);
}
else
{
fuse_reply_err(request, ENOENT);
}
}
void wf_impl_operation_open(
fuse_req_t request,
fuse_ino_t inode,
struct fuse_file_info * file_info)
{
struct wf_impl_operations_context * user_data = fuse_req_userdata(request);
struct wf_impl_jsonrpc_server * rpc = user_data->rpc;
wf_impl_jsonrpc_server_invoke(rpc, &wf_impl_operation_open_finished, request, "open", "ii", inode, file_info->flags);
}

View File

@@ -0,0 +1,95 @@
#include "webfuse/adapter/impl/operations.h"
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <jansson.h>
#include <libwebsockets.h>
#include "webfuse/adapter/impl/jsonrpc/server.h"
#define WF_MAX_READ_LENGTH 4096
static char * wf_impl_fill_buffer(
char const * data,
char const * format,
size_t count,
wf_status * status)
{
*status = WF_GOOD;
char * buffer = malloc(count + 1);
if ((NULL != buffer) && (0 < count))
{
if (0 == strcmp("identity", format))
{
memcpy(buffer, data, count); /* Flawfinder: ignore */
}
else if (0 == strcmp("base64", format))
{
lws_b64_decode_string(data, buffer, count + 1);
}
else
{
*status = WF_BAD;
}
}
return buffer;
}
static void wf_impl_operation_read_finished(void * user_data, wf_status status, json_t const * data)
{
fuse_req_t request = user_data;
char * buffer = NULL;
size_t length = 0;
if (NULL != data)
{
json_t * data_holder = json_object_get(data, "data");
json_t * format_holder = json_object_get(data, "format");
json_t * count_holder = json_object_get(data, "count");
if (json_is_string(data_holder) &&
json_is_string(format_holder) &&
json_is_integer(count_holder))
{
char const * const data = json_string_value(data_holder);
char const * const format = json_string_value(format_holder);
length = (size_t) json_integer_value(count_holder);
buffer = wf_impl_fill_buffer(data, format, length, &status);
}
else
{
status = WF_BAD_FORMAT;
}
}
if (WF_GOOD == status)
{
fuse_reply_buf(request, buffer, length);
}
else
{
fuse_reply_err(request, ENOENT);
}
free(buffer);
}
void wf_impl_operation_read(
fuse_req_t request,
fuse_ino_t inode,
size_t size,
off_t offset,
struct fuse_file_info * file_info)
{
struct wf_impl_operations_context * user_data = fuse_req_userdata(request);
struct wf_impl_jsonrpc_server * rpc = user_data->rpc;
int const length = (size <= WF_MAX_READ_LENGTH) ? (int) size : WF_MAX_READ_LENGTH;
int handle = (file_info->fh & INT_MAX);
wf_impl_jsonrpc_server_invoke(rpc, &wf_impl_operation_read_finished, request, "read", "iiii", inode, handle, (int) offset, length);
}

View File

@@ -0,0 +1,147 @@
#include "webfuse/adapter/impl/operations.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/core/util.h"
#define WF_DIRBUFFER_INITIAL_SIZE 1024
struct wf_impl_operation_readdir_context
{
fuse_req_t request;
size_t size;
off_t offset;
};
struct wf_impl_dirbuffer
{
char * data;
size_t position;
size_t capacity;
};
static void wf_impl_dirbuffer_init(
struct wf_impl_dirbuffer * buffer)
{
buffer->data = malloc(WF_DIRBUFFER_INITIAL_SIZE);
buffer->position = 0;
buffer->capacity = WF_DIRBUFFER_INITIAL_SIZE;
}
static void wf_impl_dirbuffer_dispose(
struct wf_impl_dirbuffer * buffer)
{
free(buffer->data);
}
static void wf_impl_dirbuffer_add(
fuse_req_t request,
struct wf_impl_dirbuffer * buffer,
char const * name,
fuse_ino_t inode)
{
size_t const size = fuse_add_direntry(request, NULL, 0, name, NULL, 0);
size_t remaining = buffer->capacity - buffer->position;
while (remaining < size)
{
buffer->capacity *= 2;
buffer->data = realloc(buffer->data, buffer->capacity);
remaining = buffer->capacity - buffer->position;
}
struct stat stat_buffer;
memset(&stat_buffer, 0, sizeof(struct stat));
stat_buffer.st_ino = inode;
fuse_add_direntry(request,
&buffer->data[buffer->position], remaining, name,
&stat_buffer, buffer->position + size);
buffer->position += size;
}
static size_t wf_impl_min(size_t a, size_t b)
{
return (a < b) ? a : b;
}
static void wf_impl_operation_readdir_finished(
void * user_data,
wf_status status,
json_t const * result)
{
struct wf_impl_operation_readdir_context * context = user_data;
struct wf_impl_dirbuffer buffer;
wf_impl_dirbuffer_init(&buffer);
if (NULL != result)
{
if (json_is_array(result))
{
bool buffer_full = false;
size_t const count = json_array_size(result);
for(size_t i = 0; (!buffer_full) && (i < count); i++)
{
json_t * entry =json_array_get(result, i);
if (json_is_object(entry))
{
json_t * name_holder = json_object_get(entry, "name");
json_t * inode_holder = json_object_get(entry, "inode");
if ((NULL != name_holder) && (json_is_string(name_holder)) &&
(NULL != inode_holder) && (json_is_integer(inode_holder)))
{
char const * name = json_string_value(name_holder);
fuse_ino_t entry_inode = (fuse_ino_t) json_integer_value(inode_holder);
wf_impl_dirbuffer_add(context->request, &buffer, name, entry_inode);
}
}
}
}
}
if (WF_GOOD == status)
{
if (((size_t) context->offset) < buffer.position)
{
fuse_reply_buf(context->request, &buffer.data[context->offset],
wf_impl_min(buffer.position - context->offset, context->size));
}
else
{
fuse_reply_buf(context->request, NULL, 0);
}
}
else
{
fuse_reply_err(context->request, ENOENT);
}
wf_impl_dirbuffer_dispose(&buffer);
free(context);
}
void wf_impl_operation_readdir (
fuse_req_t request,
fuse_ino_t inode,
size_t size,
off_t offset,
struct fuse_file_info * WF_UNUSED_PARAM(file_info))
{
struct wf_impl_operations_context * user_data = fuse_req_userdata(request);
struct wf_impl_jsonrpc_server * rpc = user_data->rpc;
struct wf_impl_operation_readdir_context * readdir_context = malloc(sizeof(struct wf_impl_operation_readdir_context));
readdir_context->request = request;
readdir_context->size = size;
readdir_context->offset = offset;
wf_impl_jsonrpc_server_invoke(rpc, &wf_impl_operation_readdir_finished, readdir_context, "readdir", "i", inode);
}

View File

@@ -0,0 +1,55 @@
#ifndef WF_ADAPTER_IMPL_OPERATIONS_H
#define WF_ADAPTER_IMPL_OPERATIONS_H
#include "webfuse/adapter/impl/fuse_wrapper.h"
#ifdef __cplusplus
extern "C" {
#endif
struct wf_impl_jsonrpc_server;
struct wf_impl_operations_context
{
struct wf_impl_jsonrpc_server * rpc;
double timeout;
};
extern void wf_impl_operation_lookup (
fuse_req_t req,
fuse_ino_t parent,
char const * name);
extern void wf_impl_operation_getattr (
fuse_req_t request,
fuse_ino_t inode,
struct fuse_file_info *file_info);
extern void wf_impl_operation_readdir (
fuse_req_t request,
fuse_ino_t inode,
size_t size,
off_t offset,
struct fuse_file_info *file_info);
extern void wf_impl_operation_open(
fuse_req_t request,
fuse_ino_t inode,
struct fuse_file_info * file_info);
extern void wf_impl_operation_close(
fuse_req_t request,
fuse_ino_t inode,
struct fuse_file_info * file_info);
extern void wf_impl_operation_read(
fuse_req_t request,
fuse_ino_t ino, size_t size, off_t off,
struct fuse_file_info *fi);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,153 @@
#include "webfuse/adapter/server.h"
#include <stdlib.h>
#include <stdbool.h>
#include <libwebsockets.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse/adapter/impl/server_config.h"
#include "webfuse/adapter/impl/server_protocol.h"
#define WF_DISABLE_LWS_LOG 0
#define WF_SERVER_PROTOCOL_COUNT 3
#define WF_SERVER_TIMEOUT (1 * 1000)
struct wf_server
{
struct wf_server_config config;
struct wf_server_protocol protocol;
struct lws_protocols ws_protocols[WF_SERVER_PROTOCOL_COUNT];
struct lws_context * context;
volatile bool shutdown_requested;
struct lws_http_mount mount;
struct lws_context_creation_info info;
};
static bool wf_impl_server_tls_enabled(
struct wf_server * server)
{
return ((server->config.key_path != NULL) && (server->config.cert_path != NULL));
}
static struct lws_context * wf_impl_server_context_create(
struct wf_server * server)
{
lws_set_log_level(WF_DISABLE_LWS_LOG, NULL);
memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT);
server->ws_protocols[0].name = "http";
server->ws_protocols[0].callback = lws_callback_http_dummy;
server->ws_protocols[1].name = "fs";
wf_impl_server_protocol_init_lws(&server->protocol, &server->ws_protocols[1]);
memset(&server->mount, 0, sizeof(struct lws_http_mount));
server->mount.mount_next = NULL,
server->mount.mountpoint = "/",
server->mount.origin = server->config.document_root,
server->mount.def = "index.html",
server->mount.origin_protocol = LWSMPRO_FILE,
server->mount.mountpoint_len = 1,
memset(&server->info, 0, sizeof(struct lws_context_creation_info));
server->info.port = server->config.port;
server->info.mounts = &server->mount;
server->info.protocols = server->ws_protocols;
server->info.vhost_name = server->config.vhost_name;
server->info.ws_ping_pong_interval = 10;
server->info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
if (NULL == server->config.document_root)
{
// disable http
server->info.protocols = &server->ws_protocols[1];
server->info.mounts = NULL;
}
if (wf_impl_server_tls_enabled(server))
{
server->info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
server->info.ssl_cert_filepath = server->config.cert_path;
server->info.ssl_private_key_filepath = server->config.key_path;
}
struct lws_context * const context = lws_create_context(&server->info);
return context;
}
static bool wf_impl_server_check_mountpoint(
struct wf_server_config * config)
{
bool result = false;
if (NULL != config->mount_point)
{
struct stat info;
int const rc = stat(config->mount_point, &info);
result = ((0 == rc) && (S_ISDIR(info.st_mode)));
if (!result)
{
result = (0 == mkdir(config->mount_point, 0755));
}
}
return result;
}
struct wf_server * wf_impl_server_create(
struct wf_server_config * config)
{
struct wf_server * server = NULL;
if (wf_impl_server_check_mountpoint(config))
{
server = malloc(sizeof(struct wf_server));
if (NULL != server)
{
if (wf_impl_server_protocol_init(&server->protocol, config->mount_point))
{
server->shutdown_requested = false;
wf_impl_server_config_clone(config, &server->config);
wf_impl_authenticators_move(&server->config.authenticators, &server->protocol.authenticators);
server->context = wf_impl_server_context_create(server);
}
else
{
free(server);
server = NULL;
}
}
}
return server;
}
void wf_impl_server_dispose(
struct wf_server * server)
{
lws_context_destroy(server->context);
wf_impl_server_protocol_cleanup(&server->protocol);
wf_impl_server_config_cleanup(&server->config);
free(server);
}
void wf_impl_server_run(
struct wf_server * server)
{
int n = 0;
while ((0 <= n) && (!server->shutdown_requested))
{
n = lws_service(server->context, WF_SERVER_TIMEOUT);
}
}
void wf_impl_server_shutdown(
struct wf_server * server)
{
server->shutdown_requested = true;
}

View File

@@ -0,0 +1,29 @@
#ifndef WF_ADAPTER_IMPL_SERVER_H
#define WF_ADAPTER_IMPL_SERVER_H
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_server;
struct wf_server_config;
extern struct wf_server * wf_impl_server_create(
struct wf_server_config * config);
extern void wf_impl_server_dispose(
struct wf_server * server);
extern void wf_impl_server_run(
struct wf_server * server);
extern void wf_impl_server_shutdown(
struct wf_server * server);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,127 @@
#include "webfuse/adapter/impl/server_config.h"
#include <stdlib.h>
#include <string.h>
static char * wf_impl_server_config_strdup(char const * value)
{
char * result = NULL;
if (NULL != value)
{
result = strdup(value);
}
return result;
}
void wf_impl_server_config_init(
struct wf_server_config * config)
{
memset(config, 0, sizeof(struct wf_server_config));
wf_impl_authenticators_init(&config->authenticators);
}
void wf_impl_server_config_cleanup(
struct wf_server_config * config)
{
wf_impl_authenticators_cleanup(&config->authenticators);
free(config->mount_point);
free(config->document_root);
free(config->key_path);
free(config->cert_path);
free(config->vhost_name);
wf_impl_server_config_init(config);
}
void wf_impl_server_config_clone(
struct wf_server_config * config,
struct wf_server_config * clone)
{
clone->mount_point = wf_impl_server_config_strdup(config->mount_point);
clone->document_root = wf_impl_server_config_strdup(config->document_root);
clone->key_path = wf_impl_server_config_strdup(config->key_path);
clone->cert_path = wf_impl_server_config_strdup(config->cert_path);
clone->vhost_name = wf_impl_server_config_strdup(config->vhost_name);
clone->port = config->port;
wf_impl_authenticators_clone(&config->authenticators, &clone->authenticators);
}
struct wf_server_config * wf_impl_server_config_create(void)
{
struct wf_server_config * config = malloc(sizeof(struct wf_server_config));
if (NULL != config)
{
wf_impl_server_config_init(config);
}
return config;
}
void wf_impl_server_config_dispose(
struct wf_server_config * config)
{
wf_impl_server_config_cleanup(config);
free(config);
}
void wf_impl_server_config_set_mountpoint(
struct wf_server_config * config,
char const * mount_point)
{
free(config->mount_point);
config->mount_point = strdup(mount_point);
}
void wf_impl_server_config_set_documentroot(
struct wf_server_config * config,
char const * document_root)
{
free(config->document_root);
config->document_root = strdup(document_root);
}
void wf_impl_server_config_set_keypath(
struct wf_server_config * config,
char const * key_path)
{
free(config->key_path);
config->key_path = strdup(key_path);
}
void wf_impl_server_config_set_certpath(
struct wf_server_config * config,
char const * cert_path)
{
free(config->cert_path);
config->cert_path = strdup(cert_path);
}
void wf_impl_server_config_set_vhostname(
struct wf_server_config * config,
char const * vhost_name)
{
free(config->vhost_name);
config->vhost_name = strdup(vhost_name);
}
void wf_impl_server_config_set_port(
struct wf_server_config * config,
int port)
{
config->port = port;
}
void wf_impl_server_config_add_authenticator(
struct wf_server_config * config,
char const * type,
wf_authenticate_fn * authenticate,
void * user_data
)
{
wf_impl_authenticators_add(&config->authenticators, type, authenticate, user_data);
}

View File

@@ -0,0 +1,72 @@
#ifndef WF_ADAPTER_IMPL_SERVER_CONFIG_H
#define WF_ADAPTER_IMPL_SERVER_CONFIG_H
#include "webfuse/adapter/impl/authenticators.h"
#ifdef __cplusplus
extern "C" {
#endif
struct wf_server_config
{
char * mount_point;
char * document_root;
char * key_path;
char * cert_path;
char * vhost_name;
int port;
struct wf_impl_authenticators authenticators;
};
extern struct wf_server_config * wf_impl_server_config_create(void);
extern void wf_impl_server_config_dispose(
struct wf_server_config * config);
extern void wf_impl_server_config_init(
struct wf_server_config * config);
extern void wf_impl_server_config_cleanup(
struct wf_server_config * config);
extern void wf_impl_server_config_clone(
struct wf_server_config * config,
struct wf_server_config * clone);
extern void wf_impl_server_config_set_mountpoint(
struct wf_server_config * config,
char const * mount_point);
extern void wf_impl_server_config_set_documentroot(
struct wf_server_config * config,
char const * document_root);
extern void wf_impl_server_config_set_keypath(
struct wf_server_config * config,
char const * key_path);
extern void wf_impl_server_config_set_certpath(
struct wf_server_config * config,
char const * cert_path);
extern void wf_impl_server_config_set_vhostname(
struct wf_server_config * config,
char const * vhost_name);
extern void wf_impl_server_config_set_port(
struct wf_server_config * config,
int port);
extern void wf_impl_server_config_add_authenticator(
struct wf_server_config * config,
char const * type,
wf_authenticate_fn * authenticate,
void * user_data
);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,166 @@
#include "webfuse/adapter/impl/server_protocol.h"
#include <stdlib.h>
#include <libwebsockets.h>
#include "webfuse/core/message.h"
#include "webfuse/core/util.h"
#include "webfuse/adapter/impl/filesystem.h"
static int wf_impl_server_protocol_callback(
struct lws * wsi,
enum lws_callback_reasons reason,
void * WF_UNUSED_PARAM(user),
void * in,
size_t len)
{
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
struct wf_server_protocol * protocol = ws_protocol->user;
wf_impl_timeout_manager_check(&protocol->timeout_manager);
struct wf_impl_session * session = wf_impl_session_manager_get(&protocol->session_manager, wsi);
switch (reason)
{
case LWS_CALLBACK_PROTOCOL_INIT:
{
lws_sock_file_fd_type fd;
fd.filefd = wf_impl_filesystem_get_fd(&protocol->filesystem);
if (!lws_adopt_descriptor_vhost(lws_get_vhost(wsi), LWS_ADOPT_RAW_FILE_DESC, fd, ws_protocol->name, NULL))
{
fprintf(stderr, "error: unable to adopt fd");
}
}
break;
case LWS_CALLBACK_ESTABLISHED:
session = wf_impl_session_manager_add(
&protocol->session_manager,
wsi,
&protocol->authenticators,
&protocol->rpc);
if (NULL != session)
{
wf_impl_session_authenticate(session, NULL);
}
break;
case LWS_CALLBACK_CLOSED:
wf_impl_session_manager_remove(&protocol->session_manager, wsi);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
if (NULL != session)
{
wf_impl_session_onwritable(session);
}
break;
case LWS_CALLBACK_RECEIVE:
if (NULL != session)
{
wf_impl_session_receive(session, in, len);
}
break;
case LWS_CALLBACK_RAW_RX_FILE:
wf_impl_filesystem_process_request(&protocol->filesystem);
break;
default:
break;
}
return 0;
}
static bool wf_impl_server_protocol_invoke(
void * user_data,
json_t const * request)
{
struct wf_server_protocol * protocol = user_data;
struct wf_impl_session * session = &protocol->session_manager.session;
struct wf_message * message = wf_message_create(request);
bool const result = wf_impl_session_send(session, message);
return result;
}
struct wf_server_protocol * wf_impl_server_protocol_create(
char * mount_point)
{
struct wf_server_protocol * protocol = malloc(sizeof(struct wf_server_protocol));
if (NULL != protocol)
{
if (!wf_impl_server_protocol_init(protocol, mount_point))
{
free(protocol);
protocol = NULL;
}
}
return protocol;
}
void wf_impl_server_protocol_dispose(
struct wf_server_protocol * protocol)
{
wf_impl_server_protocol_cleanup(protocol);
free(protocol);
}
void wf_impl_server_protocol_init_lws(
struct wf_server_protocol * protocol,
struct lws_protocols * lws_protocol)
{
lws_protocol->callback = &wf_impl_server_protocol_callback;
lws_protocol->per_session_data_size = 0;
lws_protocol->user = protocol;
}
bool wf_impl_server_protocol_init(
struct wf_server_protocol * protocol,
char * mount_point)
{
wf_impl_timeout_manager_init(&protocol->timeout_manager);
wf_impl_session_manager_init(&protocol->session_manager);
wf_impl_authenticators_init(&protocol->authenticators);
wf_impl_jsonrpc_server_init(&protocol->rpc, &protocol->timeout_manager);
wf_impl_jsonrpc_server_add(&protocol->rpc, "lookup", &wf_impl_server_protocol_invoke, protocol);
wf_impl_jsonrpc_server_add(&protocol->rpc, "getattr", &wf_impl_server_protocol_invoke, protocol);
wf_impl_jsonrpc_server_add(&protocol->rpc, "readdir", &wf_impl_server_protocol_invoke, protocol);
wf_impl_jsonrpc_server_add(&protocol->rpc, "open", &wf_impl_server_protocol_invoke, protocol);
wf_impl_jsonrpc_server_add(&protocol->rpc, "close", &wf_impl_server_protocol_invoke, protocol);
wf_impl_jsonrpc_server_add(&protocol->rpc, "read", &wf_impl_server_protocol_invoke, protocol);
bool const success = wf_impl_filesystem_init(&protocol->filesystem, &protocol->rpc, mount_point);
// cleanup on error
if (!success)
{
wf_impl_jsonrpc_server_cleanup(&protocol->rpc);
wf_impl_authenticators_cleanup(&protocol->authenticators);
wf_impl_timeout_manager_cleanup(&protocol->timeout_manager);
wf_impl_session_manager_cleanup(&protocol->session_manager);
}
return success;
}
void wf_impl_server_protocol_cleanup(
struct wf_server_protocol * protocol)
{
wf_impl_filesystem_cleanup(&protocol->filesystem);
wf_impl_jsonrpc_server_cleanup(&protocol->rpc);
wf_impl_timeout_manager_cleanup(&protocol->timeout_manager);
wf_impl_authenticators_cleanup(&protocol->authenticators);
wf_impl_session_manager_cleanup(&protocol->session_manager);
}
void wf_impl_server_protocol_add_authenticator(
struct wf_server_protocol * protocol,
char const * type,
wf_authenticate_fn * authenticate,
void * user_data)
{
wf_impl_authenticators_add(&protocol->authenticators, type, authenticate, user_data);
}

View File

@@ -0,0 +1,53 @@
#ifndef WF_ADAPTER_IMPL_SERVER_PROTOCOL_H
#define WF_ADAPTER_IMPL_SERVER_PROTOCOL_H
#include "webfuse/adapter/impl/filesystem.h"
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/adapter/impl/time/timeout_manager.h"
#include "webfuse/adapter/impl/authenticators.h"
#include "webfuse/adapter/impl/session_manager.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct lws_protocols;
struct wf_server_protocol
{
struct wf_impl_timeout_manager timeout_manager;
struct wf_impl_filesystem filesystem;
struct wf_impl_jsonrpc_server rpc;
struct wf_impl_authenticators authenticators;
struct wf_impl_session_manager session_manager;
};
extern bool wf_impl_server_protocol_init(
struct wf_server_protocol * protocol,
char * mount_point);
extern void wf_impl_server_protocol_cleanup(
struct wf_server_protocol * protocol);
extern struct wf_server_protocol * wf_impl_server_protocol_create(
char * mount_point);
extern void wf_impl_server_protocol_dispose(
struct wf_server_protocol * protocol);
extern void wf_impl_server_protocol_init_lws(
struct wf_server_protocol * protocol,
struct lws_protocols * lws_protocol);
extern void wf_impl_server_protocol_add_authenticator(
struct wf_server_protocol * protocol,
char const * type,
wf_authenticate_fn * authenticate,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,84 @@
#include "webfuse/adapter/impl/session.h"
#include "webfuse/adapter/impl/authenticators.h"
#include "webfuse/core/message_queue.h"
#include "webfuse/core/message.h"
#include "webfuse/adapter/impl/jsonrpc/server.h"
#include <libwebsockets.h>
#include <stddef.h>
void wf_impl_session_init(
struct wf_impl_session * session,
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_jsonrpc_server * rpc)
{
session->wsi = wsi;
session->is_authenticated = false;
session->authenticators = authenticators;
session->rpc = rpc;
wf_message_queue_init(&session->queue);
}
void wf_impl_session_cleanup(
struct wf_impl_session * session)
{
wf_message_queue_cleanup(&session->queue);
session->is_authenticated = false;
session->wsi = NULL;
session->authenticators = NULL;
session->rpc = NULL;
}
void wf_impl_session_authenticate(
struct wf_impl_session * session,
struct wf_credentials * creds)
{
session->is_authenticated = wf_impl_authenticators_authenticate(session->authenticators, creds);
}
bool wf_impl_session_send(
struct wf_impl_session * session,
struct wf_message * message)
{
bool result = (session->is_authenticated) && (NULL != session->wsi);
if (result)
{
wf_message_queue_push(&session->queue, message);
lws_callback_on_writable(session->wsi);
result = true;
}
else
{
wf_message_dispose(message);
}
return result;
}
void wf_impl_session_onwritable(
struct wf_impl_session * session)
{
if (!wf_message_queue_empty(&session->queue))
{
struct wf_message * message = wf_message_queue_pop(&session->queue);
lws_write(session->wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wf_message_dispose(message);
if (!wf_message_queue_empty(&session->queue))
{
lws_callback_on_writable(session->wsi);
}
}
}
void wf_impl_session_receive(
struct wf_impl_session * session,
char const * data,
size_t length)
{
wf_impl_jsonrpc_server_onresult(session->rpc, data, length);
}

View File

@@ -0,0 +1,64 @@
#ifndef WF_ADAPTER_IMPL_SESSION_H
#define WF_ADAPTER_IMPL_SESSION_H
#ifndef __cplusplus
#include <stdbool.h>
#include <stddef.h>
#else
#include <cstddef>
using std::size_t;
#endif
#include "webfuse/core/message_queue.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct lws;
struct wf_message;
struct wf_credentials;
struct wf_impl_authenticators;
struct wf_impl_jsonrpc_server;
struct wf_impl_session
{
struct lws * wsi;
bool is_authenticated;
struct wf_message_queue queue;
struct wf_impl_authenticators * authenticators;
struct wf_impl_jsonrpc_server * rpc;
};
extern void wf_impl_session_init(
struct wf_impl_session * session,
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_jsonrpc_server * rpc);
extern void wf_impl_session_authenticate(
struct wf_impl_session * session,
struct wf_credentials * creds);
extern bool wf_impl_session_send(
struct wf_impl_session * session,
struct wf_message * message);
extern void wf_impl_session_receive(
struct wf_impl_session * session,
char const * data,
size_t length);
extern void wf_impl_session_onwritable(
struct wf_impl_session * session);
extern void wf_impl_session_cleanup(
struct wf_impl_session * session);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,54 @@
#include "webfuse/adapter/impl/session_manager.h"
#include <stddef.h>
void wf_impl_session_manager_init(
struct wf_impl_session_manager * manager)
{
wf_impl_session_init(&manager->session, NULL, NULL, NULL);
}
void wf_impl_session_manager_cleanup(
struct wf_impl_session_manager * manager)
{
wf_impl_session_cleanup(&manager->session);
}
struct wf_impl_session * wf_impl_session_manager_add(
struct wf_impl_session_manager * manager,
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_jsonrpc_server * rpc)
{
struct wf_impl_session * session = NULL;
if (NULL == manager->session.wsi)
{
session = &manager->session;
wf_impl_session_init(&manager->session, wsi, authenticators, rpc);
}
return session;
}
struct wf_impl_session * wf_impl_session_manager_get(
struct wf_impl_session_manager * manager,
struct lws * wsi)
{
struct wf_impl_session * session = NULL;
if (wsi == manager->session.wsi)
{
session = &manager->session;
}
return session;
}
void wf_impl_session_manager_remove(
struct wf_impl_session_manager * manager,
struct lws * wsi)
{
if (wsi == manager->session.wsi)
{
wf_impl_session_cleanup(&manager->session);
manager->session.wsi = NULL;
}
}

View File

@@ -0,0 +1,46 @@
#ifndef WF_ADAPTER_IMPL_SESSION_MANAGER_H
#define WF_ADAPTER_IMPL_SESSION_MANAGER_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include "webfuse/adapter/impl/session.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct lws;
struct wf_impl_session_manager
{
struct wf_impl_session session;
};
extern void wf_impl_session_manager_init(
struct wf_impl_session_manager * manager);
extern void wf_impl_session_manager_cleanup(
struct wf_impl_session_manager * manager);
extern struct wf_impl_session * wf_impl_session_manager_add(
struct wf_impl_session_manager * manager,
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_jsonrpc_server * rpc);
extern struct wf_impl_session * wf_impl_session_manager_get(
struct wf_impl_session_manager * manager,
struct lws * wsi);
extern void wf_impl_session_manager_remove(
struct wf_impl_session_manager * manager,
struct lws * wsi);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,84 @@
#include "webfuse/adapter/impl/time/timeout_manager_intern.h"
#include <stddef.h>
#include "webfuse/adapter/impl/time/timer_intern.h"
#include "webfuse/adapter/impl/time/timepoint.h"
void wf_impl_timeout_manager_init(
struct wf_impl_timeout_manager * manager)
{
manager->timers = NULL;
}
void wf_impl_timeout_manager_cleanup(
struct wf_impl_timeout_manager * manager)
{
struct wf_impl_timer * timer = manager->timers;
while (NULL != timer)
{
struct wf_impl_timer * next = timer->next;
wf_impl_timer_trigger(timer);
timer = next;
}
manager->timers = NULL;
}
void wf_impl_timeout_manager_check(
struct wf_impl_timeout_manager * manager)
{
struct wf_impl_timer * timer = manager->timers;
while (NULL != timer)
{
struct wf_impl_timer * next = timer->next;
if (wf_impl_timer_is_timeout(timer))
{
wf_impl_timeout_manager_removetimer(manager, timer);
wf_impl_timer_trigger(timer);
}
timer = next;
}
}
void wf_impl_timeout_manager_addtimer(
struct wf_impl_timeout_manager * manager,
struct wf_impl_timer * timer)
{
if (NULL != manager->timers)
{
manager->timers->prev = timer;
}
timer->next = manager->timers;
timer->prev = NULL;
manager->timers = timer;
}
void wf_impl_timeout_manager_removetimer(
struct wf_impl_timeout_manager * manager,
struct wf_impl_timer * timer)
{
struct wf_impl_timer * prev = timer->prev;
struct wf_impl_timer * next = timer->next;
if (NULL != prev)
{
prev->next = next;
}
if (NULL != next)
{
next->prev = prev;
}
if (manager->timers == timer)
{
manager->timers = next;
}
}

View File

@@ -0,0 +1,29 @@
#ifndef WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_H
#define WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_H
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_impl_timer;
struct wf_impl_timeout_manager
{
struct wf_impl_timer * timers;
};
extern void wf_impl_timeout_manager_init(
struct wf_impl_timeout_manager * manager);
extern void wf_impl_timeout_manager_cleanup(
struct wf_impl_timeout_manager * manager);
extern void wf_impl_timeout_manager_check(
struct wf_impl_timeout_manager * manager);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
#ifndef WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_INTERN_H
#define WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_INTERN_H
#include "webfuse/adapter/impl/time/timeout_manager.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wf_impl_timeout_manager_addtimer(
struct wf_impl_timeout_manager * manager,
struct wf_impl_timer * timer);
extern void wf_impl_timeout_manager_removetimer(
struct wf_impl_timeout_manager * manager,
struct wf_impl_timer * timer);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,31 @@
#include "webfuse/adapter/impl/time/timepoint.h"
#include <time.h>
#define WF_MSEC_PER_SEC ((wf_impl_timepoint) 1000)
#define WF_NSEC_PER_MSEC ((wf_impl_timepoint) 1000 * 1000)
wf_impl_timepoint wf_impl_timepoint_now(void)
{
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
wf_impl_timepoint const now = (tp.tv_sec * WF_MSEC_PER_SEC) + (tp.tv_nsec / WF_NSEC_PER_MSEC);
return now;
}
wf_impl_timepoint wf_impl_timepoint_in_msec(wf_impl_timediff value)
{
wf_impl_timepoint const now = wf_impl_timepoint_now();
wf_impl_timepoint result = now + ((wf_impl_timepoint) value);
return result;
}
bool wf_impl_timepoint_is_elapsed(wf_impl_timepoint tp)
{
wf_impl_timepoint const now = wf_impl_timepoint_now();
wf_impl_timediff const diff = (wf_impl_timediff) (tp - now);
return (0 > diff);
}

View File

@@ -0,0 +1,31 @@
#ifndef WF_ADAPTER_IMPL_TIME_TIMEPOINT_H
#define WF_ADAPTER_IMPL_TIME_TIMEPOINT_H
#ifndef __cplusplus
#include <stdbool.h>
#include <inttypes.h>
#else
#include <cinttypes>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
typedef uint64_t wf_impl_timepoint;
typedef int64_t wf_impl_timediff;
extern wf_impl_timepoint wf_impl_timepoint_now(void);
extern wf_impl_timepoint wf_impl_timepoint_in_msec(
wf_impl_timediff value);
extern bool wf_impl_timepoint_is_elapsed(
wf_impl_timepoint timepoint);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,65 @@
#include "webfuse/adapter/impl/time/timer_intern.h"
#include "webfuse/adapter/impl/time/timeout_manager_intern.h"
#include <stddef.h>
#include <string.h>
void wf_impl_timer_init(
struct wf_impl_timer * timer,
struct wf_impl_timeout_manager * manager)
{
timer->manager = manager;
timer->timeout = 0;
timer->timeout_handler = NULL;
timer->user_data = NULL;
timer->prev = NULL;
timer->next = NULL;
}
void wf_impl_timer_cleanup(
struct wf_impl_timer * timer)
{
memset(timer, 0, sizeof(struct wf_impl_timer));
}
void wf_impl_timer_start(
struct wf_impl_timer * timer,
wf_impl_timepoint absolute_timeout,
wf_impl_timer_timeout_fn * handler,
void * user_data)
{
timer->timeout = absolute_timeout;
timer->timeout_handler = handler;
timer->user_data = user_data;
wf_impl_timeout_manager_addtimer(timer->manager, timer);
}
void wf_impl_timer_cancel(
struct wf_impl_timer * timer)
{
wf_impl_timeout_manager_removetimer(timer->manager, timer);
timer->timeout = 0;
timer->timeout_handler = NULL;
timer->user_data = NULL;
}
bool wf_impl_timer_is_timeout(
struct wf_impl_timer * timer)
{
return wf_impl_timepoint_is_elapsed(timer->timeout);
}
void wf_impl_timer_trigger(
struct wf_impl_timer * timer)
{
if (NULL != timer->timeout_handler)
{
timer->prev = NULL;
timer->next = NULL;
timer->timeout_handler(timer);
}
}

View File

@@ -0,0 +1,48 @@
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H
#define WF_ADAPTER_IMPL_TIME_TIMER_H
#include "webfuse/adapter/impl/time/timepoint.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wf_impl_timer;
struct wf_impl_timeout_manager;
typedef void wf_impl_timer_timeout_fn(struct wf_impl_timer * timer);
struct wf_impl_timer
{
struct wf_impl_timeout_manager * manager;
wf_impl_timepoint timeout;
wf_impl_timer_timeout_fn * timeout_handler;
void * user_data;
struct wf_impl_timer * next;
struct wf_impl_timer * prev;
};
extern void wf_impl_timer_init(
struct wf_impl_timer * timer,
struct wf_impl_timeout_manager * manager);
extern void wf_impl_timer_cleanup(
struct wf_impl_timer * timer);
extern void wf_impl_timer_start(
struct wf_impl_timer * timer,
wf_impl_timepoint absolute_timeout,
wf_impl_timer_timeout_fn * handler,
void * user_data);
extern void wf_impl_timer_cancel(
struct wf_impl_timer * timer);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,25 @@
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_INTERN_H
#define WF_ADAPTER_IMPL_TIME_TIMER_INTERN_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include "webfuse/adapter/impl/time/timer.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern bool wf_impl_timer_is_timeout(
struct wf_impl_timer * timer);
extern void wf_impl_timer_trigger(
struct wf_impl_timer * timer);
#ifdef __cplusplus
}
#endif
#endif