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

240
lib/webfuse/provider/api.c Normal file
View File

@@ -0,0 +1,240 @@
#include "webfuse_provider.h"
#include "webfuse/provider/impl/request.h"
#include "webfuse/provider/impl/operation/getattr.h"
#include "webfuse/provider/impl/operation/lookup.h"
#include "webfuse/provider/impl/operation/readdir.h"
#include "webfuse/provider/impl/operation/open.h"
#include "webfuse/provider/impl/operation/close.h"
#include "webfuse/provider/impl/operation/read.h"
#include "webfuse/provider/impl/client_protocol.h"
#include "webfuse/provider/impl/client_config.h"
#include "webfuse/provider/impl/client.h"
#include "webfuse/provider/impl/dirbuffer.h"
// respond
void wfp_respond_error(
struct wfp_request * request,
wf_status status)
{
wfp_impl_respond_error(request, status);
}
void wfp_respond_getattr(
struct wfp_request * request,
struct stat const * stat)
{
wfp_impl_respond_getattr(request, stat);
}
void wfp_respond_lookup(
struct wfp_request * request,
struct stat const * stat)
{
wfp_impl_respond_lookup(request, stat);
}
void wfp_respond_open(
struct wfp_request * request,
uint32_t handle)
{
wfp_impl_respond_open(request, handle);
}
void wfp_respond_read(
struct wfp_request * request,
char const * data,
size_t length)
{
wfp_impl_respond_read(request, data, length);
}
void wfp_respond_readdir(
struct wfp_request * request,
struct wfp_dirbuffer * dirbuffer)
{
wfp_impl_respond_readdir(request, dirbuffer);
}
// config
struct wfp_client_config * wfp_client_config_create(void)
{
return wfp_impl_client_config_create();
}
void wfp_client_config_dispose(
struct wfp_client_config * config)
{
wfp_impl_client_config_dispose(config);
}
void wfp_client_config_set_userdata(
struct wfp_client_config * config,
void * user_data)
{
wfp_impl_client_config_set_userdata(config, user_data);
}
void wfp_client_config_set_keypath(
struct wfp_client_config * config,
char const * key_path)
{
wfp_impl_client_config_set_keypath(config, key_path);
}
void wfp_client_config_set_certpath(
struct wfp_client_config * config,
char const * cert_path)
{
wfp_impl_client_config_set_certpath(config, cert_path);
}
void wfp_client_config_set_onconnected(
struct wfp_client_config * config,
wfp_connected_fn * handler)
{
wfp_impl_client_config_set_onconnected(config, handler);
}
void wfp_client_config_set_ondisconnected(
struct wfp_client_config * config,
wfp_disconnected_fn * handler)
{
wfp_impl_client_config_set_ondisconnected(config, handler);
}
void wfp_client_config_set_ontimer(
struct wfp_client_config * config,
wfp_ontimer_fn * handler)
{
wfp_impl_client_config_set_ontimer(config, handler);
}
void wfp_client_config_set_onlookup(
struct wfp_client_config * config,
wfp_lookup_fn * handler)
{
wfp_impl_client_config_set_onlookup(config, handler);
}
void wfp_client_config_set_ongetattr(
struct wfp_client_config * config,
wfp_getattr_fn * handler)
{
wfp_impl_client_config_set_ongetattr(config, handler);
}
void wfp_client_config_set_onreaddir(
struct wfp_client_config * config,
wfp_readdir_fn * handler)
{
wfp_impl_client_config_set_onreaddir(config, handler);
}
void wfp_client_config_set_onopen(
struct wfp_client_config * config,
wfp_open_fn * handler)
{
wfp_impl_client_config_set_onopen(config, handler);
}
void wfp_client_config_set_onclose(
struct wfp_client_config * config,
wfp_close_fn * handler)
{
wfp_impl_client_config_set_onclose(config, handler);
}
void wfp_client_config_set_onread(
struct wfp_client_config * config,
wfp_read_fn * handler)
{
wfp_impl_client_config_set_onread(config, handler);
}
// protocol
struct wfp_client_protocol * wfp_client_protocol_create(
struct wfp_provider const * provider,
void * user_data)
{
return wfp_impl_client_protocol_create(provider, user_data);
}
void wfp_client_protocol_dispose(
struct wfp_client_protocol * protocol)
{
wfp_impl_client_protocol_dispose(protocol);
}
void wfp_client_protocol_init_lws(
struct wfp_client_protocol * protocol,
struct lws_protocols * lws_protocol)
{
wfp_impl_client_protocol_init_lws(protocol, lws_protocol);
}
// client
struct wfp_client * wfp_client_create(
struct wfp_client_config * config)
{
return wfp_impl_client_create(config);
}
void wfp_client_connect(
struct wfp_client * client,
char const * url)
{
wfp_impl_client_connect(client, url);
}
void wfp_client_disconnect(
struct wfp_client * client)
{
wfp_impl_client_disconnect(client);
}
void wfp_client_dispose(
struct wfp_client * client)
{
wfp_impl_client_dispose(client);
}
void wfp_client_run(
struct wfp_client * client)
{
wfp_impl_client_run(client);
}
void wfp_client_shutdown(
struct wfp_client * client)
{
wfp_impl_client_shutdown(client);
}
// dirbuffer
struct wfp_dirbuffer * wfp_dirbuffer_create(void)
{
return wfp_impl_dirbuffer_create();
}
void wfp_dirbuffer_dispose(
struct wfp_dirbuffer * buffer)
{
wfp_impl_dirbuffer_dispose(buffer);
}
void wfp_dirbuffer_add(
struct wfp_dirbuffer * buffer,
char const * name,
ino_t inode)
{
wfp_impl_dirbuffer_add(buffer, name, inode);
}

