1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

refactored message creation; enhanced implementation of some operations

This commit is contained in:
Falk Werner 2019-03-03 12:48:58 +01:00
parent 95e4d96d79
commit bc38b5d9b1
15 changed files with 243 additions and 146 deletions

View File

@ -109,7 +109,6 @@ set(WSFS_PROVIDER_SOURCES
lib/wsfs/provider/client_protocol.c
lib/wsfs/provider/provider.c
lib/wsfs/provider/request.c
lib/wsfs/provider/operation/error.c
lib/wsfs/provider/operation/lookup.c
lib/wsfs/provider/operation/getattr.c
lib/wsfs/provider/operation/readdir.c

View File

@ -30,6 +30,38 @@ struct fs
struct fs_file const * files;
};
static struct fs_dir const * fs_getdir(
struct fs * fs,
ino_t inode)
{
for (size_t i = 0; 0 != fs->directories[i].inode; i++)
{
struct fs_dir const * dir = &fs->directories[i];
if (inode == dir->inode)
{
return dir;
}
}
return NULL;
}
static struct fs_file const * fs_getfile(
struct fs * fs,
ino_t inode)
{
for (size_t i = 0; 0 != fs->files[i].inode; i++)
{
struct fs_file const * f = &fs->files[i];
if (inode == f->inode)
{
return f;
}
}
return NULL;
}
static void fs_lookup(
struct wsfsp_request * request,
ino_t parent,
@ -44,15 +76,42 @@ static void fs_lookup(
wsfsp_respond_error(request, -1);
}
static void fs_getattr(
struct wsfsp_request * request,
ino_t inode,
void * user_data)
{
(void) inode;
(void) user_data;
puts("getattr");
struct fs * fs = (struct fs*) user_data;
struct fs_dir const * dir = fs_getdir(fs, inode);
if (NULL != dir)
{
puts("found: dir");
struct stat stat;
memset(&stat, 0, sizeof(stat));
stat.st_ino = inode;
stat.st_mode = S_IFDIR | 0555;
wsfsp_respond_getattr(request, &stat);
return;
}
struct fs_file const * f = fs_getfile(fs, inode);
{
puts("found: file");
struct stat stat;
memset(&stat, 0, sizeof(stat));
stat.st_ino = inode;
stat.st_mode = S_IFREG | 0555;
stat.st_size = f->content_length;
wsfsp_respond_getattr(request, &stat);
return;
}
puts("getattr: not found");
wsfsp_respond_error(request, -1);
}

View File

@ -23,59 +23,49 @@ static int wsfs_server_protocol_callback(
switch (reason)
{
case LWS_CALLBACK_PROTOCOL_INIT:
{
lws_sock_file_fd_type fd;
fd.filefd = wsfs_filesystem_get_fd(&protocol->filesystem);
if (!lws_adopt_descriptor_vhost(lws_get_vhost(wsi), LWS_ADOPT_RAW_FILE_DESC, fd, ws_protocol->name, NULL))
{
puts("error: unable to adopt fd");
{
lws_sock_file_fd_type fd;
fd.filefd = wsfs_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;
break;
case LWS_CALLBACK_ESTABLISHED:
{
if (NULL == protocol->wsi)
{
protocol->wsi = wsi;
}
}
break;
break;
case LWS_CALLBACK_CLOSED:
{
if (wsi == protocol->wsi)
{
protocol->wsi = NULL;
wsfs_message_queue_cleanup(&protocol->queue);
}
}
break;
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
{
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue)))
{
struct wsfs_message * message = wsfs_message_queue_pop(&protocol->queue);
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wsfs_message_dispose(message);
}
}
break;
case LWS_CALLBACK_RECEIVE:
{
wsfs_jsonrpc_server_onresult(&protocol->rpc, in, len);
}
break;
case LWS_CALLBACK_RAW_RX_FILE:
{
wsfs_filesystem_process_request(&protocol->filesystem);
}
break;
default:
break;
}
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue)))
{
lws_callback_on_writable(wsi);
if (!wsfs_message_queue_empty(&protocol->queue))
{
lws_callback_on_writable(wsi);
}
}
break;
case LWS_CALLBACK_RECEIVE:
wsfs_jsonrpc_server_onresult(&protocol->rpc, in, len);
break;
case LWS_CALLBACK_RAW_RX_FILE:
wsfs_filesystem_process_request(&protocol->filesystem);
break;
default:
break;
}
return 0;
@ -90,21 +80,15 @@ static bool wsfs_server_protocol_invoke(
if (NULL != protocol->wsi)
{
size_t length = json_dumpb(request, NULL, 0, JSON_COMPACT);
if (0 < length)
{
struct wsfs_message * message = wsfs_message_create(length);
json_dumpb(request, message->data, length, JSON_COMPACT);
struct wsfs_message * message = wsfs_message_create(request);
if (NULL != message)
{
wsfs_message_queue_push(&protocol->queue, message);
lws_callback_on_writable(protocol->wsi);
// ToDo: add timeout
result = true;
}
}
return result;
}

View File

@ -3,15 +3,29 @@
#include <stdlib.h>
#include <libwebsockets.h>
struct wsfs_message * wsfs_message_create(size_t length)
extern struct wsfs_message * wsfs_message_create(json_t const * value)
{
char * data = malloc(sizeof(struct wsfs_message) + LWS_PRE + length);
struct wsfs_message * message = (struct wsfs_message *) data;
if (NULL != message)
#if 0
char * msg = json_dumps(value, JSON_COMPACT);
puts(msg);
free(msg);
#endif
struct wsfs_message * message = NULL;
size_t const length = json_dumpb(value, NULL, 0, JSON_COMPACT);
if (0 < length)
{
message->data = &data[sizeof(struct wsfs_message) + LWS_PRE];
message->length = length;
message->next = NULL;
char * data = malloc(sizeof(struct wsfs_message) + LWS_PRE + length);
message = (struct wsfs_message *) data;
if (NULL != message)
{
message->data = &data[sizeof(struct wsfs_message) + LWS_PRE];
message->length = length;
message->next = NULL;
json_dumpb(value, message->data, length, JSON_COMPACT);
}
}
return message;

View File

@ -8,6 +8,8 @@
using std::size_t;
#endif
#include <jansson.h>
struct wsfs_message
{
struct wsfs_message * next;
@ -20,7 +22,7 @@ extern "C"
{
#endif
extern struct wsfs_message * wsfs_message_create(size_t length);
extern struct wsfs_message * wsfs_message_create(json_t const * value);
extern void wsfs_message_dispose(
struct wsfs_message * message);

View File

@ -22,15 +22,14 @@ static void wsfsp_client_protocol_respond(
json_t * response,
void * user_data)
{
// ToDo: implment me
(void) user_data;
struct wsfsp_client_protocol * protocol = (struct wsfsp_client_protocol *) user_data;
char * value = json_dumps(response, 0);
if (NULL != value)
struct wsfs_message * message = wsfs_message_create(response);
if (NULL != message)
{
puts(value);
wsfs_message_queue_push(&protocol->queue, message);
lws_callback_on_writable(protocol->wsi);
}
free(value);
}
static void wsfsp_client_protocol_process_request(
@ -48,7 +47,6 @@ static void wsfsp_client_protocol_process_request(
.request = &protocol->request
};
puts("wsfsp_provider_invoke");
wsfsp_provider_invoke(&context, request);
json_decref(request);
}
@ -70,7 +68,6 @@ static int wsfsp_client_protocol_callback(
switch (reason)
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
puts("established");
protocol->provider.connected(protocol->user_data);
break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
@ -82,23 +79,25 @@ static int wsfsp_client_protocol_callback(
case LWS_CALLBACK_CLIENT_RECEIVE:
wsfsp_client_protocol_process_request(protocol, in, len);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
// fall-through
case LWS_CALLBACK_CLIENT_WRITEABLE:
{
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue)))
{
struct wsfs_message * message = wsfs_message_queue_pop(&protocol->queue);
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wsfs_message_dispose(message);
if (!wsfs_message_queue_empty(&protocol->queue))
{
lws_callback_on_writable(wsi);
}
}
}
break;
default:
break;
}
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue)))
{
lws_callback_on_writable(wsi);
}
}
return 0;

