From: alex Date: Sun, 5 Dec 2021 00:38:54 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=c92df93eb5e762688b46418fad2afb6c80d84134;p=seeder ... --- diff --git a/inc/torrent.h b/inc/torrent.h index f67ed38..7266833 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -27,6 +27,7 @@ struct torrent { int torrent_add(struct torrent*,struct file*); int torrent_file(struct torrent*); ssize_t torrent_file_info(struct torrent*,uint8_t**); +char *torrent_file_path(unsigned char*,size_t); void torrent_free(struct torrent*); int torrent_init(struct torrent**,const char*,const char*,unsigned long long); char *torrent_magnet(struct torrent*); diff --git a/src/torrent.c b/src/torrent.c index db20559..e971b65 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -2,13 +2,15 @@ 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 char *torrent_file_path(unsigned char*,size_t); 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; @@ -30,8 +32,45 @@ int torrent_add(struct torrent *p, struct file *f) { } static int torrent_add_file(struct torrent *p, struct file *f) { - if(hashmap_insert(p->files,f->root,crypto_hash_sha256_BYTES,f)<0) { return -1; } + 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; } @@ -147,7 +186,7 @@ clean: #define TORRENT_FILE_DIRECTORY "/torrents" #define TORRENT_FILE_EXTENSION ".torrent" -static char *torrent_file_path(unsigned char *infohash, size_t len) { +char *torrent_file_path(unsigned char *infohash, size_t len) { char *path, *p; char hex[(crypto_hash_sha256_BYTES<<1)+1]; @@ -215,6 +254,7 @@ 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; @@ -272,6 +312,37 @@ 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; } @@ -323,6 +394,6 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name, return -1; } -char *torrent_magnet(struct torrent *p) { - return NULL; -} +//char *torrent_magnet(struct torrent *p) { +// return NULL; +//}