Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 739 lines (678 sloc) 27.348 kB
4d4d90b @hzeller o More legalese
authored
1 // Folve - A fuse filesystem that convolves audio files on-the-fly.
2 //
e9c07dd @hzeller o add documentation
authored
3 // Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
cca212e @hzeller o document compile error with old version of fuse.
authored
4 //
e9c07dd @hzeller o add documentation
authored
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
18 #include "folve-filesystem.h"
19
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
20 #include <FLAC/metadata.h>
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
21 #include <assert.h>
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
22 #include <dirent.h>
e9c07dd @hzeller o add documentation
authored
23 #include <errno.h>
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
24 #include <fcntl.h>
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
25 #include <sndfile.h>
e9c07dd @hzeller o add documentation
authored
26 #include <stdio.h>
27 #include <string.h>
28 #include <strings.h>
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
29 #include <sys/stat.h>
30 #include <sys/types.h>
5830c5a @hzeller o Switch logging to syslog()
authored
31 #include <syslog.h>
e9c07dd @hzeller o add documentation
authored
32 #include <unistd.h>
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
33
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
34 #include <map>
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
35 #include <string>
6c6dedb @hzeller o Some readme tweaks and readability improvements.
authored
36 #include <zita-convolver.h>
e9c07dd @hzeller o add documentation
authored
37
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
38 #include "conversion-buffer.h"
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
39 #include "file-handler-cache.h"
40 #include "file-handler.h"
2fee1d6 @hzeller o extract sound processor
authored
41 #include "sound-processor.h"
8268c37 @hzeller o Bubble up error messages in status server.
authored
42 #include "util.h"
6c6dedb @hzeller o Some readme tweaks and readability improvements.
authored
43
fd65cee @hzeller o add basic zita filter.
authored
44 #include "zita-config.h"
45
8268c37 @hzeller o Bubble up error messages in status server.
authored
46 using folve::Appendf;
3e883b7 @hzeller o Match filters in sequence
authored
47 using folve::StringPrintf;
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
48 using folve::DLogf;
b5c1e96 @hzeller o more useful output on status server.
authored
49
e9c07dd @hzeller o add documentation
authored
50 namespace {
51
5476cc2 @hzeller o provide absolute path.
authored
52 // Very simple filter that just passes the original file through. Used for
53 // everything that is not a sound-file.
f1ef960 @hzeller o a bit HTML polishing.
authored
54 class PassThroughHandler : public FileHandler {
e9c07dd @hzeller o add documentation
authored
55 public:
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
56 PassThroughHandler(int filedes, const std::string &filter_id,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
57 const HandlerStats &known_stats)
f1ef960 @hzeller o a bit HTML polishing.
authored
58 : FileHandler(filter_id), filedes_(filedes),
59 file_size_(-1), max_accessed_(0), info_stats_(known_stats) {
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
60 DLogf("Creating PassThrough filter for '%s'", known_stats.filename.c_str());
f1ef960 @hzeller o a bit HTML polishing.
authored
61 struct stat st;
62 file_size_ = (Stat(&st) == 0) ? st.st_size : -1;
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
63 info_stats_.filter_dir = ""; // pass through.
e9c07dd @hzeller o add documentation
authored
64 }
f1ef960 @hzeller o a bit HTML polishing.
authored
65 ~PassThroughHandler() { close(filedes_); }
680f590 @hzeller o report opening error.
authored
66
e9c07dd @hzeller o add documentation
authored
67 virtual int Read(char *buf, size_t size, off_t offset) {
68 const int result = pread(filedes_, buf, size, offset);
79e7608 @hzeller o Don't add a message if not necessary to the pass through filter.
authored
69 max_accessed_ = std::max(max_accessed_, (long unsigned int) offset + result);
e9c07dd @hzeller o add documentation
authored
70 return result == -1 ? -errno : result;
71 }
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
72 virtual int Stat(struct stat *st) {
73 return fstat(filedes_, st);
74 }
e2c7d54 @hzeller o There was a report of a 2.7.1 ICE, apparently on a generated move
authored
75 virtual void GetHandlerStatus(HandlerStats *stats) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
76 *stats = info_stats_;
f1ef960 @hzeller o a bit HTML polishing.
authored
77 if (file_size_ > 0) {
78 stats->progress = 1.0 * max_accessed_ / file_size_;
79 }
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
80 }
e2c7d54 @hzeller o There was a report of a 2.7.1 ICE, apparently on a generated move
authored
81
e9c07dd @hzeller o add documentation
authored
82 private:
83 const int filedes_;
f1ef960 @hzeller o a bit HTML polishing.
authored
84 size_t file_size_;
85 long unsigned int max_accessed_;
8268c37 @hzeller o Bubble up error messages in status server.
authored
86 HandlerStats info_stats_;
e9c07dd @hzeller o add documentation
authored
87 };
88
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
89 class SndFileHandler :
90 public FileHandler,
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
91 public ConversionBuffer::SoundSource {
e9c07dd @hzeller o add documentation
authored
92 public:
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
93 // Attempt to create a SndFileHandler from the given file descriptor. This
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
94 // returns NULL if this is not a sound-file or if there is no available
95 // convolution filter configuration available.
8268c37 @hzeller o Bubble up error messages in status server.
authored
96 // "partial_file_info" will be set to information known so far, including
97 // error message.
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
98 static FileHandler *Create(FolveFilesystem *fs,
99 int filedes, const char *fs_path,
100 const std::string &underlying_file,
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
101 const std::string &filter_subdir,
8268c37 @hzeller o Bubble up error messages in status server.
authored
102 const std::string &zita_config_dir,
103 HandlerStats *partial_file_info) {
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
104 SF_INFO in_info;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
105 memset(&in_info, 0, sizeof(in_info));
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
106 SNDFILE *snd = sf_open_fd(filedes, SFM_READ, &in_info, 0);
107 if (snd == NULL) {
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
108 DLogf("File %s: %s", underlying_file.c_str(), sf_strerror(NULL));
8268c37 @hzeller o Bubble up error messages in status server.
authored
109 partial_file_info->message = sf_strerror(NULL);
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
110 return NULL;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
111 }
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
112
7e756eb @hzeller o Fix problem with re-initializing the convproc. Turns out that the c…
authored
113 int bits = 16;
31bd9ce @hzeller o README update.
authored
114 if ((in_info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_24) bits = 24;
115 if ((in_info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_32) bits = 32;
e2619a6 @hzeller o skipping debug message.
authored
116
8268c37 @hzeller o Bubble up error messages in status server.
authored
117 // Remember whatever we could got to know in the partial file info.
118 Appendf(&partial_file_info->format, "%.1fkHz, %d Bit",
119 in_info.samplerate / 1000.0, bits);
120 partial_file_info->duration_seconds = in_info.frames / in_info.samplerate;
121
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
122 SoundProcessor *processor = fs->processor_pool()
cf8ddb8 @hzeller o Remove TODO: pass out detailed error msg when failing to
authored
123 ->GetOrCreate(zita_config_dir, in_info.samplerate, in_info.channels, bits,
124 &partial_file_info->message);
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
125 if (processor == NULL) {
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
126 sf_close(snd);
127 return NULL;
b5c1e96 @hzeller o more useful output on status server.
authored
128 }
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
129 const int seconds = in_info.frames / in_info.samplerate;
130 DLogf("File %s, %.1fkHz, %d Bit, %d:%02d: filter config %s",
131 underlying_file.c_str(), in_info.samplerate / 1000.0, bits,
132 seconds / 60, seconds % 60,
133 processor->config_file().c_str());
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
134 return new SndFileHandler(fs, fs_path, filter_subdir,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
135 underlying_file, filedes, snd, in_info,
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
136 *partial_file_info, processor);
e9c07dd @hzeller o add documentation
authored
137 }
138
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
139 virtual ~SndFileHandler() {
680f590 @hzeller o report opening error.
authored
140 Close();
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
141 delete output_buffer_;
142 }
143
e9c07dd @hzeller o add documentation
authored
144 virtual int Read(char *buf, size_t size, off_t offset) {
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
145 if (error_) return -1;
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
146 // If this is a skip suspiciously at the very end of the file as
147 // reported by stat, we don't do any encoding, just return garbage.
1118ad9 @hzeller o comment update.
authored
148 // (otherwise we'd to convolve up to that point).
149 //
150 // While indexing, media players do this sometimes apparently.
151 // And sometimes not even to the very end but 'almost' at the end.
152 // So add some FudeOverhang
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
153 static const int kFudgeOverhang = 512;
abb0471 @hzeller o remove debug message from last commit.
authored
154 // But of course only if this is really a skip, not a regular approaching
155 // end-of-file.
bcb77e7 @hzeller o Ran with Amarok over my files. Looks like it sometimes only 'almost'
authored
156 if (output_buffer_->FileSize() < offset
157 && (int) (offset + size + kFudgeOverhang) >= file_stat_.st_size) {
158 const int pretended_bytes = std::min((off_t)size,
159 file_stat_.st_size - offset);
160 if (pretended_bytes > 0) {
161 memset(buf, 0x00, pretended_bytes);
162 return pretended_bytes;
680f590 @hzeller o report opening error.
authored
163 } else {
164 return 0;
165 }
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
166 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
167 // The following read might block and call WriteToSoundfile() until the
168 // buffer is filled.
169 return output_buffer_->Read(buf, size, offset);
e9c07dd @hzeller o add documentation
authored
170 }
171
e2c7d54 @hzeller o There was a report of a 2.7.1 ICE, apparently on a generated move
authored
172 virtual void GetHandlerStatus(HandlerStats *stats) {
b4eec5c @hzeller o libboost caused too many troubles in embedded systems with weak
authored
173 folve::MutexLock l(&stats_mutex_);
f00e6a4 @hzeller o Don't do unprotected read on status information such as
authored
174 if (processor_ != NULL) {
175 base_stats_.max_output_value = processor_->max_output_value();
176 }
8268c37 @hzeller o Bubble up error messages in status server.
authored
177 *stats = base_stats_;
2fee1d6 @hzeller o extract sound processor
authored
178 const int frames_done = in_info_.frames - input_frames_left_;
179 if (frames_done == 0 || in_info_.frames == 0)
223f392 @hzeller o Have a separate HandlerStats object that contains relevant
authored
180 stats->progress = 0.0;
181 else
2fee1d6 @hzeller o extract sound processor
authored
182 stats->progress = 1.0 * frames_done / in_info_.frames;
f00e6a4 @hzeller o Don't do unprotected read on status information such as
authored
183
d93a35b @hzeller o Uh, typo in gapless message.
authored
184 if (base_stats_.max_output_value > 1.0) {
a5c6382 @hzeller o Remember the max output value in the stats
authored
185 // TODO: the status server could inspect this value and make better
186 // rendering.
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
187 base_stats_.message =
ce22b6e @hzeller o change wording from 'overdrive' to 'clipping' so that
authored
188 StringPrintf("Output clipping! "
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
189 "(max=%.3f; Multiply gain with <= %.5f<br/>in %s)",
a5c6382 @hzeller o Remember the max output value in the stats
authored
190 base_stats_.max_output_value,
191 1.0 / base_stats_.max_output_value,
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
192 processor_ != NULL
193 ? processor_->config_file().c_str()
194 : "filter");
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
195 }
b5c1e96 @hzeller o more useful output on status server.
authored
196 }
197
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
198 virtual int Stat(struct stat *st) {
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
199 if (output_buffer_->FileSize() > start_estimating_size_) {
2fee1d6 @hzeller o extract sound processor
authored
200 const int frames_done = in_info_.frames - input_frames_left_;
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
201 if (frames_done > 0) {
2fee1d6 @hzeller o extract sound processor
authored
202 const float estimated_end = 1.0 * in_info_.frames / frames_done;
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
203 off_t new_size = estimated_end * output_buffer_->FileSize();
204 // Report a bit bigger size which is less harmful than programs
205 // reading short.
206 new_size += 16384;
453489f @hzeller o smallish whitespace changes.
authored
207 if (new_size > file_stat_.st_size) { // Only go forward in size.
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
208 file_stat_.st_size = new_size;
209 }
210 }
211 }
212 *st = file_stat_;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
213 return 0;
214 }
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
215
e9c07dd @hzeller o add documentation
authored
216 private:
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
217 // TODO(hzeller): trim parameter list.
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
218 SndFileHandler(FolveFilesystem *fs, const char *fs_path,
219 const std::string &filter_dir,
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
220 const std::string &underlying_file,
221 int filedes, SNDFILE *snd_in,
8268c37 @hzeller o Bubble up error messages in status server.
authored
222 const SF_INFO &in_info, const HandlerStats &file_info,
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
223 SoundProcessor *processor)
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
224 : FileHandler(filter_dir), fs_(fs),
2fee1d6 @hzeller o extract sound processor
authored
225 filedes_(filedes), snd_in_(snd_in), in_info_(in_info),
226 base_stats_(file_info),
b5c1e96 @hzeller o more useful output on status server.
authored
227 error_(false), output_buffer_(NULL),
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
228 snd_out_(NULL), processor_(processor),
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
229 input_frames_left_(in_info.frames) {
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
230
231 // Initial stat that we're going to report to clients. We'll adapt
232 // the filesize as we see it grow. Some clients continuously monitor
233 // the size of the file to check when to stop.
234 fstat(filedes_, &file_stat_);
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
235 start_estimating_size_ = 0.4 * file_stat_.st_size;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
236
8e36eff @hzeller o comment update.
authored
237 // The flac header we get is more rich than what we can create via
238 // sndfile. So if we have one, just copy it.
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
239 copy_flac_header_verbatim_ = LooksLikeInputIsFlac(in_info, filedes);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
240
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
241 // Create a conversion buffer that creates a soundfile of a particular
8e36eff @hzeller o comment update.
authored
242 // format that we choose here. Essentially we want to generate mostly what
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
243 // our input is.
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
244 SF_INFO out_info = in_info;
31bd9ce @hzeller o README update.
authored
245 out_info.seekable = 0;
246 if ((in_info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_OGG) {
8e36eff @hzeller o comment update.
authored
247 // If the input was ogg, we're re-coding this to flac, because it
248 // wouldn't let us stream the output.
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
249 out_info.format = SF_FORMAT_FLAC;
f11d8cd @hzeller o implement initial skip mode detection.
authored
250 out_info.format |= SF_FORMAT_PCM_16;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
251 }
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
252 else if ((in_info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) {
253 out_info.format = SF_FORMAT_FLAC; // recode as flac.
254 out_info.format |= SF_FORMAT_PCM_24;
31bd9ce @hzeller o README update.
authored
255 }
256 else { // original format.
257 out_info.format = in_info.format;
258 }
259
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
260 output_buffer_ = new ConversionBuffer(this, out_info);
261 }
262
f8cb9e5 @hzeller o redacting MD5.
authored
263 virtual void SetOutputSoundfile(ConversionBuffer *out_buffer,
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
264 SNDFILE *sndfile) {
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
265 snd_out_ = sndfile;
266 if (snd_out_ == NULL) {
267 error_ = true;
5830c5a @hzeller o Switch logging to syslog()
authored
268 syslog(LOG_ERR, "Opening output: %s", sf_strerror(NULL));
8268c37 @hzeller o Bubble up error messages in status server.
authored
269 base_stats_.message = sf_strerror(NULL);
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
270 return;
271 }
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
272 if (copy_flac_header_verbatim_) {
8e36eff @hzeller o comment update.
authored
273 out_buffer->set_sndfile_writes_enabled(false);
f8cb9e5 @hzeller o redacting MD5.
authored
274 CopyFlacHeader(out_buffer);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
275 } else {
f8cb9e5 @hzeller o redacting MD5.
authored
276 out_buffer->set_sndfile_writes_enabled(true);
8e36eff @hzeller o comment update.
authored
277 GenerateHeaderFromInputFile(out_buffer);
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
278 }
279 // Now flush the header: that way if someone only reads the metadata, then
280 // our AddMoreSoundData() is never called.
8e36eff @hzeller o comment update.
authored
281 // We need to do this even if we copied our own header: that way we make
282 // sure that the sndfile-header is flushed into the nirwana before we
283 // re-enable sndfile_writes.
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
284 sf_command(snd_out_, SFC_UPDATE_HEADER_NOW, NULL, 0);
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
285
286 // -- time for some hackery ...
287 // If we have copied the header over from the original, we need to
288 // redact the values for min/max blocksize and min/max framesize with
289 // what SNDFILE is going to use, otherwise programs will trip over this.
290 // http://flac.sourceforge.net/format.html
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
291 if (copy_flac_header_verbatim_) {
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
292 out_buffer->WriteCharAt((1152 & 0xFF00) >> 8, 8);
293 out_buffer->WriteCharAt((1152 & 0x00FF) , 9);
335d185 @hzeller o Fix blocksize header; this was once an experiment that seems
authored
294 out_buffer->WriteCharAt((1152 & 0xFF00) >> 8, 10);
295 out_buffer->WriteCharAt((1152 & 0x00FF) , 11);
296 for (int i = 12; i < 18; ++i) out_buffer->WriteCharAt(0, i); // framesize
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
297 } else {
298 // .. and if SNDFILE writes the header, it misses out in writing the
299 // number of samples to be expected. So let's fill that in.
300 // The MD5 sum starts at position strlen("fLaC") + 4 + 18 = 26
301 // The 32 bits before that are the samples (and another 4 bit before that,
302 // ignoring that for now).
2fee1d6 @hzeller o extract sound processor
authored
303 out_buffer->WriteCharAt((in_info_.frames & 0xFF000000) >> 24, 22);
304 out_buffer->WriteCharAt((in_info_.frames & 0x00FF0000) >> 16, 23);
305 out_buffer->WriteCharAt((in_info_.frames & 0x0000FF00) >> 8, 24);
306 out_buffer->WriteCharAt((in_info_.frames & 0x000000FF), 25);
a85bebb @hzeller o Fix Flac header manually. Luckily the streamheader is a fixed
authored
307 }
f8cb9e5 @hzeller o redacting MD5.
authored
308
309 out_buffer->set_sndfile_writes_enabled(true); // ready for sound-stream.
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
310 DLogf("Header init done (%s).", base_stats_.filename.c_str());
550c4e0 @hzeller o allow short read in header area, but not in the stream
authored
311 out_buffer->HeaderFinished();
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
312 }
313
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
314 bool HasStarted() { return in_info_.frames != input_frames_left_; }
315 virtual bool AcceptProcessor(SoundProcessor *passover_processor) {
316 if (HasStarted()) {
317 DLogf("Gapless attempt: Cannot bridge gap to already open file %s",
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
318 base_stats_.filename.c_str());
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
319 return false;
1113409 @hzeller o Diagnostic if gapless cannot be done, because the other file is
authored
320 }
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
321 assert(processor_);
322 if (passover_processor->config_file() != processor_->config_file()
323 || (passover_processor->config_file_timestamp()
324 != processor_->config_file_timestamp())) {
325 DLogf("Gapless: Configuration changed; can't use %p to join gapless.",
326 passover_processor);
9af16fe @hzeller o Gapless string should break when configuration changes.
authored
327 return false;
328 }
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
329 // Ok, so don't use the processor we already have, but use the other one.
330 fs_->processor_pool()->Return(processor_);
331 processor_ = passover_processor;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
332 if (!processor_->is_input_buffer_complete()) {
7b499ea @hzeller o Uh, log() is actually ln() and log10f() is log() :)
authored
333 // Fill with our beginning so that the donor can finish its processing.
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
334 input_frames_left_ -= processor_->FillBuffer(snd_in_);
335 }
336 base_stats_.in_gapless = true;
337 return true;
338 }
339
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
340 static bool ExtractDirAndSuffix(const std::string &filename,
341 std::string *dir, std::string *suffix) {
342 const std::string::size_type slash_pos = filename.find_last_of('/');
343 if (slash_pos == std::string::npos) return false;
344 *dir = filename.substr(0, slash_pos + 1);
345 const std::string::size_type dot_pos = filename.find_last_of('.');
346 if (dot_pos != std::string::npos && dot_pos > slash_pos) {
347 *suffix = filename.substr(dot_pos);
348 }
349 return true;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
350 }
351
47c2b03 @hzeller o better skip mode detection. Only if someone _really_ jumps to the
authored
352 virtual bool AddMoreSoundData() {
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
353 if (!input_frames_left_)
354 return false;
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
355 if (processor_->pending_writes() > 0) {
356 processor_->WriteProcessed(snd_out_, processor_->pending_writes());
357 return input_frames_left_;
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
358 }
2fee1d6 @hzeller o extract sound processor
authored
359 const int r = processor_->FillBuffer(snd_in_);
9c0ba15 @hzeller o work around broken files - don't endlessloop :)
authored
360 if (r == 0) {
2fee1d6 @hzeller o extract sound processor
authored
361 syslog(LOG_ERR, "Expected %d frames left, "
5830c5a @hzeller o Switch logging to syslog()
authored
362 "but got EOF; corrupt file '%s' ?",
2fee1d6 @hzeller o extract sound processor
authored
363 input_frames_left_, base_stats_.filename.c_str());
0706452 @hzeller o Report broken input file in UI.
authored
364 base_stats_.message = "Premature EOF in input file.";
9c0ba15 @hzeller o work around broken files - don't endlessloop :)
authored
365 input_frames_left_ = 0;
366 Close();
367 return false;
368 }
b4eec5c @hzeller o libboost caused too many troubles in embedded systems with weak
authored
369 stats_mutex_.Lock();
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
370 input_frames_left_ -= r;
b4eec5c @hzeller o libboost caused too many troubles in embedded systems with weak
authored
371 stats_mutex_.Unlock();
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
372 if (!input_frames_left_ && !processor_->is_input_buffer_complete()
373 && fs_->gapless_processing()) {
374 typedef std::set<std::string> DirSet;
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
375 DirSet dirset;
376 std::string fs_dir, file_suffix;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
377 FileHandler *next_file = NULL;
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
378 DirSet::const_iterator found;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
379 const bool passed_processor
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
380 = (ExtractDirAndSuffix(base_stats_.filename, &fs_dir, &file_suffix)
381 && fs_->ListDirectory(fs_dir, file_suffix, &dirset)
382 && (found = dirset.upper_bound(base_stats_.filename)) != dirset.end()
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
383 && (next_file = fs_->GetOrCreateHandler(found->c_str()))
384 && next_file->AcceptProcessor(processor_));
385 if (passed_processor) {
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
386 DLogf("Processor %p: Gapless pass-on from "
387 "'%s' to alphabetically next '%s'", processor_,
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
388 base_stats_.filename.c_str(), found->c_str());
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
389 }
b4eec5c @hzeller o libboost caused too many troubles in embedded systems with weak
authored
390 stats_mutex_.Lock();
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
391 processor_->WriteProcessed(snd_out_, r);
b4eec5c @hzeller o libboost caused too many troubles in embedded systems with weak
authored
392 stats_mutex_.Unlock();
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
393 if (passed_processor) {
394 base_stats_.out_gapless = true;
7b499ea @hzeller o Uh, log() is actually ln() and log10f() is log() :)
authored
395 SaveOutputValues();
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
396 processor_ = NULL; // we handed over ownership.
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
397 }
398 if (next_file) fs_->Close(found->c_str(), next_file);
399 } else {
b4eec5c @hzeller o libboost caused too many troubles in embedded systems with weak
authored
400 stats_mutex_.Lock();
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
401 processor_->WriteProcessed(snd_out_, r);
b4eec5c @hzeller o libboost caused too many troubles in embedded systems with weak
authored
402 stats_mutex_.Unlock();
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
403 }
31bd9ce @hzeller o README update.
authored
404 if (input_frames_left_ == 0) {
680f590 @hzeller o report opening error.
authored
405 Close();
31bd9ce @hzeller o README update.
authored
406 }
0edb358 @hzeller o attempt to remove global references in the zita config.
authored
407 return input_frames_left_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
408 }
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
409
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
410 // TODO add as a utility function to ConversionBuffer ?
8e36eff @hzeller o comment update.
authored
411 void CopyBytes(int fd, off_t pos, ConversionBuffer *out, size_t len) {
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
412 char buf[256];
413 while (len > 0) {
414 ssize_t r = pread(fd, buf, std::min(sizeof(buf), len), pos);
8e36eff @hzeller o comment update.
authored
415 if (r <= 0) return;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
416 out->Append(buf, r);
417 len -= r;
418 pos += r;
419 }
420 }
421
f8cb9e5 @hzeller o redacting MD5.
authored
422 void CopyFlacHeader(ConversionBuffer *out_buffer) {
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
423 DLogf("Provide FLAC header from original file %s",
424 base_stats_.filename.c_str());
f8cb9e5 @hzeller o redacting MD5.
authored
425 out_buffer->Append("fLaC", 4);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
426 off_t pos = 4;
427 unsigned char header[4];
f8cb9e5 @hzeller o redacting MD5.
authored
428 bool need_finish_padding = false;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
429 while (pread(filedes_, header, sizeof(header), pos) == sizeof(header)) {
430 pos += sizeof(header);
431 bool is_last = header[0] & 0x80;
432 unsigned int type = header[0] & 0x7F;
433 unsigned int byte_len = (header[1] << 16) + (header[2] << 8) + header[3];
5098e07 @hzeller o improve logging.
authored
434 const char *extra_info = "";
f8cb9e5 @hzeller o redacting MD5.
authored
435 need_finish_padding = false;
436 if (type == FLAC__METADATA_TYPE_STREAMINFO && byte_len == 34) {
437 out_buffer->Append(&header, sizeof(header));
8e36eff @hzeller o comment update.
authored
438 // Copy everything but the MD5 at the end - which we set to empty.
f8cb9e5 @hzeller o redacting MD5.
authored
439 CopyBytes(filedes_, pos, out_buffer, byte_len - 16);
440 for (int i = 0; i < 16; ++i) out_buffer->Append("\0", 1);
5098e07 @hzeller o improve logging.
authored
441 extra_info = "Streaminfo; redact MD5.";
f8cb9e5 @hzeller o redacting MD5.
authored
442 }
443 else if (type == FLAC__METADATA_TYPE_SEEKTABLE) {
444 // The SEEKTABLE header we skip, because it is bogus after encoding.
5830c5a @hzeller o Switch logging to syslog()
authored
445 // TODO append log (skip the seektable)
f8cb9e5 @hzeller o redacting MD5.
authored
446 need_finish_padding = is_last; // if we were last, force finish block.
5098e07 @hzeller o improve logging.
authored
447 extra_info = "Skip seektable.";
f8cb9e5 @hzeller o redacting MD5.
authored
448 }
449 else {
450 out_buffer->Append(&header, sizeof(header));
451 CopyBytes(filedes_, pos, out_buffer, byte_len);
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
452 }
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
453 DLogf(" %02x %02x %02x %02x type: %d, len: %6u %s %s ",
454 header[0], header[1], header[2], header[3],
455 type, byte_len, is_last ? "(last)" : "(cont)", extra_info);
f8cb9e5 @hzeller o redacting MD5.
authored
456 pos += byte_len;
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
457 if (is_last)
458 break;
459 }
f8cb9e5 @hzeller o redacting MD5.
authored
460 if (need_finish_padding) { // if the last block was not is_last: pad.
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
461 DLogf("write padding");
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
462 memset(&header, 0, sizeof(header));
463 header[0] = 0x80 /* is last */ | FLAC__METADATA_TYPE_PADDING;
f8cb9e5 @hzeller o redacting MD5.
authored
464 out_buffer->Append(&header, sizeof(header));
7e27dab @hzeller o initial version of copying header. Not sure yet though if
authored
465 }
466 }
467
8e36eff @hzeller o comment update.
authored
468 void GenerateHeaderFromInputFile(ConversionBuffer *out_buffer) {
7689c9a @hzeller o preparation to extract classes from folve-filesystem into their
authored
469 DLogf("Generate header from original ID3-tags.");
8e36eff @hzeller o comment update.
authored
470 out_buffer->set_sndfile_writes_enabled(true);
471 // Copy ID tags that are supported by sndfile.
472 for (int i = SF_STR_FIRST; i <= SF_STR_LAST; ++i) {
473 const char *s = sf_get_string(snd_in_, i);
474 if (s != NULL) {
475 sf_set_string(snd_out_, i, s);
476 }
477 }
478 }
479
7b499ea @hzeller o Uh, log() is actually ln() and log10f() is log() :)
authored
480 void SaveOutputValues() {
a5c6382 @hzeller o Remember the max output value in the stats
authored
481 if (processor_) {
482 base_stats_.max_output_value = processor_->max_output_value();
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
483 processor_->ResetMaxValues();
60477fe @hzeller o Detect output overdrive and give useful message what to do.
authored
484 }
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
485 }
a5c6382 @hzeller o Remember the max output value in the stats
authored
486
fb3fb9a @hzeller o Log clipping even if we pass on the processor.
authored
487 void Close() {
488 if (snd_out_ == NULL) return; // done.
7b499ea @hzeller o Uh, log() is actually ln() and log10f() is log() :)
authored
489 SaveOutputValues();
a5c6382 @hzeller o Remember the max output value in the stats
authored
490 if (base_stats_.max_output_value > 1.0) {
491 syslog(LOG_ERR, "Observed output clipping in '%s': "
492 "Max=%.3f; Multiply gain with <= %.5f in %s",
493 base_stats_.filename.c_str(), base_stats_.max_output_value,
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
494 1.0 / base_stats_.max_output_value,
495 processor_ != NULL ? processor_->config_file().c_str() : "filter");
a5c6382 @hzeller o Remember the max output value in the stats
authored
496 }
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
497 fs_->processor_pool()->Return(processor_);
7b499ea @hzeller o Uh, log() is actually ln() and log10f() is log() :)
authored
498 processor_ = NULL;
0b0bd33 @hzeller Fix bug: the last couple of sound-samples were not flushed. We want
authored
499 // We can't disable buffer writes here, because outfile closing will flush
500 // the last couple of sound samples.
680f590 @hzeller o report opening error.
authored
501 if (snd_in_) sf_close(snd_in_);
502 if (snd_out_) sf_close(snd_out_);
503 snd_out_ = NULL;
504 close(filedes_);
505 }
506
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
507 bool LooksLikeInputIsFlac(const SF_INFO &sndinfo, int filedes) {
508 if ((sndinfo.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
509 return false;
510 // However some files contain flac encoded stuff, but are not flac files
511 // by themselve. So we can't copy headers verbatim. Sanity check header.
512 char flac_magic[4];
513 if (pread(filedes, flac_magic, sizeof(flac_magic), 0) != sizeof(flac_magic))
514 return false;
515 return memcmp(flac_magic, "fLaC", sizeof(flac_magic)) == 0;
516 }
517
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
518 FolveFilesystem *const fs_;
e9c07dd @hzeller o add documentation
authored
519 const int filedes_;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
520 SNDFILE *const snd_in_;
2fee1d6 @hzeller o extract sound processor
authored
521 const SF_INFO in_info_;
7c7bda6 @hzeller o attempt to create a sndfile for every file, if that fails, fall
authored
522
b4eec5c @hzeller o libboost caused too many troubles in embedded systems with weak
authored
523 folve::Mutex stats_mutex_;
2fee1d6 @hzeller o extract sound processor
authored
524 HandlerStats base_stats_; // UI information about current file.
525
526 struct stat file_stat_; // we dynamically report a changing size.
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
527 off_t start_estimating_size_; // essentially const.
528
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
529 bool error_;
bea11d6 @hzeller o Don't use snprintf, but use a new function Appendf() to assemble
authored
530 bool copy_flac_header_verbatim_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
531 ConversionBuffer *output_buffer_;
532 SNDFILE *snd_out_;
533
534 // Used in conversion.
2fee1d6 @hzeller o extract sound processor
authored
535 SoundProcessor *processor_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
536 int input_frames_left_;
e9c07dd @hzeller o add documentation
authored
537 };
538 } // namespace
539
8c7e5c1 @hzeller o Only if -D is given, the debug toggle is active in the UI.
authored
540 FolveFilesystem::FolveFilesystem()
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
541 : debug_ui_enabled_(false), gapless_processing_(false),
950f192 @hzeller o Wire up processor-pool to folve-filesystem.
authored
542 open_file_cache_(4), processor_pool_(3),
543 total_file_openings_(0), total_file_reopen_(0) {
8c7e5c1 @hzeller o Only if -D is given, the debug toggle is active in the UI.
authored
544 }
ba7fde8 @hzeller o implement size estimate. If the user stat() or fstat() the file,
authored
545
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
546 FileHandler *FolveFilesystem::CreateFromDescriptor(
547 int filedes,
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
548 const std::string &config_dir,
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
549 const char *fs_path,
550 const std::string &underlying_file) {
8268c37 @hzeller o Bubble up error messages in status server.
authored
551 HandlerStats file_info;
552 file_info.filename = fs_path;
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
553 file_info.filter_dir = config_dir;
554 if (!config_dir.empty()) {
555 const std::string full_config_path = base_config_dir_ + "/" + config_dir;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
556 FileHandler *filter = SndFileHandler::Create(this, filedes, fs_path,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
557 underlying_file,
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
558 config_dir, full_config_path,
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
559 &file_info);
560 if (filter != NULL) return filter;
561 }
8b18938 @hzeller o some comment changes. Ready to do some flac handling.
authored
562 // Every other file-type is just passed through as is.
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
563 return new PassThroughHandler(filedes, config_dir, file_info);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
564 }
565
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
566 std::string FolveFilesystem::CacheKey(const std::string &config_path,
567 const char *fs_path) {
568 return config_path + fs_path;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
569 }
570
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
571 FileHandler *FolveFilesystem::GetOrCreateHandler(const char *fs_path) {
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
572 const std::string config_path = current_config_subdir_;
573 const std::string cache_key = CacheKey(config_path, fs_path);
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
574 const std::string underlying_file = underlying_dir() + fs_path;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
575 FileHandler *handler = open_file_cache_.FindAndPin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
576 if (handler == NULL) {
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
577 int filedes = open(underlying_file.c_str(), O_RDONLY);
680f590 @hzeller o report opening error.
authored
578 if (filedes < 0)
579 return NULL;
b5c1e96 @hzeller o more useful output on status server.
authored
580 ++total_file_openings_;
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
581 handler = CreateFromDescriptor(filedes, config_path,
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
582 fs_path, underlying_file);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
583 handler = open_file_cache_.InsertPinned(cache_key, handler);
b5c1e96 @hzeller o more useful output on status server.
authored
584 } else {
585 ++total_file_reopen_;
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
586 }
587 return handler;
e9c07dd @hzeller o add documentation
authored
588 }
589
a9d3e53 @hzeller o Found a project name: "Folve". Some renamings because of that.
authored
590 int FolveFilesystem::StatByFilename(const char *fs_path, struct stat *st) {
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
591 const std::string cache_key = CacheKey(current_config_subdir_, fs_path);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
592 FileHandler *handler = open_file_cache_.FindAndPin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
593 if (handler == 0)
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
594 return -1;
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
595 ssize_t result = handler->Stat(st);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
596 open_file_cache_.Unpin(cache_key);
15b305a @hzeller o Build infrastructure to re-use file handler objects. Files seem to
authored
597 return result;
9a8ea56 @hzeller o add infrastructure to provide Stat() output of currently open
authored
598 }
599
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
600 void FolveFilesystem::Close(const char *fs_path, const FileHandler *handler) {
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
601 assert(handler != NULL);
602 const std::string cache_key = CacheKey(handler->filter_dir(), fs_path);
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
603 open_file_cache_.Unpin(cache_key);
604 }
605
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
606 static bool IsDirectory(const std::string &path) {
607 if (path.empty()) return false;
608 struct stat st;
609 if (stat(path.c_str(), &st) != 0)
610 return false;
611 return (st.st_mode & S_IFMT) == S_IFDIR;
612 }
613
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
614 bool FolveFilesystem::ListDirectory(const std::string &fs_dir,
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
615 const std::string &suffix,
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
616 std::set<std::string> *files) {
617 const std::string real_dir = underlying_dir() + fs_dir;
618 DIR *dp = opendir(real_dir.c_str());
619 if (dp == NULL) return false;
620 struct dirent *dent;
621 while ((dent = readdir(dp)) != NULL) {
ac40d85 @hzeller o Make sure we match the alphabetically next file with
authored
622 if (!folve::HasSuffix(dent->d_name, suffix))
623 continue;
08bbf28 @hzeller o First shot at gapless. Works pretty well for my test-case.
authored
624 files->insert(fs_dir + dent->d_name);
625 }
626 closedir(dp);
627 return true;
628 }
629
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
630 bool FolveFilesystem::SanitizeConfigSubdir(std::string *subdir_path) const {
631 if (base_config_dir_.length() + 1 + subdir_path->length() > PATH_MAX)
632 return false; // uh, someone wants to buffer overflow us ?
633 const std::string to_verify_path = base_config_dir_ + "/" + *subdir_path;
634 char all_path[PATH_MAX];
635 // This will as well eat symbolic links that break out, though one could
636 // argue that that would be sane. We could think of some light
637 // canonicalization that only removes ./ and ../
638 const char *verified = realpath(to_verify_path.c_str(), all_path);
639 if (verified == NULL) { // bogus directory.
640 return false;
641 }
642 if (strncmp(verified, base_config_dir_.c_str(),
643 base_config_dir_.length()) != 0) {
644 // Attempt to break out with ../-tricks.
645 return false;
646 }
647 if (!IsDirectory(verified))
648 return false;
649
650 // Derive from sanitized dir. So someone can write lowpass/../highpass
651 // or '.' for empty filter. Or ./highpass. And all work.
652 *subdir_path = ((strlen(verified) == base_config_dir_.length())
653 ? "" // chose subdir '.'
654 : verified + base_config_dir_.length() + 1 /*slash*/);
655 return true;
656 }
657
c113afd @hzeller o Adapt curl, wget documentation
authored
658 bool FolveFilesystem::SwitchCurrentConfigDir(const std::string &subdir_in) {
659 std::string subdir = subdir_in;
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
660 if (!subdir.empty() && !SanitizeConfigSubdir(&subdir)) {
661 syslog(LOG_INFO, "Invalid config switch attempt to '%s'",
662 subdir_in.c_str());
663 return false;
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
664 }
665 if (subdir != current_config_subdir_) {
666 current_config_subdir_ = subdir;
667 if (subdir.empty()) {
5830c5a @hzeller o Switch logging to syslog()
authored
668 syslog(LOG_INFO, "Switching to pass-through mode.");
669 } else {
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
670 syslog(LOG_INFO, "Switching config directory to '%s'", subdir.c_str());
5830c5a @hzeller o Switch logging to syslog()
authored
671 }
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
672 return true;
5830c5a @hzeller o Switch logging to syslog()
authored
673 }
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
674 return false;
e9c07dd @hzeller o add documentation
authored
675 }
fd65cee @hzeller o add basic zita filter.
authored
676
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
677 bool FolveFilesystem::CheckInitialized() {
678 if (underlying_dir().empty()) {
679 fprintf(stderr, "Don't know the underlying directory to read from.\n");
680 return false;
681 }
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
682
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
683 if (!IsDirectory(underlying_dir())) {
684 fprintf(stderr, "<underlying-dir>: '%s' not a directory.\n",
685 underlying_dir().c_str());
686 return false;
687 }
688
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
689 if (base_config_dir_.empty() || !IsDirectory(base_config_dir_)) {
690 fprintf(stderr, "<config-dir>: '%s' not a directory.\n",
691 base_config_dir_.c_str());
692 return false;
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
693 }
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
694
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
695 return true;
696 }
697
698 void FolveFilesystem::SetupInitialConfig() {
699 std::set<std::string> available_dirs = ListConfigDirs(true);
700 // Some sanity checks.
701 if (available_dirs.size() == 1) {
702 syslog(LOG_NOTICE, "No filter configuration directories given. "
703 "Any files will be just passed through verbatim.");
704 }
705 if (available_dirs.size() > 1) {
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
706 // By default, lets set the index to the first filter the user provided.
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
707 SwitchCurrentConfigDir(*++available_dirs.begin());
ee675fe @hzeller o Provide a way to select between different filter diretories.
authored
708 }
fd65cee @hzeller o add basic zita filter.
authored
709 }
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
710
711 const std::set<std::string> FolveFilesystem::GetAvailableConfigDirs() const {
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
712 return ListConfigDirs(false);
713 }
714
715 const std::set<std::string> FolveFilesystem::ListConfigDirs(bool warn_invalid)
716 const {
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
717 std::set<std::string> result;
718 result.insert(""); // empty directory: pass-through.
719 DIR *dp = opendir(base_config_dir_.c_str());
720 if (dp == NULL) return result;
721 struct dirent *dent;
722 while ((dent = readdir(dp)) != NULL) {
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
723 std::string subdir = dent->d_name;
724 if (subdir == "." || subdir == "..")
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
725 continue;
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
726 if (!SanitizeConfigSubdir(&subdir)) {
727 if (warn_invalid) {
728 syslog(LOG_INFO, "Note: '%s' ignored in config directory; not a "
729 "directory or pointing outside base directory.", dent->d_name);
730 }
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
731 continue;
a90d922 @hzeller o Separate sanitizing of config subdirectories.
authored
732 }
733 result.insert(subdir);
31096bb @hzeller o remove option -c and add -C: give a base directory for all filters.
authored
734 }
735 closedir(dp);
736 return result;
737 }
738
Something went wrong with that request. Please try again.