mirror of
https://github.com/falk-werner/webfuse-provider
synced 2024-10-27 20:44:10 +00:00
added uuid mountpoint and factory
This commit is contained in:
parent
6a94cea6f9
commit
cfadf85f49
@ -16,6 +16,8 @@ add_library(webfuse-adapter-static STATIC
|
||||
lib/webfuse/adapter/impl/credentials.c
|
||||
lib/webfuse/adapter/impl/operations.c
|
||||
lib/webfuse/adapter/impl/mountpoint.c
|
||||
lib/webfuse/adapter/impl/uuid_mountpoint_factory.c
|
||||
lib/webfuse/adapter/impl/uuid_mountpoint.c
|
||||
lib/webfuse/adapter/impl/time/timepoint.c
|
||||
lib/webfuse/adapter/impl/time/timer.c
|
||||
lib/webfuse/adapter/impl/time/timeout_manager.c
|
||||
|
@ -9,8 +9,8 @@ extern "C"
|
||||
struct wf_mountpoint;
|
||||
|
||||
typedef void
|
||||
wf_mountpoint_ondispose_fn(
|
||||
struct wf_mountpoint * mountpoint);
|
||||
wf_mountpoint_userdata_dispose_fn(
|
||||
void * user_data);
|
||||
|
||||
extern struct wf_mountpoint *
|
||||
wf_mountpoint_create(
|
||||
@ -25,9 +25,10 @@ wf_mountpoint_get_path(
|
||||
struct wf_mountpoint const * mountpoint);
|
||||
|
||||
extern void
|
||||
wf_mountpoint_set_ondispose(
|
||||
struct wf_mountpoint * mountpoint,
|
||||
wf_mountpoint_ondispose_fn * ondispose);
|
||||
wf_mountpoint_set_userdata(
|
||||
struct wf_mountpoint * mointpoint,
|
||||
void * user_data,
|
||||
wf_mountpoint_userdata_dispose_fn * dispose);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -160,9 +160,10 @@ wf_mountpoint_get_path(
|
||||
}
|
||||
|
||||
void
|
||||
wf_mountpoint_set_ondispose(
|
||||
wf_mountpoint_set_userdata(
|
||||
struct wf_mountpoint * mountpoint,
|
||||
wf_mountpoint_ondispose_fn * ondispose)
|
||||
void * user_data,
|
||||
wf_mountpoint_userdata_dispose_fn * dispose)
|
||||
{
|
||||
wf_impl_mountpoint_set_ondispose(mountpoint, ondispose);
|
||||
wf_impl_mountpoint_set_userdata(mountpoint, user_data, dispose);
|
||||
}
|
||||
|
@ -6,23 +6,18 @@
|
||||
struct wf_mountpoint
|
||||
{
|
||||
char * path;
|
||||
wf_mountpoint_ondispose_fn * ondispose;
|
||||
void * user_data;
|
||||
wf_mountpoint_userdata_dispose_fn * dispose;
|
||||
};
|
||||
|
||||
static void wf_impl_mountpoint_default_ondispose(
|
||||
struct wf_mountpoint * mountpoint)
|
||||
{
|
||||
(void) mountpoint;
|
||||
// empty
|
||||
}
|
||||
|
||||
struct wf_mountpoint *
|
||||
wf_impl_mountpoint_create(
|
||||
char const * path)
|
||||
{
|
||||
struct wf_mountpoint * mountpoint = malloc(sizeof(struct wf_mountpoint));
|
||||
mountpoint->path = strdup(path);
|
||||
mountpoint->ondispose = &wf_impl_mountpoint_default_ondispose;
|
||||
mountpoint->user_data = NULL;
|
||||
mountpoint->dispose = NULL;
|
||||
|
||||
return mountpoint;
|
||||
}
|
||||
@ -31,7 +26,10 @@ void
|
||||
wf_impl_mountpoint_dispose(
|
||||
struct wf_mountpoint * mountpoint)
|
||||
{
|
||||
mountpoint->ondispose(mountpoint);
|
||||
if (NULL != mountpoint->dispose)
|
||||
{
|
||||
mountpoint->dispose(mountpoint->user_data);
|
||||
}
|
||||
|
||||
free(mountpoint->path);
|
||||
free(mountpoint);
|
||||
@ -44,10 +42,12 @@ wf_impl_mountpoint_get_path(
|
||||
return mountpoint->path;
|
||||
}
|
||||
|
||||
void
|
||||
wf_impl_mountpoint_set_ondispose(
|
||||
extern void
|
||||
wf_impl_mountpoint_set_userdata(
|
||||
struct wf_mountpoint * mountpoint,
|
||||
wf_mountpoint_ondispose_fn * ondispose)
|
||||
void * user_data,
|
||||
wf_mountpoint_userdata_dispose_fn * dispose)
|
||||
{
|
||||
mountpoint->ondispose = ondispose;
|
||||
mountpoint->user_data = user_data;
|
||||
mountpoint->dispose = dispose;
|
||||
}
|
||||
|
@ -21,9 +21,10 @@ wf_impl_mountpoint_get_path(
|
||||
struct wf_mountpoint const * mountpoint);
|
||||
|
||||
extern void
|
||||
wf_impl_mountpoint_set_ondispose(
|
||||
struct wf_mountpoint * mointpoint,
|
||||
wf_mountpoint_ondispose_fn * ondispose);
|
||||
wf_impl_mountpoint_set_userdata(
|
||||
struct wf_mountpoint * mountpoint,
|
||||
void * user_data,
|
||||
wf_mountpoint_userdata_dispose_fn * dispose);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
123
lib/webfuse/adapter/impl/uuid_mountpoint.c
Normal file
123
lib/webfuse/adapter/impl/uuid_mountpoint.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include "webfuse/adapter/impl/uuid_mountpoint.h"
|
||||
#include "webfuse/adapter/impl/mountpoint.h"
|
||||
|
||||
#include "webfuse/core/string.h"
|
||||
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct wf_impl_uuid_mountpoint_data
|
||||
{
|
||||
char * id;
|
||||
char * filesystem_path;
|
||||
char * default_path;
|
||||
char * full_path;
|
||||
};
|
||||
|
||||
static char * wf_impl_uuid_mountpoint_create_id(void)
|
||||
{
|
||||
uuid_t uuid;
|
||||
uuid_generate(uuid);
|
||||
char id[UUID_STR_LEN];
|
||||
uuid_unparse(uuid, id);
|
||||
|
||||
return strdup(id);
|
||||
}
|
||||
|
||||
static bool wf_impl_uuid_mountpoint_is_link_broken(char const * path, char const * id)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool wf_impl_uuid_mountpoint_link_first_subdir(
|
||||
char const * link_path,
|
||||
char const * path)
|
||||
{
|
||||
bool result = false;
|
||||
DIR * dir = opendir(path);
|
||||
if (NULL != dir)
|
||||
{
|
||||
struct dirent * entry = readdir(dir);
|
||||
while (NULL != entry)
|
||||
{
|
||||
if ((DT_DIR == entry->d_type) && ('.' != entry->d_name[0]))
|
||||
{
|
||||
symlink(entry->d_name, link_path);
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
entry = readdir(dir);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
wf_impl_uuid_mountpoint_data_dispose(
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_impl_uuid_mountpoint_data * data = user_data;
|
||||
|
||||
rmdir(data->full_path);
|
||||
|
||||
if (wf_impl_filesystem_is_link_broken(data->default_path, data->id))
|
||||
{
|
||||
unlink(data->default_path);
|
||||
|
||||
bool const success = wf_impl_filesystem_link_first_subdir(data->default_path, data->filesystem_path);
|
||||
if (!success)
|
||||
{
|
||||
rmdir(data->filesystem_path);
|
||||
}
|
||||
}
|
||||
|
||||
free(data->id);
|
||||
free(data->filesystem_path);
|
||||
free(data->default_path);
|
||||
free(data->full_path);
|
||||
free(data);
|
||||
}
|
||||
|
||||
struct wf_mountpoint *
|
||||
wf_impl_uuid_mountpoint_create(
|
||||
char const * root_path,
|
||||
char const * filesystem)
|
||||
{
|
||||
struct wf_impl_uuid_mountpoint_data * data = malloc(sizeof(struct wf_impl_uuid_mountpoint_data));
|
||||
data->filesystem_path = wf_create_string("%s/%s", root_path, filesystem);
|
||||
mkdir(data->filesystem_path, 0755);
|
||||
|
||||
data->id = wf_impl_uuid_mountpoint_create_id();
|
||||
char * full_path = wf_create_string("%s/%s/%s", root_path, filesystem, data->id);
|
||||
mkdir(data->full_path, 0755);
|
||||
|
||||
data->default_path = wf_create_string("%s/%s/default", root_path, data->filesystem_path);
|
||||
symlink(data->id, data->default_path);
|
||||
|
||||
struct wf_mountpoint * mountpoint = wf_impl_mountpoint_create(full_path);
|
||||
wf_impl_mountpoint_set_userdata(mountpoint, data, &wf_impl_uuid_mountpoint_data_dispose);
|
||||
|
||||
return mountpoint;
|
||||
}
|
20
lib/webfuse/adapter/impl/uuid_mountpoint.h
Normal file
20
lib/webfuse/adapter/impl/uuid_mountpoint.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef WF_IMPL_UUID_MOUNTPOINT_H
|
||||
#define WF_IMPL_UUID_MOUNTPOINT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_mountpoint;
|
||||
|
||||
extern struct wf_mountpoint *
|
||||
wf_impl_uuid_mountpoint_create(
|
||||
char const * root_path,
|
||||
char const * filesystem);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
47
lib/webfuse/adapter/impl/uuid_mountpoint_factory.c
Normal file
47
lib/webfuse/adapter/impl/uuid_mountpoint_factory.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include "webfuse/adapter/impl/uuid_mountpoint_factory.h"
|
||||
#include "webfuse/adapter/impl/uuid_mountpoint.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_impl_uuid_mountpoint_factory
|
||||
{
|
||||
char * root_path;
|
||||
};
|
||||
|
||||
void *
|
||||
wf_impl_uuid_mountpoint_factory_create(
|
||||
char * root_path)
|
||||
{
|
||||
mkdir(root_path, 0755);
|
||||
|
||||
struct wf_impl_uuid_mountpoint_factory * factory = malloc(sizeof(struct wf_impl_uuid_mountpoint_factory));
|
||||
factory->root_path = strdup(root_path);
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
void
|
||||
wf_impl_uuid_mountpoint_factory_dispose(
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_impl_uuid_mountpoint_factory * factory = user_data;
|
||||
|
||||
rmdir(factory->root_path);
|
||||
free(factory->root_path);
|
||||
free(factory);
|
||||
}
|
||||
|
||||
struct wf_mountpoint *
|
||||
wf_impl_uuid_mountpoint_factory_create_mountpoint(
|
||||
char const * filesystem,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_impl_uuid_mountpoint_factory * factory = user_data;
|
||||
|
||||
return wf_impl_uuid_mountpoint_create(factory->root_path, filesystem);
|
||||
}
|
29
lib/webfuse/adapter/impl/uuid_mountpoint_factory.h
Normal file
29
lib/webfuse/adapter/impl/uuid_mountpoint_factory.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef WF_IMPL_UUID_MOUNTPOINT_FACTORY_H
|
||||
#define WF_IMPL_UUID_MOUNTPOINT_FACTORY_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_mountpoint;
|
||||
|
||||
extern void *
|
||||
wf_impl_uuid_mountpoint_factory_create(
|
||||
char * root_path);
|
||||
|
||||
extern void
|
||||
wf_impl_uuid_mountpoint_factory_dispose(
|
||||
void * user_data);
|
||||
|
||||
extern struct wf_mountpoint *
|
||||
wf_impl_uuid_mountpoint_factory_create_mountpiont(
|
||||
char const * filesystem,
|
||||
void * user_data);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -4,17 +4,17 @@
|
||||
|
||||
namespace
|
||||
{
|
||||
class MockMountpointDisposer
|
||||
class MockUserDataDisposer
|
||||
{
|
||||
public:
|
||||
MOCK_METHOD1(ondispose, void(wf_mountpoint * mountpoint));
|
||||
MOCK_METHOD1(dispose, void(void * mountpoint));
|
||||
};
|
||||
|
||||
MockMountpointDisposer * global_disposer = nullptr;
|
||||
MockUserDataDisposer * global_disposer = nullptr;
|
||||
|
||||
void ondispose(wf_mountpoint * mountpoint)
|
||||
void ondispose(void * user_data)
|
||||
{
|
||||
global_disposer->ondispose(mountpoint);
|
||||
global_disposer->dispose(user_data);
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,14 +32,16 @@ TEST(mountpoint, get_path)
|
||||
|
||||
TEST(mountpoint, ondispose)
|
||||
{
|
||||
MockMountpointDisposer disposer;
|
||||
MockUserDataDisposer disposer;
|
||||
global_disposer = &disposer;
|
||||
|
||||
wf_mountpoint * mountpoint = wf_mountpoint_create("/some/path");
|
||||
ASSERT_NE(nullptr, mountpoint);
|
||||
|
||||
wf_mountpoint_set_ondispose(mountpoint, ondispose);
|
||||
EXPECT_CALL(disposer, ondispose(mountpoint)).Times(1);
|
||||
int value = 42;
|
||||
void * user_data = reinterpret_cast<void*>(&value);
|
||||
wf_mountpoint_set_userdata(mountpoint, user_data, ondispose);
|
||||
EXPECT_CALL(disposer, dispose(user_data)).Times(1);
|
||||
|
||||
wf_mountpoint_dispose(mountpoint);
|
||||
}
|
Loading…
Reference in New Issue
Block a user