| 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 |
#include "fuse_local.h" |
| 30 |
|
| 31 |
#include "cache.h" |
| 32 |
#include "config.h" |
| 33 |
#include "link.h" |
| 34 |
#include "log.h" |
| 35 |
|
| 36 |
|
| 37 |
#define BYPASS_FH ((uint64_t)-1) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#if defined(__APPLE__) && defined(__MACH__) |
| 44 |
#define FUSE_DARWIN_ENABLE_EXTENSIONS 0 |
| 45 |
#endif |
| 46 |
|
| 47 |
#define FUSE_USE_VERSION 30 |
| 48 |
#include <fuse.h> |
| 49 |
|
| 50 |
#include <errno.h> |
| 51 |
#include <string.h> |
| 52 |
#include <unistd.h> |
| 53 |
|
| 54 |
static void *fs_init(struct fuse_conn_info *conn, struct fuse_config *cfg) |
| 55 |
{ |
| 56 |
(void)conn; |
| 57 |
(void)cfg; |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
static int fs_release(const char *path, struct fuse_file_info *fi) |
| 63 |
{ |
| 64 |
lprintf(info, "%s\n", path); |
| 65 |
(void)path; |
| 66 |
if (CACHE_SYSTEM_INIT && fi->fh && fi->fh != BYPASS_FH) { |
| 67 |
Cache_close((Cache *)fi->fh); |
| 68 |
} |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
static int fs_getattr(const char *path, struct stat *stbuf, |
| 74 |
struct fuse_file_info *ffi_buf) |
| 75 |
{ |
| 76 |
(void)ffi_buf; |
| 77 |
int res = 0; |
| 78 |
memset(stbuf, 0, sizeof(struct stat)); |
| 79 |
|
| 80 |
if (!strcmp(path, "/")) { |
| 81 |
stbuf->st_mode = S_IFDIR | 0755; |
| 82 |
stbuf->st_nlink = 1; |
| 83 |
} else { |
| 84 |
Link *link = path_to_Link(path); |
| 85 |
if (!link) { |
| 86 |
return -ENOENT; |
| 87 |
} |
| 88 |
struct timespec spec = {0}; |
| 89 |
spec.tv_sec = link->time; |
| 90 |
#if defined(__APPLE__) && defined(__MACH__) |
| 91 |
stbuf->st_mtime = spec.tv_sec; |
| 92 |
stbuf->st_atime = spec.tv_sec; |
| 93 |
stbuf->st_ctime = spec.tv_sec; |
| 94 |
#else |
| 95 |
stbuf->st_mtim = spec; |
| 96 |
#endif |
| 97 |
switch (link->type) { |
| 98 |
case LINK_DIR: |
| 99 |
stbuf->st_mode = S_IFDIR | 0755; |
| 100 |
stbuf->st_nlink = 1; |
| 101 |
break; |
| 102 |
case LINK_FILE: |
| 103 |
stbuf->st_mode = S_IFREG | 0444; |
| 104 |
stbuf->st_nlink = 1; |
| 105 |
stbuf->st_size = link->content_length; |
| 106 |
stbuf->st_blksize = 128L * 1024L; |
| 107 |
stbuf->st_blocks = (link->content_length) / 512; |
| 108 |
break; |
| 109 |
default: |
| 110 |
LinkTable_unref(link->parent_table); |
| 111 |
return -ENOENT; |
| 112 |
} |
| 113 |
LinkTable_unref(link->parent_table); |
| 114 |
} |
| 115 |
stbuf->st_uid = getuid(); |
| 116 |
stbuf->st_gid = getgid(); |
| 117 |
|
| 118 |
return res; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
static int fs_read(const char *path, char *buf, size_t size, off_t offset, |
| 123 |
struct fuse_file_info *fi) |
| 124 |
{ |
| 125 |
long received; |
| 126 |
if (CACHE_SYSTEM_INIT) { |
| 127 |
if (fi->fh == BYPASS_FH) { |
| 128 |
received = path_download(path, buf, size, offset); |
| 129 |
} else if (fi->fh) { |
| 130 |
received = Cache_read((Cache *)fi->fh, buf, size, offset); |
| 131 |
} else { |
| 132 |
received = 0; |
| 133 |
} |
| 134 |
} else { |
| 135 |
received = path_download(path, buf, size, offset); |
| 136 |
} |
| 137 |
return received; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
static int fs_open(const char *path, struct fuse_file_info *fi) |
| 142 |
{ |
| 143 |
lprintf(info, "%s\n", path); |
| 144 |
Link *link = path_to_Link(path); |
| 145 |
if (!link) { |
| 146 |
return -ENOENT; |
| 147 |
} |
| 148 |
if ((fi->flags & O_RDWR) != O_RDONLY) { |
| 149 |
LinkTable_unref(link->parent_table); |
| 150 |
return -EROFS; |
| 151 |
} |
| 152 |
if (CACHE_SYSTEM_INIT) { |
| 153 |
if (link->content_length == 0) { |
| 154 |
fi->fh = 0; |
| 155 |
LinkTable_unref(link->parent_table); |
| 156 |
return 0; |
| 157 |
} |
| 158 |
int bypass_cache = 0; |
| 159 |
off_t file_size = (off_t)link->content_length; |
| 160 |
if (file_size < 0 || (size_t)file_size != link->content_length |
| 161 |
|| (CONFIG.cache_min_size >= 0 && file_size < CONFIG.cache_min_size) |
| 162 |
|| (CONFIG.cache_max_size >= 0 |
| 163 |
&& file_size > CONFIG.cache_max_size)) { |
| 164 |
bypass_cache = 1; |
| 165 |
} |
| 166 |
if (bypass_cache) { |
| 167 |
|
| 168 |
fi->fh = BYPASS_FH; |
| 169 |
LinkTable_unref(link->parent_table); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
fi->fh = (uint64_t)Cache_open(path); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
if (!fi->fh) { |
| 177 |
lprintf(fatal, "Cache file creation failure for %s.\n", path); |
| 178 |
} |
| 179 |
} |
| 180 |
LinkTable_unref(link->parent_table); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
static int fs_opendir(const char *path, struct fuse_file_info *fi) |
| 185 |
{ |
| 186 |
LinkTable *linktbl = path_to_LinkTable(path); |
| 187 |
if (!linktbl) { |
| 188 |
return -ENOENT; |
| 189 |
} |
| 190 |
fi->fh = (uint64_t)linktbl; |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
static int fs_releasedir(const char *path, struct fuse_file_info *fi) |
| 195 |
{ |
| 196 |
LinkTable *linktbl = (LinkTable *)fi->fh; |
| 197 |
if (linktbl) { |
| 198 |
if (strcmp(path, "/") != 0) { |
| 199 |
LinkTable_mark_orphaned(linktbl); |
| 200 |
} |
| 201 |
LinkTable_unref(linktbl); |
| 202 |
} |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add, |
| 210 |
off_t offset, struct fuse_file_info *fi, |
| 211 |
enum fuse_readdir_flags fr_flags) |
| 212 |
{ |
| 213 |
(void)path; |
| 214 |
(void)offset; |
| 215 |
(void)fr_flags; |
| 216 |
LinkTable *linktbl = (LinkTable *)fi->fh; |
| 217 |
|
| 218 |
if (!linktbl) { |
| 219 |
return -ENOENT; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
dir_add(buf, ".", NULL, 0, 0); |
| 227 |
dir_add(buf, "..", NULL, 0, 0); |
| 228 |
|
| 229 |
for (int i = 1; i < linktbl->size; i++) { |
| 230 |
Link *link = linktbl->links[i]; |
| 231 |
if (link->type != LINK_INVALID) { |
| 232 |
dir_add(buf, link->linkname, NULL, 0, 0); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
static struct fuse_operations fs_oper = {.getattr = fs_getattr, |
| 240 |
.opendir = fs_opendir, |
| 241 |
.readdir = fs_readdir, |
| 242 |
.releasedir = fs_releasedir, |
| 243 |
.open = fs_open, |
| 244 |
.read = fs_read, |
| 245 |
.init = fs_init, |
| 246 |
.release = fs_release}; |
| 247 |
|
| 248 |
int fuse_local_init(int argc, char **argv) |
| 249 |
{ |
| 250 |
return fuse_main(argc, argv, &fs_oper, NULL); |
| 251 |
} |