From: alex <alex@infiniteadaptability.org>
Date: Fri, 15 Apr 2022 23:28:57 +0000 (-0700)
Subject: ...
X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=ed88d8329a79214a134e194a561521f44ba62d5d;p=seeder

...
---

diff --git a/inc/add.h b/inc/add.h
index 87b4025..f7a179c 100644
--- a/inc/add.h
+++ b/inc/add.h
@@ -8,9 +8,19 @@
 #include<hashmap.h>
 #include<log.h>
 
+struct add_queue_torrent {
+	struct torrent *torrent;
+	struct add_queue_torrent *next;
+};
+
+struct add_queue_entry {
+	struct file *file;
+	struct add_queue_torrent *torrents;
+};
+
 #define ADD_MESSAGE_ADDING_TORRENT "adding all files in %s to torrent named %s\n"
-#define ADD_MESSAGE_ADDED_FILE "added file %s\n"
-#define ADD_MESSAGE_HASH_FILE_FAILED "failed to hash %s\n"
+#define ADD_MESSAGE_ADDED_FILE "added file: %s\n"
+#define ADD_MESSAGE_HASH_FILE_FAILED "failed to hash file: %s\n"
 
 #define ADD_QUEUE_INITIAL_SIZE 8
 
diff --git a/src/add.c b/src/add.c
index 8d29e75..b354724 100644
--- a/src/add.c
+++ b/src/add.c
@@ -8,9 +8,12 @@ static struct torrent *current_torrent;
 
 static int add_find_all();
 static void *add_hash(void*);
+static int add_queue_entry_add_torrent(struct add_queue_entry*,struct torrent*);
+static void add_queue_entry_free(struct add_queue_entry*);
+static int add_queue_entry_init(struct add_queue_entry**);
 static int add_queue_resize();
