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);
+static int torrent_files_resize(struct torrent *torrent_p, struct hash_map**, size_t new_size);
int torrent_add(struct torrent *p, struct file *f) {
const char *path;
static int torrent_add_file(struct torrent *p, struct file *f) {
int i;
- while((i = torrent_add_file_by_root(p->files,f))<=0) {
+ while((i = torrent_add_file_by_root(p->files.roots,f))<=0) {
if(i<0) { return -1; }
- if(torrent_files_resize(p,p->files->size<<1)<0) { return -1; }
+ if(torrent_files_resize(
+ p, /* torrent_p */
+ &(p->files.roots), /* map */
+ p->files.roots->size<<1 /* new_size */
+ )<0) { return -1; }
}
- while((i = torrent_add_file_by_path(p->files,f))<=0) {
+ while((i = torrent_add_file_by_path(p->files.paths,f))<=0) {
if(i<0) { return -1; }
- if(torrent_files_resize(p,p->files->size<<1)<0) { return -1; }
+ if(torrent_files_resize(
+ p, /* torrent_p */
+ &(p->files.paths), /* map */
+ p->files.paths->size<<1 /* new_size */
+ )<0) { return -1; }
}
return 1;
return 1;
}
-static int torrent_files_resize(struct torrent *torrent_p, size_t new_size) {
+static int torrent_files_resize(struct torrent *torrent_p, struct hash_map **map, size_t new_size) {
struct file *p;
struct hash_map *new, *old;
- int ret1, ret2;
+ int ret, flag;
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];
- /*
- * cases:
- * 1.) p = NULL
- * 2.) p = added by torrent_add_file_by_root, not already added
- * 3.) p = added by torrent_add_file_by_path, not already added
- * 4.) p = added by torrent_add_file_by_root, already added
- * 5.) p = added by torrent_add_file_by_path, already added
- */
- if(p!=NULL) {
- found = hashmap_find(torrent_p->files,p->path,strlen(p->path));
- if(
- if((ret1 = torrent_add_file_by_root(new,p))<=0) {
- if(ret1<0) { goto clean; }
- printf("%s already found [root]\n",p->name);
- }
- if((ret2 = torrent_add_file_by_path(new,p))<=0) {
- if(ret2<0) { goto clean; }
- printf("%s already found [path]\n",p->name);
- }
-
- if((!ret1)&&(!ret2)) { goto resize; }
- }
+ flag = 0;
+ if(torrent_p->files.paths==(*map)) {
+ flag = 1;
}
- printf("after\n");
- for(size_t i=0;i<new->size;i++) {
- p = new->map[i];
- printf("%lu: ",i);
- if(p==NULL) {
- printf("NULL\n");
- } else {
- printf("%s\n",p->name);
+ for(size_t i=0;i<(*map)->size;i++) {
+ p = (*map)->map[i];
+ if(p!=NULL) {
+ if(flag) {
+ if((ret = hashmap_insert(new,p->path,strlen(p->path),p))<=0) {
+ if(ret<0) { goto clean; }
+ goto resize;
+ }
+ } else {
+ if((ret = hashmap_insert(new,p->root,crypto_hash_sha256_BYTES,p))<=0) {
+ if(ret<0) { goto clean; }
+ goto resize;
+ }
+ }
}
}
- hashmap_clear(torrent_p->files);
-
- old = torrent_p->files;
- torrent_p->files = new;
+ hashmap_clear(*map);
+ old = *map;
+ *map = new;
hashmap_free(old);
resize:
hashmap_clear(new);
hashmap_free(new);
- printf("resizing to %lu\n",new_size<<1);
- return torrent_files_resize(torrent_p,new_size<<1);
+ return torrent_files_resize(torrent_p,map,new_size<<1);
clean:
hashmap_clear(new);
hashmap_free(new);
assert(torrent->tree->entries->next->next->file==file1);
assert(torrent->tree->entries->next->next->next->file==file2);
- p = hashmap_find(torrent->files,file1->path,strlen(file1->path));
+ p = hashmap_find(torrent->files.paths,file1->path,strlen(file1->path));
assert(p==file1);
- p = hashmap_find(torrent->files,file1->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(torrent->files.roots,file1->root,crypto_hash_sha256_BYTES);
assert(p==file1);
- p = hashmap_find(torrent->files,file2->path,strlen(file2->path));
+ p = hashmap_find(torrent->files.paths,file2->path,strlen(file2->path));
assert(p==file2);
- p = hashmap_find(torrent->files,file2->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(torrent->files.roots,file2->root,crypto_hash_sha256_BYTES);
assert(p==file2);
- p = hashmap_find(torrent->files,file3->path,strlen(file3->path));
+ p = hashmap_find(torrent->files.paths,file3->path,strlen(file3->path));
assert(p==file3);
- p = hashmap_find(torrent->files,file3->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(torrent->files.roots,file3->root,crypto_hash_sha256_BYTES);
assert(p==file3);
- p = hashmap_find(torrent->files,file4->path,strlen(file4->path));
+ p = hashmap_find(torrent->files.paths,file4->path,strlen(file4->path));
assert(p==file4);
- p = hashmap_find(torrent->files,file4->root,crypto_hash_sha256_BYTES);
+ p = hashmap_find(torrent->files.roots,file4->root,crypto_hash_sha256_BYTES);
assert(p==file4);
torrent_free(torrent);