int opt_add_watch(const char *directory) {
struct torrent *p;
- char *name;
+ char *dup, *name;
if(NULL==directory) { return -1; }
return -1;
}
- name = strdup(directory);
- if(NULL==name) { return -1; }
- name = basename(name);
-
if(torrent_init(&p,global_options.piece_length)<0) { return -1; }
-
+
p->root = strdup(directory);
if(NULL==p->root) { goto clean; }
+ /*
+ * basename modifies buffer in place
+ * and should not be passed to free(3)
+ */
+ dup = strdup(directory);
+ if(NULL==dup) { goto clean; }
+ name = basename(dup);
+
p->name = strdup(name);
+ free(dup);
if(NULL==p->name) { goto clean; }
- free(name);
p->feed_url = strdup(global_options.feed_url);
if(NULL==p->feed_url) { goto clean; }
(*torrent_p)->root = NULL;
(*torrent_p)->name = NULL;
(*torrent_p)->feed_url = NULL;
- (*torrent_p)->pieces = NULL;
+
(*torrent_p)->piece_length = piece_length;
+
memset((*torrent_p)->infohash,0,crypto_hash_sha256_BYTES);
if(tree_init(&((*torrent_p)->tree))<0) { goto clean; }
if(hashmap_init(&((*torrent_p)->files.paths),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
if(hashmap_init(&((*torrent_p)->files.roots),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
+ (*torrent_p)->pieces = NULL;
+ (*torrent_p)->pieces_size = 0;
+
+ (*torrent_p)->watch_fd = -1;
+
return 1;
clean:
torrent_free(*torrent_p);
assert(torrent_init(&torrent,1000)<0);
assert(torrent_init(&torrent,16384)==1);
+
+ assert(NULL==torrent->root);
+ assert(NULL==torrent->name);
+ assert(NULL==torrent->feed_url);
+
+ assert(16384==torrent->piece_length);
+
+ assert(torrent->tree!=NULL);
+ assert(torrent->files.paths!=NULL);
+ assert(torrent->files.roots!=NULL);
+
+ assert(NULL==torrent->pieces);
+ assert(0==torrent->pieces_size);
+
+ assert(-1==torrent->watch_fd);
+
torrent_free(torrent);
}