2019-03-26 22:04:53 +00:00
|
|
|
#include "webfuse/adapter/impl/filesystem.h"
|
2020-04-04 21:16:25 +00:00
|
|
|
#include "webfuse/adapter/impl/operation/context.h"
|
2020-04-04 06:32:26 +00:00
|
|
|
#include "webfuse/adapter/impl/operation/open.h"
|
2020-04-04 06:55:59 +00:00
|
|
|
#include "webfuse/adapter/impl/operation/close.h"
|
2020-04-04 13:41:33 +00:00
|
|
|
#include "webfuse/adapter/impl/operation/read.h"
|
2020-04-04 18:23:50 +00:00
|
|
|
#include "webfuse/adapter/impl/operation/readdir.h"
|
2020-04-04 20:26:15 +00:00
|
|
|
#include "webfuse/adapter/impl/operation/getattr.h"
|
2020-04-04 21:16:25 +00:00
|
|
|
#include "webfuse/adapter/impl/operation/lookup.h"
|
2019-04-17 20:51:16 +00:00
|
|
|
#include "webfuse/adapter/impl/session.h"
|
2020-02-16 20:03:17 +00:00
|
|
|
#include "webfuse/adapter/impl/mountpoint.h"
|
2019-04-17 20:51:16 +00:00
|
|
|
|
|
|
|
#include "webfuse/core/string.h"
|
|
|
|
|
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2019-02-05 23:58:51 +00:00
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
#include <stdlib.h>
|
2019-02-05 23:58:51 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2019-03-26 14:35:33 +00:00
|
|
|
static struct fuse_lowlevel_ops const filesystem_operations =
|
2019-02-05 23:58:51 +00:00
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
.lookup = &wf_impl_operation_lookup,
|
|
|
|
.getattr = &wf_impl_operation_getattr,
|
|
|
|
.readdir = &wf_impl_operation_readdir,
|
|
|
|
.open = &wf_impl_operation_open,
|
|
|
|
.release = &wf_impl_operation_close,
|
|
|
|
.read = &wf_impl_operation_read
|
2019-02-05 23:58:51 +00:00
|
|
|
};
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
static void wf_impl_filesystem_cleanup(
|
|
|
|
struct wf_impl_filesystem * filesystem)
|
|
|
|
{
|
|
|
|
fuse_session_reset(filesystem->session);
|
|
|
|
fuse_session_unmount(filesystem->session);
|
|
|
|
fuse_session_destroy(filesystem->session);
|
|
|
|
filesystem->session = NULL;
|
|
|
|
|
|
|
|
free(filesystem->buffer.mem);
|
|
|
|
fuse_opt_free_args(&filesystem->args);
|
|
|
|
|
2020-02-16 20:03:17 +00:00
|
|
|
wf_mountpoint_dispose(filesystem->mountpoint);
|
2019-04-17 20:51:16 +00:00
|
|
|
|
|
|
|
free(filesystem->user_data.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool wf_impl_filesystem_init(
|
|
|
|
struct wf_impl_filesystem * filesystem,
|
|
|
|
struct wf_impl_session * session,
|
2020-02-16 20:03:17 +00:00
|
|
|
char const * name,
|
|
|
|
struct wf_mountpoint * mountpoint)
|
2019-04-17 20:51:16 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
2019-02-05 23:58:51 +00:00
|
|
|
char * argv[] = {"", NULL};
|
|
|
|
filesystem->args.argc = 1;
|
|
|
|
filesystem->args.argv = argv;
|
|
|
|
filesystem->args.allocated = 0;
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
filesystem->user_data.session = session;
|
2019-02-05 23:58:51 +00:00
|
|
|
filesystem->user_data.timeout = 1.0;
|
2019-04-17 20:51:16 +00:00
|
|
|
filesystem->user_data.name = strdup(name);
|
2019-02-05 23:58:51 +00:00
|
|
|
memset(&filesystem->buffer, 0, sizeof(struct fuse_buf));
|
|
|
|
|
2020-02-16 20:03:17 +00:00
|
|
|
filesystem->mountpoint = mountpoint;
|
2019-04-17 20:51:16 +00:00
|
|
|
|
2019-02-05 23:58:51 +00:00
|
|
|
filesystem->session = fuse_session_new(
|
|
|
|
&filesystem->args,
|
2019-03-26 14:35:33 +00:00
|
|
|
&filesystem_operations,
|
|
|
|
sizeof(filesystem_operations),
|
2019-02-05 23:58:51 +00:00
|
|
|
&filesystem->user_data);
|
|
|
|
if (NULL != filesystem->session)
|
|
|
|
{
|
2020-02-16 20:03:17 +00:00
|
|
|
char const * path = wf_mountpoint_get_path(filesystem->mountpoint);
|
|
|
|
result = (0 == fuse_session_mount(filesystem->session, path));
|
2019-04-17 20:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
lws_sock_file_fd_type fd;
|
|
|
|
fd.filefd = fuse_session_fd(filesystem->session);
|
|
|
|
struct lws_protocols const * protocol = lws_get_protocol(session->wsi);
|
|
|
|
filesystem->wsi = lws_adopt_descriptor_vhost(lws_get_vhost(session->wsi), LWS_ADOPT_RAW_FILE_DESC, fd, protocol->name, session->wsi);
|
|
|
|
|
|
|
|
if (NULL == filesystem->wsi)
|
|
|
|
{
|
|
|
|
wf_impl_filesystem_cleanup(filesystem);
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 02:08:02 +00:00
|
|
|
return result;
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
struct wf_impl_filesystem * wf_impl_filesystem_create(
|
|
|
|
struct wf_impl_session * session,
|
2020-02-16 20:03:17 +00:00
|
|
|
char const * name,
|
|
|
|
struct wf_mountpoint * mountpoint)
|
2019-02-05 23:58:51 +00:00
|
|
|
{
|
2019-04-17 20:51:16 +00:00
|
|
|
struct wf_impl_filesystem * filesystem = malloc(sizeof(struct wf_impl_filesystem));
|
2020-03-21 20:22:22 +00:00
|
|
|
bool success = wf_impl_filesystem_init(filesystem, session, name, mountpoint);
|
|
|
|
if (!success)
|
2019-02-05 23:58:51 +00:00
|
|
|
{
|
2020-03-21 20:22:22 +00:00
|
|
|
free(filesystem);
|
|
|
|
filesystem = NULL;
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
return filesystem;
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
void wf_impl_filesystem_dispose(
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_filesystem * filesystem)
|
2019-02-05 23:58:51 +00:00
|
|
|
{
|
2019-04-17 20:51:16 +00:00
|
|
|
wf_impl_filesystem_cleanup(filesystem);
|
|
|
|
free(filesystem);
|
2019-02-05 23:58:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
void wf_impl_filesystem_process_request(
|
|
|
|
struct wf_impl_filesystem * filesystem)
|
2019-02-05 23:58:51 +00:00
|
|
|
{
|
|
|
|
int const result = fuse_session_receive_buf(filesystem->session, &filesystem->buffer);
|
|
|
|
if (0 < result)
|
|
|
|
{
|
|
|
|
fuse_session_process_buf(filesystem->session, &filesystem->buffer);
|
|
|
|
}
|
|
|
|
}
|