]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sun, 17 Oct 2021 07:01:27 +0000 (00:01 -0700)
committeralex <[email protected]>
Sun, 17 Oct 2021 07:01:27 +0000 (00:01 -0700)
src/torrent.c

index 72e3927d8f81a025cf5b1ad26be72eb0ede91852..89ceb2bd6d91ba450e4c15afd06cd7efaae48d0e 100644 (file)
@@ -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;
 }