...
authoralex <[email protected]>
Sun, 5 Dec 2021 20:16:16 +0000 (12:16 -0800)
committeralex <[email protected]>
Sun, 5 Dec 2021 20:16:16 +0000 (12:16 -0800)
Makefile.am
src/torrent/add.c [new file with mode: 0644]
src/torrent/file.c [moved from src/torrent.c with 64% similarity]
src/torrent/free.c [new file with mode: 0644]
src/torrent/init.c [new file with mode: 0644]
src/torrent/magnet.c [new file with mode: 0644]

index aef125587bbe46428056a06e4caa95c04e0ad9d3..c380572530fad4758631e0aeb54e81fcfb7d4485 100644 (file)
@@ -40,7 +40,11 @@ seederd_SOURCES = \
        src/session.c \
        src/setup.c \
        src/shutdown.c \
-       src/torrent.c \
+       src/torrent/add.c \
+       src/torrent/file.c \
+       src/torrent/free.c \
+       src/torrent/init.c \
+       src/torrent/magnet.c \
        src/tree.c \
        src/usage.c \
        src/watch.c
diff --git a/src/torrent/add.c b/src/torrent/add.c
new file mode 100644 (file)
index 0000000..e14bace
--- /dev/null
@@ -0,0 +1,100 @@
+#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;
+}
similarity index 64%
rename from src/torrent.c
rename to src/torrent/file.c
index e971b6577c6206b85f211a4210cf66ad1853a8df..9b48dbf48905c70f34c2e23a9738b760c3c954db 100644 (file)
@@ -1,79 +1,13 @@
 #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;
@@ -254,7 +188,6 @@ 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;
@@ -311,89 +244,3 @@ 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; }
-
-       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;
-//}
diff --git a/src/torrent/free.c b/src/torrent/free.c
new file mode 100644 (file)
index 0000000..5952053
--- /dev/null
@@ -0,0 +1,18 @@
+#include<torrent.h>
+
+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);
+}
diff --git a/src/torrent/init.c b/src/torrent/init.c
new file mode 100644 (file)
index 0000000..0f90a33
--- /dev/null
@@ -0,0 +1,34 @@
+#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;
+}
diff --git a/src/torrent/magnet.c b/src/torrent/magnet.c
new file mode 100644 (file)
index 0000000..16a006d
--- /dev/null
@@ -0,0 +1,5 @@
+#include<torrent.h>
+
+char *torrent_magnet(struct torrent *p) {
+       return NULL;
+}