2014-11-18 00:14:33 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include "FuseThread.h"
|
|
|
|
#include <csignal>
|
2016-02-11 12:53:42 +01:00
|
|
|
#include <cpp-utils/assert/assert.h>
|
|
|
|
#include "fspp/fuse/Fuse.h"
|
2014-11-18 00:14:33 +01:00
|
|
|
|
2015-12-16 00:20:38 +01:00
|
|
|
using boost::thread;
|
|
|
|
using boost::chrono::seconds;
|
2014-11-18 00:14:33 +01:00
|
|
|
using std::string;
|
2016-05-10 01:07:02 +02:00
|
|
|
using std::vector;
|
|
|
|
namespace bf = boost::filesystem;
|
2014-11-18 00:14:33 +01:00
|
|
|
|
|
|
|
using fspp::fuse::Fuse;
|
|
|
|
|
|
|
|
FuseThread::FuseThread(Fuse *fuse)
|
2015-10-17 20:35:17 +02:00
|
|
|
:_fuse(fuse), _child() {
|
2014-11-18 00:14:33 +01:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:07:02 +02:00
|
|
|
void FuseThread::start(const bf::path &mountDir, const vector<string> &fuseOptions) {
|
|
|
|
_child = thread([this, mountDir, fuseOptions] () {
|
|
|
|
_fuse->run(mountDir, fuseOptions);
|
2014-11-18 00:14:33 +01:00
|
|
|
});
|
|
|
|
//Wait until it is running (busy waiting is simple and doesn't hurt much here)
|
|
|
|
while(!_fuse->running()) {}
|
2016-02-14 01:18:19 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
// On Mac OS X, _fuse->running() returns true too early, because osxfuse calls init() when it's not ready yet. Give it a bit time.
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
#endif
|
2014-11-18 00:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FuseThread::stop() {
|
2018-01-15 02:58:48 +01:00
|
|
|
if (0 != pthread_kill(_child.native_handle(), SIGINT)) {
|
|
|
|
throw std::runtime_error("Error sending stop signal");
|
|
|
|
}
|
|
|
|
bool thread_stopped = _child.try_join_for(seconds(10));
|
2015-12-16 00:20:38 +01:00
|
|
|
ASSERT(thread_stopped, "FuseThread could not be stopped");
|
2014-11-21 01:12:04 +01:00
|
|
|
//Wait until it is properly shutdown (busy waiting is simple and doesn't hurt much here)
|
|
|
|
while (_fuse->running()) {}
|
2014-11-18 00:14:33 +01:00
|
|
|
}
|