src/opt/piecel.c \
src/opt/set.c \
src/opt/watch.c \
+ src/opt/worker.c \
src/setup.c \
src/torrent.c \
src/usage.c \
#ifndef __DEFAULT_H_
#define __DEFAULT_H_
+#include<unistd.h>
+
#include<log.h>
#include<opt.h>
#include<torrent.h>
#include<util.h>
+struct options {
+ unsigned int worker_threads;
+ unsigned long long piece_length;
+};
+
+extern struct options global_options;
+
#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_ENV_LOADING_END "finished 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_UNKNOWN_OPTION "unknown option %s\n"
#define OPT_MESSAGE_WATCH_ADD_SUCCESS "watching %s\n"
#define OPT_MESSAGE_WATCH_INVALID_DIRECTORY "watching directory %s failed\n"
+#define OPT_MESSAGE_WORKER_THREADS_INVALID "invalid value to option worker_threads: %u\n"
+#define OPT_MESSAGE_WORKER_THREADS_SET "number of worker threads set to %u\n"
int opt_add_watch(char*);
int opt_load_config_file(char*);
int opt_set(char*,char*);
void opt_set_log_level(enum log_level);
int opt_set_piece_length(char*);
+int opt_set_worker_threads(char*);
#endif
char *root;
};
+extern struct torrent **torrents;
+
void torrent_free(struct torrent*);
int torrent_init(struct torrent**,char*);
#ifndef __UTIL_H_
#define __UTIL_H_
+#include<dirent.h>
#include<stdio.h>
#include<sys/stat.h>
+int file_filter(char*);
+void find(char*);
int is_directory(char*);
int is_file(char*);
#include<default.h>
+struct options global_options;
+
int defaults() {
// logging needs to be setup first
logging_thread = pthread_self();
log_info(DEFAULT_MESSAGE_SETTING_DEFAULTS);
- opt_set_piece_length("16384");
+ if(opt_set_piece_length("16384")<0) { return -1; }
+
+ {
+ char buf[10];
+ snprintf(buf,10,"%lu",sysconf(_SC_NPROCESSORS_ONLN));
+ if(opt_set_worker_threads(buf)<0) { return -1; }
+ }
return 1;
}
{"quiet", no_argument, &verbose_flag, LOG_LEVEL_SILENT},
{"verbose", no_argument, &verbose_flag, LOG_LEVEL_VERBOSE},
{"watch", required_argument, 0, 'w'},
+ {"worker-threads", required_argument, 0, 1},
{0,0,0,0}
};
if((c = getopt_long(argc,argv,"c:dhqvw:",long_options,&option_index))==-1) { break; }
switch(c) {
+ case 1:
+ if(opt_set_worker_threads(optarg)<0) { return EXIT_FAILURE; }
+ break;
case 'c':
if(opt_load_config_file(optarg)<0) { return EXIT_FAILURE; }
break;
CHECK_ENV("CONFIG",opt_load_config_file);
CHECK_ENV("PIECE_LENGTH",opt_set_piece_length);
+ CHECK_ENV("WORKER_THREADS",opt_set_worker_threads);
+
+ log_info(OPT_MESSAGE_ENV_LOADING_END);
return 1;
}
return -1;
}
+ global_options.piece_length = i;
log_info(OPT_MESSAGE_PIECE_LENGTH_SET,i);
- return -1;
+
+ return 1;
}
struct option_lookup_table_entry option_lookup_table[] = {
{"piece_length",&opt_set_piece_length},
{"watch",&opt_add_watch},
+ {"worker_threads",&opt_set_worker_threads},
{NULL,NULL}
};
--- /dev/null
+#include<opt.h>
+
+int opt_set_worker_threads(char *str) {
+ char *end;
+
+ global_options.worker_threads = strtoul(str,&end,10);
+ if(0==global_options.worker_threads) {
+ log_err(OPT_MESSAGE_WORKER_THREADS_INVALID,str);
+ return -1;
+ }
+
+ log_info(OPT_MESSAGE_WORKER_THREADS_SET,global_options.worker_threads);
+ return 1;
+}
#include<torrent.h>
+struct torrent **torrents;
+
void torrent_free(struct torrent *p) {
free(p->root);
free(p);
log_err("\t--quiet, -q\n");
log_err("\t--verbose, -v\n");
log_err("\t--watch=<directory>, -w <directory>\n");
+ log_err("\t--worker-threads=<number of threads>\n");
}
--- /dev/null
+#include<util.h>
+
+int file_filter(char *path) {
+ /* scandir requires non-zero return values to include */
+ char *p;
+ if(NULL==path) { return false; }
+
+ p = strrchr(path, '/');
+
+ if(NULL==p) {
+ p = path;
+ } else {
+ p++;
+ }
+
+ if(p[0]=='.') { return 0; }
+
+ return 1;
+}
+
+void find(char *path) {
+ if(is_file(path)<0) {
+
+}