Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 204 lines (170 sloc) 6.583 kb
98a5725 Henner Zeller 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 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
19
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <sys/time.h>
28 #include <limits.h>
29 #include <stdlib.h>
30
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
31 #include "folve-filesystem.h"
8d1fbe7 Henner Zeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
32 #include "status-server.h"
e9c07dd Henner Zeller o add documentation
authored
33
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
34 #define FOLVE_VERSION "v. 0.79 &mdash; 2012-09-15"
35
36 // Compilation unit variables to communicate with the
37 static FolveFilesystem *folve_fs = NULL;
38 static const char *underlying_dir = NULL;
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
39
7c7bda6 Henner Zeller o attempt to create a sndfile for every file, if that fails, fall
authored
40 static int has_suffix_string (const char *str, const char *suffix) {
41 if (!str || !suffix)
42 return 0;
43 size_t str_len = strlen(str);
44 size_t suffix_len = strlen(suffix);
45 if (suffix_len > str_len)
46 return 0;
47 return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
48 }
49
50 static char *concat_path(char *buf, const char *a, const char *b) {
51 strcpy(buf, a);
52 strcat(buf, b);
53 return buf;
54 }
55
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
56 // Given a relative path from the root of the mounted file-system, get the
57 // original file from the source filesystem.
0aee3bb Henner Zeller o Show base dir that is mounted.
authored
58 // TODO(hzeller): move the ogg.fuse.flac logic into convolver-filesystem.
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
59 static const char *assemble_orig_path(char *buf, const char *path) {
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
60 char *result = concat_path(buf, underlying_dir, path);
7c7bda6 Henner Zeller o attempt to create a sndfile for every file, if that fails, fall
authored
61 static const char magic_ogg_rewrite[] = ".ogg.fuse.flac";
62 if (has_suffix_string(result, magic_ogg_rewrite)) {
63 *(result + strlen(result) - strlen(".fuse.flac")) = '\0';
64 }
65 return result;
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
66 }
67
7c7bda6 Henner Zeller o attempt to create a sndfile for every file, if that fails, fall
authored
68 // Essentially lstat(). Just forward to the original filesystem (this
73093dc Henner Zeller o Keep open file-descriptor.
authored
69 // will by lying: our convolved files are of different size...)
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
70 static int fuseconv_getattr(const char *path, struct stat *stbuf) {
9a8ea56 Henner Zeller o add infrastructure to provide Stat() output of currently open
authored
71 // If this is a currently open filename, we might be able to output a better
72 // estimate.
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
73 int result = folve_fs->StatByFilename(path, stbuf);
9a8ea56 Henner Zeller o add infrastructure to provide Stat() output of currently open
authored
74 if (result == 0) return result;
75
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
76 char path_buf[PATH_MAX];
9a8ea56 Henner Zeller o add infrastructure to provide Stat() output of currently open
authored
77 result = lstat(assemble_orig_path(path_buf, path), stbuf);
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
78 if (result == -1)
79 return -errno;
80
81 return 0;
82 }
83
73093dc Henner Zeller o Keep open file-descriptor.
authored
84 // readdir(). Just forward to original filesystem.
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
85 static int fuseconv_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
86 off_t offset, struct fuse_file_info *fi) {
87 DIR *dp;
88 struct dirent *de;
89 char path_buf[PATH_MAX];
90
91 dp = opendir(assemble_orig_path(path_buf, path));
92 if (dp == NULL)
93 return -errno;
94
7c7bda6 Henner Zeller o attempt to create a sndfile for every file, if that fails, fall
authored
95 char ogg_rewrite_buffer[PATH_MAX];
96
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
97 while ((de = readdir(dp)) != NULL) {
98 struct stat st;
99 memset(&st, 0, sizeof(st));
100 st.st_ino = de->d_ino;
101 st.st_mode = de->d_type << 12;
7c7bda6 Henner Zeller o attempt to create a sndfile for every file, if that fails, fall
authored
102 const char *entry_name = de->d_name;
103 if (has_suffix_string(entry_name, ".ogg")) {
104 // For ogg files, we pretend they actually end with 'flac', because
105 // we transparently rewrite them.
106 entry_name = concat_path(ogg_rewrite_buffer, entry_name, ".fuse.flac");
107 }
108 if (filler(buf, entry_name, &st, 0))
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
109 break;
110 }
111
112 closedir(dp);
113 return 0;
114 }
115
73093dc Henner Zeller o Keep open file-descriptor.
authored
116 // readlink(): forward to original filesystem.
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
117 static int fuseconv_readlink(const char *path, char *buf, size_t size) {
118 char path_buf[PATH_MAX];
119 const int result = readlink(assemble_orig_path(path_buf, path),
120 buf, size - 1);
121 if (result == -1)
122 return -errno;
123
124 buf[result] = '\0';
125 return 0;
126 }
127
128 static int fuseconv_open(const char *path, struct fuse_file_info *fi) {
8b18938 Henner Zeller o some comment changes. Ready to do some flac handling.
authored
129 // We want to be allowed to only return part of the requested data in read().
130 // That way, we can separate reading the ID3-tags from
131 // decoding of the music stream - that way indexing should be fast.
132 // Setting the flag 'direct_io' allows us to return partial results.
133 fi->direct_io = 1;
134
e9c07dd Henner Zeller o add documentation
authored
135 // The file-handle has the neat property to be 64 bit - so we can actually
8d1fbe7 Henner Zeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
136 // store a pointer to our filte robject in there :)
8b18938 Henner Zeller o some comment changes. Ready to do some flac handling.
authored
137 // (Yay, someone was thinking while developing that API).
15b305a Henner Zeller o Build infrastructure to re-use file handler objects. Files seem to
authored
138 char path_buf[PATH_MAX];
139 const char *orig_path = assemble_orig_path(path_buf, path);
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
140 FileHandler * handler = folve_fs->CreateHandler(path, orig_path);
680f590 Henner Zeller o report opening error.
authored
141 if (handler == NULL)
142 return -errno;
143 fi->fh = (uint64_t) handler;
144 return 0;
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
145 }
146
147 static int fuseconv_read(const char *path, char *buf, size_t size, off_t offset,
148 struct fuse_file_info *fi) {
8d1fbe7 Henner Zeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
149 return reinterpret_cast<FileHandler *>(fi->fh)->Read(buf, size, offset);
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
150 }
151
73093dc Henner Zeller o Keep open file-descriptor.
authored
152 static int fuseconv_release(const char *path, struct fuse_file_info *fi) {
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
153 folve_fs->Close(path);
8d1fbe7 Henner Zeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
154 return 0;
9a8ea56 Henner Zeller o add infrastructure to provide Stat() output of currently open
authored
155 }
156
157 static int fuseconv_fgetattr(const char *path, struct stat *result,
158 struct fuse_file_info *fi) {
8d1fbe7 Henner Zeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
159 return reinterpret_cast<FileHandler *>(fi->fh)->Stat(result);
73093dc Henner Zeller o Keep open file-descriptor.
authored
160 }
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
161
162
8b18938 Henner Zeller o some comment changes. Ready to do some flac handling.
authored
163 static int usage(const char *prog) {
31bd9ce Henner Zeller o README update.
authored
164 fprintf(stderr, "usage: %s <config-dir> <original-dir> <mount-point>\n",
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
165 prog);
8b18938 Henner Zeller o some comment changes. Ready to do some flac handling.
authored
166 return 1;
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
167 }
168
169 int main(int argc, char *argv[]) {
8b18938 Henner Zeller o some comment changes. Ready to do some flac handling.
authored
170 if (argc < 4) {
171 return usage(argv[0]);
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
172 }
173
174 // First, let's extract our configuration.
8d1fbe7 Henner Zeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
175 const char *config_dir = argv[1];
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
176 underlying_dir = argv[2];
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
177 argc -=2;
178 argv += 2;
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
179 folve_fs = new FolveFilesystem(FOLVE_VERSION, underlying_dir, config_dir);
8d1fbe7 Henner Zeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
180
b5c1e96 Henner Zeller o more useful output on status server.
authored
181 // TODO(hzeller): make this configurable
a9d3e53 Henner Zeller o Found a project name: "Folve". Some renamings because of that.
authored
182 StatusServer *statusz = new StatusServer(folve_fs);
b5c1e96 Henner Zeller o more useful output on status server.
authored
183 statusz->Start(17322);
8d1fbe7 Henner Zeller o Move fuse-convolve.c to fuse-convolve.cc. Make filter-interface
authored
184
185 struct fuse_operations fuseconv_operations;
186 memset(&fuseconv_operations, 0, sizeof(fuseconv_operations));
187
188 // Basic operations to make navigation work.
189 fuseconv_operations.readdir = fuseconv_readdir;
190 fuseconv_operations.readlink = fuseconv_readlink;
191
192 // open() and close() file.
193 fuseconv_operations.open = fuseconv_open;
194 fuseconv_operations.release = fuseconv_release;
195
196 // Actual workhorse: reading a file and returning predicted file-size
197 fuseconv_operations.read = fuseconv_read;
198 fuseconv_operations.fgetattr = fuseconv_fgetattr;
199 fuseconv_operations.getattr = fuseconv_getattr;
98a5725 Henner Zeller o Start implementation. Just a basic forwarding filesystem for
authored
200
201 // Lazy: let the rest handle by fuse provided main.
202 return fuse_main(argc, argv, &fuseconv_operations, NULL);
203 }
Something went wrong with that request. Please try again.