View File

@@ -0,0 +1,119 @@
#include "webfuse/provider/impl/client.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <libwebsockets.h>
#include "webfuse/provider/impl/provider.h"
#include "webfuse/provider/impl/client_protocol.h"
#include "webfuse/provider/impl/client_config.h"
#include "webfuse/provider/impl/url.h"
#define WFP_PROTOCOL ("fs")
#define WFP_DISABLE_LWS_LOG 0
#define WFP_CLIENT_PROTOCOL_COUNT 2
#define WFP_CLIENT_TIMEOUT (1 * 1000)
struct wfp_client
{
volatile bool is_running;
struct wfp_client_protocol protocol;
struct lws_context_creation_info info;
struct lws_protocols protocols[WFP_CLIENT_PROTOCOL_COUNT];
struct lws_context * context;
char * key_path;
char * cert_path;
};
struct wfp_client * wfp_impl_client_create(
struct wfp_client_config * config)
{
lws_set_log_level(WFP_DISABLE_LWS_LOG, NULL);
struct wfp_client * client = malloc(sizeof(struct wfp_client));
if (NULL != client)
{
client->is_running = true;
wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data);
memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT);
client->protocols[0].name = "fs";
wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]);
memset(&client->info, 0, sizeof(struct lws_context_creation_info));
client->info.port = CONTEXT_PORT_NO_LISTEN;
client->info.protocols = client->protocols;
client->info.uid = -1;
client->info.gid = -1;
if ((NULL != config->cert_path) && (NULL != config->key_path))
{
}
client->context = lws_create_context(&client->info);
}
return client;
}
void wfp_impl_client_dispose(
struct wfp_client * client)
{
lws_context_destroy(client->context);
wfp_impl_client_protocol_cleanup(&client->protocol);
free(client);
}
void wfp_impl_client_connect(
struct wfp_client * client,
char const * url)
{
struct wfp_impl_url url_data;
bool const success = wfp_impl_url_init(&url_data, url);
if (success)
{
struct lws_client_connect_info info;
memset(&info, 0, sizeof(struct lws_client_connect_info));
info.context = client->context;
info.port = url_data.port;
info.address = url_data.host;
info.path = url_data.path;
info.host = info.address;
info.origin = info.address;
info.ssl_connection = (url_data.use_tls) ? LCCSCF_USE_SSL : 0;
info.protocol = WFP_PROTOCOL;
info.pwsi = &client->protocol.wsi;
lws_client_connect_via_info(&info);
wfp_impl_url_cleanup(&url_data);
}
}
void wfp_impl_client_disconnect(
struct wfp_client * client)
{
(void) client;
// ToDo: implement me
}
void wfp_impl_client_run(
struct wfp_client * client)
{
while (client->is_running)
{
lws_service(client->context, WFP_CLIENT_TIMEOUT);
}
}
void wfp_impl_client_shutdown(
struct wfp_client * client)
{
client->is_running = false;
}

View File

