]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Wed, 13 Apr 2022 01:18:35 +0000 (18:18 -0700)
committeralex <[email protected]>
Wed, 13 Apr 2022 01:18:35 +0000 (18:18 -0700)
inc/piece.h
inc/torrent.h
src/torrent/free.c
src/torrent/init.c
src/torrent/piece.c

index de80352fa5cca33686a73b563d3e97b5ba062927..3cce33c3e3b2a2c2db5f62c44ab9d9aeaed47039 100644 (file)
@@ -14,6 +14,8 @@ struct piece {
 
 #include<torrent.h>
 
+int piece_init(struct piece**);
+void piece_free(struct piece*);
 struct piece* piece_load(const struct torrent*,size_t);
 
 #endif
index ee1c66f84c71d66610bd180b46ede4e366954121..76239200b089c5765d1a48c2b4a6de6b03d788f7 100644 (file)
@@ -9,6 +9,7 @@
 #include<file.h>
 #include<fs.h>
 #include<hashmap.h>
+#include<piece.h>
 #include<tree.h>
 
 struct torrent_files {
@@ -26,6 +27,9 @@ struct torrent {
        struct tree *tree;
        struct torrent_files files;
 
+       struct piece **pieces;
+       size_t pieces_size;
+
        uint8_t infohash[crypto_hash_sha256_BYTES];
 
        int watch_fd;
index abf64936f75149f7437894caafb4d475df574aa5..8be6e51acd426c13ce1c70f1cd6061642d84fa07 100644 (file)
@@ -13,6 +13,8 @@ void torrent_free(struct torrent *p) {
        hashmap_free(p->files.paths);
        hashmap_free(p->files.roots);
 
+       if(p->pieces!=NULL) { free(p->pieces); }
+
        if(p->tree!=NULL) { tree_free(p->tree); }
        
        free(p);
index 692c1802745f5a625a5bde88df6770acd58726fc..443cf14c59a603f5b15d55b199e88531b571fcdd 100644 (file)
@@ -16,6 +16,7 @@ int torrent_init(struct torrent **torrent_p, unsigned long long piece_length) {
        (*torrent_p)->root = NULL;
        (*torrent_p)->name = NULL;
        (*torrent_p)->feed_url = NULL;
+       (*torrent_p)->pieces = NULL;
        (*torrent_p)->piece_length = piece_length;
        
        memset((*torrent_p)->infohash,0,crypto_hash_sha256_BYTES);
index 9e595599c60c886b6674e248dfcdaa2cdb21021c..f5a774789ddd999139a38dc59dbdab43678882cc 100644 (file)
@@ -2,6 +2,7 @@
 
 static int piece_layer_sort(const void*, const void*);
 static int torrent_bencode_piece_layers(FILE*,struct file*);
+static int torrent_piece_layers(struct torrent*);
 
 #define PIECE_LAYER_BENCODED_LENGTH 2+1+32+2+1+32
 
@@ -29,10 +30,10 @@ static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) {
        return 1;
 }
 
-int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
-       const unsigned char **hashes;
-       size_t index, pieces;
+static int torrent_piece_layers(struct torrent *p) {
        struct file *file_p;
+       struct block *block_p;
+       size_t index, pieces;
 
        pieces = 0;
        for(size_t i=0;i<p->files.roots->size;i++) {
@@ -42,38 +43,40 @@ int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
                }
        }
 
-       hashes = malloc(sizeof(const unsigned char*)*pieces);
-       if(NULL==hashes) { return -1; }
+       // sort hashes first
+       qsort(hashes,pieces,sizeof(const unsigned char*),&piece_layers_sort);
+       
+       torrent->pieces = malloc(sizeof(struct piece)*pieces);
+       if(NULL==torrent->pieces) { return -1; }
 
        index = 0;
        for(size_t i=0;i<p->files.roots->size;i++) {
                file_p = (struct file*)p->files.roots->map[i];
                if(file_p!=NULL) {
                        if(file_p->size>p->piece_length) {
-                               hashes[index] = file_p->root->hash;
-                               index++;
+                               block_p = file_p->piece_layers;
+                               while(block_p!=NULL) {
+                                       torrent->pieces[index] = file_p->root->hash;
+                                       index++;
+                               }
                        }
                }
        }
 
-       qsort(hashes,pieces,sizeof(const unsigned char*),&piece_layer_sort);
+       return -1;
+}
 
-       for(size_t i=0;i<pieces;i++) {
-               file_p = hashmap_find(p->files.roots,hashes[i],crypto_hash_sha256_BYTES);
-               if(NULL==file_p) { goto clean; }
+int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
+       if(torrent_piece_layers(p)<0) { return -1; }
 
-               if(torrent_bencode_piece_layers(fp,file_p)<0) { goto clean; }
+       for(size_t i=0;i<p->pieces_size;i++) {
+               if(torrent_bencode_piece_layers(fp,file_p)<0) { return -1; }
        }
 
-       free(hashes);
-
        return 1;
-clean:
-       free(hashes);
-       return -1;
 }
 
-static int piece_layer_sort(const void *p1, const void *p2) {
+static int piece_layers_sort(const void *p1, const void *p2) {
        if(NULL==p1) { return 1; }
        if(NULL==p2) { return -1; }