From c09c1b5d77dc21750e91e58c94a5a89951959298 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Tue, 16 Apr 2019 20:28:29 +0200 Subject: [PATCH] removes use of PATH_MAX --- CMakeLists.txt | 2 + lib/webfuse/adapter/impl/filesystem.c | 67 +++++++++++---------------- lib/webfuse/adapter/impl/filesystem.h | 3 ++ lib/webfuse/adapter/impl/session.c | 1 - lib/webfuse/core/string.c | 35 ++++++++++++++ lib/webfuse/core/string.h | 21 +++++++++ test/test_string.cc | 18 +++++++ 7 files changed, 107 insertions(+), 40 deletions(-) create mode 100644 lib/webfuse/core/string.c create mode 100644 lib/webfuse/core/string.h create mode 100644 test/test_string.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 51b2d66..7b8b5f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ add_library(webfuse-core STATIC lib/webfuse/core/message.c lib/webfuse/core/message_queue.c lib/webfuse/core/status.c + lib/webfuse/core/string.c ) set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core) @@ -287,6 +288,7 @@ add_executable(alltests test/test_credentials.cc test/test_authenticator.cc test/test_authenticators.cc + test/test_string.cc ) target_link_libraries(alltests PUBLIC webfuse-adapter-static webfuse-provider-static webfuse-core ${EXTRA_LIBS} ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES}) diff --git a/lib/webfuse/adapter/impl/filesystem.c b/lib/webfuse/adapter/impl/filesystem.c index 1f37a90..22c827a 100644 --- a/lib/webfuse/adapter/impl/filesystem.c +++ b/lib/webfuse/adapter/impl/filesystem.c @@ -2,6 +2,8 @@ #include "webfuse/adapter/impl/operations.h" #include "webfuse/adapter/impl/session.h" +#include "webfuse/core/string.h" + #include #include @@ -9,7 +11,6 @@ #include #include #include -#include #include #include @@ -40,9 +41,9 @@ static bool wf_impl_filesystem_is_link_broken(char const * path, char const * id { bool result = false; - char buffer[PATH_MAX]; - ssize_t count = readlink(path, buffer, PATH_MAX); - if ((0 < count) && (count < PATH_MAX)) + char buffer[UUID_STR_LEN]; + ssize_t count = readlink(path, buffer, UUID_STR_LEN); + if ((0 < count) && (count < UUID_STR_LEN)) { buffer[count] = '\0'; result = (0 == strcmp(buffer, id)); @@ -51,13 +52,12 @@ static bool wf_impl_filesystem_is_link_broken(char const * path, char const * id return result; } -static bool wf_impl_filesystem_get_first_subdir( - char const * serviceDir, - char * buffer, - size_t buffer_size) +static bool wf_impl_filesystem_link_first_subdir( + char const * link_path, + char const * path) { bool result = false; - DIR * dir = opendir(serviceDir); + DIR * dir = opendir(path); if (NULL != dir) { struct dirent * entry = readdir(dir); @@ -65,8 +65,7 @@ static bool wf_impl_filesystem_get_first_subdir( { if ((DT_DIR == entry->d_type) && ('.' != entry->d_name[0])) { - buffer[0] = '\0'; - strncat(buffer, entry->d_name, buffer_size); /* Flawfinder: ignore */ + symlink(entry->d_name, link_path); result = true; break; } @@ -91,34 +90,25 @@ static void wf_impl_filesystem_cleanup( free(filesystem->buffer.mem); fuse_opt_free_args(&filesystem->args); - struct wf_impl_session * session = filesystem->user_data.session; - char path[PATH_MAX]; - snprintf(path, PATH_MAX, "%s/%s/%s", session->mount_point, filesystem->user_data.name, filesystem->id); - rmdir(path); + rmdir(filesystem->root_path); - char serviceDir[PATH_MAX]; - snprintf(serviceDir, PATH_MAX, "%s/%s", session->mount_point, filesystem->user_data.name); - - snprintf(path, PATH_MAX, "%s/%s/default", session->mount_point, filesystem->user_data.name); - if (wf_impl_filesystem_is_link_broken(path, filesystem->id)) + if (wf_impl_filesystem_is_link_broken(filesystem->default_path, filesystem->id)) { - unlink(path); + unlink(filesystem->default_path); - char firstDir[PATH_MAX]; - bool found = wf_impl_filesystem_get_first_subdir(serviceDir, firstDir, PATH_MAX); - if (found) + bool const success = wf_impl_filesystem_link_first_subdir(filesystem->default_path, filesystem->service_path); + if (!success) { - symlink(firstDir, path); - } - else - { - rmdir(serviceDir); + rmdir(filesystem->service_path); } } free(filesystem->user_data.name); free(filesystem->id); + free(filesystem->root_path); + free(filesystem->default_path); + free(filesystem->service_path); } @@ -130,6 +120,7 @@ static bool wf_impl_filesystem_init( bool result = false; wf_dlist_item_init(&filesystem->item); + char * argv[] = {"", NULL}; filesystem->args.argc = 1; filesystem->args.argv = argv; @@ -138,19 +129,17 @@ static bool wf_impl_filesystem_init( filesystem->user_data.session = session; filesystem->user_data.timeout = 1.0; filesystem->user_data.name = strdup(name); - filesystem->id = wf_impl_filesystem_create_id(); memset(&filesystem->buffer, 0, sizeof(struct fuse_buf)); - char path[PATH_MAX]; - snprintf(path, PATH_MAX, "%s/%s", session->mount_point, name); - mkdir(path, 0755); + filesystem->service_path = wf_create_string("%s/%s", session->mount_point, name); + mkdir(filesystem->service_path, 0755); - snprintf(path, PATH_MAX, "%s/%s/%s", session->mount_point, name, filesystem->id); - mkdir(path, 0755); + filesystem->id = wf_impl_filesystem_create_id(); + filesystem->root_path = wf_create_string("%s/%s/%s", session->mount_point, name, filesystem->id); + mkdir(filesystem->root_path, 0755); - char defaultPath[PATH_MAX]; - snprintf(defaultPath, PATH_MAX, "%s/%s/default", session->mount_point, name); - symlink(filesystem->id, defaultPath); + filesystem->default_path = wf_create_string("%s/%s/default", session->mount_point, name); + symlink(filesystem->id, filesystem->default_path); filesystem->session = fuse_session_new( &filesystem->args, @@ -159,7 +148,7 @@ static bool wf_impl_filesystem_init( &filesystem->user_data); if (NULL != filesystem->session) { - result = (0 == fuse_session_mount(filesystem->session, path)); + result = (0 == fuse_session_mount(filesystem->session, filesystem->root_path)); } if (result) diff --git a/lib/webfuse/adapter/impl/filesystem.h b/lib/webfuse/adapter/impl/filesystem.h index 4374963..1fcd9c4 100644 --- a/lib/webfuse/adapter/impl/filesystem.h +++ b/lib/webfuse/adapter/impl/filesystem.h @@ -27,6 +27,9 @@ struct wf_impl_filesystem struct lws * wsi; char * name; char * id; + char * service_path; + char * default_path; + char * root_path; }; extern struct wf_impl_filesystem * wf_impl_filesystem_create( diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index 1455de3..8111a9e 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -12,7 +12,6 @@ #include #include #include -#include static bool wf_impl_session_send( json_t * request, diff --git a/lib/webfuse/core/string.c b/lib/webfuse/core/string.c new file mode 100644 index 0000000..c5117eb --- /dev/null +++ b/lib/webfuse/core/string.c @@ -0,0 +1,35 @@ +#include "webfuse/core/string.h" + +#include +#include + +char * wf_create_string(char const * format, ...) +{ + char * result = NULL; + + va_list measure_args; + va_start(measure_args, format); + char buffer; + int needed = vsnprintf(&buffer, 1, format, measure_args); + va_end(measure_args); + + if (0 <= needed) + { + result = malloc(needed + 1); + if (NULL != result) + { + va_list args; + va_start(args, format); + int count = vsnprintf(result, needed + 1, format, args); + va_end(args); + + if ((count < 0) || (needed < count)) + { + free(result); + result = NULL; + } + } + } + + return result; +} diff --git a/lib/webfuse/core/string.h b/lib/webfuse/core/string.h new file mode 100644 index 0000000..00a686b --- /dev/null +++ b/lib/webfuse/core/string.h @@ -0,0 +1,21 @@ +#ifndef WF_CORE_STRING_H +#define WF_CORE_STRING_H + +#ifndef __cplusplus +#include +#else +#include +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern char * wf_create_string(char const * format, ...); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/test/test_string.cc b/test/test_string.cc new file mode 100644 index 0000000..17e501a --- /dev/null +++ b/test/test_string.cc @@ -0,0 +1,18 @@ +#include +#include + +#include "webfuse/core/string.h" + +TEST(wf_string_create, Default) +{ + char * value = wf_create_string("test %s/%d", "hello", 42); + ASSERT_STREQ("test hello/42", value); + free(value); +} + +TEST(wf_string_create, EmptyString) +{ + char * value = wf_create_string(""); + ASSERT_STREQ("", value); + free(value); +}