Permalink
Browse files

o Only start status server after we're daemonized, otherwise

  the thread is borked.
  • Loading branch information...
hzeller committed Sep 17, 2012
1 parent 49eca7f commit 0330f769884776f363a32628abf7c90e21296d55
Showing with 64 additions and 45 deletions.
  1. +6 −4 folve-filesystem.h
  2. +57 −40 folve-main.cc
  3. +1 −1 status-server.cc
View
@@ -24,6 +24,8 @@
class FolveFilesystem {
public:
+ // version_info and underlying_dir need to stay allocated by the calling
+ // context.
FolveFilesystem(const char *version_info, const char *underlying_dir,
const char *zita_config_dir);
@@ -40,8 +42,8 @@ class FolveFilesystem {
// errno value on failure.
void Close(const char *fs_path);
- const std::string &version() const { return version_info_; }
- const std::string &underlying_dir() const { return underlying_dir_; }
+ const char *version() const { return version_info_; }
+ const char *underlying_dir() const { return underlying_dir_; }
FileHandlerCache *handler_cache() { return &open_file_cache_; }
// Some stats.
@@ -53,8 +55,8 @@ class FolveFilesystem {
FileHandler *CreateFromDescriptor(int filedes, const char *fs_path,
const char *underlying_file);
- const std::string version_info_;
- const std::string underlying_dir_;
+ const char *const version_info_;
+ const char *const underlying_dir_;
const std::string zita_config_dir_;
FileHandlerCache open_file_cache_;
int total_file_openings_;
View
@@ -37,8 +37,11 @@
#endif
// Compilation unit variables to communicate with the fuse callbacks.
-static FolveFilesystem *folve_fs = NULL;
-static const char *underlying_dir = NULL;
+static struct FolveParams {
+ FolveParams() : fs(NULL), status_port(-1) {}
+ FolveFilesystem *fs;
+ int status_port;
+} folve;
static int has_suffix_string (const char *str, const char *suffix) {
if (!str || !suffix)
@@ -60,7 +63,7 @@ static char *concat_path(char *buf, const char *a, const char *b) {
// original file from the source filesystem.
// TODO(hzeller): move the ogg.fuse.flac logic into convolver-filesystem.
static const char *assemble_orig_path(char *buf, const char *path) {
- char *result = concat_path(buf, underlying_dir, path);
+ char *result = concat_path(buf, folve.fs->underlying_dir(), path);
static const char magic_ogg_rewrite[] = ".ogg.fuse.flac";
if (has_suffix_string(result, magic_ogg_rewrite)) {
*(result + strlen(result) - strlen(".fuse.flac")) = '\0';
@@ -70,10 +73,10 @@ static const char *assemble_orig_path(char *buf, const char *path) {
// Essentially lstat(). Just forward to the original filesystem (this
// will by lying: our convolved files are of different size...)
-static int fuseconv_getattr(const char *path, struct stat *stbuf) {
+static int folve_getattr(const char *path, struct stat *stbuf) {
// If this is a currently open filename, we might be able to output a better
// estimate.
- int result = folve_fs->StatByFilename(path, stbuf);
+ int result = folve.fs->StatByFilename(path, stbuf);
if (result == 0) return result;
char path_buf[PATH_MAX];
@@ -85,8 +88,8 @@ static int fuseconv_getattr(const char *path, struct stat *stbuf) {
}
// readdir(). Just forward to original filesystem.
-static int fuseconv_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
- off_t offset, struct fuse_file_info *fi) {
+static int folve_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
+ off_t offset, struct fuse_file_info *fi) {
DIR *dp;
struct dirent *de;
char path_buf[PATH_MAX];
@@ -117,7 +120,7 @@ static int fuseconv_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
}
// readlink(): forward to original filesystem.
-static int fuseconv_readlink(const char *path, char *buf, size_t size) {
+static int folve_readlink(const char *path, char *buf, size_t size) {
char path_buf[PATH_MAX];
const int result = readlink(assemble_orig_path(path_buf, path),
buf, size - 1);
@@ -128,7 +131,7 @@ static int fuseconv_readlink(const char *path, char *buf, size_t size) {
return 0;
}
-static int fuseconv_open(const char *path, struct fuse_file_info *fi) {
+static int folve_open(const char *path, struct fuse_file_info *fi) {
// We want to be allowed to only return part of the requested data in read().
// That way, we can separate reading the ID3-tags from
// decoding of the music stream - that way indexing should be fast.
@@ -140,28 +143,49 @@ static int fuseconv_open(const char *path, struct fuse_file_info *fi) {
// (Yay, someone was thinking while developing that API).
char path_buf[PATH_MAX];
const char *orig_path = assemble_orig_path(path_buf, path);
- FileHandler * handler = folve_fs->CreateHandler(path, orig_path);
+ FileHandler *handler = folve.fs->CreateHandler(path, orig_path);
if (handler == NULL)
return -errno;
fi->fh = (uint64_t) handler;
return 0;
}
-static int fuseconv_read(const char *path, char *buf, size_t size, off_t offset,
- struct fuse_file_info *fi) {
+static int folve_read(const char *path, char *buf, size_t size, off_t offset,
+ struct fuse_file_info *fi) {
return reinterpret_cast<FileHandler *>(fi->fh)->Read(buf, size, offset);
}
-static int fuseconv_release(const char *path, struct fuse_file_info *fi) {
- folve_fs->Close(path);
+static int folve_release(const char *path, struct fuse_file_info *fi) {
+ folve.fs->Close(path);
return 0;
}
-static int fuseconv_fgetattr(const char *path, struct stat *result,
- struct fuse_file_info *fi) {
+static int folve_fgetattr(const char *path, struct stat *result,
+ struct fuse_file_info *fi) {
return reinterpret_cast<FileHandler *>(fi->fh)->Stat(result);
}
+static void *folve_init(struct fuse_conn_info *conn) {
+ // If we're running in the foreground, we like to be seen on stderr as well.
+ const int ident_len = 20;
+ char *ident = (char*) malloc(ident_len); // openlog() keeps reference. Leaks.
+ snprintf(ident, ident_len, "[folve:%d]", getpid());
+ openlog(ident, LOG_CONS|LOG_PERROR, LOG_USER);
+ syslog(LOG_INFO, "started.");
+
+ if (folve.status_port > 0) {
+ // Need to start status server after we're daemonized.
+ StatusServer *status_server = new StatusServer(folve.fs);
+ if (status_server->Start(folve.status_port)) {
+ syslog(LOG_INFO, "HTTP status server on port %d",
+ folve.status_port);
+ } else {
+ syslog(LOG_ERR, "Couldn't start HTTP server on port %d\n",
+ folve.status_port);
+ }
+ }
+ return NULL;
+}
static int usage(const char *prg) {
fprintf(stderr, "usage: %s <config-dir> <original-dir> <mount-point>\n", prg);
@@ -181,13 +205,10 @@ int main(int argc, char *argv[]) {
return usage(progname);
}
- // If we're running in the foreground, we like to be seen on stderr as well.
- openlog("[folve]", LOG_CONS|LOG_PERROR, LOG_USER);
- syslog(LOG_INFO, "started.");
-
- // First, let's extract our configuration.
+ // First, let's extract our own configuration, redact them from argv[] and
+ // then pass everything on to fo
const char *config_dir = argv[1];
- underlying_dir = argv[2];
+ const char *underlying_dir = argv[2];
argc -=2;
argv += 2;
if (!IsDirectory(config_dir)) {
@@ -198,33 +219,29 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "%s <underlying-dir>: not a directory.\n", underlying_dir);
return usage(progname);
}
- folve_fs = new FolveFilesystem(FOLVE_VERSION, underlying_dir, config_dir);
+ folve.fs = new FolveFilesystem(FOLVE_VERSION, underlying_dir, config_dir);
// TODO(hzeller): make this configurable
- int port = 17322;
- if (port > 0) {
- StatusServer *statusz = new StatusServer(folve_fs);
- if (!statusz->Start(port)) {
- syslog(LOG_ERR, "Couldn't start HTTP server on port %d\n", port);
- }
- }
+ folve.status_port = 17322;
+
+ struct fuse_operations folve_operations;
+ memset(&folve_operations, 0, sizeof(folve_operations));
- struct fuse_operations fuseconv_operations;
- memset(&fuseconv_operations, 0, sizeof(fuseconv_operations));
+ folve_operations.init = folve_init;
// Basic operations to make navigation work.
- fuseconv_operations.readdir = fuseconv_readdir;
- fuseconv_operations.readlink = fuseconv_readlink;
+ folve_operations.readdir = folve_readdir;
+ folve_operations.readlink = folve_readlink;
// open() and close() file.
- fuseconv_operations.open = fuseconv_open;
- fuseconv_operations.release = fuseconv_release;
+ folve_operations.open = folve_open;
+ folve_operations.release = folve_release;
// Actual workhorse: reading a file and returning predicted file-size
- fuseconv_operations.read = fuseconv_read;
- fuseconv_operations.fgetattr = fuseconv_fgetattr;
- fuseconv_operations.getattr = fuseconv_getattr;
+ folve_operations.read = folve_read;
+ folve_operations.fgetattr = folve_fgetattr;
+ folve_operations.getattr = folve_getattr;
// Lazy: let the rest handle by fuse provided main.
- return fuse_main(argc, argv, &fuseconv_operations, NULL);
+ return fuse_main(argc, argv, &folve_operations, NULL);
}
View
@@ -169,7 +169,7 @@ const std::string &StatusServer::CreatePage() {
"<a href='https://github.com/hzeller/folve#readme'>Folve</a>"
" %s</center>\n"
"Convolving audio files from <code>%s</code><br/>\n",
- filesystem_->version().c_str(), filesystem_->underlying_dir().c_str());
+ filesystem_->version(), filesystem_->underlying_dir());
std::vector<HandlerStats> stat_list;
filesystem_->handler_cache()->GetStats(&stat_list);

0 comments on commit 0330f76

Please sign in to comment.