@@ -0,0 +1,49 @@
#ifndef WF_PROVIDER_IMPL_CLIENT_H
#define WF_PROVIDER_IMPL_CLIENT_H
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_client;
struct wfp_client_config;
extern struct wfp_client * wfp_impl_client_create(
struct wfp_client_config * config);
extern void wfp_impl_client_set_keypath(
struct wfp_client * client,
char * key_path);
extern void wfp_impl_client_set_certpath(
struct wfp_client * client,
char * cert_path);
extern void wfp_impl_client_connect(
struct wfp_client * client,
char const * url);
extern void wfp_impl_client_disconnect(
struct wfp_client * client);
extern void wfp_impl_client_settimeout(
struct wfp_client * client,
unsigned int timepoint);
extern void wfp_impl_client_dispose(
struct wfp_client * client);
extern void wfp_impl_client_run(
struct wfp_client * client);
extern void wfp_impl_client_shutdown(
struct wfp_client * client);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,112 @@
#include "webfuse/provider/impl/client_config.h"
#include "webfuse/provider/impl/provider.h"
#include <stdlib.h>
#include <string.h>
struct wfp_client_config * wfp_impl_client_config_create(void)
{
struct wfp_client_config * config = malloc(sizeof(struct wfp_client_config));
if (NULL != config)
{
wfp_impl_provider_init(&config->provider);
config->user_data = NULL;
config->key_path = NULL;
config->cert_path = NULL;
}
return config;
}
void wfp_impl_client_config_dispose(
struct wfp_client_config * config)
{
free(config->key_path);
free(config->cert_path);
free(config);
}
void wfp_impl_client_config_set_userdata(
struct wfp_client_config * config,
void * user_data)
{
config->user_data = user_data;
}
void wfp_impl_client_config_set_keypath(
struct wfp_client_config * config,
char const * key_path)
{
free(config->key_path);
config->key_path = strdup(key_path);
}
void wfp_impl_client_config_set_certpath(
struct wfp_client_config * config,
char const * cert_path)
{
free(config->cert_path);
config->cert_path = strdup(cert_path);
}
void wfp_impl_client_config_set_onconnected(
struct wfp_client_config * config,
wfp_connected_fn * handler)
{
config->provider.connected = handler;
}
void wfp_impl_client_config_set_ondisconnected(
struct wfp_client_config * config,
wfp_disconnected_fn * handler)
{
config->provider.disconnected = handler;
}
void wfp_impl_client_config_set_ontimer(
struct wfp_client_config * config,
wfp_ontimer_fn * handler)
{
config->provider.ontimer = handler;
}
void wfp_impl_client_config_set_onlookup(
struct wfp_client_config * config,
wfp_lookup_fn * handler)
{
config->provider.lookup = handler;
}
void wfp_impl_client_config_set_ongetattr(
struct wfp_client_config * config,
wfp_getattr_fn * handler)
{
config->provider.getattr = handler;
}
void wfp_impl_client_config_set_onreaddir(
struct wfp_client_config * config,
wfp_readdir_fn * handler)
{
config->provider.readdir = handler;
}
void wfp_impl_client_config_set_onopen(
struct wfp_client_config * config,
wfp_open_fn * handler)
{
config->provider.open = handler;
}
void wfp_impl_client_config_set_onclose(
struct wfp_client_config * config,
wfp_close_fn * handler)
{
config->provider.close = handler;
}
void wfp_impl_client_config_set_onread(
struct wfp_client_config * config,
wfp_read_fn * handler)
{
config->provider.read = handler;
}

View File

@@ -0,0 +1,77 @@
#ifndef WF_PROVIDER_IMPL_CLIENT_CONFIG_H
#define WF_PROVIDER_IMPL_CLIENT_CONFIG_H
#include "webfuse/provider/client_config.h"
#include "webfuse/provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_client_config
{
struct wfp_provider provider;
void * user_data;
char * key_path;
char * cert_path;
};
extern struct wfp_client_config * wfp_impl_client_config_create(void);
extern void wfp_impl_client_config_dispose(
struct wfp_client_config * config);
extern void wfp_impl_client_config_set_userdata(
struct wfp_client_config * config,
void * user_data);
extern void wfp_impl_client_config_set_keypath(
struct wfp_client_config * config,
char const * key_path);
extern void wfp_impl_client_config_set_certpath(
struct wfp_client_config * config,
char const * cert_path);
extern void wfp_impl_client_config_set_onconnected(
struct wfp_client_config * config,
wfp_connected_fn * handler);
extern void wfp_impl_client_config_set_ondisconnected(
struct wfp_client_config * config,
wfp_disconnected_fn * handler);
extern void wfp_impl_client_config_set_ontimer(
struct wfp_client_config * config,
wfp_ontimer_fn * handler);
extern void wfp_impl_client_config_set_onlookup(
struct wfp_client_config * config,
wfp_lookup_fn * handler);
extern void wfp_impl_client_config_set_ongetattr(
struct wfp_client_config * config,
wfp_getattr_fn * handler);
extern void wfp_impl_client_config_set_onreaddir(
struct wfp_client_config * config,
wfp_readdir_fn * handler);
extern void wfp_impl_client_config_set_onopen(
struct wfp_client_config * config,
wfp_open_fn * handler);
extern void wfp_impl_client_config_set_onclose(
struct wfp_client_config * config,
wfp_close_fn * handler);
extern void wfp_impl_client_config_set_onread(
struct wfp_client_config * config,
wfp_read_fn * handler);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,149 @@
#include "webfuse/provider/impl/client_protocol.h"
#include <stdlib.h>
#include <string.h>
#include <libwebsockets.h>
#include <jansson.h>
#include "webfuse/provider/impl/provider.h"
#include "webfuse/core/util.h"
#include "webfuse/core/message.h"
static void wfp_impl_client_protocol_respond(
json_t * response,
void * user_data)
{
struct wfp_client_protocol * protocol = (struct wfp_client_protocol *) user_data;
struct wf_message * message = wf_message_create(response);
if (NULL != message)
{
wf_message_queue_push(&protocol->queue, message);
lws_callback_on_writable(protocol->wsi);
}
}
static void wfp_impl_client_protocol_process_request(
struct wfp_client_protocol * protocol,
char const * message,
size_t length)
{
json_t * request = json_loadb(message, length, 0, NULL);
if (NULL != request)
{
struct wfp_impl_invokation_context context =
{
.provider = &protocol->provider,
.user_data = protocol->user_data,
.request = &protocol->request
};
wfp_impl_provider_invoke(&context, request);
json_decref(request);
}
}
static int wfp_impl_client_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 wfp_client_protocol * protocol = (NULL != ws_protocol) ? ws_protocol->user: NULL;
if (NULL != protocol)
{
switch (reason)
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
protocol->provider.connected(protocol->user_data);
break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
protocol->provider.disconnected(protocol->user_data);
break;
case LWS_CALLBACK_CLIENT_CLOSED:
protocol->provider.connected(protocol->user_data);
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
wfp_impl_client_protocol_process_request(protocol, in, len);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
// fall-through
case LWS_CALLBACK_CLIENT_WRITEABLE:
if ((wsi == protocol->wsi) && (!wf_message_queue_empty(&protocol->queue)))
{
struct wf_message * message = wf_message_queue_pop(&protocol->queue);
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wf_message_dispose(message);
if (!wf_message_queue_empty(&protocol->queue))
{
lws_callback_on_writable(wsi);
}
}
break;
default:
break;
}
}
return 0;
}
void wfp_impl_client_protocol_init(
struct wfp_client_protocol * protocol,
struct wfp_provider const * provider,
void * user_data)
{
wf_message_queue_init(&protocol->queue);
protocol->wsi = NULL;
protocol->request.respond = &wfp_impl_client_protocol_respond;
protocol->request.user_data = protocol;
protocol->user_data = user_data;
wfp_impl_provider_init_from_prototype(&protocol->provider, provider);
}
void wfp_impl_client_protocol_cleanup(
struct wfp_client_protocol * protocol)
{
wf_message_queue_cleanup(&protocol->queue);
}
struct wfp_client_protocol * wfp_impl_client_protocol_create(
struct wfp_provider const * provider,
void * user_data)
{
struct wfp_client_protocol * protocol = malloc(sizeof(struct wfp_client_protocol));
if (NULL != protocol)
{
wfp_impl_client_protocol_init(protocol, provider, user_data);
}
return protocol;
}
void wfp_impl_client_protocol_dispose(
struct wfp_client_protocol * protocol)
{
wfp_impl_client_protocol_cleanup(protocol);
free(protocol);
}
void wfp_impl_client_protocol_init_lws(
struct wfp_client_protocol * protocol,
struct lws_protocols * lws_protocol)
{
lws_protocol->callback = &wfp_impl_client_protocol_callback;
lws_protocol->per_session_data_size = 0;
lws_protocol->user = protocol;
}

