-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcallbacks.c
257 lines (222 loc) · 6.5 KB
/
callbacks.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "callbacks.h"
#include "xattrdb.h"
#define MAX_PATH 4096
#define XATTRDB_FILE_NAME "/.xattrdb"
char source_dir_storage[MAX_PATH] = "";
char *source_dir = source_dir_storage;
static void destination_path(char* dpath, const char *path) {
snprintf(dpath, MAX_PATH, "%s%s", source_dir, path);
}
static int check_res(const int res) {
if(res == -1) {
return -errno;
}
return 0;
}
int getattr_cb(const char *path, struct stat *st_data) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = lstat(dpath, st_data);
return check_res(res);
}
int readlink_cb(const char *path, char *buf, size_t size) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = readlink(dpath, buf, size - 1);
if(res != - 1) {
buf[res] = 0;
}
return check_res(res);
}
int mknod_cb(const char *path, mode_t mode, dev_t dev) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = mknod(dpath, mode, dev);
return check_res(res);
}
int mkdir_cb(const char *path, mode_t mode) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = mkdir(dpath, mode);
return check_res(res);
}
int unlink_cb(const char *path) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = unlink(dpath);
if(res != - 1) {
xattrdb_removepath(path);
}
return check_res(res);
}
int rmdir_cb(const char *path) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = rmdir(dpath);
// TODO cleanup database
return check_res(res);
}
int symlink_cb(const char *target, const char *linkpath) {
char dlinkpath[MAX_PATH];
destination_path(dlinkpath, linkpath);
const int res = symlink(target, dlinkpath);
return check_res(res);
}
int rename_cb(const char *oldpath, const char *newpath) {
char doldpath[MAX_PATH];
destination_path(doldpath, oldpath);
char dnewpath[MAX_PATH];
destination_path(dnewpath, newpath);
const int res = rename(doldpath, dnewpath);
if(res != -1) {
xattrdb_renamepath(oldpath, newpath);
}
return check_res(res);
}
int link_cb(const char *oldpath, const char *newpath) {
char doldtarget[MAX_PATH];
destination_path(doldtarget, oldpath);
char dnewpath[MAX_PATH];
destination_path(dnewpath, newpath);
const int res = link(doldtarget, dnewpath);
return check_res(res);
}
int chmod_cb(const char *path, mode_t mode) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = chmod(dpath, mode);
return check_res(res);
}
int chown_cb(const char *path, uid_t owner, gid_t group) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = lchown(dpath, owner, group);
return check_res(res);
}
int truncate_cb(const char *path, off_t size) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = truncate(dpath, size);
return check_res(res);
}
int utimens_cb(const char *path, const struct timespec tv[2]) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = utimensat(0 /*ignored*/, dpath, tv, AT_SYMLINK_NOFOLLOW);
return check_res(res);
}
int open_cb(const char *path, struct fuse_file_info *fi) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = open(dpath, fi->flags);
if(res == -1) {
return -errno;
}
fi->fh = res;
return 0;
}
int read_cb(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
const int res = pread(fi->fh, buf, size, offset);
if(res == -1) {
return -errno;
}
return res;
}
int write_cb(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
const int res = pwrite(fi->fh, buf, size, offset);
if(res == -1) {
return -errno;
}
return res;
}
int statfs_cb(const char *path, struct statvfs *st_buf) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = statvfs(dpath, st_buf);
return check_res(res);
}
int flush_cb(const char *path, struct fuse_file_info *fi) {
const int res = fsync(fi->fh);
return check_res(res);
}
int release_cb(const char *path, struct fuse_file_info *fi) {
const int res = close(fi->fh);
return check_res(res);
}
int fsync_cb(const char *path, int isdatasync, struct fuse_file_info *fi) {
const int res = fsync(fi->fh);
return check_res(res);
}
int setxattr_cb(const char *path, const char *name, const char *value, size_t size, int flags) {
return xattrdb_set(path, name, value, size) ? 0 : -EIO;
}
int getxattr_cb(const char *path, const char *name, char *value, size_t size) {
unsigned int c;
bool res = xattrdb_get(path, name, &c, value, size);
if(res && c == 0) {
return -ENODATA;
}
return res ? c : -EIO;
}
int listxattr_cb(const char *path, char *list, size_t size) {
unsigned int c;
bool res = xattrdb_list(path, &c, list, size);
if(res && c == 0) {
return -ENODATA;
}
return res ? c : -EIO;
}
int removexattr_cb(const char *path, const char *name) {
return xattrdb_removename(path, name) ? 0 : -EIO;
}
int opendir_cb (const char *path, struct fuse_file_info *fi) {
char dpath[MAX_PATH];
destination_path(dpath, path);
DIR *dp = opendir(dpath);
if(dp == NULL) {
return -errno;
}
fi->fh = (uint64_t)dp;
return 0;
}
int readdir_cb(const char *path, void *buf, fuse_fill_dir_t filler,off_t offset, struct fuse_file_info *fi) {
struct dirent *de;
while((de = readdir((DIR *)fi->fh)) != NULL) {
struct stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if(filler(buf, de->d_name, &st, 0)) {
break;
}
}
return 0;
}
int releasedir_cb(const char *path, struct fuse_file_info *fi) {
const int res = closedir((DIR *)fi->fh);
return check_res(res);
}
int access_cb(const char *path, int mode) {
char dpath[MAX_PATH];
destination_path(dpath, path);
const int res = access(dpath, mode);
return check_res(res);
}
void *init_cb(struct fuse_conn_info *conn) {
char dpath[MAX_PATH];
destination_path(dpath, XATTRDB_FILE_NAME);
xattrdb_open(dpath);
return NULL;
}
void destroy_cb(void *data) {
xattrdb_close();
}