From: alex Date: Thu, 14 Oct 2021 01:31:32 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=632d7ce5e5210d3911b57e539af7cea6d84c6811;p=seeder ... --- diff --git a/inc/hashmap.h b/inc/hashmap.h index 8888772..d947918 100644 --- a/inc/hashmap.h +++ b/inc/hashmap.h @@ -9,6 +9,7 @@ struct hash_map { unsigned char key[crypto_shorthash_KEYBYTES]; }; +void hashmap_clear(struct hash_map*); void *hashmap_find(struct hash_map*,void*,size_t); void hashmap_free(struct hash_map*); int hashmap_init(struct hash_map**,size_t); diff --git a/src/add.c b/src/add.c index 4567130..d352340 100644 --- a/src/add.c +++ b/src/add.c @@ -13,6 +13,7 @@ pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER; //} static struct hash_map *add_queue; +static struct torrent *current_torrent; static int add_find_all(); static int add_queue_resize(); @@ -33,18 +34,19 @@ int add() { } hashmap_free(add_queue); - return -1; + return 1; } static int add_find_all(struct torrent *p) { log_info(ADD_MESSAGE_ADDING_TORRENT,p->root,p->name); + current_torrent = p; if(ftw(p->root,&ftw_helper,64)<0) { perror("ftw"); return -1; } - return -1; + return 1; } static int add_queue_resize(size_t new_size) { @@ -59,15 +61,14 @@ static int add_queue_resize(size_t new_size) { if(p!=NULL) { if((ret = hashmap_insert(new,p->path,strlen(p->path),p))<=0) { if(ret<0) { return -1; } + hashmap_clear(new); hashmap_free(new); return add_queue_resize(new_size<<1); } } } - for(size_t i=0;isize;i++) { - add_queue->map[i] = NULL; - } + hashmap_clear(add_queue); old = add_queue; add_queue = new; @@ -77,11 +78,9 @@ static int add_queue_resize(size_t new_size) { return 1; } -static int add_to_queue(const char *path) { - struct file *to_add; +static int add_to_queue(struct file *to_add) { int ret; - if(file_init(&to_add,path)<0) { return -1; } while((ret = hashmap_insert(add_queue,path,strlen(path),to_add))<=0) { if(ret<0) { return -1; } if(add_queue_resize(add_queue->size<<1)<0) { return -1; } @@ -91,6 +90,8 @@ static int add_to_queue(const char *path) { } static int ftw_helper(const char *path, const struct stat *st, int typeflag) { + struct file *to_add; + if(typeflag!=FTW_F) { return 0; } switch(global_options.file_filter) { case FILE_FILTER_IGNORE_DOTFILES: @@ -101,7 +102,11 @@ static int ftw_helper(const char *path, const struct stat *st, int typeflag) { break; } - if(add_to_queue(path)<0) { return -1; } + if(file_init(&to_add,path)<0) { return -1; } + + if(add_to_queue(to_add)<0) { return -1; } + + if(torrent_add(current_torrent,to_add)<0) { return -1; } return 0; } diff --git a/src/hashmap.c b/src/hashmap.c index 541c116..0825f0e 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -1,5 +1,11 @@ #include +void hashmap_clear(struct hash_map *p) { + for(size_t i=0;isize;i++) { + p->map[i] = NULL; + } +} + void *hashmap_find(struct hash_map *p, void *key, size_t key_size) { unsigned char hash[crypto_shorthash_BYTES]; size_t index; diff --git a/src/torrent.c b/src/torrent.c index da3c64a..72e3927 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -2,9 +2,26 @@ struct torrent **torrents; +static int torrent_add_file(struct torrent *p, struct file *f); + +int torrent_add(struct torrent *p, struct file *f) { + if(NULL==p) { return -1; } + if(NULL==f) { return -1; } + + if(tree_add(p->file_tree,f)<0) { return -1; } + if(torrent_add_file(p,f)<0) { return -1; } + + return 1; +} + +static int torrent_add_file(struct torrent *p, struct file *f) { + return -1; +} + void torrent_free(struct torrent *p) { if(p->root!=NULL) { free(p->root); } if(p->name!=NULL) { free(p->name); } + if(p->files!=NULL) { hashmap_free(p->files); } if(p->file_tree!=NULL) { tree_free(p->file_tree); } free(p); } @@ -19,6 +36,7 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name) (*torrent_p)->root = NULL; (*torrent_p)->name = NULL; (*torrent_p)->file_tree = NULL; + (*torrent_p)->files = NULL; (*torrent_p)->root = strdup(root); if(NULL==(*torrent_p)->root) { diff --git a/src/tree.c b/src/tree.c index ac9f086..3943382 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1,5 +1,9 @@ #include +int tree_add(struct tree *p, struct file *p) { + return -1; +} + void tree_free(struct tree *p) { while(p->file_count>0) { file_free(p->files[p->file_count-1]);