From: alex Date: Sun, 5 Dec 2021 20:16:16 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=4807dcce4634ab1e854aab6e842aec1ea8762688;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index aef1255..c380572 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,7 +40,11 @@ seederd_SOURCES = \ src/session.c \ src/setup.c \ src/shutdown.c \ - src/torrent.c \ + src/torrent/add.c \ + src/torrent/file.c \ + src/torrent/free.c \ + src/torrent/init.c \ + src/torrent/magnet.c \ src/tree.c \ src/usage.c \ src/watch.c diff --git a/src/torrent/add.c b/src/torrent/add.c new file mode 100644 index 0000000..e14bace --- /dev/null +++ b/src/torrent/add.c @@ -0,0 +1,100 @@ +#include + +static int torrent_add_file(struct torrent*,struct file*); +static int torrent_add_file_by_path(struct hash_map*, struct file*); +static int torrent_add_file_by_root(struct hash_map*,struct file*); +static int torrent_files_resize(struct torrent *torrent_p, size_t new_size); + +int torrent_add(struct torrent *p, struct file *f) { + const char *path; + + if(NULL==p) { return -1; } + if(NULL==f) { return -1; } + + path = strstr(f->path,p->root); + if((path!=NULL)&&(path==f->path)) { + path += strlen(p->root); + } else { + path = f->path; + } + + if(tree_add(p->tree,path,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) { + int i; + + while( + ((i = torrent_add_file_by_root(p->files,f))<=0) || + ((i = torrent_add_file_by_path(p->files,f))<=0) + ) { + if(i<0) { return -1; } + + if(torrent_files_resize(p,p->files->size<<1)<0) { return -1; } + } + + return 1; +} + +static int torrent_add_file_by_path(struct hash_map *files, struct file *f) { + struct file *p; + int i; + + if((i = hashmap_insert(files,f->path,strlen(f->path),f))<0) { + if(i<0) { return -1; } + + p = hashmap_find(files,f->path,strlen(f->path)); + if(p!=NULL) { return 0; } + } + + return 1; +} + +static int torrent_add_file_by_root(struct hash_map *files, struct file *f) { + struct file *p; + int i; + + if((i = hashmap_insert(files,f->root,crypto_hash_sha256_BYTES,f))<0) { + if(i<0) { return -1; } + + p = hashmap_find(files,f->root,crypto_hash_sha256_BYTES); + if(p!=NULL) { return 0; } + } + + return 1; +} + +static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) { + struct file *p; + struct hash_map *new, *old; + int ret; + + if(hashmap_init(&new,new_size)<0) { return -1; } + + for(size_t i=0;ifiles->size;i++) { + p = torrent_p->files->map[i]; + if(p!=NULL) { + if( + ((ret = torrent_add_file_by_root(new,p))<=0) || + ((ret = torrent_add_file_by_path(new,p))<=0) + ) { + if(ret<0) { return -1; } + hashmap_clear(new); + hashmap_free(new); + return torrent_files_resize(torrent_p,new_size<<1); + } + } + } + + hashmap_clear(torrent_p->files); + + old = torrent_p->files; + torrent_p->files = new; + + hashmap_free(old); + + return 1; +} diff --git a/src/torrent.c b/src/torrent/file.c similarity index 64% rename from src/torrent.c rename to src/torrent/file.c index e971b65..9b48dbf 100644 --- a/src/torrent.c +++ b/src/torrent/file.c @@ -1,79 +1,13 @@ #include static int piece_layer_sort(const void*,const void*); -static int torrent_add_file(struct torrent*,struct file*); -static int torrent_add_file_by_path(struct hash_map*, struct file*); -static int torrent_add_file_by_root(struct hash_map*,struct file*); static int torrent_file_announce(FILE*,struct torrent*); static int torrent_file_piece_layers(FILE*,struct torrent*); static int torrent_file_write_end(FILE*); static int torrent_file_write_info(FILE*,uint8_t*,size_t); static int torrent_file_write_piece_layers(FILE*,struct torrent*); static int torrent_file_write_start(FILE*); -static int torrent_files_resize(struct torrent *torrent_p, size_t new_size); -int torrent_add(struct torrent *p, struct file *f) { - const char *path; - - if(NULL==p) { return -1; } - if(NULL==f) { return -1; } - - path = strstr(f->path,p->root); - if((path!=NULL)&&(path==f->path)) { - path += strlen(p->root); - } else { - path = f->path; - } - - if(tree_add(p->tree,path,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) { - int i; - - while( - ((i = torrent_add_file_by_root(p->files,f))<=0) || - ((i = torrent_add_file_by_path(p->files,f))<=0) - ) { - if(i<0) { return -1; } - - if(torrent_files_resize(p,p->files->size<<1)<0) { return -1; } - } - - return 1; -} - -static int torrent_add_file_by_path(struct hash_map *files, struct file *f) { - struct file *p; - int i; - - if((i = hashmap_insert(files,f->path,strlen(f->path),f))<0) { - if(i<0) { return -1; } - - p = hashmap_find(files,f->path,strlen(f->path)); - if(p!=NULL) { return 0; } - } - - return 1; -} - -static int torrent_add_file_by_root(struct hash_map *files, struct file *f) { - struct file *p; - int i; - - if((i = hashmap_insert(files,f->root,crypto_hash_sha256_BYTES,f))<0) { - if(i<0) { return -1; } - - p = hashmap_find(files,f->root,crypto_hash_sha256_BYTES); - if(p!=NULL) { return 0; } - } - - return 1; -} - int torrent_file(struct torrent *torrent_p) { uint8_t *info; FILE *fp; @@ -254,7 +188,6 @@ static int torrent_file_piece_layers(FILE *fp, struct torrent *p) { // return -1; } - static int piece_layer_sort(const void *p1, const void *p2) { struct block *a, *b; a = (struct block*)p1; @@ -311,89 +244,3 @@ static int torrent_file_write_start(FILE *fp) { return 1; } - -static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) { - struct file *p; - struct hash_map *new, *old; - int ret; - - if(hashmap_init(&new,new_size)<0) { return -1; } - - for(size_t i=0;ifiles->size;i++) { - p = torrent_p->files->map[i]; - if(p!=NULL) { - if( - ((ret = torrent_add_file_by_root(new,p))<=0) || - ((ret = torrent_add_file_by_path(new,p))<=0) - ) { - if(ret<0) { return -1; } - hashmap_clear(new); - hashmap_free(new); - return torrent_files_resize(torrent_p,new_size<<1); - } - } - } - - hashmap_clear(torrent_p->files); - - old = torrent_p->files; - torrent_p->files = new; - - hashmap_free(old); - - return 1; -} - -void torrent_free(struct torrent *p) { - if(NULL==p) { return; } - - if(p->root!=NULL) { free(p->root); } - if(p->name!=NULL) { free(p->name); } - - if(p->files!=NULL) { - // clear first, tree_free will also free files - hashmap_clear(p->files); - hashmap_free(p->files); - } - - if(p->tree!=NULL) { tree_free(p->tree); } - - free(p); -} - -#define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8 - -int torrent_init(struct torrent **torrent_p, const char *root, const char *name, unsigned long long piece_length) { - if(NULL==torrent_p) { return -1; } - if(NULL==root) { return -1; } - if(NULL==name) { return -1; } - - if((piece_length<16384)||(!(piece_length&&!(piece_length&(piece_length-1))))) { return -1; } - - *(torrent_p) = malloc(sizeof(struct torrent)); - if(NULL==(*torrent_p)) { - perror("malloc"); - return -1; - } - - (*torrent_p)->root = strdup(root); - if(NULL==(*torrent_p)->root) { goto clean; } - - (*torrent_p)->name = strdup(name); - if(NULL==(*torrent_p)->name) { goto clean; } - - (*torrent_p)->piece_length = piece_length; - - if(tree_init(&((*torrent_p)->tree))<0) { goto clean; } - - if(hashmap_init(&((*torrent_p)->files),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; } - - return 1; - clean: - torrent_free(*torrent_p); - return -1; -} - -//char *torrent_magnet(struct torrent *p) { -// return NULL; -//} diff --git a/src/torrent/free.c b/src/torrent/free.c new file mode 100644 index 0000000..5952053 --- /dev/null +++ b/src/torrent/free.c @@ -0,0 +1,18 @@ +#include + +void torrent_free(struct torrent *p) { + if(NULL==p) { return; } + + if(p->root!=NULL) { free(p->root); } + if(p->name!=NULL) { free(p->name); } + + if(p->files!=NULL) { + // clear first, tree_free will also free files + hashmap_clear(p->files); + hashmap_free(p->files); + } + + if(p->tree!=NULL) { tree_free(p->tree); } + + free(p); +} diff --git a/src/torrent/init.c b/src/torrent/init.c new file mode 100644 index 0000000..0f90a33 --- /dev/null +++ b/src/torrent/init.c @@ -0,0 +1,34 @@ +#include + +#define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8 + +int torrent_init(struct torrent **torrent_p, const char *root, const char *name, unsigned long long piece_length) { + if(NULL==torrent_p) { return -1; } + if(NULL==root) { return -1; } + if(NULL==name) { return -1; } + + if((piece_length<16384)||(!(piece_length&&!(piece_length&(piece_length-1))))) { return -1; } + + *(torrent_p) = malloc(sizeof(struct torrent)); + if(NULL==(*torrent_p)) { + perror("malloc"); + return -1; + } + + (*torrent_p)->root = strdup(root); + if(NULL==(*torrent_p)->root) { goto clean; } + + (*torrent_p)->name = strdup(name); + if(NULL==(*torrent_p)->name) { goto clean; } + + (*torrent_p)->piece_length = piece_length; + + if(tree_init(&((*torrent_p)->tree))<0) { goto clean; } + + if(hashmap_init(&((*torrent_p)->files),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; } + + return 1; + clean: + torrent_free(*torrent_p); + return -1; +} diff --git a/src/torrent/magnet.c b/src/torrent/magnet.c new file mode 100644 index 0000000..16a006d --- /dev/null +++ b/src/torrent/magnet.c @@ -0,0 +1,5 @@ +#include + +char *torrent_magnet(struct torrent *p) { + return NULL; +}