View File

@ -29,15 +29,10 @@ void wsfsp_close(
}
void wsfsp_close_default(
ino_t inode,
uint32_t handle,
int flags,
void * user_data)
ino_t WSFS_UNUSED_PARAM(inode),
uint32_t WSFS_UNUSED_PARAM(handle),
int WSFS_UNUSED_PARAM(flags),
void * WSFS_UNUSED_PARAM(user_data))
{
(void) inode;
(void) handle;
(void) flags;
(void) user_data;
// empty
}

View File

@ -1,19 +0,0 @@
#include "wsfs/provider/operation/error.h"
#include <jansson.h>
#include "wsfs/provider/request.h"
void wsfsp_respond_error(
struct wsfsp_request * request,
int 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);
wsfsp_request_dispose(request);
}

View File

@ -1,7 +1,11 @@
#include "wsfs/provider/operation/getattr_intern.h"
#include <stdio.h>
#include <stdbool.h>
#include "wsfs/provider/operation/error.h"
#include "wsfs/provider/request.h"
#include "wsfs/util.h"
void wsfsp_getattr(
@ -14,7 +18,7 @@ void wsfsp_getattr(
{
json_t * inode_holder = json_array_get(params, 0);
if ((NULL != inode_holder) && (json_is_integer(inode_holder)))
if (json_is_integer(inode_holder))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
struct wsfsp_request * request = wsfsp_request_create(context->request, id);
@ -26,12 +30,9 @@ void wsfsp_getattr(
void wsfsp_getattr_default(
struct wsfsp_request * request,
ino_t inode,
void * user_data)
ino_t WSFS_UNUSED_PARAM(inode),
void * WSFS_UNUSED_PARAM(user_data))
{
(void) inode;
(void) user_data;
wsfsp_respond_error(request, -1);
}
@ -39,8 +40,26 @@ void wsfsp_respond_getattr(
struct wsfsp_request * request,
struct stat const * stat)
{
(void) request;
(void) stat;
bool const is_file = (0 != (stat->st_mode & S_IFREG));
bool const is_dir = (0 != (stat->st_mode & S_IFDIR));
// ToDo: implement me
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"));
}
wsfsp_respond(request, result);
}

View File

@ -1,17 +1,29 @@
#include "wsfs/provider/operation/lookup_intern.h"
#include <stdio.h>
#include "wsfs/provider/operation/error.h"
#include "wsfs/util.h"
void wsfsp_lookup(
struct wsfsp_invokation_context * context,
json_t * params,
int id)
{
(void) context;
(void) params;
(void) 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);
puts("lookup");
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 wsfsp_request * request = wsfsp_request_create(context->request, id);
context->provider->lookup(request, inode, name, context->user_data);
}
}
}
void wsfsp_respond_lookup(
@ -21,18 +33,15 @@ void wsfsp_respond_lookup(
(void) request;
(void) stat;
wsfsp_respond_error(request, -1);
}
void wsfsp_lookup_default(
struct wsfsp_request * request,
ino_t parent,
char const * name,
void * user_data)
ino_t WSFS_UNUSED_PARAM(parent),
char const * WSFS_UNUSED_PARAM(name),
void * WSFS_UNUSED_PARAM(user_data))
{
(void) parent;
(void) name;
(void) user_data;
wsfsp_respond_error(request, -1);
}

