diff --git a/dfs.c b/dfs.c index b580cb54d7a23de8e0d1e629ed6d571a04677696..fafd1830a7ccb9b6e2b86db6b811ec76ccbcb62e 100644 --- a/dfs.c +++ b/dfs.c @@ -1,62 +1,59 @@ #include "dfs.h" -void stack_enqueue(struct node *n, struct list_head *stack) +void enqueue(struct directory *dr, struct list_head *stack) { if (list_empty(stack)) INIT_LIST_HEAD(stack); - list_add(&n->list, stack); + list_add(&dr->list, stack); } -struct node *stack_dequeue(struct list_head *stack) +struct directory *dequeue(struct list_head *stack) { - struct node *n; + struct directory *dr; if (list_empty(stack)) return NULL; - n = list_first_entry(stack, struct node, list); - list_del(&n->list); + dr = list_first_entry(stack, struct directory, list); + list_del(&dr->list); - return n; + return dr; } -void add_visited(struct node *n, struct list_head *visited) +bool search(char *path, struct list_head *visited) { - if (list_empty(visited)) - INIT_LIST_HEAD(visited); - list_add(&n->list, visited); -} - -bool is_visited(char *path, struct list_head *visited) // just visited -{ - struct node *tmp; + struct directory *dr; - list_for_each_entry(tmp, visited, list) { - if (strncmp(tmp->path, path, 1024) == 0) + list_for_each_entry(dr, visited, list) { + if (strncmp(dr->path, path, 1024) == 0) return true; } return false; } -void clear_list(struct list_head *visited) +void clear_list(struct list_head *list) { - struct node *n, *tmp; + struct directory *dr, *tmp; + print_list_dfs(list); + + if (list_empty(list)) + return; - list_for_each_entry_safe(n, tmp, visited, list) { - free(n->path); - list_del(&n->list); - free(n); + list_for_each_entry_safe(dr, tmp, list, list) { + free(dr->path); + list_del(&dr->list); + free(dr); } } -void print_list_dfs(struct list_head *collection_of_nodes_and_stuff) +void print_list_dfs(struct list_head *list) { - struct node *n; + struct directory *dr; - if (list_empty(collection_of_nodes_and_stuff)) + if (list_empty(list)) return; - list_for_each_entry(n, collection_of_nodes_and_stuff, list) { - printf("path: %s\n", n->path); + list_for_each_entry(dr, list, list) { + printf("path: %s\n", dr->path); } } \ No newline at end of file diff --git a/dfs.h b/dfs.h index f6d72c7a6e91169e986c0f38c8c43109cf13786c..e37636fc725b5c4ca2018b5c1fc77cccb68dbc12 100644 --- a/dfs.h +++ b/dfs.h @@ -2,16 +2,14 @@ #define DFS_H #include "common.h" -struct node { +struct directory { struct list_head list; - char path[PATH_MAX]; //inefficient space complexity with defined memory allocation? - char *name; + char *path; }; -void stack_enqueue(struct node *n, struct list_head *stack); -struct node *stack_dequeue(struct list_head *stack); -void add_visited(struct node *n, struct list_head *visited); -bool is_visited(char *path, struct list_head *visited); +void enqueue(struct directory *n, struct list_head *stack); +struct directory *dequeue(struct list_head *stack); +bool search(char *path, struct list_head *visited); void clear_list(struct list_head *visited); void print_list_dfs(struct list_head *collection_of_nodes_and_stuff); #endif \ No newline at end of file diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index e882fa931adbdaeeeade2932211c49796d026573..1992d63c89f0cb39f461557b2b21d46954116b9b 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -219,34 +219,39 @@ fail: return NULL; } -char *dfs_get_path_name(char *folder_name) +char *get_device_name(char *dir_name) { - char *path, *name, new_path[PATH_MAX]; - struct stat st; + char *path, *name, tmp_path[PATH_MAX]; + struct directory *dr, *sub_dr; struct dirent *de; - DIR *dr; - struct node *n, *new_n; + struct stat st; + int path_len; + DIR *dir; - n = malloc(sizeof(struct node)); - snprintf(n->path, PATH_MAX, "%s", folder_name); //remain allocated? - stack_enqueue(n, &stack); + dr = malloc(sizeof(struct directory)); + if (!dr) { + perror("malloc"); + goto fail; + } + dr->path = strdup(dir_name); + //snprintf(dr->path, PATH_MAX, "%s", dir_name); //remain allocated? + enqueue(dr, &stack); while (!list_empty(&stack)) { - n = stack_dequeue(&stack); - path = n->path; - dr = opendir(path); - if (!dr) { + dr = dequeue(&stack); + dir = opendir(dr->path); + if (!dir) { perror("opendir"); goto fail_opendir; } - if (!is_visited(path, &visited)) { - add_visited(n, &visited); - while ((de = readdir(dr)) != NULL) { + if (!search(dr->path, &visited)) { + enqueue(dr, &visited); + while ((de = readdir(dir)) != NULL) { if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; - if (fstatat(dirfd(dr), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { + if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { perror("fstatat"); continue; } @@ -255,39 +260,61 @@ char *dfs_get_path_name(char *folder_name) continue; if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) { - snprintf(new_path, PATH_MAX, "%s/%s", path, de->d_name); - if (!is_visited(new_path, &visited)) { - new_n = malloc(sizeof(struct node)); - strncpy(new_n->path, new_path, (PATH_MAX-1)); - //new_n->path= strdup(path); - stack_enqueue(new_n, &stack); + path_len = strlen(dr->path) + strlen(de->d_name)+2; + path = calloc(path_len, 1); + if (!path) { + perror("calloc"); + goto fail_calloc; } + + snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name); + if (!search(path, &visited)) { + sub_dr = malloc(sizeof(struct directory)); + if (!sub_dr) { + perror("malloc"); + goto fail_dir; + } + sub_dr->path = path; + enqueue(sub_dr, &stack); + } else + free(path); continue; } - if (strcmp(reverse_lexer(&path, '/'), "net") == 0) { + if (strstr(dr->path, "net")) { printf("de->d_name %s\n", de->d_name); name = strdup(de->d_name); goto success; } } } - closedir(dr); + closedir(dir); } +fail_dir: + //free(path); +fail_calloc: clear_list(&visited); fail_opendir: + //clear_list(&stack); +fail: return NULL; success: - closedir(dr); + printf("name %s\n", name); + clear_list(&visited); + printf("name %s\n", name); + clear_list(&stack); + printf("name %s\n", name); + closedir(dir); return name; } char *get_interface_name(char *usb_path, char *sub_dir_name) { - char path[PATH_MAX], *name; + char path[PATH_MAX]; snprintf(path, PATH_MAX, "%s/%s/", usb_path, sub_dir_name); - return dfs_get_path_name(path); + + return get_device_name(path); } int get_devices(void) @@ -317,6 +344,7 @@ int get_devices(void) continue; } name = get_interface_name(usb_path, de->d_name); + printf("returned name %s!\n", name); if (!name) continue;