-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounterfs.c
373 lines (304 loc) · 7.03 KB
/
counterfs.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
/* Maximum buffer length (number and newline) */
#define MAXLEN 12
typedef struct entry entry;
typedef struct entry_data entry_data;
typedef struct entry_rw_data entry_rw_data;
struct entry
{
char *name;
entry_data *data;
entry *prev, *next;
};
struct entry_data
{
size_t size;
unsigned int c;
time_t atime, mtime;
nlink_t nlink;
};
struct entry_rw_data
{
char *rbuf, *wbuf;
size_t wsize;
};
entry *ehead, *etail;
void
add_entry(const char *nm, entry_data *data)
{
entry *ent = (entry *) malloc(sizeof(entry));
char *name = strdup(nm);
ent->name = name;
ent->prev = ent->next = NULL;
if (data) {
ent->data = data;
data->nlink++;
} else {
ent->data = (entry_data *) malloc(sizeof(entry_data));
ent->data->nlink = 1;
ent->data->size = 2;
ent->data->c = 0;
}
if (!ehead)
ehead = etail = ent;
else {
etail->next = ent;
ent->prev = etail;
etail = ent;
}
}
int
find_entry(const char *nm, entry **ent)
{
entry *cur = ehead;
*ent = NULL;
while (cur) {
if (strcmp(nm, cur->name) == 0) {
*ent = cur;
return 1;
}
cur = cur->next;
}
return 0;
}
int
remove_entry(const char *nm)
{
entry *ent;
if (find_entry(nm, &ent)) {
if (ent->prev)
ent->prev->next = ent->next;
else
ehead = ent->next;
if (ent->next)
ent->next->prev = ent->prev;
else
etail = ent->prev;
ent->data->nlink--;
if (!ent->data->nlink)
free(ent->data);
free(ent->name);
free(ent);
return 1;
}
return 0;
}
/* Check that path has subdirectories */
int
has_subdir(const char *path)
{
return strchr(path + 1, '/') != NULL;
}
int
counter_getattr(const char *path, struct stat *stbuf)
{
entry *ent;
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (!has_subdir(path + 1) && find_entry(path + 1, &ent)) {
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_nlink = ent->data->nlink;
stbuf->st_size = ent->data->size;
stbuf->st_atime = ent->data->atime;
stbuf->st_mtime = ent->data->mtime;
}
else if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else
res = -ENOENT;
return res;
}
int
counter_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
entry *cur;
(void) offset;
(void) fi;
if(strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
cur = ehead;
while (cur) {
filler(buf, cur->name, NULL, 0);
cur = cur->next;
}
return 0;
}
int
counter_open(const char *path, struct fuse_file_info *fi)
{
entry *ent;
entry_rw_data *rw;
if (has_subdir(path))
return -ENOENT;
if (!find_entry(path + 1, &ent))
return -ENOENT;
rw = (entry_rw_data *) malloc(sizeof(entry_rw_data));
rw->rbuf = (char *) malloc(MAXLEN);
rw->wbuf = (char *) malloc(MAXLEN);
rw->wsize = 0;
memset(rw->wbuf, 0, MAXLEN);
fi->direct_io = 1;
fi->fh = (uint64_t) rw;
sprintf(rw->rbuf, "%u\n", ent->data->c);
ent->data->size = strlen(rw->rbuf);
ent->data->c++;
return 0;
}
int
counter_truncate(const char *path, off_t offset)
{
entry *ent;
if (has_subdir(path))
return -ENOENT;
if (!find_entry(path + 1, &ent))
return -ENOENT;
return 0;
}
int
counter_read(const char *path, char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
size_t len;
entry_rw_data *rw = (entry_rw_data *) fi->fh;
len = strlen(rw->rbuf);
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, rw->rbuf + offset, size);
return size;
}
return 0;
}
int
counter_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
entry_rw_data *rw = (entry_rw_data *) fi->fh;
if (rw && offset < MAXLEN) {
if (offset + size > MAXLEN - 1)
size = MAXLEN - offset - 1;
if (offset + size > rw->wsize)
rw->wsize = offset + size;
memcpy(rw->wbuf + offset, buf, size);
return size;
}
return 0;
}
int
counter_create(const char *path, mode_t mod, struct fuse_file_info *fi)
{
(void) fi;
if (has_subdir(path))
return -ENOENT;
add_entry(path + 1, NULL);
return 0;
}
int
counter_utimens(const char *path, const struct timespec tv[2])
{
entry *ent;
if (has_subdir(path))
return -ENOENT;
if (!find_entry(path + 1, &ent))
return -ENOENT;
ent->data->atime = tv[0].tv_sec;
ent->data->mtime = tv[1].tv_sec;
return 0;
}
int
counter_unlink(const char *path)
{
if (has_subdir(path))
return -ENOENT;
if (!remove_entry(path + 1))
return -ENOENT;
return 0;
}
int
counter_rename(const char *from, const char *to)
{
entry *ent;
if (has_subdir(from))
return -ENOENT;
if (!find_entry(from + 1, &ent))
return -ENOENT;
remove_entry(to + 1);
free(ent->name);
ent->name = strdup(to + 1);
return 0;
}
int
counter_flush(const char *path, struct fuse_file_info *fi)
{
entry *ent;
unsigned int new_c = 0;
int size;
entry_rw_data *rw = (entry_rw_data *) fi->fh;
printf("Flushing\n");
if (has_subdir(path))
return -ENOENT;
if (!find_entry(path + 1, &ent))
return -ENOENT;
if (rw && rw->wsize) {
sscanf(rw->wbuf, "%u", &new_c);
ent->data->c = new_c;
sprintf(rw->wbuf, "%u%n", new_c, &size);
ent->data->size = size;
}
return 0;
}
int
counter_release(const char *path, struct fuse_file_info *fi)
{
if (fi->fh) {
entry_rw_data *rw = (entry_rw_data *) fi->fh;
free(rw->rbuf);
free(rw->wbuf);
free(rw);
}
return 0;
}
int
counter_link(const char *from, const char *to)
{
entry *ent;
if (has_subdir(from))
return -ENOENT;
if (!find_entry(from + 1, &ent))
return -ENOENT;
add_entry(to + 1, ent->data);
return 0;
}
struct fuse_operations counter_oper = {
.getattr = counter_getattr,
.unlink = counter_unlink,
.rename = counter_rename,
.link = counter_link,
.truncate = counter_truncate,
.open = counter_open,
.read = counter_read,
.write = counter_write,
.flush = counter_flush,
.release = counter_release,
.readdir = counter_readdir,
.create = counter_create,
.utimens = counter_utimens,
};
int
main(int argc, char *argv[])
{
ehead = etail = NULL;
return fuse_main(argc, argv, &counter_oper, NULL);
}