From d5578f60737452b224cbbd36444b5968441fdf77 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Mon, 4 May 2009 14:42:29 +0200 Subject: [PATCH] fix searchpath handling --- pidfile.c | 2 ++ torrent-stats.c | 4 ++++ torrentfile.c | 28 ++++++++++++++++++---------- torrentfile.h | 2 ++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/pidfile.c b/pidfile.c index 9de2d3d..f5dd0be 100644 --- a/pidfile.c +++ b/pidfile.c @@ -44,6 +44,8 @@ pid_t pidfile_check(const char *filename, int remove_stale) int len = read(fd, buf, sizeof(buf) -1); buf[len] = '\0'; + close(fd); + char *tmp; pid_t pid = strtol(buf, &tmp, 10); if (len == 0 || tmp == buf) diff --git a/torrent-stats.c b/torrent-stats.c index c018b8d..0cb5de0 100644 --- a/torrent-stats.c +++ b/torrent-stats.c @@ -30,6 +30,7 @@ #include "pidfile.h" #include "process.h" #include "signals.h" +#include "torrentfile.h" #define DEFAULT_CONFIG "torrent-stats.conf" #define DEFAULT_LOGFILE "torrent-stats.log" @@ -114,6 +115,9 @@ int main(int argc, char *argv[]) signal_set_callback(SIGHUP, event_loop_break); while (1) { + if (torrentfile_init() < 0) + break; + if (ctcs_init() < 0) break; diff --git a/torrentfile.c b/torrentfile.c index 49f5251..3875a92 100644 --- a/torrentfile.c +++ b/torrentfile.c @@ -30,6 +30,10 @@ #include "process.h" #include "torrentfile.h" +static const char *searchpath; +static const char *ctorrent_bin; +static const char *statserv; + LIST_HEAD(torrent_list); struct torrent_file * find_create_torrent(const char *fullpath) @@ -112,14 +116,8 @@ static int null_read(int fd, void *privdata) int seed_torrent(struct torrent_file *torrent) { - const char *path = config_get_string("global", "search-path", NULL); - if (path == NULL) { - log_print(LOG_WARN, "requesting torrentfile, but no search path given"); - return -1; - } - char buf[256]; - int len = snprintf(buf, sizeof(buf), "%s/%s", path, torrent->name); + int len = snprintf(buf, sizeof(buf), "%s/%s", searchpath, torrent->name); if (len < 0 || len >= sizeof(buf)) { log_print(LOG_WARN, "filename > max"); return -1; @@ -131,11 +129,9 @@ int seed_torrent(struct torrent_file *torrent) return -1; } - const char *ctorrent_bin = config_get_string("global", "ctorrent-bin", "/usr/bin/ctorrent"); - const char *statserv = config_get_string("global", "statserv", "127.0.0.1:2780"); char *const args[] = { (char *)ctorrent_bin, "-S", (char *)statserv, "-f", buf, NULL }; - torrent->child = childproc_alloc(args, path); + torrent->child = childproc_alloc(args, searchpath); if (childproc_fork(torrent->child, child_exit, torrent) < 0) { log_print(LOG_ERROR, "spawn_child(%s)", args[0]); childproc_free(torrent->child); @@ -151,3 +147,15 @@ int seed_torrent(struct torrent_file *torrent) return 0; } + +int torrentfile_init(void) +{ + searchpath = config_get_string("global", "search-path", NULL); + if (searchpath == NULL) + log_print(LOG_WARN, "disable torrent autostart"); + + ctorrent_bin = config_get_string("global", "ctorrent-bin", "/usr/bin/ctorrent"); + statserv = config_get_string("global", "statserv", "127.0.0.1:2780"); + + return 0; +} diff --git a/torrentfile.h b/torrentfile.h index c7cddec..6fecc2b 100644 --- a/torrentfile.h +++ b/torrentfile.h @@ -28,4 +28,6 @@ int destroy_torrent(struct torrent_file *torrent); int seed_torrent(struct torrent_file *torrent); +int torrentfile_init(void); + #endif /* _TORRENTFILE_H_ */