View File

@@ -0,0 +1,49 @@
#ifndef WF_PROVIDER_IMPL_CLIENT_PROTOCOL_H
#define WF_PROVIDER_IMPL_CLIENT_PROTOCOL_H
#include "webfuse/provider/impl/provider.h"
#include "webfuse/provider/impl/request.h"
#include "webfuse/core/message_queue.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_provider;
struct lws_protocols;
struct wfp_client_protocol
{
struct wfp_request request;
struct wfp_provider provider;
void * user_data;
struct lws * wsi;
struct wf_message_queue queue;
};
extern void wfp_impl_client_protocol_init(
struct wfp_client_protocol * protocol,
struct wfp_provider const * provider,
void * user_data);
extern void wfp_impl_client_protocol_cleanup(
struct wfp_client_protocol * protocol);
extern struct wfp_client_protocol * wfp_impl_client_protocol_create(
struct wfp_provider const * provider,
void * user_data);
extern void wfp_impl_client_protocol_dispose(
struct wfp_client_protocol * protocol);
extern void wfp_impl_client_protocol_init_lws(
struct wfp_client_protocol * protocol,
struct lws_protocols * lws_protocol);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,45 @@
#include "webfuse/provider/impl/dirbuffer.h"
#include <stdlib.h>
struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void)
{
struct wfp_dirbuffer * buffer = malloc(sizeof(struct wfp_dirbuffer));
if (NULL != buffer)
{
buffer->entries = json_array();
}
return buffer;
}
void wfp_impl_dirbuffer_dispose(
struct wfp_dirbuffer * buffer)
{
if (NULL != buffer->entries)
{
json_decref(buffer->entries);
}
free(buffer);
}
void wfp_impl_dirbuffer_add(
struct wfp_dirbuffer * buffer,
char const * name,
ino_t inode)
{
json_t * entry = json_object();
json_object_set_new(entry, "name", json_string(name));
json_object_set_new(entry, "inode", json_integer(inode));
json_array_append_new(buffer->entries, entry);
}
json_t * wfp_impl_dirbuffer_take(
struct wfp_dirbuffer * buffer)
{
json_t * entries = buffer->entries;
buffer->entries = NULL;
return entries;
}

View File

