summaryrefslogtreecommitdiff
path: root/netfs/fuse/fuseApp.cpp
blob: 2f3b288381f268a99a73d738cdff145cc937ae8e (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
#include <pch.hpp>
#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(int & argc, char ** argv) :
	_argc(argc),
	_argv(argv),
	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 (ic) {
		ic->destroy();
	}
}

void *
NetFS::FuseApp::init(struct fuse_conn_info *)
{
	ic = Ice::initialize(_argc, _argv);
	fc = Slicer::Deserialize<Slicer::XmlFileDeserializer, NetFS::Client::Configuration>(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 (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::connectToService()
{
	if (!service) {
		Lock(_lock);
		auto e = fc->Resources.find(resourceName);
		if (e == fc->Resources.end()) {
			throw std::runtime_error("No such resource: " + resourceName);
		}

		auto proxyAddr = e->second->ServiceIdentity;
		for (const auto & ep : e->second->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) {
		auto e = fc->Resources.find(resourceName);
		if (e == fc->Resources.end()) {
			throw std::runtime_error("No such export: " + resourceName);
		}
		volume = service->connect(e->second->ExportName, "bar");
		if (!volume) {
			throw "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);
		}
	}
	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 (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 { UserEntCache::instance.getName(c->uid), GroupEntCache::instance.getName(c->gid) };
}