-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiskfile.c
100 lines (84 loc) · 2.37 KB
/
diskfile.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
#include "diskfile.h"
#include <fuse.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#define FOREACH_ENTRY(_var) \
for (diskfile_entry *_var = diskfile_entries; \
_var < diskfile_entries + diskfile_entries_count; \
++_var)
diskfile_entry diskfile_entries[DISKFILE_MAX_ENTRIES];
size_t diskfile_entries_count = 0;
static off_t
diskfile_source_size(const char *path) {
// Regular files are easy
struct stat st;
int err = lstat(path, &st);
if (err == 0 && S_ISREG(st.st_mode)) {
return st.st_size;
}
return diskfile_device_size(path);
}
static int
diskfile_getattr(const char *path, struct stat *stbuf) {
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) { /* The root directory of our file system. */
stbuf->st_mode = S_IFDIR | 0555;
stbuf->st_nlink = 2 + diskfile_entries_count;
return 0;
}
FOREACH_ENTRY(entry) {
if (strcmp(path, entry->dest) == 0) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
if (entry->size == -1)
entry->size = diskfile_source_size(entry->source);
stbuf->st_size = entry->size;
return 0;
}
}
return -ENOENT;
}
static int
diskfile_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi) {
if (strcmp(path, "/") != 0) /* We only recognize the root directory. */
return -ENOENT;
filler(buf, ".", NULL, 0); /* Current directory (.) */
filler(buf, "..", NULL, 0); /* Parent directory (..) */
FOREACH_ENTRY(entry) {
filler(buf, entry->dest + 1, NULL, 0);
}
return 0;
}
static int
diskfile_open(const char *path, struct fuse_file_info *fi) {
FOREACH_ENTRY(entry) {
if (strcmp(path, entry->dest) == 0) {
if ((fi->flags & O_ACCMODE) != O_RDONLY)
return -EACCES;
fi->fh = open(entry->source, O_RDONLY);
return 0;
}
}
return -ENOENT;
}
static int
diskfile_release(const char *path, struct fuse_file_info *fi) {
close(fi->fh);
return 0;
}
static int
diskfile_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
return pread(fi->fh, buf, size, offset);
}
struct fuse_operations diskfile_operations = {
.getattr = diskfile_getattr,
.readdir = diskfile_readdir,
.open = diskfile_open,
.release = diskfile_release,
.read = diskfile_read,
};