Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 287 lines (246 sloc) 9.287 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);
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
77 if (result == 0) return result;
78
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
79 char path_buf[PATH_MAX];
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
80 result = lstat(assemble_orig_path(path_buf, path), stbuf);
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
81 if (result == -1)
82 return -errno;
83
84 return 0;
85 }
86
73093dc @hzeller o Keep open file-descriptor.
authored
87 // readdir(). Just forward to original filesystem.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
88 static int folve_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
89 off_t offset, struct fuse_file_info *fi) {
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
90 DIR *dp;
91 struct dirent *de;
92 char path_buf[PATH_MAX];
93
94 dp = opendir(assemble_orig_path(path_buf, path));
95 if (dp == NULL)
96 return -errno;
97
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
98 char ogg_rewrite_buffer[PATH_MAX];
99
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
100 while ((de = readdir(dp)) != NULL) {
101 struct stat st;
102 memset(&st, 0, sizeof(st));
103 st.st_ino = de->d_ino;
104 st.st_mode = de->d_type << 12;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
105 const char *entry_name = de->d_name;
106 if (has_suffix_string(entry_name, ".ogg")) {
107 // For ogg files, we pretend they actually end with 'flac', because
108 // we transparently rewrite them.
109 entry_name = concat_path(ogg_rewrite_buffer, entry_name, ".fuse.flac");
110 }
111 if (filler(buf, entry_name, &st, 0))
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
112 break;
113 }
114
115 closedir(dp);
116 return 0;
117 }
118
73093dc @hzeller o Keep open file-descriptor.
authored
119 // readlink(): forward to original filesystem.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
120 static int folve_readlink(const char *path, char *buf, size_t size) {
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
121 char path_buf[PATH_MAX];
122 const int result = readlink(assemble_orig_path(path_buf, path),
123 buf, size - 1);
124 if (result == -1)
125 return -errno;
126
127 buf[result] = '\0';
128 return 0;
129 }
130
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
131 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
132 // We want to be allowed to only return part of the requested data in read().
133 // That way, we can separate reading the ID3-tags from
134 // decoding of the music stream - that way indexing should be fast.
135 // Setting the flag 'direct_io' allows us to return partial results.
136 fi->direct_io = 1;
137
e9c07dd @hzeller o add documentation
authored
138 // The file-handle has the neat property to be 64 bit - so we can actually
b698853 @hzeller o README updates.
authored
139 // stuff a pointer to our file handler object in there :)
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
140 // (Yay, someone was thinking while developing that API).
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
141 char path_buf[PATH_MAX];
142 const char *orig_path = assemble_orig_path(path_buf, path);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
143 FileHandler *handler = folve_rt.fs->CreateHandler(path, orig_path);
680f590 @hzeller o report opening error.
authored
144 if (handler == NULL)
145 return -errno;
146 fi->fh = (uint64_t) handler;
147 return 0;
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
148 }
149
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
150 static int folve_read(const char *path, char *buf, size_t size, off_t offset,
151 struct fuse_file_info *fi) {
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
152 return reinterpret_cast<FileHandler *>(fi->fh)->Read(buf, size, offset);
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
153 }
154
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
155 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
156 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
157 return 0;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
158 }
159
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
160 static int folve_fgetattr(const char *path, struct stat *result,
161 struct fuse_file_info *fi) {
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
162 return reinterpret_cast<FileHandler *>(fi->fh)->Stat(result);
73093dc @hzeller o Keep open file-descriptor.
authored
163 }
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
164
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
165 static void *folve_init(struct fuse_conn_info *conn) {
166 // If we're running in the foreground, we like to be seen on stderr as well.
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);
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
174
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
175 if (folve_rt.status_port > 0) {
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
176 // Need to start status server after we're daemonized.
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
177 StatusServer *status_server = new StatusServer(folve_rt.fs);
178 if (status_server->Start(folve_rt.status_port)) {
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
179 syslog(LOG_INFO, "HTTP status server on port %d",
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
180 folve_rt.status_port);
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
181 } else {
182 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
183 folve_rt.status_port);
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
184 }
185 }
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
186
187 // Some sanity checks.
188 if (folve_rt.fs->config_dirs().size() == 1) {
189 syslog(LOG_NOTICE, "No filter configuration directories given. "
190 "Any files will be just passed through verbatim.");
191 }
192 if (folve_rt.fs->config_dirs().size() > 2 && folve_rt.status_port < 0) {
193 syslog(LOG_WARNING, "Multiple filter configurations given, but no HTTP "
194 "status port. You only can switch filters via the HTTP interface; "
195 "add -p <port>");
196 }
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
197 return NULL;
198 }
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
199
0a3400d @hzeller o Write when we're exiting.
authored
200 static void folve_destroy(void *) {
db66f7d @hzeller o print which directory is serviced.
authored
201 syslog(LOG_INFO, "Exiting.");
0a3400d @hzeller o Write when we're exiting.
authored
202 }
203
b698853 @hzeller o README updates.
authored
204 static int usage(const char *prg) {
205 fprintf(stderr, "usage: %s <config-dir> <original-dir> <mount-point>\n", prg);
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
206 return 1;
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
207 }
208
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
209 struct FolveConfig {
210 FolveConfig() : base_dir(NULL), config_dir(NULL), port(-1) {}
211 const char *base_dir;
212 const char *mount_point;
213 const char *config_dir;
214 int port;
215 };
216
217 enum {
218 FOLVE_OPT_PORT = 42,
219 FOLVE_OPT_CONFIG,
220 };
221
222 int FolveOptionHandling(void *data, const char *arg, int key,
223 struct fuse_args *outargs) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
224 FolveRuntime *rt = (FolveRuntime*) data;
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
225 switch (key) {
226 case FUSE_OPT_KEY_NONOPT:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
227 // First non-opt: our underlying dir.
228 if (rt->fs->underlying_dir().empty()) {
229 rt->fs->set_underlying_dir(arg);
230 return 0; // we consumed this.
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
231 } else {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
232 rt->mount_point = strdup(arg); // remmber as FYI
233 return 1; // .. but leave it to fuse
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
234 }
235 case FOLVE_OPT_PORT:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
236 rt->status_port = atoi(arg + 2); // strip "-p"
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
237 return 0;
238 case FOLVE_OPT_CONFIG:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
239 rt->fs->add_config_dir(arg + 2); // strip "-c"
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
240 return 0;
241 }
242 return 1;
243 }
244
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
245 int main(int argc, char *argv[]) {
1e9ff21 @hzeller o main: report if parameters are not directories.
authored
246 const char *progname = argv[0];
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
247 if (argc < 4) {
1e9ff21 @hzeller o main: report if parameters are not directories.
authored
248 return usage(progname);
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
249 }
49eca7f @hzeller o add syslog as preparation for parameter handling.
authored
250
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
251 folve_rt.fs = new FolveFilesystem();
252
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
253 static struct fuse_opt folve_options[] = {
254 FUSE_OPT_KEY("-p ", FOLVE_OPT_PORT),
255 FUSE_OPT_KEY("-c ", FOLVE_OPT_CONFIG),
256 FUSE_OPT_END
257 };
258 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
259 fuse_opt_parse(&args, &folve_rt, folve_options, FolveOptionHandling);
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
260
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
261 if (!folve_rt.fs->CheckInitialized()) {
1e9ff21 @hzeller o main: report if parameters are not directories.
authored
262 return usage(progname);
263 }
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
264
265 struct fuse_operations folve_operations;
266 memset(&folve_operations, 0, sizeof(folve_operations));
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
267
0a3400d @hzeller o Write when we're exiting.
authored
268 // Start/stop. Will write to syslog and start auxiliary http service.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
269 folve_operations.init = folve_init;
0a3400d @hzeller o Write when we're exiting.
authored
270 folve_operations.destroy = folve_destroy;
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
271
272 // Basic operations to make navigation work.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
273 folve_operations.readdir = folve_readdir;
274 folve_operations.readlink = folve_readlink;
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
275
276 // open() and close() file.
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
277 folve_operations.open = folve_open;
278 folve_operations.release = folve_release;
8d1fbe7 @hzeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
279
280 // Actual workhorse: reading a file and returning predicted file-size
0330f76 @hzeller o Only start status server after we're daemonized, otherwise
authored
281 folve_operations.read = folve_read;
282 folve_operations.fgetattr = folve_fgetattr;
283 folve_operations.getattr = folve_getattr;
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
284
27c44cd @hzeller o Use fuse option handling to extract our flags and options.
authored
285 return fuse_main(args.argc, args.argv, &folve_operations, NULL);
98a5725 @hzeller o Start implementation. Just a basic forwarding filesystem for
authored
286 }
Something went wrong with that request. Please try again.