1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

added basic C++ provider infrastructure

This commit is contained in:
Falk Werner 2022-12-30 19:44:55 +01:00
parent 9036aba41b
commit 4e1c9e144c
13 changed files with 594 additions and 66 deletions

View File

@ -9,6 +9,7 @@ pkg_check_modules(LWS REQUIRED IMPORTED_TARGET libwebsockets)
add_library(webfuse_static STATIC
src/webfuse/webfuse.cpp
src/webfuse/provider.cpp
src/webfuse/fuse.cpp
src/webfuse/filesystem.cpp
src/webfuse/filesystem/status.cpp
@ -19,6 +20,7 @@ add_library(webfuse_static STATIC
src/webfuse/filesystem/empty_filesystem.cpp
src/webfuse/ws/config.cpp
src/webfuse/ws/server.cpp
src/webfuse/ws/client.cpp
src/webfuse/ws/messagewriter.cpp
src/webfuse/ws/messagereader.cpp
)
@ -31,12 +33,23 @@ add_executable(webfuse
target_link_libraries(webfuse PRIVATE webfuse_static)
if(NOT(WITHOUT_PROVIDER))
add_executable(webfuse_provider
src/provider_main.cpp)
target_link_libraries(webfuse_provider PRIVATE webfuse_static)
endif()
if(NOT(WITHOUT_TEST))
pkg_check_modules(GTEST REQUIRED gtest_main)
pkg_check_modules(GMOCK REQUIRED gmock)
add_executable(alltests
test-src/webfuse/test/thread.cpp
test-src/webfuse/test/tempdir.cpp
test-src/webfuse/test_app.cpp
test-src/webfuse/filesystem/test_status.cpp
test-src/webfuse/filesystem/test_accessmode.cpp
@ -44,7 +57,7 @@ if(NOT(WITHOUT_TEST))
test-src/webfuse/filesystem/test_filemode.cpp
)
target_include_directories(alltests PRIVATE ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})
target_include_directories(alltests PRIVATE test-src ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})
target_compile_options(alltests PRIVATE
${GTEST_CFLAGS} ${GTEST_CFLAGS_OTHER}
${GMOCK_CFLAGS} ${GMOCK_CFLAGS_OTHER}

162
src/provider_main.cpp Normal file
View File

@ -0,0 +1,162 @@
#include "webfuse/provider.hpp"
#include <csignal>
#include <iostream>
namespace
{
static bool shutdown_requested = false;
void on_signal(int _)
{
(void) _;
shutdown_requested = true;
}
class filesystem: public webfuse::filesystem_i
{
public:
explicit filesystem(std::string const & base_path)
: base_path_(base_path)
{
}
~filesystem() override
{
}
int access(std::string const & path, int mode) override
{
return -ENOENT;
}
int getattr(std::string const & path, struct stat * attr) override
{
return -ENOENT;
}
int readlink(std::string const & path, std::string & out) override
{
return -ENOENT;
}
int symlink(std::string const & target, std::string const & linkpath) override
{
return -ENOENT;
}
int link(std::string const & old_path, std::string const & new_path) override
{
return -ENOENT;
}
int rename(std::string const & old_path, std::string const & new_path, int flags) override
{
return -ENOENT;
}
int chmod(std::string const & path, mode_t mode) override
{
return -ENOENT;
}
int chown(std::string const & path, uid_t uid, gid_t gid) override
{
return -ENOENT;
}
int truncate(std::string const & path, uint64_t size, uint64_t handle) override
{
return -ENOENT;
}
int fsync(std::string const & path, bool is_datasync, uint64_t handle) override
{
return -ENOENT;
}
int utimens(std::string const &path, struct timespec tv[2], uint64_t handle) override
{
return -ENOENT;
}
int open(std::string const & path, int flags, uint64_t & handle) override
{
return -ENOENT;
}
int mknod(std::string const & path, mode_t mode, dev_t rdev) override
{
return -ENOENT;
}
int create(std::string const & path, mode_t mode, uint64_t & handle) override
{
return -ENOENT;
}
int release(std::string const & path, uint64_t handle) override
{
return -ENOENT;
}
int unlink(std::string const & path) override
{
return -ENOENT;
}
int read(std::string const & path, char * buffer, size_t buffer_size, uint64_t offset, uint64_t handle) override
{
return -ENOENT;
}
int write(std::string const & path, char const * buffer, size_t buffer_size, uint64_t offset, uint64_t handle) override
{
return -ENOENT;
}
int mkdir(std::string const & path, mode_t mode) override
{
return -ENOENT;
}
int readdir(std::string const & path, std::vector<std::string> & entries, uint64_t handle) override
{
return -ENOENT;
}
int rmdir(std::string const & path) override
{
return -ENOENT;
}
int statfs(std::string const & path, struct statvfs * statistics) override
{
return -ENOENT;
}
private:
std::string base_path_;
};
}
int main(int argc, char* argv[])
{
signal(SIGINT, &on_signal);
signal(SIGTERM, &on_signal);
filesystem fs(".");
webfuse::provider provider(fs);
provider.connect("ws://localhost:8080/");
while (!shutdown_requested)
{
provider.service();
}
return EXIT_SUCCESS;
}

View File

@ -22,7 +22,7 @@ int filesystem::access(std::string const & path, int mode)
{
try
{
messagewriter req(message_type::access_req);
messagewriter req(request_type::access);
req.write_str(path);
req.write_access_mode(mode);
auto reader = proxy.perform(std::move(req));
@ -38,7 +38,7 @@ int filesystem::getattr(std::string const & path, struct stat * attr)
{
try
{
messagewriter req(message_type::getattr_req);
messagewriter req(request_type::getattr);
req.write_str(path);
auto reader = proxy.perform(std::move(req));
int const result = reader.read_result();
@ -58,7 +58,7 @@ int filesystem::readlink(std::string const & path, std::string & out)
{
try
{
messagewriter req(message_type::readlink_req);
messagewriter req(request_type::readlink);
req.write_str(path);
auto reader = proxy.perform(std::move(req));
int const result = reader.read_result();
@ -78,7 +78,7 @@ int filesystem::symlink(std::string const & target, std::string const & linkpath
{
try
{
messagewriter req(message_type::symlink_req);
messagewriter req(request_type::symlink);
req.write_str(target);
req.write_str(linkpath);
auto reader = proxy.perform(std::move(req));
@ -94,7 +94,7 @@ int filesystem::link(std::string const & old_path, std::string const & new_path)
{
try
{
messagewriter req(message_type::link_req);
messagewriter req(request_type::link);
req.write_str(old_path);
req.write_str(new_path);
auto reader = proxy.perform(std::move(req));
@ -110,7 +110,7 @@ int filesystem::rename(std::string const & old_path, std::string const & new_pat
{
try
{
messagewriter req(message_type::rename_req);
messagewriter req(request_type::rename);
req.write_str(old_path);
req.write_str(new_path);
req.write_rename_flags(flags);
@ -127,7 +127,7 @@ int filesystem::chmod(std::string const & path, mode_t mode)
{
try
{
messagewriter req(message_type::chmod_req);
messagewriter req(request_type::chmod);
req.write_str(path);
req.write_mode(mode);
auto reader = proxy.perform(std::move(req));
@ -143,7 +143,7 @@ int filesystem::chown(std::string const & path, uid_t uid, gid_t gid)
{
try
{
messagewriter req(message_type::chown_req);
messagewriter req(request_type::chown);
req.write_str(path);
req.write_uid(uid);
req.write_gid(gid);
@ -160,7 +160,7 @@ int filesystem::truncate(std::string const & path, uint64_t size, uint64_t handl
{
try
{
messagewriter req(message_type::truncate_req);
messagewriter req(request_type::truncate);
req.write_str(path);
req.write_u64(size);
req.write_u64(handle);
@ -177,7 +177,7 @@ int filesystem::fsync(std::string const & path, bool is_datasync, uint64_t handl
{
try
{
messagewriter req(message_type::fsync_req);
messagewriter req(request_type::fsync);
req.write_str(path);
req.write_bool(is_datasync);
req.write_u64(handle);
@ -194,7 +194,7 @@ int filesystem::utimens(std::string const &path, struct timespec tv[2], uint64_t
{
try
{
messagewriter req(message_type::utimens_req);
messagewriter req(request_type::utimens);
req.write_str(path);
req.write_time(tv[0]);
req.write_time(tv[1]);
@ -213,7 +213,7 @@ int filesystem::open(std::string const & path, int flags, uint64_t & handle)
{
try
{
messagewriter req(message_type::open_req);
messagewriter req(request_type::open);
req.write_str(path);
req.write_openflags(flags);
auto reader = proxy.perform(std::move(req));
@ -234,7 +234,7 @@ int filesystem::mknod(std::string const & path, mode_t mode, dev_t rdev)
{
try
{
messagewriter req(message_type::mknod_req);
messagewriter req(request_type::mknod);
req.write_str(path);
req.write_mode(mode);
req.write_u64(rdev);
@ -251,7 +251,7 @@ int filesystem::create(std::string const & path, mode_t mode, uint64_t & handle)
{
try
{
messagewriter req(message_type::create_req);
messagewriter req(request_type::create);
req.write_str(path);
req.write_mode(mode);
auto reader = proxy.perform(std::move(req));
@ -272,7 +272,7 @@ int filesystem::release(std::string const & path, uint64_t handle)
{
try
{
messagewriter req(message_type::release_req);
messagewriter req(request_type::release);
req.write_str(path);
req.write_u64(handle);
auto reader = proxy.perform(std::move(req));
@ -288,7 +288,7 @@ int filesystem::unlink(std::string const & path)
{
try
{
messagewriter req(message_type::unlink_req);
messagewriter req(request_type::unlink);
req.write_str(path);
auto reader = proxy.perform(std::move(req));
return reader.read_result();
@ -303,7 +303,7 @@ int filesystem::read(std::string const & path, char * buffer, size_t buffer_size
{
try
{
messagewriter req(message_type::read_req);
messagewriter req(request_type::read);
req.write_str(path);
req.write_u32(buffer_size);
req.write_u64(offset);
@ -334,7 +334,7 @@ int filesystem::write(std::string const & path, char const * buffer, size_t buff
{
try
{
messagewriter req(message_type::write_req);
messagewriter req(request_type::write);
req.write_str(path);
req.write_data(buffer, buffer_size);
req.write_u64(offset);
@ -352,7 +352,7 @@ int filesystem::mkdir(std::string const & path, mode_t mode)
{
try
{
messagewriter req(message_type::mkdir_req);
messagewriter req(request_type::mkdir);
req.write_str(path);
req.write_mode(mode);
auto reader = proxy.perform(std::move(req));
@ -368,7 +368,7 @@ int filesystem::readdir(std::string const & path, std::vector<std::string> & ent
{
try
{
messagewriter req(message_type::readdir_req);
messagewriter req(request_type::readdir);
req.write_str(path);
auto resp = proxy.perform(std::move(req));
int result = resp.read_result();
@ -388,7 +388,7 @@ int filesystem::rmdir(std::string const & path)
{
try
{
messagewriter req(message_type::rmdir_req);
messagewriter req(request_type::rmdir);
req.write_str(path);
auto reader = proxy.perform(std::move(req));
return reader.read_result();
@ -403,7 +403,7 @@ int filesystem::statfs(std::string const & path, struct statvfs * statistics)
{
try
{
messagewriter req(message_type::statfs_req);
messagewriter req(request_type::statfs);
req.write_str(path);
auto reader = proxy.perform(std::move(req));
int result = reader.read_result();

View File

@ -1,38 +0,0 @@
#ifndef WEBFUSE_MESSAGETYPE_HPP
#define WEBFUSE_MESSAGETYPE_HPP
#include <cinttypes>
namespace webfuse
{
enum class message_type: uint8_t
{
access_req = 0x01,
getattr_req = 0x02,
readlink_req = 0x03,
symlink_req = 0x04,
link_req = 0x05,
rename_req = 0x06,
chmod_req = 0x07,
chown_req = 0x08,
truncate_req = 0x09,
fsync_req = 0x0a,
open_req = 0x0b,
mknod_req = 0x0c,
create_req = 0x0d,
release_req = 0x0e,
unlink_req = 0x0f,
read_req = 0x10,
write_req = 0x11,
mkdir_req = 0x12,
readdir_req = 0x13,
rmdir_req = 0x14,
statfs_req = 0x15,
utimens_req = 0x16
};
}
#endif

94
src/webfuse/provider.cpp Normal file
View File

@ -0,0 +1,94 @@
#include "webfuse/provider.hpp"
#include "webfuse/ws/client.hpp"
namespace webfuse
{
class provider::detail
{
public:
detail(filesystem_i & fs)
: fs_(fs)
, client([this](auto& reader) { return this->on_message(reader); })
{
}
~detail()
{
}
void connect(std::string const & url)
{
client.connect(url);
}
void service()
{
client.service();
}
messagewriter on_message(messagereader & reader)
{
auto message_id = reader.read_u32();
auto request_type = reader.read_u8();
messagewriter writer(response_type::unknown);
writer.set_id(message_id);
switch (request_type)
{
default:
break;
}
return std::move(writer);
}
private:
filesystem_i & fs_;
ws_client client;
};
provider::provider(filesystem_i & fs)
: d(new detail(fs))
{
}
provider::~provider()
{
delete d;
}
provider::provider(provider && other)
{
this->d = other.d;
other.d = nullptr;
}
provider& provider::operator=(provider && other)
{
if (this != &other)
{
delete this->d;
this->d = other.d;
other.d = nullptr;
}
return *this;
}
void provider::connect(std::string const & url)
{
d->connect(url);
}
void provider::service()
{
d->service();
}
}

28
src/webfuse/provider.hpp Normal file
View File

@ -0,0 +1,28 @@
#ifndef WEBFUSE_PROVIDER_I_HPP
#define WEBFUSE_PROVIDER_I_HPP
#include "webfuse/filesystem/filesystem_i.hpp"
#include <string>
namespace webfuse
{
class provider
{
provider(provider const &) = delete;
provider& operator=(provider const &) = delete;
public:
provider(filesystem_i & fs);
~provider();
provider(provider && other);
provider& operator=(provider && other);
void connect(std::string const & url);
void service();
private:
class detail;
detail * d;
};
}
#endif

View File

@ -0,0 +1,38 @@
#ifndef WEBFUSE_REQUEST_TYPE
#define WEBFUSE_REQUEST_TYPE
#include <cinttypes>
namespace webfuse
{
enum class request_type: uint8_t
{
unknown = 0x00,
access = 0x01,
getattr = 0x02,
readlink = 0x03,
symlink = 0x04,
link = 0x05,
rename = 0x06,
chmod = 0x07,
chown = 0x08,
truncate = 0x09,
fsync = 0x0a,
open = 0x0b,
mknod = 0x0c,
create = 0x0d,
release = 0x0e,
unlink = 0x0f,
read = 0x10,
write = 0x11,
mkdir = 0x12,
readdir = 0x13,
rmdir = 0x14,
statfs = 0x15,
utimens = 0x16
};
}
#endif

View File

@ -0,0 +1,38 @@
#ifndef WEBFUSE_RESPONSE_TYPE
#define WEBFUSE_RESPONSE_TYPE
#include <cinttypes>
namespace webfuse
{
enum class response_type: uint8_t
{
unknown = 0x80,
access = 0x81,
getattr = 0x82,
readlink = 0x83,
symlink = 0x84,
link = 0x85,
rename = 0x86,
chmod = 0x87,
chown = 0x88,
truncate = 0x89,
fsync = 0x8a,
open = 0x8b,
mknod = 0x8c,
create = 0x8d,
release = 0x8e,
unlink = 0x8f,
read = 0x90,
write = 0x91,
mkdir = 0x92,
readdir = 0x93,
rmdir = 0x94,
statfs = 0x95,
utimens = 0x96
};
}
#endif

150
src/webfuse/ws/client.cpp Normal file
View File

@ -0,0 +1,150 @@
#include "webfuse/ws/client.hpp"
#include <libwebsockets.h>
#include <cstring>
#include <iostream>
namespace
{
extern "C" int webfuse_client_callback(lws * wsi, lws_callback_reasons reason, void* user, void * in, size_t length)
{
int result = 0;
lws_protocols const * protocol = lws_get_protocol(wsi);
if (nullptr != protocol)
{
switch(reason)
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
std::cout << "established" << std::endl;
break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
std::cout << "connect error" << std::endl;
break;
case LWS_CALLBACK_CLIENT_CLOSED:
std::cout << "closed" << std::endl;
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
std::cout << "receive" << std::endl;
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
// fall-through
case LWS_CALLBACK_CLIENT_WRITEABLE:
std::cout << "writable" << std::endl;
break;
default:
break;
}
}
return result;
}
}
namespace webfuse
{
class ws_client::detail
{
detail(detail const &) = delete;
detail& operator=(detail const &) = delete;
detail(detail &&) = delete;
detail& operator=(detail &&) = delete;
public:
detail(ws_client_handler handler)
: handler_(handler)
{
memset(reinterpret_cast<void*>(protocols), 0, sizeof(lws_protocols) * 2);
protocols[0].callback = &webfuse_client_callback;
protocols[0].name = "webfuse2-client";
protocols[0].per_session_data_size = 0;
protocols[0].user = nullptr;
memset(reinterpret_cast<void*>(&info), 0, sizeof(lws_context_creation_info));
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = protocols;
info.uid = -1;
info.gid = -1;
context = lws_create_context(&info);
}
~detail()
{
lws_context_destroy(context);
}
void connect(std::string const & url)
{
lws_client_connect_info info;
memset(reinterpret_cast<void*>(&info), 0, sizeof(lws_client_connect_info));
info.context = context;
info.port = 8081;
info.address = "localhost";
info.host = "localhost";
info.path = "/";
info.origin = "localhost";
info.ssl_connection = 0;
info.protocol = "webfuse2";
info.local_protocol_name = "webfuse2-client";
info.pwsi = &wsi;
lws_client_connect_via_info(&info);
}
void service()
{
lws_service(context, 0);
}
private:
ws_client_handler handler_;
lws_context_creation_info info;
lws_protocols protocols[2];
lws_context * context;
lws * wsi;
};
ws_client::ws_client(ws_client_handler handler)
: d(new detail(handler))
{
}
ws_client::~ws_client()
{
delete d;
}
ws_client::ws_client(ws_client && other)
{
this->d = other.d;
other.d = nullptr;
}
ws_client& ws_client::operator=(ws_client && other)
{
if (this != &other)
{
delete this->d;
this->d = other.d;
other.d = nullptr;
}
return *this;
}
void ws_client::connect(std::string url)
{
d->connect(url);
}
void ws_client::service()
{
d->service();
}
}

33
src/webfuse/ws/client.hpp Normal file
View File

@ -0,0 +1,33 @@
#ifndef WEBFUSE_WSCLIENT_HPP
#define WEBFUSE_WSCLIENT_HPP
#include "webfuse/ws/messagewriter.hpp"
#include "webfuse/ws/messagereader.hpp"
#include <functional>
namespace webfuse
{
using ws_client_handler = std::function<messagewriter (messagereader & reader)>;
class ws_client
{
ws_client(ws_client const &) = delete;
ws_client& operator=(ws_client const &) = delete;
public:
ws_client(ws_client_handler handler);
~ws_client();
ws_client(ws_client && other);
ws_client& operator=(ws_client && other);
void connect(std::string url);
void service();
private:
class detail;
detail * d;
};
}
#endif

View File

@ -11,12 +11,20 @@ namespace webfuse
constexpr uint8_t const rename_noreplace = 0x01;
constexpr uint8_t const rename_exchange = 0x02;
messagewriter::messagewriter(message_type msg_type)
messagewriter::messagewriter(request_type req_type)
: id(0)
, data(LWS_PRE)
{
write_u32(0);
write_u8(static_cast<uint8_t>(msg_type));
write_u8(static_cast<uint8_t>(req_type));
}
messagewriter::messagewriter(response_type res_type)
: id(0)
, data(LWS_PRE)
{
write_u32(0);
write_u8(static_cast<uint8_t>(res_type));
}
messagewriter::messagewriter(messagewriter && other)

View File

@ -1,7 +1,8 @@
#ifndef WEBFUSE_MESSAGEWRITER_HPP
#define WEBFUSE_MESSAGEWRITER_HPP
#include "webfuse/message_type.hpp"
#include "webfuse/request_type.hpp"
#include "webfuse/response_type.hpp"
#include <cinttypes>
#include <string>
@ -15,7 +16,8 @@ class messagewriter
messagewriter(messagewriter const &) = delete;
messagewriter& operator=(messagewriter const &) = delete;
public:
explicit messagewriter(message_type msg_type);
explicit messagewriter(request_type req_type);
explicit messagewriter(response_type res_type);
~messagewriter() = default;
messagewriter(messagewriter && other);
messagewriter& operator=(messagewriter && other);

View File

@ -106,7 +106,7 @@ static int ws_server_callback(struct lws *wsi, enum lws_callback_reasons reason,
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
{
webfuse::messagewriter writer(webfuse::message_type::access_req);
webfuse::messagewriter writer(webfuse::request_type::unknown);
bool has_msg = false;
bool has_more = false;