diff --git a/lib/webfuse/core/path.c b/lib/webfuse/core/path.c index 464e22d..36689f0 100644 --- a/lib/webfuse/core/path.c +++ b/lib/webfuse/core/path.c @@ -100,3 +100,17 @@ wf_path_get_element( return result; } + +char const * +wf_path_get_filename( + struct wf_path * path) +{ + char const * result = NULL; + + if (0 < path->count) + { + result = path->elements[path->count - 1]; + } + + return result; +} diff --git a/lib/webfuse/core/path.h b/lib/webfuse/core/path.h index 717195a..ca31725 100644 --- a/lib/webfuse/core/path.h +++ b/lib/webfuse/core/path.h @@ -32,6 +32,10 @@ wf_path_get_element( struct wf_path * path, size_t i); +extern char const * +wf_path_get_filename( + struct wf_path * path); + #ifdef __cplusplus } #endif diff --git a/lib/webfuse/provider/impl/static_filesystem.c b/lib/webfuse/provider/impl/static_filesystem.c index 74fd9cd..decad2f 100644 --- a/lib/webfuse/provider/impl/static_filesystem.c +++ b/lib/webfuse/provider/impl/static_filesystem.c @@ -3,11 +3,14 @@ #include "webfuse/provider/dirbuffer.h" #include "webfuse/provider/operation/error.h" +#include "webfuse/core/path.h" + #include #include #include #define WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY (16) +#define WFP_STATIC_FILSYSTEM_INDOE_ROOT (1) struct wfp_static_filesystem_entry { @@ -126,13 +129,15 @@ wfp_static_filesystem_entry_get_info( } -static void +static size_t wfp_static_filesystem_add_dir( struct wfp_static_filesystem * filesystem, size_t parent, char const * name ) { + size_t result = 0; + struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_get_entry_by_name(filesystem, parent, name); if (NULL == entry) { @@ -146,7 +151,31 @@ wfp_static_filesystem_add_dir( entry->get_info = &wfp_static_filesystem_entry_get_info; entry->size = 0; entry->content = NULL; - } + + result = entry->inode; + } + + return result; +} + +static size_t +wfp_impl_static_filesystem_make_parent( + struct wfp_static_filesystem * filesystem, + struct wf_path * path) +{ + size_t result = WFP_STATIC_FILSYSTEM_INDOE_ROOT; + + size_t count = wf_path_element_count(path); + if (0 < count) + { + for(size_t i = 0; i < (count - 1); i++) + { + char const * name = wf_path_get_element(path, i); + result = wfp_static_filesystem_add_dir(filesystem, result, name); + } + } + + return result; } static void wfp_static_filesystem_stat( @@ -320,13 +349,25 @@ wfp_impl_static_filesystem_add( char const * content, size_t length) { - (void) filesystem; - (void) path; - (void) mode; - (void) content; - (void) length; + struct wf_path * path_ = wf_path_create(path); + if (NULL != path_) + { + size_t parent = wfp_impl_static_filesystem_make_parent(filesystem, path_); + struct wfp_static_filesystem_entry * entry = wfp_static_filesystem_add_entry(filesystem); + entry->parent = parent; + entry->is_file = true; + entry->name = strdup(wf_path_get_filename(path_)); + entry->mode = mode; + entry->size = length; + entry->get_info = &wfp_static_filesystem_entry_get_info; + entry->read = &wfp_static_filesystem_entry_read; + entry->user_data = entry; - // ToDo: implement me + entry->content = malloc(length); + memcpy(entry->content, content, length); + + wf_path_dispose(path_); + } } void @@ -336,12 +377,8 @@ wfp_impl_static_filesystem_add_text( int mode, char const * content) { - (void) filesystem; - (void) path; - (void) mode; - (void) content; - - // ToDo: implement me + size_t length = strlen(content); + wfp_impl_static_filesystem_add(filesystem, path, mode, content, length); } void diff --git a/test/test_static_filesystem.cc b/test/test_static_filesystem.cc index fee15d5..e7db28e 100644 --- a/test/test_static_filesystem.cc +++ b/test/test_static_filesystem.cc @@ -42,3 +42,20 @@ TEST(wfp_static_filesystem, contains_default_dirs) wfp_impl_static_filesystem_dispose(filesystem); wfp_client_config_dispose(config); } + +TEST(wfp_static_filesystem, add_text) +{ + struct wfp_client_config * config = wfp_client_config_create(); + struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config); + wfp_impl_static_filesystem_add_text(filesystem, "text.file", 666, "some text"); + + MockRequest mock; + struct wfp_request * request = request_create(&mock, 23); + char const * contained_elements[] = {"text.file", nullptr}; + EXPECT_CALL(mock, respond(ReaddirMatcher(contained_elements), 23)).Times(1); + + config->provider.readdir(request, 1, config->user_data); + + wfp_impl_static_filesystem_dispose(filesystem); + wfp_client_config_dispose(config); +}