summaryrefslogtreecommitdiff
path: root/netfs/fuse/fuseApp.cpp
blob: a752e47dfc5eec7a64edaa256eafeead9efe015a (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <pch.hpp>
#include <Glacier2/Router.h>
#include <string.h>
#include "fuseApp.h"
#include "lockHelpers.h"
#include "cache.impl.h"
#include <entCache.h>
#include <slicer/slicer.h>
#include <xml/serializer.h>

template class Cache<struct stat, std::string, IceUtil::Shared, IceUtil::Handle<Cacheable<struct stat, std::string, IceUtil::Shared>>>;
template class OptimisticCallCacheable<struct stat, std::string, IceUtil::Shared>;

NetFS::FuseApp::FuseApp(const Ice::StringSeq & a) :
	args(a),
	sessionOpened(false),
	openDirID(0),
	openFileID(0)
{
}

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

NetFS::Client::ConfigurationPtr
NetFS::FuseApp::ReadConfiguration(const std::string & path) const
{
	return Slicer::Deserialize<Slicer::XmlFileDeserializer, NetFS::Client::Configuration>(path);
}

void *
NetFS::FuseApp::init(struct fuse_conn_info *)
{
	ic = Ice::initialize(args);
	auto fc = ReadConfiguration(configPath);
	auto e = fc->Resources.find(resourceName);
	if (e == fc->Resources.end()) {
		throw std::runtime_error("No such resource: " + resourceName);
	}
	fcr = e->second;
	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 (strncmp(arg, "_netdev", 7) == 0) {
		return 0;
	}
	else if (arg[0] == '-') {
		return 1;
	}
	else if (resourceName.empty()) {
		const char * colon = strchr(arg, ':');
		resourceName = colon + 1;
		configPath.assign(arg, colon);
		return 0;
	}
	else if (mountPoint.empty()) {
		mountPoint = arg;
		return 1;
	}
	return 1;
}

void
NetFS::FuseApp::connectSession()
{
	if (!sessionOpened && ic->getDefaultRouter()) {
		Lock(_lock);
		auto router = Glacier2::RouterPrx::checkedCast(ic->getDefaultRouter());
		session = router->createSession("", "");
		if (int acmTimeout = router->getACMTimeout() > 0) {
			Ice::ConnectionPtr conn = router->ice_getCachedConnection();
			conn->setACM(acmTimeout, IceUtil::None, Ice::HeartbeatAlways);
		}
		sessionOpened = true;
	}
}

void
NetFS::FuseApp::connectToService()
{
	if (!service) {
		Lock(_lock);

		auto proxyAddr = fcr->ServiceIdentity;
		for (const auto & ep : fcr->Endpoints) {
			proxyAddr += ":" + ep;
		}
		service = NetFS::ServicePrx::checkedCast(ic->stringToProxy(proxyAddr));
		if (!service) {
			throw std::runtime_error("Invalid service proxy: " + proxyAddr);
		}
	}
}

void
NetFS::FuseApp::connectToVolume()
{
	if (!volume) {
		volume = service->connect(fcr->ExportName, fcr->AuthToken);
		if (!volume) {
			throw std::runtime_error("Invalid filesystem proxy");
		}
	}
}

void
NetFS::FuseApp::connectHandles()
{
	for (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 & !O_CREAT);
		}
	}
	for (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 (session) {
		try {
			session->ice_ping();
		}
		catch (const Ice::Exception &) {
			session = NULL;
			sessionOpened = false;
		}
	}
	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();
		connectSession();
		connectToService();
		connectToVolume();
		connectHandles();
		return 0;
	}
	if (dynamic_cast<const NetFS::AuthError *>(&e)) {
		return -EPERM;
	}
	return FuseAppBase::onError(e);
}

NetFS::ReqEnv
NetFS::FuseApp::reqEnv()
{
	connectSession();
	connectToService();
	connectToVolume();
	struct fuse_context * c = fuse_get_context();
	NetFS::ReqEnv re;
	UserEntCache::instance.getName(c->uid, &re.user);
	GroupEntCache::instance.getName(c->gid, &re.grp);
	return re;
}