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:
parent
8158e70a31
commit
c09c1b5d77
@ -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})
|
||||
|
@ -2,6 +2,8 @@
|
||||
#include "webfuse/adapter/impl/operations.h"
|
||||
#include "webfuse/adapter/impl/session.h"
|
||||
|
||||
#include "webfuse/core/string.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
@ -9,7 +11,6 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <stdlib.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;
|
||||
|
||||
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);
|
||||
if (wf_impl_filesystem_is_link_broken(filesystem->default_path, filesystem->id))
|
||||
{
|
||||
unlink(filesystem->default_path);
|
||||
|
||||
snprintf(path, PATH_MAX, "%s/%s/default", session->mount_point, filesystem->user_data.name);
|
||||
if (wf_impl_filesystem_is_link_broken(path, filesystem->id))
|
||||
bool const success = wf_impl_filesystem_link_first_subdir(filesystem->default_path, filesystem->service_path);
|
||||
if (!success)
|
||||
{
|
||||
unlink(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);
|
||||
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)
|
||||
|
@ -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(
|
||||
|
@ -12,7 +12,6 @@
|
||||
#include <libwebsockets.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
static bool wf_impl_session_send(
|
||||
json_t * request,
|
||||
|
35
lib/webfuse/core/string.c
Normal file
35
lib/webfuse/core/string.c
Normal 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
21
lib/webfuse/core/string.h
Normal 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
18
test/test_string.cc
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user