summaryrefslogtreecommitdiff
path: root/netfs/fuse/fuse.cpp
blob: d9d49fc4174e598388f7c032d17e9f170cc5478f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "pch.hpp"
#include <string.h>
#include <boost/foreach.hpp>
#include "fuse.h"
#include "lockHelpers.h"

NetFS::FuseApp::FuseApp(int & argc, char ** argv) :
	_argc(argc),
	_argv(argv),
	openDirID(0),
	openFileID(0)
{
}

NetFS::FuseApp::~FuseApp()
{
	BOOST_FOREACH(const OpenDirs::value_type & of, openDirs) {
		of.second->remote->close();
	}
	BOOST_FOREACH(const OpenFiles::value_type & of, openFiles) {
		of.second->remote->close();
	}
	volume->disconnect();
	if (ic) {
		ic->destroy();
	}
}

void *
NetFS::FuseApp::init(struct fuse_conn_info *)
{
	ic = Ice::initialize(_argc, _argv);
	fc = FuseConfig::Load(configPath);
	return NULL;
}

int
NetFS::FuseApp::opt_parse(void *, const char * arg, int, struct fuse_args *)
{
	if (strncmp(arg, "--Ice.", 6) == 0) {
		return 0;
	}
	else if (arg[0] == '-') {
		return 1;
	}
	else if (exportName.empty()) {
		const char * colon = strchr(arg, ':');
		exportName = colon + 1;
		configPath.assign(arg, colon);
		return 0;
	}
	else if (mountPoint.empty()) {
		mountPoint = arg;
		return 1;
	}
	return 1;
}

void
NetFS::FuseApp::connectToService()
{
	if (!service) {
		Lock(_lock);
		FuseConfig::ExportPtr e = fc->exports[exportName];
		const std::string & ep = *e->endpoints.begin();

		service = NetFS::ServicePrx::checkedCast(ic->stringToProxy("Service:" + ep));
		if (!service) {
			throw "Invalid service proxy";
		}
	}
}

void
NetFS::FuseApp::connectToVolume()
{
	if (!volume) {
		volume = service->connect(exportName, "bar");
		if (!volume) {
			throw "Invalid filesystem proxy";
		}
	}
}

void
NetFS::FuseApp::connectHandles()
{
	BOOST_FOREACH(const OpenFiles::value_type & of, openFiles) {
		try {
			of.second->remote->ice_ping();
		}
		catch (const Ice::ObjectNotExistException &) {
			of.second->remote = volume->open(reqEnv(), of.second->path, of.second->flags);
		}
	}
	BOOST_FOREACH(const OpenDirs::value_type & of, openDirs) {
		try {
			of.second->remote->ice_ping();
		}
		catch (const Ice::ObjectNotExistException &) {
			of.second->remote = volume->opendir(reqEnv(), of.second->path);
		}
	}
}

void
NetFS::FuseApp::verifyConnection()
{
	Lock(_lock);
	if (service) {
		try {
			service->ice_ping();
		}
		catch (const Ice::Exception &) {
			service = NULL;
		}
	}
	if (volume) {
		try {
			volume->ice_ping();
		}
		catch (const Ice::Exception &) {
			volume = NULL;
		}
	}
}

int
NetFS::FuseApp::onError(const std::exception & e) throw()
{
	if (dynamic_cast<const Ice::ObjectNotExistException *>(&e)) {
		verifyConnection();
		connectToService();
		connectToVolume();
		connectHandles();
		return 0;
	}
	return FuseAppBase::onError(e);
}

NetFS::ReqEnv
NetFS::FuseApp::reqEnv()
{
	connectToService();
	connectToVolume();
	struct fuse_context * c = fuse_get_context();
	return { uentries.getName(c->uid), gentries.getName(c->gid) };
}

int
main(int argc, char* argv[])
{
	return FuseAppBase::run(argc, argv, new NetFS::FuseApp(argc, argv));
}