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

namespace NetFS {
	FuseApp::OpenDir::OpenDir(DirectoryPrxPtr r, std::string p) : remote(std::move(r)), path(std::move(p)) { }

	template<>
	std::map<int, FuseApp::OpenDirPtr> &
	FuseApp::getMap<FuseApp::OpenDirPtr>()
	{
		return openDirs;
	}

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

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

	int
	FuseApp::readdir(const char * p, void * buf, fuse_fill_dir_t filler, off_t, struct fuse_file_info * fi,
			enum fuse_readdir_flags flags)
	{
		try {
			auto od = getProxy<OpenDirPtr>(fi->fh);
			std::string path(p);
			auto expiry = time(nullptr) + 2;
			if (flags == FUSE_READDIR_PLUS) {
				for (const auto & e : od->remote->listdir()) {
					filler(buf, e.first.c_str(), nullptr, 0, FUSE_FILL_DIR_PLUS);
					std::string k(path + e.first);
					statCache.remove(k);
					statCache.add(k, converter.convert(e.second), expiry);
				}
			}
			else {
				for (const auto & e : od->remote->readdir()) {
					filler(buf, e.c_str(), nullptr, 0, (fuse_fill_dir_flags)0);
				}
			}
			return 0;
		}
		catch (SystemError & e) {
			return -e.syserrno;
		}
	}

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

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