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

483 lines
13 KiB
C++
Raw Normal View History

#include "webfuse/provider.hpp"
2023-01-04 21:05:39 +00:00
#include "webfuse/version.hpp"
2022-12-30 22:06:47 +00:00
#include <unistd.h>
2023-01-06 20:59:19 +00:00
#include <fcntl.h>
2022-12-30 22:06:47 +00:00
#include <sys/stat.h>
#include <dirent.h>
2023-01-04 21:05:39 +00:00
#include <getopt.h>
#include <csignal>
#include <iostream>
namespace
{
2023-01-04 21:05:39 +00:00
enum class command
{
run,
show_help,
show_version
};
class context
{
public:
context(int argc, char* argv[])
: base_path(".")
, url("")
, cmd(command::run)
, exit_code()
{
struct option const long_options[] =
{
{"path" , required_argument, nullptr, 'p'},
{"url" , required_argument, nullptr, 'u'},
{"ca-path", required_argument, nullptr, 'a'},
2023-01-04 21:05:39 +00:00
{"version", no_argument , nullptr, 'v'},
{"help" , no_argument , nullptr, 'h'},
{nullptr , 0 , nullptr, 0 }
};
optind = 0;
opterr = 0;
bool finished = false;
while (!finished)
{
int option_index = 0;
const int c = getopt_long(argc, argv, "p:u:a:vh", long_options, &option_index);
2023-01-04 21:05:39 +00:00
switch (c)
{
case -1:
finished = true;
break;
case 'p':
base_path = optarg;
break;
case 'u':
url = optarg;
break;
case 'a':
ca_path = optarg;
break;
2023-01-04 21:05:39 +00:00
case 'h':
cmd = command::show_help;
break;
case 'v':
cmd = command::show_version;
break;
default:
std::cerr << "error: unknown option" << std::endl;
cmd = command::show_help;
exit_code = EXIT_FAILURE;
finished = true;
break;
}
}
if ((cmd == command::run) && (url.empty()))
{
std::cerr << "error: missing url" << std::endl;
cmd = command::show_help;
exit_code = EXIT_FAILURE;
}
}
std::string base_path;
std::string url;
std::string ca_path;
2023-01-04 21:05:39 +00:00
command cmd;
int exit_code;
};
void print_usage()
{
std::cout << R"(webfuse2 provider, (c) 2022 by Falk Werner
expose a local directory via webfuse2
Usage:
2023-01-15 20:00:10 +00:00
webfuse_provider -u <url> [-p <path>] [-a <ca_path>]
2023-01-04 21:05:39 +00:00
Options:
--url, -u set url of webfuse2 service
--path, -p set path of directory to expose (default: .)
--ca-path, -a set path of ca file (default: not set)
2023-01-04 21:05:39 +00:00
--version, -v print version and quit
--help, -h print this message and quit
Examples:
webfuse-provider -u ws://localhost:8080/
webfuse-provider -u ws://localhost:8080/ -p /some/directory
)";
}
2023-01-04 21:05:39 +00:00
void print_version()
{
std::cout << webfuse::get_version() << std::endl;
}
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)
{
2023-01-06 20:59:19 +00:00
char buffer[PATH_MAX];
char * resolved_path = ::realpath(base_path.c_str(), buffer);
if (nullptr == resolved_path)
{
throw std::runtime_error("failed to resolve path");
}
struct stat info;
int const rc = stat(resolved_path, &info);
if (!S_ISDIR(info.st_mode))
{
throw std::runtime_error("path is not a directory");
}
2023-01-06 20:59:19 +00:00
base_path_ = resolved_path;
}
~filesystem() override
{
}
int access(std::string const & path, int mode) override
{
2022-12-30 22:06:47 +00:00
auto const full_path = get_full_path(path);
auto const result = ::access(full_path.c_str(), mode);
return (result == 0) ? 0 : -errno;
}
int getattr(std::string const & path, struct stat * attr) override
{
2022-12-30 22:06:47 +00:00
auto const full_path = get_full_path(path);
auto const result = lstat(full_path.c_str(), attr);
return (result == 0) ? 0 : -errno;
}
int readlink(std::string const & path, std::string & out) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
char buffer[PATH_MAX];
int const result = ::readlink(full_path.c_str(), buffer, PATH_MAX);
if ((0 <= result) && (result < PATH_MAX))
{
buffer[result] = '\0';
out = buffer;
}
return (result >= 0) ? 0 : -errno;
}
2023-01-06 20:59:19 +00:00
int symlink(std::string const & from, std::string const & to) override
{
2023-01-06 20:59:19 +00:00
auto const full_from = ('/' == from.at(0)) ? get_full_path(from) : from;
auto const full_to = get_full_path(to);
int const result = ::symlink(full_from.c_str(), full_to.c_str());
return (result == 0) ? 0 : -errno;
}
int link(std::string const & old_path, std::string const & new_path) override
{
2023-01-06 20:59:19 +00:00
auto const from = get_full_path(old_path);
auto const to = get_full_path(new_path);
int const result = ::link(from.c_str(), to.c_str());
return (result == 0) ? 0 : -errno;
}
int rename(std::string const & old_path, std::string const & new_path, int flags) override
{
2023-01-06 20:59:19 +00:00
auto const full_old = get_full_path(old_path);
auto const full_new = get_full_path(new_path);
int const result = ::renameat2(-1, full_old.c_str(), -1, full_new.c_str(), flags);
return (result == 0) ? 0 : -errno;
}
int chmod(std::string const & path, mode_t mode) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const result = ::chmod(full_path.c_str(), mode);
return (result == 0) ? 0 : -errno;
}
int chown(std::string const & path, uid_t uid, gid_t gid) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const result = ::chown(full_path.c_str(), uid, gid);
return (result == 0) ? 0 : -errno;
}
int truncate(std::string const & path, uint64_t size, uint64_t handle) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int result = 0;
if (handle == webfuse::invalid_handle)
{
result = ::truncate(full_path.c_str(), size);
}
else
{
result = ::ftruncate(static_cast<int>(handle), size);
}
return (result == 0) ? 0 : -errno;
}
int fsync(std::string const & path, bool is_datasync, uint64_t handle) override
{
2023-01-06 20:59:19 +00:00
int result = 0;
if (handle != webfuse::invalid_handle)
{
if (!is_datasync)
{
result = ::fsync(static_cast<int>(handle));
}
else
{
result = ::fdatasync(static_cast<int>(handle));
}
}
// we do not sync files, which are not open
return (result == 0) ? 0 : -errno;
}
int utimens(std::string const &path, struct timespec const tv[2], uint64_t handle) override
{
2023-01-06 20:59:19 +00:00
int result = 0;
if (handle == webfuse::invalid_handle)
{
auto const full_path = get_full_path(path);
result = ::utimensat(-1, full_path.c_str(), tv, 0);
}
else
{
result = ::futimens(static_cast<int>(handle), tv);
}
return (result == 0) ? 0 : -errno;
}
int open(std::string const & path, int flags, uint64_t & handle) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const fd = ::open(full_path.c_str(), flags);
if (0 <= fd)
{
handle = static_cast<int>(fd);
}
return (0 <= fd) ? 0 : -errno;
}
int mknod(std::string const & path, mode_t mode, dev_t rdev) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const result = ::mknod(full_path.c_str(), mode, rdev);
return (result == 0) ? 0 : -errno;
}
int create(std::string const & path, mode_t mode, uint64_t & handle) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const fd = ::creat(full_path.c_str(), mode);
if (0 <= fd)
{
handle = static_cast<int>(fd);
}
return (0 <= fd) ? 0 : -errno;
}
int release(std::string const & path, uint64_t handle) override
{
2023-01-06 20:59:19 +00:00
int result = 0;
if (handle != webfuse::invalid_handle)
{
result = ::close(static_cast<int>(handle));
}
return (result == 0) ? 0 : -errno;
}
int unlink(std::string const & path) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const result = ::unlink(full_path.c_str());
return (result == 0) ? 0 : -errno;
}
int read(std::string const & path, char * buffer, size_t buffer_size, uint64_t offset, uint64_t handle) override
{
2023-01-06 20:59:19 +00:00
int result = -1;
if (handle != webfuse::invalid_handle)
{
auto const full_path = get_full_path(path);
int fd = ::open(full_path.c_str(), O_RDONLY);
if (0 <= fd)
{
result = ::pread(fd, buffer, buffer_size, offset);
::close(fd);
}
}
else
{
result = ::pread(static_cast<int>(handle), buffer, buffer_size, offset);
}
return (result >= 0) ? result : -errno;
}
int write(std::string const & path, char const * buffer, size_t buffer_size, uint64_t offset, uint64_t handle) override
{
2023-01-06 20:59:19 +00:00
int result = -1;
if (handle == webfuse::invalid_handle)
{
auto const full_path = get_full_path(path);
int fd = ::open(full_path.c_str(), O_WRONLY);
if (0 <= fd)
{
result = ::pwrite(fd, buffer, buffer_size, offset);
::close(fd);
}
}
else
{
result = ::pwrite(static_cast<int>(handle), buffer, buffer_size, offset);
}
return (result >= 0) ? result : -errno;
}
int mkdir(std::string const & path, mode_t mode) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const result = ::mkdir(full_path.c_str(), mode);
return (result == 0) ? 0 : -errno;
}
2023-01-01 17:49:59 +00:00
int readdir(std::string const & path, std::vector<std::string> & entries) override
{
2022-12-30 22:06:47 +00:00
auto const full_path = get_full_path(path);
int result = 0;
DIR * directory = opendir(full_path.c_str());
if (NULL != directory)
{
dirent * entry = ::readdir(directory);
while (entry != nullptr)
{
entries.push_back(std::string(entry->d_name));
entry = ::readdir(directory);
}
closedir(directory);
}
else
{
result = -errno;
}
return result;
}
int rmdir(std::string const & path) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const result = ::rmdir(full_path.c_str());
return (result == 0) ? 0 : -errno;
}
int statfs(std::string const & path, struct statvfs * statistics) override
{
2023-01-06 20:59:19 +00:00
auto const full_path = get_full_path(path);
int const result = ::statvfs(full_path.c_str(), statistics);
return (result == 0) ? 0 : -errno;
}
private:
2022-12-30 22:06:47 +00:00
std::string get_full_path(std::string const & path)
{
return base_path_ + path;
}
std::string base_path_;
};
}
int main(int argc, char* argv[])
{
2023-01-04 21:05:39 +00:00
context ctx(argc, argv);
2023-01-04 21:05:39 +00:00
switch (ctx.cmd)
{
2023-01-04 21:05:39 +00:00
case command::run:
2023-01-06 20:59:19 +00:00
try
2023-01-04 21:05:39 +00:00
{
signal(SIGINT, &on_signal);
signal(SIGTERM, &on_signal);
filesystem fs(ctx.base_path);
webfuse::provider provider(fs, ctx.ca_path);
2023-01-04 21:05:39 +00:00
provider.set_connection_listener([](bool connected) {
if (!connected)
{
shutdown_requested = true;
}
});
provider.connect(ctx.url);
while (!shutdown_requested)
{
provider.service();
}
}
2023-01-06 20:59:19 +00:00
catch (std::exception const & ex)
{
std::cerr << "error: " << ex.what() << std::endl;
ctx.exit_code = EXIT_FAILURE;
}
catch (...)
{
std::cerr << "error: unspecified error" << std::endl;
ctx.exit_code = EXIT_FAILURE;
}
2023-01-04 21:05:39 +00:00
break;
case command::show_version:
print_version();
break;
case command::show_help:
// fall-through
default:
print_usage();
break;
}
2023-01-04 21:05:39 +00:00
return ctx.exit_code;
}