From bc38b5d9b1aafc6611e21168ac655deb89505bcf Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 3 Mar 2019 12:48:58 +0100 Subject: [PATCH] refactored message creation; enhanced implementation of some operations --- CMakeLists.txt | 1 - example/provider/main.c | 65 +++++++++++++++++++++++-- lib/wsfs/adapter/server_protocol.c | 70 +++++++++++---------------- lib/wsfs/message.c | 28 ++++++++--- lib/wsfs/message.h | 4 +- lib/wsfs/provider/client_protocol.c | 29 ++++++----- lib/wsfs/provider/operation/close.c | 13 ++--- lib/wsfs/provider/operation/error.c | 19 -------- lib/wsfs/provider/operation/getattr.c | 37 ++++++++++---- lib/wsfs/provider/operation/lookup.c | 31 +++++++----- lib/wsfs/provider/operation/open.c | 11 ++--- lib/wsfs/provider/operation/read.c | 19 +++----- lib/wsfs/provider/operation/readdir.c | 27 +++++++---- lib/wsfs/provider/request.c | 31 ++++++++++++ lib/wsfs/provider/request.h | 4 ++ 15 files changed, 243 insertions(+), 146 deletions(-) delete mode 100644 lib/wsfs/provider/operation/error.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 76f66f5..a4312c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/example/provider/main.c b/example/provider/main.c index 15a4fdb..5e7bd93 100644 --- a/example/provider/main.c +++ b/example/provider/main.c @@ -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); } diff --git a/lib/wsfs/adapter/server_protocol.c b/lib/wsfs/adapter/server_protocol.c index be402b1..fa53e0d 100644 --- a/lib/wsfs/adapter/server_protocol.c +++ b/lib/wsfs/adapter/server_protocol.c @@ -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; } diff --git a/lib/wsfs/message.c b/lib/wsfs/message.c index eedd392..b4431a3 100644 --- a/lib/wsfs/message.c +++ b/lib/wsfs/message.c @@ -3,15 +3,29 @@ #include #include -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; diff --git a/lib/wsfs/message.h b/lib/wsfs/message.h index c2cc2b6..e5f7f4d 100644 --- a/lib/wsfs/message.h +++ b/lib/wsfs/message.h @@ -8,6 +8,8 @@ using std::size_t; #endif +#include + 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); diff --git a/lib/wsfs/provider/client_protocol.c b/lib/wsfs/provider/client_protocol.c index 0fc203a..21685fa 100644 --- a/lib/wsfs/provider/client_protocol.c +++ b/lib/wsfs/provider/client_protocol.c @@ -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; diff --git a/lib/wsfs/provider/operation/close.c b/lib/wsfs/provider/operation/close.c index e8a7d12..4ef6837 100644 --- a/lib/wsfs/provider/operation/close.c +++ b/lib/wsfs/provider/operation/close.c @@ -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 } \ No newline at end of file diff --git a/lib/wsfs/provider/operation/error.c b/lib/wsfs/provider/operation/error.c deleted file mode 100644 index da01a89..0000000 --- a/lib/wsfs/provider/operation/error.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "wsfs/provider/operation/error.h" -#include -#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); -} diff --git a/lib/wsfs/provider/operation/getattr.c b/lib/wsfs/provider/operation/getattr.c index 9a4d2f4..03425ef 100644 --- a/lib/wsfs/provider/operation/getattr.c +++ b/lib/wsfs/provider/operation/getattr.c @@ -1,7 +1,11 @@ #include "wsfs/provider/operation/getattr_intern.h" + #include +#include #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); } diff --git a/lib/wsfs/provider/operation/lookup.c b/lib/wsfs/provider/operation/lookup.c index 34e89c9..e809e57 100644 --- a/lib/wsfs/provider/operation/lookup.c +++ b/lib/wsfs/provider/operation/lookup.c @@ -1,17 +1,29 @@ #include "wsfs/provider/operation/lookup_intern.h" #include #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); } diff --git a/lib/wsfs/provider/operation/open.c b/lib/wsfs/provider/operation/open.c index f599718..8bb5573 100644 --- a/lib/wsfs/provider/operation/open.c +++ b/lib/wsfs/provider/operation/open.c @@ -1,6 +1,7 @@ #include "wsfs/provider/operation/open_intern.h" #include #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); } diff --git a/lib/wsfs/provider/operation/read.c b/lib/wsfs/provider/operation/read.c index 40f2eda..ac802d8 100644 --- a/lib/wsfs/provider/operation/read.c +++ b/lib/wsfs/provider/operation/read.c @@ -1,6 +1,7 @@ #include "wsfs/provider/operation/read_intern.h" #include #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); } diff --git a/lib/wsfs/provider/operation/readdir.c b/lib/wsfs/provider/operation/readdir.c index 979ac09..7005a32 100644 --- a/lib/wsfs/provider/operation/readdir.c +++ b/lib/wsfs/provider/operation/readdir.c @@ -1,27 +1,35 @@ #include "wsfs/provider/operation/readdir_intern.h" + #include + #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); } diff --git a/lib/wsfs/provider/request.c b/lib/wsfs/provider/request.c index 4774615..e5e8d5b 100644 --- a/lib/wsfs/provider/request.c +++ b/lib/wsfs/provider/request.c @@ -1,6 +1,7 @@ #include "wsfs/provider/request.h" #include +#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); } \ No newline at end of file diff --git a/lib/wsfs/provider/request.h b/lib/wsfs/provider/request.h index d094a3a..157a5f7 100644 --- a/lib/wsfs/provider/request.h +++ b/lib/wsfs/provider/request.h @@ -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