#include<torrent.h>
+static int piece_layer_sort(const void*,const void*);
static int torrent_add_file(struct torrent*,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 *fp, struct torrent *p) {
+ size_t index, pieces;
+ struct file *file_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];
+ if(file_p!=NULL) {
+ pieces += block_length(file_p->piece_layers);
+ }
+ }
+
+ piece_layers = malloc(sizeof(struct block*)*pieces);
+ if(NULL==piece_layers) {
+ return -1;
+ }
+
+ index = 0;
+ for(size_t i=0;i<p->files->size;i++) {
+ file_p = (struct file*)p->files->map[i];
+ if(file_p!=NULL) {
+ block_p = file_p->piece_layers;
+ while(block_p!=NULL) {
+ piece_layers[index] = block_p;
+ index++;
+ block_p = block_p->next;
+ }
+ }
+ }
+
+ qsort(piece_layers,pieces,sizeof(struct block*),&piece_layer_sort);
+
return -1;
+//clean:
+// free(piece_layers);
+// return -1;
+}
+
+static int piece_layer_sort(const void *p1, const void *p2) {
+ struct block *a, *b;
+ a = (struct block*)p1;
+ b = (struct block*)p2;
+
+ if(a==NULL) { return 1; }
+ if(b==NULL) { return -1; }
+
+ return memcmp(a->hash,b->hash,crypto_hash_sha256_BYTES);
}
static int torrent_file_write_end(FILE *fp) {