From: alex Date: Thu, 14 Oct 2021 22:34:01 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=7be6bbe39af92c0dd1b2eaba3fedba133484e27a;p=seeder ... --- diff --git a/inc/hashmap.h b/inc/hashmap.h index d947918..d30d22f 100644 --- a/inc/hashmap.h +++ b/inc/hashmap.h @@ -10,10 +10,10 @@ struct hash_map { }; void hashmap_clear(struct hash_map*); -void *hashmap_find(struct hash_map*,void*,size_t); +void *hashmap_find(struct hash_map*,const void*,size_t); void hashmap_free(struct hash_map*); int hashmap_init(struct hash_map**,size_t); int hashmap_insert(struct hash_map*,const void*,size_t,void*); -void *hashmap_remove(struct hash_map*,void*,size_t); +void *hashmap_remove(struct hash_map*,const void*,size_t); #endif diff --git a/inc/torrent.h b/inc/torrent.h index 7ba4a41..46cce89 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -16,6 +16,7 @@ struct torrent { struct hash_map *files; }; +int torrent_add(struct torrent*,struct file*); void torrent_free(struct torrent*); int torrent_init(struct torrent**,const char*,const char*); diff --git a/inc/tree.h b/inc/tree.h index 3ca98d0..a27273b 100644 --- a/inc/tree.h +++ b/inc/tree.h @@ -12,6 +12,7 @@ struct tree { size_t directory_count; }; +int tree_add(struct tree*,struct file*); void tree_free(struct tree*); int tree_init(struct tree**); diff --git a/src/add.c b/src/add.c index d352340..8f26341 100644 --- a/src/add.c +++ b/src/add.c @@ -79,9 +79,15 @@ static int add_queue_resize(size_t new_size) { } static int add_to_queue(struct file *to_add) { + struct file *p; int ret; + size_t len; + + len = strlen(to_add->path); + p = hashmap_find(add_queue,to_add->path,len); + if(p!=NULL) { return 0; } - while((ret = hashmap_insert(add_queue,path,strlen(path),to_add))<=0) { + while((ret = hashmap_insert(add_queue,to_add->path,len,to_add))<=0) { if(ret<0) { return -1; } if(add_queue_resize(add_queue->size<<1)<0) { return -1; } } diff --git a/src/hashmap.c b/src/hashmap.c index 0825f0e..7dfdf06 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -6,7 +6,7 @@ void hashmap_clear(struct hash_map *p) { } } -void *hashmap_find(struct hash_map *p, void *key, size_t key_size) { +void *hashmap_find(struct hash_map *p, const void *key, size_t key_size) { unsigned char hash[crypto_shorthash_BYTES]; size_t index; @@ -66,7 +66,7 @@ int hashmap_insert(struct hash_map *p, const void *key, size_t key_size, void *v return 1; } -void *hashmap_remove(struct hash_map *p, void *key, size_t key_size) { +void *hashmap_remove(struct hash_map *p, const void *key, size_t key_size) { unsigned char hash[crypto_shorthash_BYTES]; size_t index; void *removed; diff --git a/src/tree.c b/src/tree.c index 3943382..9b826a0 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1,6 +1,54 @@ #include -int tree_add(struct tree *p, struct file *p) { +static int tree_add_directory(struct tree*,const char*); +static int tree_add_file(struct tree*,struct file*); + +int tree_add(struct tree *root, struct file *p) { + char *str, *p, *prev; + struct tree *tree; + int index; + + if(NULL==root) { return -1; } + tree = root; + + str = strdup(p->path); + if(NULL==str) { return -1; } + + prev = str; + p = strtok(str,"/"); + while(p) { + if((index = tree_find_directory(tree,p))<0) { + if((index = tree_add_directory(p))<0) { goto clean; } + } + tree = tree->directories[index]; + + prev = p; + p = strtok(NULL,"/"); + } + + if(tree_add_file(tree,prev)<0) { goto clean; } + + free(str); + + return 1; + clean: + free(str); + return -1; +} + +static int tree_add_directory(struct tree *p, const char *dirname) { + return -1; +} + +static int tree_add_file(struct tree *tree, struct file *p) { + return -1; +} + +int tree_find_directory(struct tree *p, const char *dirname) { + return -1; +} + +int tree_find_entry(struct tree *p, const char *filename) { return -1; }