2020-02-20 16:15:13 +00:00
|
|
|
#include "webfuse/tests/integration/server.hpp"
|
2019-05-19 12:33:42 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <unistd.h>
|
2020-03-21 08:11:18 +00:00
|
|
|
#include <sys/stat.h>
|
2019-05-19 12:33:42 +00:00
|
|
|
#include "webfuse_adapter.h"
|
|
|
|
#include "webfuse/adapter/impl/server.h"
|
|
|
|
|
|
|
|
#define WF_PATH_MAX (100)
|
|
|
|
|
2020-03-21 08:11:18 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
|
|
|
static void webfuse_test_server_cleanup_mountpoint(
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
char * path = reinterpret_cast<char*>(user_data);
|
|
|
|
rmdir(path);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wf_mountpoint *
|
|
|
|
webfuse_test_server_create_mountpoint(
|
|
|
|
char const * filesystem,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
char const * base_dir = reinterpret_cast<char const*>(user_data);
|
|
|
|
char path[WF_PATH_MAX];
|
|
|
|
snprintf(path, WF_PATH_MAX, "%s/%s", base_dir, filesystem);
|
|
|
|
mkdir(path, 0755);
|
|
|
|
struct wf_mountpoint * mountpoint = wf_mountpoint_create(path);
|
|
|
|
wf_mountpoint_set_userdata(
|
|
|
|
mountpoint,
|
|
|
|
reinterpret_cast<void*>(strdup(path)),
|
|
|
|
&webfuse_test_server_cleanup_mountpoint);
|
|
|
|
|
|
|
|
return mountpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:33:42 +00:00
|
|
|
namespace webfuse_test
|
|
|
|
{
|
|
|
|
|
|
|
|
class Server::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
: is_shutdown_requested(false)
|
|
|
|
{
|
|
|
|
snprintf(base_dir, WF_PATH_MAX, "%s", "/tmp/webfuse_test_integration_XXXXXX");
|
2020-03-01 13:00:49 +00:00
|
|
|
char const * result = mkdtemp(base_dir);
|
|
|
|
if (NULL == result)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("unable to create temp dir");
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
config = wf_server_config_create();
|
|
|
|
wf_server_config_set_port(config, 8080);
|
2020-03-21 08:11:18 +00:00
|
|
|
wf_server_config_set_mountpoint_factory(config,
|
|
|
|
&webfuse_test_server_create_mountpoint,
|
|
|
|
reinterpret_cast<void*>(base_dir));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
server = wf_server_create(config);
|
|
|
|
|
|
|
|
while (!wf_impl_server_is_operational(server))
|
|
|
|
{
|
|
|
|
wf_server_service(server, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
thread = std::thread(Run, this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
RequestShutdown();
|
|
|
|
thread.join();
|
|
|
|
rmdir(base_dir);
|
|
|
|
wf_server_dispose(server);
|
|
|
|
wf_server_config_dispose(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsShutdownRequested()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(shutdown_lock);
|
|
|
|
return is_shutdown_requested;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void RequestShutdown()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(shutdown_lock);
|
|
|
|
is_shutdown_requested = true;
|
2020-03-07 16:13:16 +00:00
|
|
|
wf_server_interrupt(server);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Run(Server::Private * context)
|
|
|
|
{
|
|
|
|
while (!context->IsShutdownRequested())
|
|
|
|
{
|
|
|
|
wf_server_service(context->server, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::mutex shutdown_lock;
|
|
|
|
std::thread thread;
|
|
|
|
bool is_shutdown_requested;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
char base_dir[WF_PATH_MAX];
|
|
|
|
wf_server_config * config;
|
|
|
|
wf_server * server;
|
|
|
|
};
|
|
|
|
|
|
|
|
Server::Server()
|
|
|
|
: d(new Server::Private())
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Server::~Server()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const * Server::GetBaseDir(void) const
|
|
|
|
{
|
|
|
|
return d->base_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|