Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 307 lines (268 sloc) 10.301 kb
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
1 // Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 // Use latest version.
17 #define FUSE_USE_VERSION 26
18 #include <fuse.h>
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
19
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
20 #include <dirent.h>
21 #include <errno.h>
b698853 @hzeller o README updates.
authored
22 #include <fcntl.h>
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
23 #include <limits.h>
b698853 @hzeller o README updates.
authored
24 #include <stdio.h>
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
25 #include <stdlib.h>
b698853 @hzeller o README updates.
authored
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
49eca7f @hzeller o add syslog as preparation for parameter handling.
authored
29 #include <syslog.h>
b698853 @hzeller o README updates.
authored
30 #include <unistd.h>
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
31
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
32 #include "folve-filesystem.h"
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
33 #include "status-server.h"
e9c07dd @hzeller o add documentation
authored
34
6c6dedb @hzeller o Some readme tweaks and readability improvements.
authored
35 // Compilation unit variables to communicate with the fuse callbacks.
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
36 static struct FolveRuntime {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
37 FolveRuntime() : fs(NULL), mount_point(NULL), status_port(-1) {}
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
38 FolveFilesystem *fs;
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
39 const char *mount_point;
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
40 int status_port;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
41 } folve_rt;
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
42
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
43 static int has_suffix_string (const char *str, const char *suffix) {
44 if (!str || !suffix)
45 return 0;
46 size_t str_len = strlen(str);
47 size_t suffix_len = strlen(suffix);
48 if (suffix_len > str_len)
49 return 0;
50 return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
51 }
52
53 static char *concat_path(char *buf, const char *a, const char *b) {
54 strcpy(buf, a);
55 strcat(buf, b);
56 return buf;
57 }
58
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
59 // Given a relative path from the root of the mounted file-system, get the
60 // original file from the source filesystem.
0aee3bb @hzeller o Show base dir that is mounted.
authored
61 // TODO(hzeller): move the ogg.fuse.flac logic into convolver-filesystem.
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
62 static const char *assemble_orig_path(char *buf, const char *path) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
63 char *result = concat_path(buf, folve_rt.fs->underlying_dir().c_str(), path);
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
64 static const char magic_ogg_rewrite[] = ".ogg.fuse.flac";
65 if (has_suffix_string(result, magic_ogg_rewrite)) {
66 *(result + strlen(result) - strlen(".fuse.flac")) = '\0';
67 }
68 return result;
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
69 }
70
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
71 // Essentially lstat(). Just forward to the original filesystem (this
73093dc @hzeller o Keep open file-descriptor.
authored
72 // will by lying: our convolved files are of different size...)
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
73 static int folve_getattr(const char *path, struct stat *stbuf) {
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
74 // If this is a currently open filename, we might be able to output a better
75 // estimate.
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
76 int result = folve_rt.fs->StatByFilename(path, stbuf);
369a0a7 @hzeller o Writing is not possible. Show permissions as readonly.
authored
77 if (result != 0) {
78 char path_buf[PATH_MAX];
79 result = lstat(assemble_orig_path(path_buf, path), stbuf);
80 if (result == -1)
81 return -errno;
82 }
83 // Whatever write mode was there before: now things are readonly.
84 stbuf->st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
85 return 0;
86 }
87
73093dc @hzeller o Keep open file-descriptor.
authored
88 // readdir(). Just forward to original filesystem.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
89 static int folve_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
90 off_t offset, struct fuse_file_info *fi) {
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
91 DIR *dp;
92 struct dirent *de;
93 char path_buf[PATH_MAX];
94
95 dp = opendir(assemble_orig_path(path_buf, path));
96 if (dp == NULL)
97 return -errno;
98
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
99 char ogg_rewrite_buffer[PATH_MAX];
100
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
101 while ((de = readdir(dp)) != NULL) {
102 struct stat st;
103 memset(&st, 0, sizeof(st));
104 st.st_ino = de->d_ino;
105 st.st_mode = de->d_type << 12;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
106 const char *entry_name = de->d_name;
107 if (has_suffix_string(entry_name, ".ogg")) {
108 // For ogg files, we pretend they actually end with 'flac', because
109 // we transparently rewrite them.
110 entry_name = concat_path(ogg_rewrite_buffer, entry_name, ".fuse.flac");
111 }
112 if (filler(buf, entry_name, &st, 0))
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
113 break;
114 }
115
116 closedir(dp);
117 return 0;
118 }
119
73093dc @hzeller o Keep open file-descriptor.
authored
120 // readlink(): forward to original filesystem.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
121 static int folve_readlink(const char *path, char *buf, size_t size) {
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
122 char path_buf[PATH_MAX];
123 const int result = readlink(assemble_orig_path(path_buf, path),
124 buf, size - 1);
125 if (result == -1)
126 return -errno;
127
128 buf[result] = '\0';
129 return 0;
130 }
131
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
132 static int folve_open(const char *path, struct fuse_file_info *fi) {
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
133 // We want to be allowed to only return part of the requested data in read().
134 // That way, we can separate reading the ID3-tags from
135 // decoding of the music stream - that way indexing should be fast.
136 // Setting the flag 'direct_io' allows us to return partial results.
137 fi->direct_io = 1;
138
e9c07dd @hzeller o add documentation
authored
139 // The file-handle has the neat property to be 64 bit - so we can actually
b698853 @hzeller o README updates.
authored
140 // stuff a pointer to our file handler object in there :)
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
141 // (Yay, someone was thinking while developing that API).
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
142 char path_buf[PATH_MAX];
143 const char *orig_path = assemble_orig_path(path_buf, path);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
144 FileHandler *handler = folve_rt.fs->CreateHandler(path, orig_path);
680f590 @hzeller o report opening error.
authored
145 if (handler == NULL)
146 return -errno;
147 fi->fh = (uint64_t) handler;
148 return 0;
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
149 }
150
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
151 static int folve_read(const char *path, char *buf, size_t size, off_t offset,
152 struct fuse_file_info *fi) {
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
153 return reinterpret_cast<FileHandler *>(fi->fh)->Read(buf, size, offset);
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
154 }
155
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
156 static int folve_release(const char *path, struct fuse_file_info *fi) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
157 folve_rt.fs->Close(path, reinterpret_cast<FileHandler *>(fi->fh));
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
158 return 0;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
159 }
160
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
161 static int folve_fgetattr(const char *path, struct stat *result,
162 struct fuse_file_info *fi) {
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
163 return reinterpret_cast<FileHandler *>(fi->fh)->Stat(result);
73093dc @hzeller o Keep open file-descriptor.
authored
164 }
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
165
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
166 static void *folve_init(struct fuse_conn_info *conn) {
167 const int ident_len = 20;
168 char *ident = (char*) malloc(ident_len); // openlog() keeps reference. Leaks.
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
169 snprintf(ident, ident_len, "folve[%d]", getpid());
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
170 openlog(ident, LOG_CONS|LOG_PERROR, LOG_USER);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
171 syslog(LOG_INFO, "Version " FOLVE_VERSION " started. "
172 "Serving '%s' on mount point '%s'",
173 folve_rt.fs->underlying_dir().c_str(), folve_rt.mount_point);
5830c5a @hzeller o Switch logging to syslog()
authored
174 if (folve_rt.fs->IsDebugMode()) {
175 syslog(LOG_INFO, "Debug logging enabled (-D)");
176 }
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
177
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
178 if (folve_rt.status_port > 0) {
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
179 // Need to start status server after we're daemonized.
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
180 StatusServer *status_server = new StatusServer(folve_rt.fs);
181 if (status_server->Start(folve_rt.status_port)) {
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
182 syslog(LOG_INFO, "HTTP status server on port %d",
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
183 folve_rt.status_port);
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
184 } else {
185 syslog(LOG_ERR, "Couldn't start HTTP server on port %d\n",
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
186 folve_rt.status_port);
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
187 }
188 }
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
189
190 // Some sanity checks.
191 if (folve_rt.fs->config_dirs().size() == 1) {
192 syslog(LOG_NOTICE, "No filter configuration directories given. "
193 "Any files will be just passed through verbatim.");
194 }
195 if (folve_rt.fs->config_dirs().size() > 2 && folve_rt.status_port < 0) {
196 syslog(LOG_WARNING, "Multiple filter configurations given, but no HTTP "
197 "status port. You only can switch filters via the HTTP interface; "
198 "add -p <port>");
199 }
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
200 return NULL;
201 }
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
202
0a3400d @hzeller o Write when we're exiting.
authored
203 static void folve_destroy(void *) {
db66f7d @hzeller o print which directory is serviced.
authored
204 syslog(LOG_INFO, "Exiting.");
0a3400d @hzeller o Write when we're exiting.
authored
205 }
206
b698853 @hzeller o README updates.
authored
207 static int usage(const char *prg) {
5830c5a @hzeller o Switch logging to syslog()
authored
208 printf("usage: %s [options] <original-dir> <mount-point>\n", prg);
209 printf("Options: (in sequence of usefulness)\n"
062a435 @hzeller o change configuration directory wording.
authored
210 "\t-c <cfg-dir> : Convolver configuration directory.\n"
8c7e5c1 @hzeller o Only if -D is given, the debug toggle is active in the UI.
authored
211 "\t You can supply this option multiple times:\n"
5830c5a @hzeller o Switch logging to syslog()
authored
212 "\t you'll get a drop-down select on the HTTP "
213 "status page.\n"
214 "\t-p <port> : Port to run the HTTP status server on.\n"
8c7e5c1 @hzeller o Only if -D is given, the debug toggle is active in the UI.
authored
215 "\t-D : Moderate volume Folve debug messages to syslog.\n"
216 "\t Can then also be toggled in the UI.\n"
5830c5a @hzeller o Switch logging to syslog()
authored
217 "\t-f : Operate in foreground; useful for debugging.\n"
218 "\t-o <mnt-opt> : other generic mount parameters passed to fuse.\n"
219 "\t-d : High volume fuse debug log. Implies -f.\n");
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
220 return 1;
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
221 }
222
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
223 struct FolveConfig {
224 FolveConfig() : base_dir(NULL), config_dir(NULL), port(-1) {}
225 const char *base_dir;
226 const char *mount_point;
227 const char *config_dir;
228 int port;
229 };
230
231 enum {
232 FOLVE_OPT_PORT = 42,
233 FOLVE_OPT_CONFIG,
5830c5a @hzeller o Switch logging to syslog()
authored
234 FOLVE_OPT_DEBUG,
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
235 };
236
237 int FolveOptionHandling(void *data, const char *arg, int key,
238 struct fuse_args *outargs) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
239 FolveRuntime *rt = (FolveRuntime*) data;
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
240 switch (key) {
241 case FUSE_OPT_KEY_NONOPT:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
242 // First non-opt: our underlying dir.
243 if (rt->fs->underlying_dir().empty()) {
244 rt->fs->set_underlying_dir(arg);
245 return 0; // we consumed this.
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
246 } else {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
247 rt->mount_point = strdup(arg); // remmber as FYI
248 return 1; // .. but leave it to fuse
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
249 }
250 case FOLVE_OPT_PORT:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
251 rt->status_port = atoi(arg + 2); // strip "-p"
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
252 return 0;
253 case FOLVE_OPT_CONFIG:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
254 rt->fs->add_config_dir(arg + 2); // strip "-c"
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
255 return 0;
5830c5a @hzeller o Switch logging to syslog()
authored
256 case FOLVE_OPT_DEBUG:
8c7e5c1 @hzeller o Only if -D is given, the debug toggle is active in the UI.
authored
257 rt->fs->set_debug_ui_enabled(true);
5830c5a @hzeller o Switch logging to syslog()
authored
258 rt->fs->SetDebugMode(true);
259 return 0;
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
260 }
261 return 1;
262 }
263
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
264 int main(int argc, char *argv[]) {
1e9ff21 @hzeller o main: report if parameters are not directories.
authored
265 const char *progname = argv[0];
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
266 if (argc < 4) {
1e9ff21 @hzeller o main: report if parameters are not directories.
authored
267 return usage(progname);
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
268 }
49eca7f @hzeller o add syslog as preparation for parameter handling.
authored
269
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
270 folve_rt.fs = new FolveFilesystem();
271
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
272 static struct fuse_opt folve_options[] = {
5830c5a @hzeller o Switch logging to syslog()
authored
273 FUSE_OPT_KEY("-p ", FOLVE_OPT_PORT),
274 FUSE_OPT_KEY("-c ", FOLVE_OPT_CONFIG),
275 FUSE_OPT_KEY("-D", FOLVE_OPT_DEBUG),
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
276 FUSE_OPT_END
277 };
278 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
279 fuse_opt_parse(&args, &folve_rt, folve_options, FolveOptionHandling);
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
280
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
281 if (!folve_rt.fs->CheckInitialized()) {
1e9ff21 @hzeller o main: report if parameters are not directories.
authored
282 return usage(progname);
283 }
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
284
285 struct fuse_operations folve_operations;
286 memset(&folve_operations, 0, sizeof(folve_operations));
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
287
0a3400d @hzeller o Write when we're exiting.
authored
288 // Start/stop. Will write to syslog and start auxiliary http service.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
289 folve_operations.init = folve_init;
0a3400d @hzeller o Write when we're exiting.
authored
290 folve_operations.destroy = folve_destroy;
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
291
292 // Basic operations to make navigation work.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
293 folve_operations.readdir = folve_readdir;
294 folve_operations.readlink = folve_readlink;
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
295
296 // open() and close() file.
38cdeb1 @hzeller o whitespace changes.
authored
297 folve_operations.open = folve_open;
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
298 folve_operations.release = folve_release;
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
299
300 // Actual workhorse: reading a file and returning predicted file-size
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
301 folve_operations.read = folve_read;
302 folve_operations.fgetattr = folve_fgetattr;
303 folve_operations.getattr = folve_getattr;
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
304
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
305 return fuse_main(args.argc, args.argv, &folve_operations, NULL);
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
306 }
Something went wrong with that request. Please try again.