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

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

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

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

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

	int
	FuseApp::readdir(const char * path, void * buf, fuse_fill_dir_t filler, off_t, struct fuse_file_info * fileInfo,
			enum fuse_readdir_flags flags)
	{
		try {
			auto openDir = getProxy<OpenDirPtr>(fileInfo->fh);
			const std::filesystem::path fspath {path};
			auto expiry = time(nullptr) + 2;
			if (flags & FUSE_READDIR_PLUS) {
				for (const auto & entry : openDir->remote->listdir()) {
					if (auto stat = converter.convert(entry.second); stat.st_mode) {
						filler(buf, entry.first.c_str(), nullptr, 0, FUSE_FILL_DIR_PLUS);
						const auto key {std::filesystem::hash_value(fspath / entry.first)};
						statCache.remove(key);
						statCache.add(key, stat, expiry);
					}
				}
			}
			else {
				// Standard read dir cannot know the local system cannot represent the inode
				for (const auto & entry : openDir->remote->readdir()) {
					filler(buf, entry.c_str(), nullptr, 0, fuse_fill_dir_flags {});
				}
			}
			return 0;
		}
		catch (SystemError & e) {
			return -e.syserrno;
		}
	}

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

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