Debian Codegraph @

src/fuse_local.c

raw symbols SCIP
1 /*
2 * HTTPDirFS - HTTP Directory Filesystem
3 *
4 * Copyright (C) 2020-2026 Fufu Fang <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of portions of this program with the OpenSSL
21 * library.
22 */
23
24 /**
25 * \file fuse_local.c
26 * \brief FUSE mounting and operations implementation
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 /* clang-format off */
37 #define BYPASS_FH ((uint64_t)-1)
38 /* clang-format on */
39
40 /*
41 * must be included before including <fuse.h>
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 /** \brief release an opened file */
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 /** \brief return the attributes for a single file indicated by path */
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 /** \brief read a file */
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 /** \brief open a file indicated by the path */
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; /* valid empty file: bypass cache creation */
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 /* bypass cache creation due to size thresholds */
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 * The cache definitely cannot be opened for some reason.
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 * \brief read the directory indicated by the path
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 * start adding the links
225 */
226 dir_add(buf, ".", NULL, 0, 0);
227 dir_add(buf, "..", NULL, 0, 0);
228 /* We skip the head link */
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 }