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() {
}
static void *add_hash(void *unused) {
- struct file *p;
+ struct add_queue_entry *p;
+ struct add_queue_torrent *torrent_p;
while(1) {
pthread_testcancel();
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;
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);
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; }
}
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) {
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++;
}
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));
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);
}