From: alex Date: Sun, 17 Oct 2021 07:01:27 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=d8f2e63860524ca26caa106800e7a3aea1c941e3;p=seeder ... --- diff --git a/src/torrent.c b/src/torrent.c index 72e3927..89ceb2b 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -15,7 +15,9 @@ int torrent_add(struct torrent *p, struct file *f) { } static int torrent_add_file(struct torrent *p, struct file *f) { - return -1; + if(hashmap_insert(p->files,f->path,strlen(f->path),f)<0) { return -1; } + + return 1; } void torrent_free(struct torrent *p) { @@ -26,6 +28,8 @@ void torrent_free(struct torrent *p) { free(p); } +#define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8 + int torrent_init(struct torrent **torrent_p, const char *root, const char *name) { *(torrent_p) = malloc(sizeof(struct torrent)); if(NULL==(*torrent_p)) { @@ -39,18 +43,17 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name) (*torrent_p)->files = NULL; (*torrent_p)->root = strdup(root); - if(NULL==(*torrent_p)->root) { - perror("strdup"); - torrent_free(*torrent_p); - return -1; - } + if(NULL==(*torrent_p)->root) { goto clean; } (*torrent_p)->name = strdup(name); - if(NULL==(*torrent_p)->name) { - perror("strdup"); - torrent_free(*torrent_p); - return -1; - } + if(NULL==(*torrent_p)->name) { goto clean; } + + if(tree_init(&((*torrent_p)->file_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; }