@@ -0,0 +1,37 @@
#ifndef WF_PROVIDER_IMPL_DIRBUFFER_H
#define WF_PROVIDER_IMPL_DIRBUFFER_H
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_dirbuffer
{
json_t * entries;
};
extern struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void);
extern void wfp_impl_dirbuffer_dispose(
struct wfp_dirbuffer * buffer);
extern void wfp_impl_dirbuffer_add(
struct wfp_dirbuffer * buffer,
char const * name,
ino_t inode);
extern json_t * wfp_impl_dirbuffer_take(
struct wfp_dirbuffer * buffer);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,38 @@
#include "webfuse/provider/impl/operation/close.h"
#include <limits.h>
#include "webfuse/core/util.h"
void wfp_impl_close(
struct wfp_impl_invokation_context * context,
json_t * params,
int WF_UNUSED_PARAM(id))
{
size_t const param_count = json_array_size(params);
if (3 == param_count)
{
json_t * inode_holder = json_array_get(params, 0);
json_t * handle_holder = json_array_get(params, 1);
json_t * flags_holder = json_array_get(params, 2);
if (json_is_integer(inode_holder) &&
json_is_integer(handle_holder) &&
json_is_integer(flags_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
uint32_t handle = (uint32_t) (json_integer_value(handle_holder) & UINT32_MAX);
int flags = json_integer_value(flags_holder);
context->provider->close(inode, handle, flags, context->user_data);
}
}
}
void wfp_impl_close_default(
ino_t WF_UNUSED_PARAM(inode),
uint32_t WF_UNUSED_PARAM(handle),
int WF_UNUSED_PARAM(flags),
void * WF_UNUSED_PARAM(user_data))
{
// empty
}

View File

@@ -0,0 +1,26 @@
#ifndef WF_PROVIDER_IMPL_OPERATION_CLOSE_H
#define WF_PROVIDER_IMPL_OPERATION_CLOSE_H
#include "webfuse/provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_close(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_close_default(
ino_t inode,
uint32_t handle,
int flags,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,22 @@
#ifndef WFP_OPERATION_IMPL_ERROR_H
#define WFP_OPERATION_IMPL_ERROR_H
#include "webfuse/provider/api.h"
#include "webfuse/core/status.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_request;
extern WFP_API void wfp_impl_respond_error(
struct wfp_request * request,
wf_status status);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,64 @@
#include "webfuse/provider/impl/operation/getattr.h"
#include <stdbool.h>
#include "webfuse/provider/impl/operation/error.h"
#include "webfuse/provider/impl/request.h"
#include "webfuse/core/util.h"
void wfp_impl_getattr(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (1 == count)
{
json_t * inode_holder = json_array_get(params, 0);
if (json_is_integer(inode_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->getattr(request, inode, context->user_data);
}
}
}
void wfp_impl_getattr_default(
struct wfp_request * request,
ino_t WF_UNUSED_PARAM(inode),
void * WF_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
}
void wfp_impl_respond_getattr(
struct wfp_request * request,
struct stat const * stat)
{
bool const is_file = (0 != (stat->st_mode & S_IFREG));
bool const is_dir = (0 != (stat->st_mode & S_IFDIR));
json_t * result = json_object();
json_object_set_new(result, "inode", json_integer(stat->st_ino));
json_object_set_new(result, "mode", json_integer(stat->st_mode & 0777));
json_object_set_new(result, "atime", json_integer(stat->st_atime));
json_object_set_new(result, "mtime", json_integer(stat->st_mtime));
json_object_set_new(result, "ctime", json_integer(stat->st_ctime));
if (is_file)
{
json_object_set_new(result, "type", json_string("file"));
json_object_set_new(result, "size", json_integer(stat->st_size));
}
if (is_dir)
{
json_object_set_new(result, "type", json_string("dir"));
}
wfp_impl_respond(request, result);
}

View File

@@ -0,0 +1,29 @@
#ifndef WF_PROVIDER_IMPL_OPERATION_GETATTR_H
#define WF_PROVIDER_IMPL_OPERATION_GETATTR_H
#include "webfuse/provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_getattr(
struct wfp_request * request,
struct stat const * stat);
extern void wfp_impl_getattr(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_getattr_default(
struct wfp_request * request,
ino_t inode,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,68 @@
#include "webfuse/provider/impl/operation/lookup.h"
#include <stdbool.h>
#include "webfuse/provider/impl/operation/error.h"
#include "webfuse/provider/impl/request.h"
#include "webfuse/core/util.h"
void wfp_impl_lookup(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (2 == count)
{
json_t * inode_holder = json_array_get(params, 0);
json_t * name_holder = json_array_get(params, 1);
if (json_is_integer(inode_holder) &&
json_is_string(name_holder))
{
ino_t inode = json_integer_value(inode_holder);
char const * name = json_string_value(name_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->lookup(request, inode, name, context->user_data);
}
}
}
void wfp_impl_respond_lookup(
struct wfp_request * request,
struct stat const * stat)
{
bool const is_file = (0 != (stat->st_mode & S_IFREG));
bool const is_dir = (0 != (stat->st_mode & S_IFDIR));
json_t * result = json_object();
json_object_set_new(result, "inode", json_integer(stat->st_ino));
json_object_set_new(result, "mode", json_integer(stat->st_mode & 0777));
json_object_set_new(result, "atime", json_integer(stat->st_atime));
json_object_set_new(result, "mtime", json_integer(stat->st_mtime));
json_object_set_new(result, "ctime", json_integer(stat->st_ctime));
if (is_file)
{
json_object_set_new(result, "type", json_string("file"));
json_object_set_new(result, "size", json_integer(stat->st_size));
}
if (is_dir)
{
json_object_set_new(result, "type", json_string("dir"));
}
wfp_impl_respond(request, result);
}
void wfp_impl_lookup_default(
struct wfp_request * request,
ino_t WF_UNUSED_PARAM(parent),
char const * WF_UNUSED_PARAM(name),
void * WF_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
}

View File

@@ -0,0 +1,30 @@
#ifndef WF_PROVIDER_IMPL_OPERATION_LOOKUP_H
#define WF_PROVIDER_IMPL_OPERATION_LOOKUP_H
#include "webfuse/provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_lookup(
struct wfp_request * request,
struct stat const * stat);
extern void wfp_impl_lookup(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_lookup_default(
struct wfp_request * request,
ino_t parent,
char const * name,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,47 @@
#include "webfuse/provider/impl/operation/open.h"
#include "webfuse/provider/impl/operation/error.h"
#include "webfuse/provider/impl/request.h"
#include "webfuse/core/util.h"
void wfp_impl_open(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (2 == count)
{
json_t * inode_holder = json_array_get(params, 0);
json_t * flags_holder = json_array_get(params, 1);
if (json_is_integer(inode_holder) &&
json_is_integer(flags_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
int flags = (ino_t) json_integer_value(flags_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->open(request, inode, flags, context->user_data); /* Flawfinder: ignore */
}
}
}
void wfp_impl_open_default(
struct wfp_request * request,
ino_t WF_UNUSED_PARAM(inode),
int WF_UNUSED_PARAM(flags),
void * WF_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
}
void wfp_impl_respond_open(
struct wfp_request * request,
uint32_t handle)
{
json_t * result = json_object();
json_object_set_new(result, "handle", json_integer((int) handle));
wfp_impl_respond(request, result);
}

View File

@@ -0,0 +1,30 @@
#ifndef WF_PROVIDER_IMPL_OPERATION_OPEN_H
#define WF_PROVIDER_IMPL_OPERATION_OPEN_H
#include "webfuse/provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_open(
struct wfp_request * request,
uint32_t handle);
extern void wfp_impl_open(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_open_default(
struct wfp_request * request,
ino_t inode,
int flags,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,85 @@
#include "webfuse/provider/impl/operation/read.h"
#include <stdlib.h>
#include <libwebsockets.h>
#include "webfuse/provider/impl/operation/error.h"
#include "webfuse/provider/impl/request.h"
#include "webfuse/core/util.h"
void wfp_impl_read(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (4 == count)
{
json_t * inode_holder = json_array_get(params, 0);
json_t * handle_holder = json_array_get(params, 1);
json_t * offset_holder = json_array_get(params, 2);
json_t * length_holder = json_array_get(params, 3);
if (json_is_integer(inode_holder) &&
json_is_integer(handle_holder) &&
json_is_integer(offset_holder) &&
json_is_integer(length_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
int handle = json_integer_value(handle_holder);
size_t offset = json_integer_value(offset_holder);
size_t length = json_integer_value(length_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->read(request, inode, handle, offset, length, context->user_data); /* Flawfinder: ignore */
}
}
}
void wfp_impl_read_default(
struct wfp_request * request,
ino_t WF_UNUSED_PARAM(inode),
uint32_t WF_UNUSED_PARAM(handle),
size_t WF_UNUSED_PARAM(offset),
size_t WF_UNUSED_PARAM(length),
void * WF_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
}
void wfp_impl_respond_read(
struct wfp_request * request,
char const * data,
size_t length)
{
if (0 < length)
{
size_t const size = 4 * ((length / 3) + 2);
char * buffer = malloc(size);
if (NULL != buffer)
{
lws_b64_encode_string(data, length, buffer, size);
json_t * result = json_object();
json_object_set_new(result, "data", json_string(buffer));
json_object_set_new(result, "format", json_string("base64"));
json_object_set_new(result, "count", json_integer((int) length));
wfp_impl_respond(request, result);
free(buffer);
}
else
{
wfp_impl_respond_error(request, WF_BAD);
}
}
else
{
json_t * result = json_object();
json_object_set_new(result, "data", json_string(""));
json_object_set_new(result, "format", json_string("identitiy"));
json_object_set_new(result, "count", json_integer(0));
wfp_impl_respond(request, result);
}
}

View File

@@ -0,0 +1,33 @@
#ifndef WF_PROVIDER_IMPL_OPERATION_READ_H
#define WF_PROVIDER_IMPL_OPERATION_READ_H
#include "webfuse/provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_read(
struct wfp_request * request,
char const * data,
size_t length);
extern void wfp_impl_read(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_read_default(
struct wfp_request * request,
ino_t inode,
uint32_t handle,
size_t offset,
size_t length,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,42 @@
#include "webfuse/provider/impl/operation/readdir.h"
#include "webfuse/provider/impl/operation/error.h"
#include "webfuse/provider/impl/dirbuffer.h"
#include "webfuse/provider/impl/request.h"
#include "webfuse/core/util.h"
void wfp_impl_readdir(
struct wfp_impl_invokation_context * context,
json_t * params,
int id)
{
size_t const count = json_array_size(params);
if (1 == count)
{
json_t * inode_holder = json_array_get(params, 0);
if ((NULL != inode_holder) && (json_is_integer(inode_holder)))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
struct wfp_request * request = wfp_impl_request_create(context->request, id);
context->provider->readdir(request, inode, context->user_data);
}
}
}
void wfp_impl_readdir_default(
struct wfp_request * request,
ino_t WF_UNUSED_PARAM(directory),
void * WF_UNUSED_PARAM(user_data))
{
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
}
void wfp_impl_respond_readdir(
struct wfp_request * request,
struct wfp_dirbuffer * dirbuffer)
{
json_t * result = wfp_impl_dirbuffer_take(dirbuffer);
wfp_impl_respond(request, result);
}

View File

@@ -0,0 +1,29 @@
#ifndef WF_PROVIDER_IMPL_OPERATION_READDIR_H
#define WF_PROVIDER_IMPL_OPERATION_READDIR_H
#include "webfuse/provider/impl/provider.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_respond_readdir(
struct wfp_request * request,
struct wfp_dirbuffer * dirbuffer);
extern void wfp_impl_readdir(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
extern void wfp_impl_readdir_default(
struct wfp_request * request,
ino_t directory,
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,127 @@
#include "webfuse/provider/impl/provider.h"
#include <stdbool.h>
#include <string.h>
#include "webfuse/provider/impl/request.h"
#include "webfuse/provider/impl/operation/lookup.h"
#include "webfuse/provider/impl/operation/getattr.h"
#include "webfuse/provider/impl/operation/readdir.h"
#include "webfuse/provider/impl/operation/open.h"
#include "webfuse/provider/impl/operation/close.h"
#include "webfuse/provider/impl/operation/read.h"
typedef void wfp_impl_invoke_fn(
struct wfp_impl_invokation_context * context,
json_t * params,
int id);
struct wfp_impl_method
{
char const * name;
wfp_impl_invoke_fn * invoke;
bool is_notification;
};
static void wfp_impl_provider_invoke_method(
struct wfp_impl_invokation_context * context,
char const * method_name,
json_t * params,
int id)
{
static struct wfp_impl_method const methods[] =
{
{"lookup", &wfp_impl_lookup, false},
{"getattr", &wfp_impl_getattr, false},
{"readdir", &wfp_impl_readdir, false},
{"open", &wfp_impl_open, false},
{"close", &wfp_impl_close, true},
{"read", &wfp_impl_read, false}
};
static size_t const count = sizeof(methods) / sizeof(methods[0]);
for (size_t i = 0; i < count; i++)
{
struct wfp_impl_method const * method = &methods[i];
if (0 == strcmp(method_name, method->name))
{
if ((0 < id) || (method->is_notification))
{
method->invoke(context, params, id);
}
break;
}
}
}
void wfp_impl_provider_init(
struct wfp_provider * provider)
{
provider->lookup = &wfp_impl_lookup_default;
provider->getattr = &wfp_impl_getattr_default;
provider->readdir = &wfp_impl_readdir_default;
provider->open = &wfp_impl_open_default;
provider->close = &wfp_impl_close_default;
provider->read = &wfp_impl_read_default;
provider->connected = &wfp_impl_connected_default;
provider->disconnected = &wfp_impl_disconnected_default;
provider->ontimer = &wfp_impl_ontimer_default;
}
void wfp_impl_provider_init_from_prototype(
struct wfp_provider * provider,
struct wfp_provider const * prototype)
{
provider->lookup = prototype->lookup;
provider->getattr = prototype->getattr;
provider->readdir = prototype->readdir;
provider->open = prototype->open;
provider->close = prototype->close;
provider->read = prototype->read;
provider->connected = prototype->connected;
provider->disconnected = prototype->disconnected;
provider->ontimer = prototype->ontimer;
}
void wfp_impl_provider_invoke(
struct wfp_impl_invokation_context * context,
json_t * request)
{
json_t * method_holder = json_object_get(request, "method");
json_t * params = json_object_get(request, "params");
json_t * id_holder = json_object_get(request, "id");
if ((NULL != method_holder) && (json_is_string(method_holder)) &&
(NULL != params) && (json_is_array(params)))
{
char const * method = json_string_value(method_holder);
int id = json_is_integer(id_holder) ? json_integer_value(id_holder) : 0;
wfp_impl_provider_invoke_method(context, method, params, id);
}
}
void wfp_impl_connected_default(
void * user_data)
{
(void) user_data;
// empty
}
void wfp_impl_disconnected_default(
void * user_data)
{
(void) user_data;
// empty
}
void wfp_impl_ontimer_default(
void * user_data)
{
(void) user_data;
// empty
}

View File

@@ -0,0 +1,57 @@
#ifndef WF_PROVIDER_IMPL_PROVIDER_H
#define WF_PROVIDER_IMPL_PROVIDER_H
#include <jansson.h>
#include "webfuse/provider/client_config.h"
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_provider
{
wfp_connected_fn * connected;
wfp_disconnected_fn * disconnected;
wfp_ontimer_fn * ontimer;
wfp_lookup_fn * lookup;
wfp_getattr_fn * getattr;
wfp_readdir_fn * readdir;
wfp_open_fn * open;
wfp_close_fn * close;
wfp_read_fn * read;
};
struct wfp_impl_invokation_context
{
struct wfp_provider * provider;
void * user_data;
struct wfp_request * request;
};
extern void wfp_impl_provider_init(
struct wfp_provider * provider);
extern void wfp_impl_provider_init_from_prototype(
struct wfp_provider * provider,
struct wfp_provider const * prototype);
extern void wfp_impl_provider_invoke(
struct wfp_impl_invokation_context * context,
json_t * request);
extern void wfp_impl_connected_default(
void * user_data);
extern void wfp_impl_disconnected_default(
void * user_data);
extern void wfp_impl_ontimer_default(
void * user_data);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,55 @@
#include "webfuse/provider/impl/request.h"
#include <stdlib.h>
#include "webfuse/provider/impl/operation/error.h"
struct wfp_request * wfp_impl_request_create(
struct wfp_request * prototype,
int id)
{
struct wfp_request * request = malloc(sizeof(struct wfp_request));
if (NULL != request)
{
request->respond = prototype->respond;
request->user_data = prototype->user_data;
request->id = id;
}
return request;
}
void wfp_impl_request_dispose(
struct wfp_request * request)
{
free(request);
}
extern void wfp_impl_respond(
struct wfp_request * request,
json_t * result)
{
json_t * response = json_object();
json_object_set_new(response, "result", result);
json_object_set_new(response, "id", json_integer(request->id));
request->respond(response, request->user_data);
json_decref(response);
wfp_impl_request_dispose(request);
}
void wfp_impl_respond_error(
struct wfp_request * request,
wf_status status)
{
json_t * response = json_object();
json_t * error = json_object();
json_object_set_new(error, "code", json_integer(status));
json_object_set_new(response, "error", error);
json_object_set_new(response, "id", json_integer(request->id));
request->respond(response, request->user_data);
json_decref(response);
wfp_impl_request_dispose(request);
}

View File

@@ -0,0 +1,43 @@
#ifndef WF_PROVIDER_IMPL_REQUEST_H
#define WF_PROVIDER_IMPL_REQUEST_H
#include <jansson.h>
#include "webfuse/provider/impl/provider.h"
#include "webfuse/core/status.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef void wfp_impl_request_respond_fn(
json_t * response,
void * user_data);
struct wfp_request
{
wfp_impl_request_respond_fn * respond;
void * user_data;
int id;
};
extern void wfp_impl_respond_error(
struct wfp_request * request,
wf_status status);
extern struct wfp_request * wfp_impl_request_create(
struct wfp_request * prototype,
int id);
extern void wfp_impl_request_dispose(
struct wfp_request * request);
extern void wfp_impl_respond(
struct wfp_request * request,
json_t * result);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,125 @@
#include "webfuse/provider/impl/url.h"
#include <stdlib.h>
#include <string.h>
struct wfp_impl_url_protocol
{
char const * name;
size_t name_length;
int default_port;
bool use_tls;
};
static bool wfp_impl_url_readprotocol(
struct wfp_impl_url * url,
char const * * data)
{
static struct wfp_impl_url_protocol const known_protocols[] =
{
{"ws://", 5, 80, false},
{"wss://", 6, 443, true}
};
static size_t const count = (sizeof(known_protocols) / sizeof(known_protocols[0]));
bool found = false;
for(size_t i = 0; (!found) && (i < count); i++)
{
struct wfp_impl_url_protocol const * protocol = &known_protocols[i];
if (0 == strncmp(*data, protocol->name, protocol->name_length))
{
url->port = protocol->default_port;
url->use_tls = protocol->use_tls;
*data = *data + protocol->name_length;
found = true;
}
}
return found;
}
static bool wfp_impl_url_readhost(
struct wfp_impl_url * url,
char const * * data)
{
char * end = strpbrk(*data, ":/");
bool const result = (NULL != end);
if (result)
{
size_t length = end - *data;
url->host = strndup(*data, length);
*data = end;
}
return result;
}
static bool wfp_impl_url_readport(
struct wfp_impl_url * url,
char const * * data)
{
bool result;
if (':' == **data)
{
*data = *data + 1;
char * end = strchr(*data, '/');
result = (NULL != end);
if (result)
{
url->port = atoi(*data);
*data = end;
}
}
else
{
result = ('/' == **data);
}
return result;
}
static bool wfp_impl_url_readpath(
struct wfp_impl_url * url,
char const * * data)
{
bool const result = ('/' == **data);
url->path = strdup(*data);
*data = NULL;
return result;
}
bool wfp_impl_url_init(
struct wfp_impl_url * url,
char const * value)
{
memset(url, 0, sizeof(struct wfp_impl_url));
char const * data = value;
bool const result =
wfp_impl_url_readprotocol(url, &data) &&
wfp_impl_url_readhost(url, &data) &&
wfp_impl_url_readport(url, &data) &&
wfp_impl_url_readpath(url, &data)
;
if (!result)
{
wfp_impl_url_cleanup(url);
}
return result;
}
void wfp_impl_url_cleanup(
struct wfp_impl_url * url)
{
free(url->host);
free(url->path);
memset(url, 0, sizeof(struct wfp_impl_url));
}

View File

@@ -0,0 +1,32 @@
#ifndef WF_PROVIDER_IMPL_URL_H
#define WF_PROVIDER_IMPL_URL_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_impl_url
{
char * host;
int port;
char * path;
bool use_tls;
};
extern bool wfp_impl_url_init(
struct wfp_impl_url * url,
char const * value);
extern void wfp_impl_url_cleanup(
struct wfp_impl_url * url);
#ifdef __cplusplus
}
#endif
#endif