From: alex Date: Sun, 5 Sep 2021 23:14:16 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=5db323a4cc4622785272075ca0bc1bf7666804d1;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index 3767189..2fb2ad5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -11,21 +11,29 @@ endif bin_PROGRAMS = seederd seederd_SOURCES = \ + src/add.c \ src/default.c \ src/log.c \ src/main.c \ src/opt/config.c \ src/opt/env.c \ src/opt/loglevel.c \ + src/opt/piecel.c \ src/opt/set.c \ src/opt/watch.c \ src/setup.c \ - src/usage.c + src/torrent.c \ + src/usage.c \ + src/util/dir.c \ + src/util/file.c seederd_SOURCES += \ + inc/add.h \ inc/default.h \ inc/log.h \ inc/main.h \ inc/opt.h \ inc/setup.h \ - inc/usage.h + inc/torrent.h \ + inc/usage.h \ + inc/util.h diff --git a/inc/add.h b/inc/add.h new file mode 100644 index 0000000..3d8e699 --- /dev/null +++ b/inc/add.h @@ -0,0 +1,8 @@ +#ifndef __ADD_H_ +#define __ADD_H_ + +#include + +void *add(void*); + +#endif diff --git a/inc/default.h b/inc/default.h index 5d26697..4b43a4c 100644 --- a/inc/default.h +++ b/inc/default.h @@ -4,6 +4,8 @@ #include #include +#define DEFAULT_MESSAGE_SETTING_DEFAULTS "setting default values\n" + int defaults(); #endif diff --git a/inc/opt.h b/inc/opt.h index 1188d2f..b3506d2 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -2,12 +2,16 @@ #define __OPT_H_ #include -#include #include +#include +#include #define OPT_MESSAGE_CONFIG_FILE_FORMAT_ERROR "invalid format in config file %s:%d\n" +#define OPT_MESSAGE_ENV_LOADING "loading settings from environment\n" #define OPT_MESSAGE_LOADING_CONFIG_FILE "loading config file %s\n" +#define OPT_MESSAGE_PIECE_LENGTH_INVALID "invalid piece length of %llu\npiece length must be >16384 and a power of 2\n" +#define OPT_MESSAGE_PIECE_LENGTH_SET "piece length set to %llu\n" #define OPT_MESSAGE_UNABLE_OPEN_FILE "unable to open file %s\n" #define OPT_MESSAGE_UNKNOWN_OPTION "unknown option %s\n" #define OPT_MESSAGE_WATCH_ADD_SUCCESS "watching %s\n" diff --git a/inc/setup.h b/inc/setup.h index bcf246a..8d5d526 100644 --- a/inc/setup.h +++ b/inc/setup.h @@ -1,9 +1,11 @@ #ifndef __SETUP_H_ #define __SETUP_H_ +#include #include int setup(); +int setup_adding(); int setup_logging(); #endif diff --git a/inc/torrent.h b/inc/torrent.h new file mode 100644 index 0000000..21ab327 --- /dev/null +++ b/inc/torrent.h @@ -0,0 +1,15 @@ +#ifndef __TORRENT_H_ +#define __TORRENT_H_ + +#include +#include +#include + +struct torrent { + char *root; +}; + +void torrent_free(struct torrent*); +int torrent_init(struct torrent**,char*); + +#endif diff --git a/inc/util.h b/inc/util.h new file mode 100644 index 0000000..4d5febb --- /dev/null +++ b/inc/util.h @@ -0,0 +1,10 @@ +#ifndef __UTIL_H_ +#define __UTIL_H_ + +#include +#include + +int is_directory(char*); +int is_file(char*); + +#endif diff --git a/src/add.c b/src/add.c new file mode 100644 index 0000000..84984b1 --- /dev/null +++ b/src/add.c @@ -0,0 +1,5 @@ +#include + +void *add(void *p) { + return NULL; +} diff --git a/src/default.c b/src/default.c index 21896cb..cbc4a2d 100644 --- a/src/default.c +++ b/src/default.c @@ -1,6 +1,7 @@ #include int defaults() { + // logging needs to be setup first logging_thread = pthread_self(); #ifndef NDEBUG @@ -8,6 +9,11 @@ int defaults() { #else opt_set_log_level(LOG_LEVEL_DEFAULT); #endif + // end logging section + + log_info(DEFAULT_MESSAGE_SETTING_DEFAULTS); + + opt_set_piece_length("16384"); return 1; } diff --git a/src/opt/env.c b/src/opt/env.c index 0537777..4ec76db 100644 --- a/src/opt/env.c +++ b/src/opt/env.c @@ -12,6 +12,8 @@ int opt_load_from_env() { char *p; + log_info(OPT_MESSAGE_ENV_LOADING); + CHECK_ENV("CONFIG",opt_load_config_file); CHECK_ENV("PIECE_LENGTH",opt_set_piece_length); diff --git a/src/opt/piecel.c b/src/opt/piecel.c index 3636b57..1b5ba7d 100644 --- a/src/opt/piecel.c +++ b/src/opt/piecel.c @@ -1,5 +1,16 @@ #include int opt_set_piece_length(char *length) { + char *end; + unsigned long long i; + + i = strtoull(length,&end,10); + + if((i<16384)||(!(i&&!(i&(i-1))))) { + log_err(OPT_MESSAGE_PIECE_LENGTH_INVALID,i); + return -1; + } + + log_info(OPT_MESSAGE_PIECE_LENGTH_SET,i); return -1; } diff --git a/src/opt/watch.c b/src/opt/watch.c index 1734b05..320f937 100644 --- a/src/opt/watch.c +++ b/src/opt/watch.c @@ -1,21 +1,17 @@ #include int opt_add_watch(char *directory) { - struct stat st; - - if(stat(directory,&st)!=0) { - perror("stat"); + struct torrent *p; + if(!is_directory(directory)) { log_err(OPT_MESSAGE_WATCH_INVALID_DIRECTORY,directory); return -1; } - if(!S_ISDIR(st.st_mode)) { - log_err(OPT_MESSAGE_WATCH_INVALID_DIRECTORY,directory); - return -1; - } + if(torrent_init(&p,directory)<0) { return -1; } + torrent_free(p); log_msg(OPT_MESSAGE_WATCH_ADD_SUCCESS,directory); log_err("not implemented\n"); - return 1; + return -1; } diff --git a/src/setup.c b/src/setup.c index 44851a8..f7621e1 100644 --- a/src/setup.c +++ b/src/setup.c @@ -3,6 +3,18 @@ int setup() { if(setup_logging()<0) { return -1; } + if(setup_adding()<0) { return -1; } + return 1; +} + +pthread_t adding_thread; + +int setup_adding() { + if(pthread_create(&adding_thread,NULL,&add,NULL)!=0) { + perror("pthread_create"); + return -1; + } + return 1; } diff --git a/src/torrent.c b/src/torrent.c new file mode 100644 index 0000000..7f78130 --- /dev/null +++ b/src/torrent.c @@ -0,0 +1,24 @@ +#include + +void torrent_free(struct torrent *p) { + free(p->root); + free(p); +} + +int torrent_init(struct torrent **p, char *root) { + *p = malloc(sizeof(struct torrent)); + if(NULL==(*p)) { + perror("malloc"); + return -1; + } + + (*p)->root = malloc(strlen(root)+1); + if(NULL==(*p)->root) { + perror("malloc"); + free(*p); + return -1; + } + strcpy((*p)->root,root); + + return 1; +} diff --git a/src/util/dir.c b/src/util/dir.c new file mode 100644 index 0000000..07d324b --- /dev/null +++ b/src/util/dir.c @@ -0,0 +1,15 @@ +#include + +int is_directory(char *path) { + struct stat st; + + if(stat(path,&st)!=0) { + perror("stat"); + return -1; + } + + + if(!S_ISDIR(st.st_mode)) { return -1; } + + return 1; +} diff --git a/src/util/file.c b/src/util/file.c new file mode 100644 index 0000000..4fcbe59 --- /dev/null +++ b/src/util/file.c @@ -0,0 +1,14 @@ +#include + +int is_file(char *path) { + struct stat st; + + if(stat(path,&st)!=0) { + perror("stat"); + return -1; + } + + if(!S_ISREG(st.st_mode)) { return -1; } + + return 1; +}