View File

@ -1,6 +1,7 @@
#include "wsfs/provider/operation/open_intern.h"
#include <stdio.h>
#include "wsfs/provider/operation/error.h"
#include "wsfs/util.h"
void wsfsp_open(
struct wsfsp_invokation_context * context,
@ -16,14 +17,10 @@ void wsfsp_open(
void wsfsp_open_default(
struct wsfsp_request * request,
ino_t inode,
int flags,
void * user_data)
ino_t WSFS_UNUSED_PARAM(inode),
int WSFS_UNUSED_PARAM(flags),
void * WSFS_UNUSED_PARAM(user_data))
{
(void) inode;
(void) flags;
(void) user_data;
wsfsp_respond_error(request, -1);
}

View File

@ -1,6 +1,7 @@
#include "wsfs/provider/operation/read_intern.h"
#include <stdio.h>
#include "wsfs/provider/operation/error.h"
#include "wsfs/util.h"
void wsfsp_read(
struct wsfsp_invokation_context * context,
@ -11,23 +12,17 @@ void wsfsp_read(
(void) params;
(void) id;
puts("read");
puts("read");
}
void wsfsp_read_default(
struct wsfsp_request * request,
ino_t inode,
uint32_t handle,
size_t offset,
size_t length,
void * user_data)
ino_t WSFS_UNUSED_PARAM(inode),
uint32_t WSFS_UNUSED_PARAM(handle),
size_t WSFS_UNUSED_PARAM(offset),
size_t WSFS_UNUSED_PARAM(length),
void * WSFS_UNUSED_PARAM(user_data))
{
(void) inode;
(void) handle;
(void) offset;
(void) length;
(void) user_data;
wsfsp_respond_error(request, -1);
}

View File

@ -1,27 +1,35 @@
#include "wsfs/provider/operation/readdir_intern.h"
#include <stdio.h>
#include "wsfs/provider/operation/error.h"
#include "wsfs/util.h"
void wsfsp_readdir(
struct wsfsp_invokation_context * context,
json_t * params,
int id)
{
(void) context;
(void) params;
(void) id;
size_t const count = json_array_size(params);
if (1 == count)
{
json_t * inode_holder = json_array_get(params, 0);
puts("readdir");
if ((NULL != inode_holder) && (json_is_integer(inode_holder)))
{
ino_t inode = (ino_t) json_integer_value(inode_holder);
struct wsfsp_request * request = wsfsp_request_create(context->request, id);
context->provider->readdir(request, inode, context->user_data);
}
}
}
void wsfsp_readdir_default(
struct wsfsp_request * request,
ino_t directory,
void * user_data)
ino_t WSFS_UNUSED_PARAM(directory),
void * WSFS_UNUSED_PARAM(user_data))
{
(void) directory;
(void) user_data;
wsfsp_respond_error(request, -1);
}
@ -33,5 +41,6 @@ void wsfsp_respond_readdir(
(void) dirbuffer;
// ToDo: implement me
wsfsp_respond_error(request, -1);
}

View File

@ -1,6 +1,7 @@
#include "wsfs/provider/request.h"
#include <stdlib.h>
#include "wsfs/provider/operation/error.h"
struct wsfsp_request * wsfsp_request_create(
struct wsfsp_request * prototype,
@ -21,4 +22,34 @@ void wsfsp_request_dispose(
struct wsfsp_request * request)
{
free(request);
}
extern void wsfsp_respond(
struct wsfsp_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);
wsfsp_request_dispose(request);
}
void wsfsp_respond_error(
struct wsfsp_request * request,
int 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);
wsfsp_request_dispose(request);
}

View File

@ -28,6 +28,10 @@ extern struct wsfsp_request * wsfsp_request_create(
extern void wsfsp_request_dispose(
struct wsfsp_request * request);
extern void wsfsp_respond(
struct wsfsp_request * request,
json_t * result);
#ifdef __cplusplus
}
#endif