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
--- /dev/null
+#ifndef __ADD_H_
+#define __ADD_H_
+
+#include<log.h>
+
+void *add(void*);
+
+#endif
#include<log.h>
#include<opt.h>
+#define DEFAULT_MESSAGE_SETTING_DEFAULTS "setting default values\n"
+
int defaults();
#endif
#define __OPT_H_
#include<string.h>
-#include<sys/stat.h>
#include<log.h>
+#include<torrent.h>
+#include<util.h>
#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"
#ifndef __SETUP_H_
#define __SETUP_H_
+#include<add.h>
#include<log.h>
int setup();
+int setup_adding();
int setup_logging();
#endif
--- /dev/null
+#ifndef __TORRENT_H_
+#define __TORRENT_H_
+
+#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+
+struct torrent {
+ char *root;
+};
+
+void torrent_free(struct torrent*);
+int torrent_init(struct torrent**,char*);
+
+#endif
--- /dev/null
+#ifndef __UTIL_H_
+#define __UTIL_H_
+
+#include<stdio.h>
+#include<sys/stat.h>
+
+int is_directory(char*);
+int is_file(char*);
+
+#endif
--- /dev/null
+#include<add.h>
+
+void *add(void *p) {
+ return NULL;
+}
#include<default.h>
int defaults() {
+ // logging needs to be setup first
logging_thread = pthread_self();
#ifndef NDEBUG
#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;
}
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);
#include<opt.h>
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;
}
#include<opt.h>
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;
}
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;
}
--- /dev/null
+#include<torrent.h>
+
+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;
+}
--- /dev/null
+#include<util.h>
+
+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;
+}
--- /dev/null
+#include<util.h>
+
+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;
+}