--- /dev/null
+#include<torrent.h>
+
+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);
+
+int torrent_add(struct torrent *p, struct file *f) {
+ const char *path;
+
+ if(NULL==p) { return -1; }
+ if(NULL==f) { return -1; }
+
+ path = strstr(f->path,p->root);
+ if((path!=NULL)&&(path==f->path)) {
+ path += strlen(p->root);
+ } else {
+ path = f->path;
+ }
+
+ if(tree_add(p->tree,path,f)<0) { return -1; }
+ if(torrent_add_file(p,f)<0) { return -1; }
+
+ return 1;
+}
+
+static int torrent_add_file(struct torrent *p, struct file *f) {
+ 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;
+}
+
+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;
+}
#include<torrent.h>
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 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;
-
- if(NULL==p) { return -1; }
- if(NULL==f) { return -1; }
-
- path = strstr(f->path,p->root);
- if((path!=NULL)&&(path==f->path)) {
- path += strlen(p->root);
- } else {
- path = f->path;
- }
-
- if(tree_add(p->tree,path,f)<0) { return -1; }
- if(torrent_add_file(p,f)<0) { return -1; }
-
- return 1;
-}
-
-static int torrent_add_file(struct torrent *p, struct file *f) {
- 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;
-}
-
int torrent_file(struct torrent *torrent_p) {
uint8_t *info;
FILE *fp;
// return -1;
}
-
static int piece_layer_sort(const void *p1, const void *p2) {
struct block *a, *b;
a = (struct block*)p1;
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; }
-
- 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);
- }
-
- if(p->tree!=NULL) { tree_free(p->tree); }
-
- free(p);
-}
-
-#define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8
-
-int torrent_init(struct torrent **torrent_p, const char *root, const char *name, unsigned long long piece_length) {
- if(NULL==torrent_p) { return -1; }
- if(NULL==root) { return -1; }
- if(NULL==name) { return -1; }
-
- if((piece_length<16384)||(!(piece_length&&!(piece_length&(piece_length-1))))) { return -1; }
-
- *(torrent_p) = malloc(sizeof(struct torrent));
- if(NULL==(*torrent_p)) {
- perror("malloc");
- return -1;
- }
-
- (*torrent_p)->root = strdup(root);
- if(NULL==(*torrent_p)->root) { goto clean; }
-
- (*torrent_p)->name = strdup(name);
- if(NULL==(*torrent_p)->name) { goto clean; }
-
- (*torrent_p)->piece_length = piece_length;
-
- if(tree_init(&((*torrent_p)->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;
-}
-
-//char *torrent_magnet(struct torrent *p) {
-// return NULL;
-//}
--- /dev/null
+#include<torrent.h>
+
+#define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8
+
+int torrent_init(struct torrent **torrent_p, const char *root, const char *name, unsigned long long piece_length) {
+ if(NULL==torrent_p) { return -1; }
+ if(NULL==root) { return -1; }
+ if(NULL==name) { return -1; }
+
+ if((piece_length<16384)||(!(piece_length&&!(piece_length&(piece_length-1))))) { return -1; }
+
+ *(torrent_p) = malloc(sizeof(struct torrent));
+ if(NULL==(*torrent_p)) {
+ perror("malloc");
+ return -1;
+ }
+
+ (*torrent_p)->root = strdup(root);
+ if(NULL==(*torrent_p)->root) { goto clean; }
+
+ (*torrent_p)->name = strdup(name);
+ if(NULL==(*torrent_p)->name) { goto clean; }
+
+ (*torrent_p)->piece_length = piece_length;
+
+ if(tree_init(&((*torrent_p)->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;
+}