summaryrefslogtreecommitdiff
path: root/netfs/fuse/fuseDirs.cpp
blob: 03cc71277318273885c5f046b549143f7266c0f1 (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 "fuseApp.h"
#include <lockHelpers.h>
#include <entCache.h>

NetFS::FuseApp::OpenDir::OpenDir(DirectoryPrx r, const std::string & p) :
	remote(r),
	remoteV2(r->ice_getFacet() == "v2" ? DirectoryV2Prx::uncheckedCast(r) : nullptr),
	path(p)
{
}

void
NetFS::FuseApp::setProxy(OpenDirPtr od, uint64_t & fh)
{
	Lock(_lock);
	while (openDirs.find(fh = ++openDirID) != openDirs.end()) ;
	openDirs.insert({ fh, od });
}

NetFS::FuseApp::OpenDirPtr
NetFS::FuseApp::getDirProxy(uint64_t localID) const
{
	SharedLock(_lock);
	OpenDirs::const_iterator i = openDirs.find(localID);
	if (i != openDirs.end()) {
		return i->second;
	}
	throw NetFS::SystemError(EBADF);
}

void
NetFS::FuseApp::clearDirProxy(uint64_t localID)
{
	Lock(_lock);
	openDirs.erase(localID);
}

int
NetFS::FuseApp::opendir(const char * p, struct fuse_file_info * fi)
{
	try {
		auto remote = volume->opendir(reqEnv(), p);
		setProxy(new OpenDir(remote, p), fi->fh);
		return 0;
	}
	catch (NetFS::SystemError & e) {
		return -e.syserrno;
	}
}

int
NetFS::FuseApp::releasedir(const char *, struct fuse_file_info * fi)
{
	try {
		auto remote = getDirProxy(fi->fh)->remote;
		remote->close();
		clearDirProxy(fi->fh);
		return 0;
	}
	catch (NetFS::SystemError & e) {
		return -e.syserrno;
	}
}

int
NetFS::FuseApp::readdir(const char * p, void * buf, fuse_fill_dir_t filler, off_t, struct fuse_file_info * fi)
{
	try {
		auto od = getDirProxy(fi->fh);
		std::string path(p);
		path += "/";
		auto expiry = time(NULL) + 2;
		if (od->remoteV2) {
			for (const auto & e : od->remoteV2->listdir()) {
				filler(buf, e.first.c_str(), NULL, 0);
				statCache.add(path + e.first, converter.convert(e.second), expiry);
			}
		}
		else {
			for (const auto & e : od->remote->readdir()) {
				filler(buf, e.c_str(), NULL, 0);
				std::string epath = path + e;
				auto asga = volume->begin_getattr(reqEnv(), epath);
				statCache.addFactory(epath,
						[asga,this]() {
								return converter.convert(volume->end_getattr(asga));
						}, expiry);
			}
		}
		return 0;
	}
	catch (NetFS::SystemError & e) {
		return -e.syserrno;
	}
}

int
NetFS::FuseApp::mkdir(const char * p, mode_t m)
{
	try {
		volume->mkdir(reqEnv(), p, m);
		return 0;
	}
	catch (NetFS::SystemError & e) {
		return -e.syserrno;
	}
}

int
NetFS::FuseApp::rmdir(const char * p)
{
	try {
		volume->rmdir(reqEnv(), p);
		return 0;
	}
	catch (NetFS::SystemError & e) {
		return -e.syserrno;
	}
}