...
authoralex <[email protected]>
Wed, 8 Dec 2021 05:18:41 +0000 (21:18 -0800)
committeralex <[email protected]>
Wed, 8 Dec 2021 05:18:41 +0000 (21:18 -0800)
inc/torrent.h
src/torrent/add.c
src/torrent/file.c
src/torrent/free.c
src/torrent/init.c
test/unit/torrent.tests.c

index db1d775b931435b0caf2255e7eab8d6e1a5484fb..ad8aafc5d635e134143cd0c620a66226a9a0fcc3 100644 (file)
@@ -24,7 +24,7 @@ struct torrent {
        unsigned long long piece_length;
 
        struct tree *tree;
-       struct torrent_files *files;
+       struct torrent_files files;
 
        int watch_fd;
 };
index 284eff06df9f75455200f9829ee26cee666855dc..6b3b8da67848a4478b889ca2426068cbfc3df5d2 100644 (file)
@@ -3,7 +3,7 @@
 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);
+static int torrent_files_resize(struct torrent *torrent_p, struct hash_map**, size_t new_size);
 
 int torrent_add(struct torrent *p, struct file *f) {
        const char *path;
@@ -27,14 +27,22 @@ int torrent_add(struct torrent *p, struct file *f) {
 static int torrent_add_file(struct torrent *p, struct file *f) {
        int i;
 
-       while((i = torrent_add_file_by_root(p->files,f))<=0) {
+       while((i = torrent_add_file_by_root(p->files.roots,f))<=0) {
                if(i<0) { return -1; }
-               if(torrent_files_resize(p,p->files->size<<1)<0) { return -1; }
+               if(torrent_files_resize(
+                       p, /* torrent_p */
+                       &(p->files.roots), /* map */
+                       p->files.roots->size<<1 /* new_size */
+               )<0) { return -1; }
        }
 
-       while((i = torrent_add_file_by_path(p->files,f))<=0) {
+       while((i = torrent_add_file_by_path(p->files.paths,f))<=0) {
                if(i<0) { return -1; }
-               if(torrent_files_resize(p,p->files->size<<1)<0) { return -1; }
+               if(torrent_files_resize(
+                       p, /* torrent_p */
+                       &(p->files.paths), /* map */
+                       p->files.paths->size<<1 /* new_size */
+               )<0) { return -1; }
        }
 
        return 1;
@@ -68,54 +76,38 @@ static int torrent_add_file_by_root(struct hash_map *files, struct file *f) {
        return 1;
 }
 
-static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) {
+static int torrent_files_resize(struct torrent *torrent_p, struct hash_map **map, size_t new_size) {
        struct file *p;
        struct hash_map *new, *old;
-       int ret1, ret2;
+       int ret, flag;
 
        if(hashmap_init(&new,new_size)<0) { return -1; }
 
-       for(size_t i=0;i<torrent_p->files->size;i++) {
-               p = torrent_p->files->map[i];
-               /*
-                * cases:
-                * 1.) p = NULL
-                * 2.) p = added by torrent_add_file_by_root, not already added
-                * 3.) p = added by torrent_add_file_by_path, not already added
-                * 4.) p = added by torrent_add_file_by_root, already added
-                * 5.) p = added by torrent_add_file_by_path, already added
-                */
-               if(p!=NULL) {
-                       found = hashmap_find(torrent_p->files,p->path,strlen(p->path));
-                       if(
-                       if((ret1 = torrent_add_file_by_root(new,p))<=0) {
-                               if(ret1<0) { goto clean; }
-                               printf("%s already found [root]\n",p->name);
-                       }
-                       if((ret2 = torrent_add_file_by_path(new,p))<=0) {
-                               if(ret2<0) { goto clean; }
-                               printf("%s already found [path]\n",p->name);
-                       }
-
-                       if((!ret1)&&(!ret2)) { goto resize; }
-               }
+       flag = 0;
+       if(torrent_p->files.paths==(*map)) {
+               flag = 1;
        }
 
-       printf("after\n");
-       for(size_t i=0;i<new->size;i++) {
-               p = new->map[i];
-               printf("%lu: ",i);
-               if(p==NULL) {
-                       printf("NULL\n");
-               } else {
-                       printf("%s\n",p->name);
+       for(size_t i=0;i<(*map)->size;i++) {
+               p = (*map)->map[i];
+               if(p!=NULL) {
+                       if(flag) {
+                               if((ret = hashmap_insert(new,p->path,strlen(p->path),p))<=0) {
+                                       if(ret<0) { goto clean; }
+                                       goto resize;
+                               }
+                       } else {
+                               if((ret = hashmap_insert(new,p->root,crypto_hash_sha256_BYTES,p))<=0) {
+                                       if(ret<0) { goto clean; }
+                                       goto resize;
+                               }
+                       }
                }
        }
 
-       hashmap_clear(torrent_p->files);
-
-       old = torrent_p->files;
-       torrent_p->files = new;
+       hashmap_clear(*map);
+       old = *map;
+       *map = new;
 
        hashmap_free(old);
 
@@ -123,8 +115,7 @@ static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) {
 resize:
        hashmap_clear(new);
        hashmap_free(new);
-       printf("resizing to %lu\n",new_size<<1);
-       return torrent_files_resize(torrent_p,new_size<<1);
+       return torrent_files_resize(torrent_p,map,new_size<<1);
 clean:
        hashmap_clear(new);
        hashmap_free(new);
index 9b48dbf48905c70f34c2e23a9738b760c3c954db..a1763d761d121589faf01779660da96e72c65b51 100644 (file)
@@ -155,8 +155,8 @@ static int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
        struct block **piece_layers, *block_p;
 
        pieces = 0;
-       for(size_t i=0;i<p->files->size;i++) {
-               file_p = (struct file*)p->files->map[i];
+       for(size_t i=0;i<p->files.roots->size;i++) {
+               file_p = (struct file*)p->files.roots->map[i];
                if(file_p!=NULL) {
                        pieces += block_length(file_p->piece_layers);
                }
@@ -168,8 +168,8 @@ static int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
        }
 
        index = 0;
-       for(size_t i=0;i<p->files->size;i++) {
-               file_p = (struct file*)p->files->map[i];
+       for(size_t i=0;i<p->files.roots->size;i++) {
+               file_p = (struct file*)p->files.roots->map[i];
                if(file_p!=NULL) {
                        block_p = file_p->piece_layers;
                        while(block_p!=NULL) {
index 59520532db0bc9a8e3e44f72520932774055ae69..3905e669c1c6af6ab6f1b840cd9e167ec29b11c3 100644 (file)
@@ -6,11 +6,11 @@ void torrent_free(struct torrent *p) {
        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);
-       }
+       // clear first, tree_free will also free files
+       hashmap_clear(p->files.paths);
+       hashmap_clear(p->files.roots);
+       hashmap_free(p->files.paths);
+       hashmap_free(p->files.roots);
 
        if(p->tree!=NULL) { tree_free(p->tree); }
        
index 0f90a33d1b3c6b3ad51858be3c99fa4bd0546f0d..6b631f975d75e610f5c2c7edfd60c4a7ee8a1b55 100644 (file)
@@ -25,7 +25,8 @@ int torrent_init(struct torrent **torrent_p, const char *root, const char *name,
 
        if(tree_init(&((*torrent_p)->tree))<0) { goto clean; }
 
-       if(hashmap_init(&((*torrent_p)->files),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
+       if(hashmap_init(&((*torrent_p)->files.paths),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
+       if(hashmap_init(&((*torrent_p)->files.roots),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
 
        return 1;
        clean:
index e353d4409c489e1ac5b35e9011d57833a6c7e6ae..0a0ff3103dc2d5ff3ee37fdd4ae423e838ca1b78 100644 (file)
@@ -50,24 +50,24 @@ static void torrent_add_basic_test() {
                assert(torrent->tree->entries->next->next->file==file1);
                assert(torrent->tree->entries->next->next->next->file==file2);
 
-               p = hashmap_find(torrent->files,file1->path,strlen(file1->path));
+               p = hashmap_find(torrent->files.paths,file1->path,strlen(file1->path));
                assert(p==file1);
-               p = hashmap_find(torrent->files,file1->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(torrent->files.roots,file1->root,crypto_hash_sha256_BYTES);
                assert(p==file1);
 
-               p = hashmap_find(torrent->files,file2->path,strlen(file2->path));
+               p = hashmap_find(torrent->files.paths,file2->path,strlen(file2->path));
                assert(p==file2);
-               p = hashmap_find(torrent->files,file2->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(torrent->files.roots,file2->root,crypto_hash_sha256_BYTES);
                assert(p==file2);
        
-               p = hashmap_find(torrent->files,file3->path,strlen(file3->path));
+               p = hashmap_find(torrent->files.paths,file3->path,strlen(file3->path));
                assert(p==file3);
-               p = hashmap_find(torrent->files,file3->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(torrent->files.roots,file3->root,crypto_hash_sha256_BYTES);
                assert(p==file3);
        
-               p = hashmap_find(torrent->files,file4->path,strlen(file4->path));
+               p = hashmap_find(torrent->files.paths,file4->path,strlen(file4->path));
                assert(p==file4);
-               p = hashmap_find(torrent->files,file4->root,crypto_hash_sha256_BYTES);
+               p = hashmap_find(torrent->files.roots,file4->root,crypto_hash_sha256_BYTES);
                assert(p==file4);
 
                torrent_free(torrent);