-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfuse_forward.cxx
155 lines (130 loc) · 3.6 KB
/
fuse_forward.cxx
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
/**
* Copyright CERN; [email protected]
*/
#define _FILE_OFFSET_BITS 64
#define FUSE_USE_VERSION 26
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fuse.h>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using namespace std;
string *g_phys_path;
string *g_log_path;
map<int, int> *g_fd2log;
static string GetRealPath(const char *path) {
return (*g_phys_path) + string(path);
}
static string GetLogPath(const string &real_path) {
string chiffre_path(real_path);
std::replace(chiffre_path.begin(), chiffre_path.end(), '/', '-');
return *g_log_path + string("/") + chiffre_path;
}
static int MkFuseRetval(int retval) {
if (retval >= 0)
return 0;
return -errno;
}
static int ff_getattr(const char *path, struct stat *info) {
string real_path = GetRealPath(path);
int retval = lstat(real_path.c_str(), info);
return MkFuseRetval(retval);
}
static int ff_open(const char *path, struct fuse_file_info *fi) {
string real_path = GetRealPath(path);
int fd = open(real_path.c_str(), fi->flags);
if (fd >= 0) {
fi->fh = fd;
int fd_log = open(GetLogPath(real_path).c_str(),
O_CREAT | O_APPEND | O_WRONLY, 0644);
assert(fd_log >= 0);
(*g_fd2log)[fd] = fd_log;
}
return MkFuseRetval(fd);
}
static int ff_read(
const char *path,
char *buf,
size_t size,
off_t offset,
struct fuse_file_info *fi)
{
int nbytes = pread(fi->fh, buf, size, offset);
if (nbytes < 0)
return -errno;
dprintf((*g_fd2log)[fi->fh], "%ld %d\n", offset, nbytes);
return nbytes;
}
static int ff_release(const char *path, struct fuse_file_info *fi) {
int retval = close(fi->fh);
close((*g_fd2log)[fi->fh]);
g_fd2log->erase(fi->fh);
return MkFuseRetval(retval);
}
static int ff_opendir(const char *path, struct fuse_file_info *fi) {
string real_path = GetRealPath(path);
DIR *dirp = opendir(real_path.c_str());
if (dirp == NULL)
return -errno;
fi->fh = reinterpret_cast<intptr_t>(dirp);
return 0;
}
int ff_readdir(
const char *path,
void *buf,
fuse_fill_dir_t filler,
off_t offset,
struct fuse_file_info *fi)
{
DIR *dirp = reinterpret_cast<DIR *>(fi->fh);
struct dirent *entry;
while ((entry = readdir(dirp)) != NULL) {
int retval = filler(buf, entry->d_name, NULL, 0);
assert(retval == 0);
}
return 0;
}
int ff_releasedir(const char *path, struct fuse_file_info *fi) {
DIR *dirp = reinterpret_cast<DIR *>(fi->fh);
int retval = closedir(dirp);
return MkFuseRetval(retval);
}
int main(int argc, char **argv) {
if (getenv("FF_PHYS_PATH") == NULL) {
printf("Set environment variable FF_PHYS_PATH\n");
return 1;
}
if (getenv("FF_LOG_PATH") == NULL) {
printf("Set environment variable FF_LOG_PATH\n");
return 1;
}
g_phys_path = new string(getenv("FF_PHYS_PATH"));
g_log_path = new string(getenv("FF_LOG_PATH"));
if (g_phys_path->empty() || ((*g_phys_path)[0] != '/')) {
printf("FF_PHYS_PATH must be absolute\n");
return 1;
}
if (g_log_path->empty() || ((*g_log_path)[0] != '/')) {
printf("FF_LOG_PATH must be absolute\n");
return 1;
}
g_fd2log = new map<int, int>();
struct fuse_operations ff_operations;
memset(&ff_operations, 0, sizeof(ff_operations));
ff_operations.getattr = ff_getattr;
ff_operations.open = ff_open;
ff_operations.read = ff_read;
ff_operations.release = ff_release;
ff_operations.opendir = ff_opendir;
ff_operations.readdir = ff_readdir;
ff_operations.releasedir = ff_releasedir;
int retval = fuse_main(argc, argv, &ff_operations, NULL);
return retval;
}