From 8960f1ff646ca0dd58d8a868de0538d9f26d1034 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 2 Feb 2019 14:25:57 +0100 Subject: [PATCH] added implementation for ll_getatt --- src/app/www/js/filesystem.js | 36 +++++++++++---- src/app/www/js/filesystem_handler.js | 6 +++ src/app/www/js/startup.js | 5 ++- src/wsfs/jsonrpc.c | 12 +++++ src/wsfs/jsonrpc.h | 3 ++ src/wsfs/operation/getattr.c | 13 ------ src/wsfs/operation/ll_close.c | 3 ++ src/wsfs/operation/ll_getattr.c | 66 +++++++++++++++++++++++++++- src/wsfs/operation/ll_lookup.c | 8 +++- src/wsfs/operation/ll_open.c | 3 ++ src/wsfs/operation/ll_read.c | 3 ++ src/wsfs/operation/ll_readdir.c | 3 ++ 12 files changed, 134 insertions(+), 27 deletions(-) diff --git a/src/app/www/js/filesystem.js b/src/app/www/js/filesystem.js index 29d5f1c..5cb0af4 100644 --- a/src/app/www/js/filesystem.js +++ b/src/app/www/js/filesystem.js @@ -20,21 +20,41 @@ class FileSystem { constructor(root) { this.root = root; + this._inodes = { }; + + this._walk(this.root, (entry) => { this._inodes[entry.inode] = entry; }); + } + + _walk(node, callback) { + callback(node); + + const entries = node.entries; + if (entries) { + for(let entry of Object.entries(entries)) { + this._walk(entry[1], callback); + } + } } _getEntry(path) { - let curItem = this.root; + if ("number" === typeof(path)) { + const inode = path; + return this._inodes[inode]; + } + else { + let curItem = this.root; - for(let item of path.split('/')) { - if ('' !== item) { - curItem = curItem.entries && curItem.entries[item]; - if (!curItem) { - return null; + for(let item of path.split('/')) { + if ('' !== item) { + curItem = curItem.entries && curItem.entries[item]; + if (!curItem) { + return null; + } } } - } - return curItem; + return curItem; + } } getattr(path) { diff --git a/src/app/www/js/filesystem_handler.js b/src/app/www/js/filesystem_handler.js index 53c573b..796f156 100644 --- a/src/app/www/js/filesystem_handler.js +++ b/src/app/www/js/filesystem_handler.js @@ -28,6 +28,12 @@ class FileSystemHandler { result = this._fs.getattr(path); } break; + case "getattr_ll": + { + const inode = request.params[0]; + result = this._fs.getattr(inode); + } + break; case "readdir": { const path = request.params[0]; diff --git a/src/app/www/js/startup.js b/src/app/www/js/startup.js index b411656..307799f 100644 --- a/src/app/www/js/startup.js +++ b/src/app/www/js/startup.js @@ -46,11 +46,12 @@ function startup() { document.getElementById('connection').appendChild(connectionView.element); let fs = new FileSystem({ + inode: 1, mode: 0755, type: "dir", entries: { - "hello.txt" : { mode: 0444, type: "file", contents: "Hello, World!"}, - "say_hello.sh": { mode: 0555, type: "file", contents: "#!/bin/sh\necho hello\n"} + "hello.txt" : { inode: 2, mode: 0444, type: "file", contents: "Hello, World!"}, + "say_hello.sh": { inode: 3, mode: 0555, type: "file", contents: "#!/bin/sh\necho hello\n"} } }); let handler = new FileSystemHandler(fs, connection); diff --git a/src/wsfs/jsonrpc.c b/src/wsfs/jsonrpc.c index 3ad70ef..a61cf67 100644 --- a/src/wsfs/jsonrpc.c +++ b/src/wsfs/jsonrpc.c @@ -230,3 +230,15 @@ void wsfs_jsonrpc_on_message( } } +int wsfs_json_get_int(json_t * 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; +} diff --git a/src/wsfs/jsonrpc.h b/src/wsfs/jsonrpc.h index 9299236..e12f5b6 100644 --- a/src/wsfs/jsonrpc.h +++ b/src/wsfs/jsonrpc.h @@ -48,6 +48,9 @@ extern void wsfs_jsonrpc_on_message( size_t length, void * user_data); + +extern int wsfs_json_get_int(json_t * object, char const * key, int default_value); + #ifdef __cplusplus } #endif diff --git a/src/wsfs/operation/getattr.c b/src/wsfs/operation/getattr.c index d4b563d..2f162cb 100644 --- a/src/wsfs/operation/getattr.c +++ b/src/wsfs/operation/getattr.c @@ -10,19 +10,6 @@ #include "wsfs/util.h" #include "wsfs/jsonrpc.h" -static int wsfs_json_get_int(json_t * 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; -} - int wsfs_operation_getattr( char const * path, struct stat * buffer, diff --git a/src/wsfs/operation/ll_close.c b/src/wsfs/operation/ll_close.c index 61c7452..e45ea8b 100644 --- a/src/wsfs/operation/ll_close.c +++ b/src/wsfs/operation/ll_close.c @@ -2,10 +2,13 @@ #include #include "wsfs/util.h" +#include + void wsfs_operation_ll_close( fuse_req_t request, fuse_ino_t WSFS_UNUSED_PARAM(inode), struct fuse_file_info * WSFS_UNUSED_PARAM(file_info)) { + puts("close"); fuse_reply_err(request, ENOENT); } diff --git a/src/wsfs/operation/ll_getattr.c b/src/wsfs/operation/ll_getattr.c index 1f392fc..101c013 100644 --- a/src/wsfs/operation/ll_getattr.c +++ b/src/wsfs/operation/ll_getattr.c @@ -1,11 +1,73 @@ #include "wsfs/operations.h" + + #include +#include +#include + +#include +#include +#include + +#include "wsfs/jsonrpc.h" #include "wsfs/util.h" extern void wsfs_operation_ll_getattr ( fuse_req_t request, - fuse_ino_t WSFS_UNUSED_PARAM(inode), + fuse_ino_t inode, struct fuse_file_info * WSFS_UNUSED_PARAM(file_info)) { - fuse_reply_err(request, ENOENT); + printf("getattr: inode=%lu\n", inode); + + struct fuse_ctx const * context = fuse_req_ctx(request); + struct wsfs_jsonrpc * rpc = fuse_req_userdata(request); + + struct stat buffer; + json_t * data = NULL; + wsfs_status status = wsfs_jsonrpc_invoke(rpc, &data, "getattr", "i", inode); + 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 = wsfs_json_get_int(data, "size", 0); + buffer.st_atime = wsfs_json_get_int(data, "atime", 0); + buffer.st_mtime = wsfs_json_get_int(data, "mtime", 0); + buffer.st_ctime = wsfs_json_get_int(data, "ctime", 0); + + } + else + { + status = WSFS_BAD_FORMAT; + } + + json_decref(data); + } + + if (WSFS_GOOD == status) + { + fuse_reply_attr(request, &buffer, 1.0); + } + else + { + fuse_reply_err(request, ENOENT); + } } diff --git a/src/wsfs/operation/ll_lookup.c b/src/wsfs/operation/ll_lookup.c index 7ddf49f..3041db4 100644 --- a/src/wsfs/operation/ll_lookup.c +++ b/src/wsfs/operation/ll_lookup.c @@ -2,10 +2,14 @@ #include #include "wsfs/util.h" +#include + void wsfs_operation_ll_lookup ( fuse_req_t request, - fuse_ino_t WSFS_UNUSED_PARAM(parent), - char const * WSFS_UNUSED_PARAM(name)) + fuse_ino_t parent, + char const * name) { + + printf("lookup: inode=%lu, name=%s\n", parent, name); fuse_reply_err(request, ENOENT); } diff --git a/src/wsfs/operation/ll_open.c b/src/wsfs/operation/ll_open.c index 51e268e..f898b53 100644 --- a/src/wsfs/operation/ll_open.c +++ b/src/wsfs/operation/ll_open.c @@ -2,10 +2,13 @@ #include #include "wsfs/util.h" +#include + void wsfs_operation_ll_open( fuse_req_t request, fuse_ino_t WSFS_UNUSED_PARAM(inode), struct fuse_file_info * WSFS_UNUSED_PARAM(file_info)) { + puts("open"); fuse_reply_err(request, ENOENT); } diff --git a/src/wsfs/operation/ll_read.c b/src/wsfs/operation/ll_read.c index 0566ea2..8904599 100644 --- a/src/wsfs/operation/ll_read.c +++ b/src/wsfs/operation/ll_read.c @@ -2,6 +2,8 @@ #include #include "wsfs/util.h" +#include + void wsfs_operation_ll_read( fuse_req_t request, fuse_ino_t WSFS_UNUSED_PARAM(inode), @@ -9,5 +11,6 @@ void wsfs_operation_ll_read( off_t WSFS_UNUSED_PARAM(offset), struct fuse_file_info * WSFS_UNUSED_PARAM(file_info)) { + puts("read"); fuse_reply_err(request, ENOENT); } diff --git a/src/wsfs/operation/ll_readdir.c b/src/wsfs/operation/ll_readdir.c index 004da83..97012b7 100644 --- a/src/wsfs/operation/ll_readdir.c +++ b/src/wsfs/operation/ll_readdir.c @@ -2,6 +2,8 @@ #include #include "wsfs/util.h" +#include + void wsfs_operation_ll_readdir ( fuse_req_t request, fuse_ino_t WSFS_UNUSED_PARAM(inode), @@ -9,5 +11,6 @@ void wsfs_operation_ll_readdir ( off_t WSFS_UNUSED_PARAM(offset), struct fuse_file_info * WSFS_UNUSED_PARAM(file_info)) { + puts("readdir"); fuse_reply_err(request, ENOENT); }