}
static int torrent_add_file(struct torrent *p, struct file *f) {
- return -1;
+ if(hashmap_insert(p->files,f->path,strlen(f->path),f)<0) { return -1; }
+
+ return 1;
}
void torrent_free(struct torrent *p) {
free(p);
}
+#define TORRENT_FILES_HASHMAP_INITIAL_SIZE 8
+
int torrent_init(struct torrent **torrent_p, const char *root, const char *name) {
*(torrent_p) = malloc(sizeof(struct torrent));
if(NULL==(*torrent_p)) {
(*torrent_p)->files = NULL;
(*torrent_p)->root = strdup(root);
- if(NULL==(*torrent_p)->root) {
- perror("strdup");
- torrent_free(*torrent_p);
- return -1;
- }
+ if(NULL==(*torrent_p)->root) { goto clean; }
(*torrent_p)->name = strdup(name);
- if(NULL==(*torrent_p)->name) {
- perror("strdup");
- torrent_free(*torrent_p);
- return -1;
- }
+ if(NULL==(*torrent_p)->name) { goto clean; }
+
+ if(tree_init(&((*torrent_p)->file_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;
}