1
0
mirror of https://github.com/falk-werner/webfuse synced 2025-06-13 12:54:15 +00:00

removes use of PATH_MAX

This commit is contained in:
Falk Werner 2019-04-16 20:28:29 +02:00
parent 8158e70a31
commit c09c1b5d77
7 changed files with 107 additions and 40 deletions

View File

@ -56,6 +56,7 @@ add_library(webfuse-core STATIC
lib/webfuse/core/message.c lib/webfuse/core/message.c
lib/webfuse/core/message_queue.c lib/webfuse/core/message_queue.c
lib/webfuse/core/status.c lib/webfuse/core/status.c
lib/webfuse/core/string.c
) )
set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core) set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core)
@ -287,6 +288,7 @@ add_executable(alltests
test/test_credentials.cc test/test_credentials.cc
test/test_authenticator.cc test/test_authenticator.cc
test/test_authenticators.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}) target_link_libraries(alltests PUBLIC webfuse-adapter-static webfuse-provider-static webfuse-core ${EXTRA_LIBS} ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES})

View File

@ -2,6 +2,8 @@
#include "webfuse/adapter/impl/operations.h" #include "webfuse/adapter/impl/operations.h"
#include "webfuse/adapter/impl/session.h" #include "webfuse/adapter/impl/session.h"
#include "webfuse/core/string.h"
#include <libwebsockets.h> #include <libwebsockets.h>
#include <uuid/uuid.h> #include <uuid/uuid.h>
@ -9,7 +11,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
@ -40,9 +41,9 @@ static bool wf_impl_filesystem_is_link_broken(char const * path, char const * id
{ {
bool result = false; bool result = false;
char buffer[PATH_MAX]; char buffer[UUID_STR_LEN];
ssize_t count = readlink(path, buffer, PATH_MAX); ssize_t count = readlink(path, buffer, UUID_STR_LEN);
if ((0 < count) && (count < PATH_MAX)) if ((0 < count) && (count < UUID_STR_LEN))
{ {
buffer[count] = '\0'; buffer[count] = '\0';
result = (0 == strcmp(buffer, id)); 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; return result;
} }
static bool wf_impl_filesystem_get_first_subdir( static bool wf_impl_filesystem_link_first_subdir(
char const * serviceDir, char const * link_path,
char * buffer, char const * path)
size_t buffer_size)
{ {
bool result = false; bool result = false;
DIR * dir = opendir(serviceDir); DIR * dir = opendir(path);
if (NULL != dir) if (NULL != dir)
{ {
struct dirent * entry = readdir(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])) if ((DT_DIR == entry->d_type) && ('.' != entry->d_name[0]))
{ {
buffer[0] = '\0'; symlink(entry->d_name, link_path);
strncat(buffer, entry->d_name, buffer_size); /* Flawfinder: ignore */
result = true; result = true;
break; break;
} }
@ -91,34 +90,25 @@ static void wf_impl_filesystem_cleanup(
free(filesystem->buffer.mem); free(filesystem->buffer.mem);
fuse_opt_free_args(&filesystem->args); fuse_opt_free_args(&filesystem->args);
struct wf_impl_session * session = filesystem->user_data.session; rmdir(filesystem->root_path);
char path[PATH_MAX];
snprintf(path, PATH_MAX, "%s/%s/%s", session->mount_point, filesystem->user_data.name, filesystem->id);
rmdir(path);
char serviceDir[PATH_MAX]; if (wf_impl_filesystem_is_link_broken(filesystem->default_path, filesystem->id))
snprintf(serviceDir, PATH_MAX, "%s/%s", session->mount_point, filesystem->user_data.name); {
unlink(filesystem->default_path);
snprintf(path, PATH_MAX, "%s/%s/default", session->mount_point, filesystem->user_data.name); bool const success = wf_impl_filesystem_link_first_subdir(filesystem->default_path, filesystem->service_path);
if (wf_impl_filesystem_is_link_broken(path, filesystem->id)) if (!success)
{ {
unlink(path); rmdir(filesystem->service_path);
char firstDir[PATH_MAX];
bool found = wf_impl_filesystem_get_first_subdir(serviceDir, firstDir, PATH_MAX);
if (found)
{
symlink(firstDir, path);
}
else
{
rmdir(serviceDir);
} }
} }
free(filesystem->user_data.name); free(filesystem->user_data.name);
free(filesystem->id); 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; bool result = false;
wf_dlist_item_init(&filesystem->item); wf_dlist_item_init(&filesystem->item);
char * argv[] = {"", NULL}; char * argv[] = {"", NULL};
filesystem->args.argc = 1; filesystem->args.argc = 1;
filesystem->args.argv = argv; filesystem->args.argv = argv;
@ -138,19 +129,17 @@ static bool wf_impl_filesystem_init(
filesystem->user_data.session = session; filesystem->user_data.session = session;
filesystem->user_data.timeout = 1.0; filesystem->user_data.timeout = 1.0;
filesystem->user_data.name = strdup(name); filesystem->user_data.name = strdup(name);
filesystem->id = wf_impl_filesystem_create_id();
memset(&filesystem->buffer, 0, sizeof(struct fuse_buf)); memset(&filesystem->buffer, 0, sizeof(struct fuse_buf));
char path[PATH_MAX]; filesystem->service_path = wf_create_string("%s/%s", session->mount_point, name);
snprintf(path, PATH_MAX, "%s/%s", session->mount_point, name); mkdir(filesystem->service_path, 0755);
mkdir(path, 0755);
snprintf(path, PATH_MAX, "%s/%s/%s", session->mount_point, name, filesystem->id); filesystem->id = wf_impl_filesystem_create_id();
mkdir(path, 0755); filesystem->root_path = wf_create_string("%s/%s/%s", session->mount_point, name, filesystem->id);
mkdir(filesystem->root_path, 0755);
char defaultPath[PATH_MAX]; filesystem->default_path = wf_create_string("%s/%s/default", session->mount_point, name);
snprintf(defaultPath, PATH_MAX, "%s/%s/default", session->mount_point, name); symlink(filesystem->id, filesystem->default_path);
symlink(filesystem->id, defaultPath);
filesystem->session = fuse_session_new( filesystem->session = fuse_session_new(
&filesystem->args, &filesystem->args,
@ -159,7 +148,7 @@ static bool wf_impl_filesystem_init(
&filesystem->user_data); &filesystem->user_data);
if (NULL != filesystem->session) if (NULL != filesystem->session)
{ {
result = (0 == fuse_session_mount(filesystem->session, path)); result = (0 == fuse_session_mount(filesystem->session, filesystem->root_path));
} }
if (result) if (result)

View File

@ -27,6 +27,9 @@ struct wf_impl_filesystem
struct lws * wsi; struct lws * wsi;
char * name; char * name;
char * id; char * id;
char * service_path;
char * default_path;
char * root_path;
}; };
extern struct wf_impl_filesystem * wf_impl_filesystem_create( extern struct wf_impl_filesystem * wf_impl_filesystem_create(

View File

@ -12,7 +12,6 @@
#include <libwebsockets.h> #include <libwebsockets.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h>
static bool wf_impl_session_send( static bool wf_impl_session_send(
json_t * request, json_t * request,

35
lib/webfuse/core/string.c Normal file
View File

@ -0,0 +1,35 @@
#include "webfuse/core/string.h"
#include <stdio.h>
#include <stdlib.h>
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;
}

21
lib/webfuse/core/string.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef WF_CORE_STRING_H
#define WF_CORE_STRING_H
#ifndef __cplusplus
#include <stdarg.h>
#else
#include <cstdarg>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
extern char * wf_create_string(char const * format, ...);
#ifdef __cplusplus
}
#endif
#endif

18
test/test_string.cc Normal file
View File

@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#include <stdlib.h>
#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);
}