-static int add_to_queue();
-static struct file *get_next();
+static int add_to_queue(struct add_queue_entry*);
+static struct add_queue_entry *get_next();
 static int ftw_helper(const char*,const struct stat*,int);
 
 int add() {
@@ -60,7 +63,8 @@ static int add_find_all(struct torrent *p) {
 }
 
 static void *add_hash(void *unused) {
-	struct file *p;
+	struct add_queue_entry *p;
+	struct add_queue_torrent *torrent_p;
 
 	while(1) {
 		pthread_testcancel();
@@ -70,19 +74,76 @@ static void *add_hash(void *unused) {
 
 		if(NULL==p) { return NULL; }
 
-		if(file_hash(p,global_options.piece_length)<0) {
-			log_err(ADD_MESSAGE_HASH_FILE_FAILED,p->path);
+		if(file_hash(p->file,global_options.piece_length)<0) {
+			log_err(ADD_MESSAGE_HASH_FILE_FAILED,p->file->path);
 			return NULL;
 		}
 
-		log_info(ADD_MESSAGE_ADDED_FILE,p->path);
+		torrent_p = p->torrents;
+		while(torrent_p!=NULL) {
+			if(torrent_add(torrent_p->torrent,p->file)<0) { return NULL; }
+			torrent_p = torrent_p->next;
+		}
+
+		add_queue_entry_free(p);
+
+		log_info(ADD_MESSAGE_ADDED_FILE,p->file->path);
 	}
 
 	return NULL;
 }
 
+static int add_queue_entry_add_torrent(struct add_queue_entry *entry,struct torrent *torrent) {
+	struct add_queue_torrent *prev, *p;
+
+	p = malloc(sizeof(struct add_queue_torrent));
+	if(NULL==p) { return -1; }
+
+	p->torrent = torrent;
+	p->next = NULL;
+
+	if(entry->torrents==NULL) { goto first; }
+
+	prev = entry->torrents;
+	while(prev->next!=NULL) {
+		if(prev->torrent==torrent) { goto found; }
+		prev = prev->next;
+	}
+
+	prev->next = p;
+	return 1;
+first:
+	entry->torrents = p;
+	return 1;
+found:
+	free(p);
+	return 0;
+}
+
+static void add_queue_entry_free(struct add_queue_entry *p) {
+	struct add_queue_torrent *to_free;
+
+	while(p->torrents!=NULL) {
+		to_free = p->torrents;
+		p->torrents = p->torrents->next;
+		free(to_free);
+	}
+
+	free(p);
+}
+
+static int add_queue_entry_init(struct add_queue_entry **p) {
+	(*p) = malloc(sizeof(struct add_queue_entry));
+	if(NULL==(*p)) { return -1; }
+
+	(*p)->file = NULL;
+	(*p)->torrents = NULL;
+
+	return 1;
+}
+
 static int add_queue_resize(size_t new_size) {
-	struct file *p;
+	struct add_queue_entry *p;
 	struct hash_map *new, *old;
 	int ret;
 
@@ -91,7 +152,7 @@ static int add_queue_resize(size_t new_size) {
 	for(size_t i=0;i<add_queue->size;i++) {
 		p = add_queue->map[i];
 		if(p!=NULL) {
-			if((ret = hashmap_insert(new,p->path,strlen(p->path),p))<=0) {
+			if((ret = hashmap_insert(new,p->file->path,strlen(p->file->path),p))<=0) {
 				if(ret<0) { return -1; }
 				hashmap_clear(new);
 				hashmap_free(new);
@@ -110,19 +171,19 @@ static int add_queue_resize(size_t new_size) {
 	return 1;
 }
 
-static int add_to_queue(struct file *to_add) {
-	struct file *p;
+static int add_to_queue(struct add_queue_entry *to_add) {
+	struct add_queue_entry *p;
 	int ret;
 	size_t len;
 
-	len = strlen(to_add->path);
+	len = strlen(to_add->file->path);
 
-	while((ret = hashmap_insert(add_queue,to_add->path,len,to_add))<=0) {
+	while((ret = hashmap_insert(add_queue,to_add->file->path,len,to_add))<=0) {
 		if(ret<0) { return -1; }
 		
-		p = hashmap_find(add_queue,to_add->path,len);
+		p = hashmap_find(add_queue,to_add->file->path,len);
 		if(p!=NULL) {
-			if(strcmp(p->path,to_add->path)==0) { return 1; }
+			if(strcmp(p->file->path,to_add->file->path)==0) { return 1; }
 		}
 
 		if(add_queue_resize(add_queue->size<<1)<0) { return -1; }
@@ -132,7 +193,8 @@ static int add_to_queue(struct file *to_add) {
 }
 
 static int ftw_helper(const char *path, const struct stat *st, int typeflag) {
-	struct file *to_add;
+	struct file *file_p;
+	struct add_queue_entry *to_add;
 
 	if(typeflag!=FTW_F) { return 0; }
 	switch(global_options.file_filter) {
@@ -144,16 +206,19 @@ static int ftw_helper(const char *path, const struct stat *st, int typeflag) {
 			break;
 	}
 
-	if(file_init(&to_add,path)<0) { return -1; }
-	if(add_to_queue(to_add)<0) { return -1; }
+	if(file_init(&file_p,path)<0) { return -1; }
+	if(add_queue_entry_init(&to_add)<0) { return -1; }
+
+	to_add->file = file_p;
+	if(add_queue_entry_add_torrent(to_add,current_torrent)<0) { return -1; }
 
-	if(torrent_add(current_torrent,to_add)<0) { return -1; }
+	if(add_to_queue(to_add)<0) { return -1; }
 
 	return 0;
 }
 
-static struct file *get_next() {
-	struct file *p;
+static struct add_queue_entry *get_next() {
+	struct add_queue_entry *p;
 	while(add_queue_index<add_queue->size) {
 		p = add_queue->map[add_queue_index];
 		add_queue_index++;
diff --git a/test/unit/add.tests.c b/test/unit/add.tests.c
index 5615dd7..7e1f40f 100644
--- a/test/unit/add.tests.c
+++ b/test/unit/add.tests.c
@@ -27,11 +27,15 @@ int main() {
 }
 
 static void add_find_all_basic_test() {
-	struct torrent *p;
+	struct torrent *p, *p2;
+	struct add_queue_entry *f;
 
 	TORRENT_SETUP_EMPTY_EXAMPLE(&p);
+	TORRENT_SETUP_EMPTY_EXAMPLE(&p2);
 
 	assert(hashmap_init(&add_queue,ADD_QUEUE_INITIAL_SIZE)==1);
+	memset(add_queue->key,0,crypto_shorthash_KEYBYTES);
+
 	current_torrent = p;
 
 	assert(-1==add_find_all(NULL));
@@ -39,6 +43,27 @@ static void add_find_all_basic_test() {
 
 	assert(1==add_find_all(p));
 
+	assert(add_queue->map[0]==NULL);
+	assert(add_queue->map[1]==NULL);
+	assert(add_queue->map[5]==NULL);
+	assert(add_queue->map[6]==NULL);
+	assert(add_queue->map[7]==NULL);
+
+	f = add_queue->map[2];
+	assert(f!=NULL);
+	assert(strcmp(f->file->path,TEST_FILE_2)==0);
+	
+	f = add_queue->map[3];
+	assert(f!=NULL);
+	assert(strcmp(f->file->path,TEST_FILE_1)==0);
+	
+	f = add_queue->map[4];
+	assert(f!=NULL);
+	assert(strcmp(f->file->path,TEST_FILE_8)==0);
+
+	current_torrent = p2;
+	assert(1==add_find_all(p2));
+
 	assert(0);
 }