static struct tree* tree_add_directory(struct tree*,const char*);
static int tree_add_file(struct tree*,struct file*);
+static int tree_entry_init_directory(struct tree_entry**,const char*);
int tree_add(struct tree *root, const char *path, struct file *to_add) {
char *str, *p, *prev;
if(NULL==root) { return -1; }
if(NULL==to_add) { return -1; }
- if((NULL==path)||(strlen(path)<=0)) { return -1; }
+ if((NULL==path)||(0==strlen(path))) { return -1; }
tree = root;
free(str);
return 1;
- clean:
+clean:
free(str);
return -1;
}
static struct tree* tree_add_directory(struct tree *root, const char *dirname) {
- struct tree *p, *prev, *next;
+ struct tree_entry *p, *prev, *next;
int i;
if(NULL==root) { return NULL; }
if(NULL==dirname) { return NULL; }
if(0==strlen(dirname)) { return NULL; }
- p = root->directories;
+ p = root->entries;
prev = NULL;
while(p!=NULL) {
if((i = strcmp(p->name,dirname))>=0) {
- if(i==0) { return p; }
+ if(i==0) { return p->children; }
break;
}
p = p->next;
}
- if(tree_init(&next,dirname)<0) { return NULL; }
+ if(tree_entry_init_directory(&next,dirname)<0) { return NULL; }
if(prev!=NULL) {
prev->next = next;
} else {
- root->directories = next;
+ root->entries = next;
}
next->next = p;
- return next;
-}
-
-ssize_t tree_bencode(struct tree *tree, uint8_t *buf, size_t buf_len) {
- return -1;
+ return next->children;
}
-void tree_entry_free(struct tree_entry *p) {
- if(NULL==p) { return; }
-
- file_free(p->file);
- tree_entry_free(p->next);
-
- free(p);
-}
-
-int tree_entry_init(struct tree_entry **p, struct file *file) {
- if(NULL==p) { return -1; }
- if(NULL==file) { return -1; }
-
- (*p) = malloc(sizeof(struct tree_entry));
- if(NULL==(*p)) { return -1; }
-
- (*p)->file = file;
- (*p)->next = NULL;
-
- return 1;
-};
-
static int tree_add_file(struct tree *root, struct file *to_add) {
struct tree_entry *p, *prev, *next;
int i;
if(NULL==root) { return -1; }
if(NULL==to_add) { return -1; }
- p = root->files;
+ p = root->entries;
prev = NULL;
while(p!=NULL) {
- if((i = strcmp(p->file->name,to_add->name))>=0) {
- if(i==0) { return 0; }
+ if((i = strcmp(p->name,to_add->name))>=0) {
+ if(i==0) {
+ if(p->children!=NULL) { return -1; }
+ return 0;
+ }
break;
}
if(prev!=NULL) {
prev->next = next;
} else {
- root->files = next;
+ root->entries = next;
}
next->next = p;
return 1;
}
-void tree_free(struct tree *p) {
+ssize_t tree_bencode(struct tree *tree, uint8_t *buf, size_t buf_len) {
+ struct tree_entry *p;
+
+ p = tree->entries;
+ while(p!=NULL) {
+ if(p->children!=NULL) {
+ // write directory
+ // recurse into subdirectory
+ } else {
+ // write file
+ }
+ }
+
+ return -1;
+}
+
+void tree_entry_free(struct tree_entry *p) {
if(NULL==p) { return; }
+
+ if(NULL!=p->file) { file_free(p->file); }
+ if(NULL!=p->children) { tree_free(p->children); }
+
+ tree_entry_free(p->next);
+
+ free(p);
+}
- if(p->name!=NULL) {
- free(p->name);
+int tree_entry_init(struct tree_entry **p, struct file *file) {
+ if(NULL==p) { return -1; }
+ if(NULL==file) { return -1; }
+
+ (*p) = malloc(sizeof(struct tree_entry));
+ if(NULL==(*p)) { return -1; }
+
+ (*p)->name = strdup(file->name);
+ if(NULL==(*p)->name) {
+ free(p);
+ return -1;
}
- tree_entry_free(p->files);
- tree_free(p->directories);
- tree_free(p->next);
+ (*p)->children = NULL;
+ (*p)->file = file;
+ (*p)->next = NULL;
+
+ return 1;
+};
+
+static int tree_entry_init_directory(struct tree_entry **p, const char *dirname) {
+ return -1;
+}
+
+void tree_free(struct tree *p) {
+ if(NULL==p) { return; }
+
+ tree_entry_free(p->entries);
free(p);
}
-int tree_init(struct tree **p, const char *name) {
+int tree_init(struct tree **p) {
if(NULL==p) { return -1; }
(*p) = malloc(sizeof(struct tree));
return -1;
}
- if(name!=NULL) {
- (*p)->name = strdup(name);
- } else {
- (*p)->name = NULL;
- }
-
- (*p)->files = NULL;
- (*p)->directories = NULL;
- (*p)->next = NULL;
+ (*p)->entries = NULL;
+ (*p)->watch_fd = -1;
return 1;
}
#define WATCH_FLAGS IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO
static int watch_spawn_all(int fd, const char *root, struct tree *tree) {
- struct tree *p;
+ struct tree_entry *p;
if(fd<0) { return -1; }
if(NULL==root) { return -1; }
log_info(WATCH_MESSAGE_START,root);
- p = tree->next;
+ p = tree->entries;
while(p!=NULL) {
- if(watch_spawn_all(fd,root,p)<0) { return -1; }
- p = p->next;
- }
-
- p = tree->directories;
- while(p!=NULL) {
- char *newroot = concat(root,p->name);
-
- if(watch_spawn_all(fd,newroot,p)<0) {
+ if(p->children!=NULL) {
+ char *newroot = concat(root,p->name);
+ if(watch_spawn_all(fd,newroot,p->children)<0) { return -1; }
free(newroot);
- return -1;
}
-
- free(newroot);
p = p->next;
}
static int tree_deep_equals(struct tree *a, struct tree *b);
static void tree_add_basic_test();
static void tree_add_extended_test();
+static void tree_bencode_basic_test();
static void tree_init_basic_test();
int main() {
tree_init_basic_test();
tree_add_basic_test();
+ tree_bencode_basic_test();
tree_add_extended_test();
clean_env();
tree_free(tree);
}
+static void tree_bencode_basic_test() {
+ struct tree *root;
+ struct file *file;
+ uint8_t buf[100];
+ char fileA_path[] = "dir1/dir2/fileA.txt";
+ uint8_t expected[] = "d4:dir1d4:dir2d9:fileA.txtd0:d5:lengthi102411:pieces root32:00000000000000000000000000000000eeeee";
+
+ assert(1==tree_init(&root,NULL));
+
+ assert(1==file_init(&file,fileA_path));
+ memset(file->root,48,crypto_hash_sha256_BYTES);
+
+ assert(45==tree_bencode(root,buf,100));
+
+ assert(memcmp(buf,expected,45)==0);
+}
+
static void tree_init_basic_test() {
struct tree *p;