summaryrefslogtreecommitdiff
path: root/netfs/fuse.cpp
blob: 1b8493aecbd97994d73f79dc7b85ab5e12aac573 (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
#include "pchFuse.hpp"
#include <string.h>
#include <boost/foreach.hpp>
#include "fuse.h"

NetFS::ReqEnv::ReqEnv(const NetFS * netfs)
{
	struct fuse_context * c = fuse_get_context();
	tok = netfs->authtok;
	user = netfs->uentries.getName(c->uid);
	grp = netfs->gentries.getName(c->gid);
}

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

NetFS::~NetFS()
{
	if (authtok) {
		service->disconnect(authtok);
	}
	if (ic) {
		ic->destroy();
	}
}

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

int
NetFS::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::connect()
{
	LOCK;
	connect_nl();
}
void
NetFS::connect_nl()
{
	authtok = service->connect(exportName, "bar");
	BOOST_FOREACH(const OpenFiles::value_type & of, openFiles) {
		of.second->remoteID = filesystem->open(reqEnv(), of.second->path, of.second->flags);
	}
	BOOST_FOREACH(const OpenDirs::value_type & of, openDirs) {
		of.second->remoteID = filesystem->opendir(reqEnv(), of.second->path);
	}
}

int
NetFS::onError(const std::exception & e) throw()
{
	if (dynamic_cast<const NetFSComms::AuthError *>(&e)) {
		// I've become unauthenticated... reauthenticate
		connect();
		return 0;
	}
	return FuseAppBase::onError(e);
}

NetFS::ReqEnv
NetFS::reqEnv()
{
	LOCK;
	if (!service || !filesystem) {
		FuseConfig::ExportPtr e = fc->exports[exportName];
		const std::string & ep = *e->endpoints.begin();

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

		filesystem = NetFSComms::FileSystemPrx::checkedCast(ic->stringToProxy("FileSystem:" + ep));
		if (!filesystem) {
			throw "Invalid filesystem proxy";
		}
	}
	if (authtok == 0) {
		connect_nl();
	}
	return ReqEnv(this);
}

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