...
authoralex <[email protected]>
Sun, 5 Dec 2021 00:38:54 +0000 (16:38 -0800)
committeralex <[email protected]>
Sun, 5 Dec 2021 00:38:54 +0000 (16:38 -0800)
inc/torrent.h
src/torrent.c

index f67ed386d616968b843415fd0d719a9724d2c2f9..7266833e9ef07f64b8b4a71de6ebaa5e668e6539 100644 (file)
@@ -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*);
index db20559f5d2de38ca92b7955aa066503e6f63327..e971b6577c6206b85f211a4210cf66ad1853a8df 100644 (file)
@@ -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;i<torrent_p->files->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;
+//}