Skip to content
Snippets Groups Projects
Commit 6deda5c1 authored by Jakob Olsson's avatar Jakob Olsson
Browse files

dfs refactor and clean up

parent 05a048e0
No related branches found
No related tags found
No related merge requests found
#include "dfs.h" #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)) if (list_empty(stack))
INIT_LIST_HEAD(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)) if (list_empty(stack))
return NULL; return NULL;
n = list_first_entry(stack, struct node, list); dr = list_first_entry(stack, struct directory, list);
list_del(&n->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)) struct directory *dr;
INIT_LIST_HEAD(visited);
list_add(&n->list, visited);
}
bool is_visited(char *path, struct list_head *visited) // just visited
{
struct node *tmp;
list_for_each_entry(tmp, visited, list) { list_for_each_entry(dr, visited, list) {
if (strncmp(tmp->path, path, 1024) == 0) if (strncmp(dr->path, path, 1024) == 0)
return true; return true;
} }
return false; 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) { list_for_each_entry_safe(dr, tmp, list, list) {
free(n->path); free(dr->path);
list_del(&n->list); list_del(&dr->list);
free(n); 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; return;
list_for_each_entry(n, collection_of_nodes_and_stuff, list) { list_for_each_entry(dr, list, list) {
printf("path: %s\n", n->path); printf("path: %s\n", dr->path);
} }
} }
\ No newline at end of file
...@@ -2,16 +2,14 @@ ...@@ -2,16 +2,14 @@
#define DFS_H #define DFS_H
#include "common.h" #include "common.h"
struct node { struct directory {
struct list_head list; struct list_head list;
char path[PATH_MAX]; //inefficient space complexity with defined memory allocation? char *path;
char *name;
}; };
void stack_enqueue(struct node *n, struct list_head *stack); void enqueue(struct directory *n, struct list_head *stack);
struct node *stack_dequeue(struct list_head *stack); struct directory *dequeue(struct list_head *stack);
void add_visited(struct node *n, struct list_head *visited); bool search(char *path, struct list_head *visited);
bool is_visited(char *path, struct list_head *visited);
void clear_list(struct list_head *visited); void clear_list(struct list_head *visited);
void print_list_dfs(struct list_head *collection_of_nodes_and_stuff); void print_list_dfs(struct list_head *collection_of_nodes_and_stuff);
#endif #endif
\ No newline at end of file
...@@ -219,34 +219,39 @@ fail: ...@@ -219,34 +219,39 @@ fail:
return NULL; return NULL;
} }
char *dfs_get_path_name(char *folder_name) char *get_device_name(char *dir_name)
{ {
char *path, *name, new_path[PATH_MAX]; char *path, *name, tmp_path[PATH_MAX];
struct stat st; struct directory *dr, *sub_dr;
struct dirent *de; struct dirent *de;
DIR *dr; struct stat st;
struct node *n, *new_n; int path_len;
DIR *dir;
n = malloc(sizeof(struct node)); dr = malloc(sizeof(struct directory));
snprintf(n->path, PATH_MAX, "%s", folder_name); //remain allocated? if (!dr) {
stack_enqueue(n, &stack); 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)) { while (!list_empty(&stack)) {
n = stack_dequeue(&stack); dr = dequeue(&stack);
path = n->path; dir = opendir(dr->path);
dr = opendir(path); if (!dir) {
if (!dr) {
perror("opendir"); perror("opendir");
goto fail_opendir; goto fail_opendir;
} }
if (!is_visited(path, &visited)) { if (!search(dr->path, &visited)) {
add_visited(n, &visited); enqueue(dr, &visited);
while ((de = readdir(dr)) != NULL) { while ((de = readdir(dir)) != NULL) {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue; 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"); perror("fstatat");
continue; continue;
} }
...@@ -255,39 +260,61 @@ char *dfs_get_path_name(char *folder_name) ...@@ -255,39 +260,61 @@ char *dfs_get_path_name(char *folder_name)
continue; continue;
if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) { if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) {
snprintf(new_path, PATH_MAX, "%s/%s", path, de->d_name); path_len = strlen(dr->path) + strlen(de->d_name)+2;
if (!is_visited(new_path, &visited)) { path = calloc(path_len, 1);
new_n = malloc(sizeof(struct node)); if (!path) {
strncpy(new_n->path, new_path, (PATH_MAX-1)); perror("calloc");
//new_n->path= strdup(path); goto fail_calloc;
stack_enqueue(new_n, &stack);
} }
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; continue;
} }
if (strcmp(reverse_lexer(&path, '/'), "net") == 0) { if (strstr(dr->path, "net")) {
printf("de->d_name %s\n", de->d_name); printf("de->d_name %s\n", de->d_name);
name = strdup(de->d_name); name = strdup(de->d_name);
goto success; goto success;
} }
} }
} }
closedir(dr); closedir(dir);
} }
fail_dir:
//free(path);
fail_calloc:
clear_list(&visited); clear_list(&visited);
fail_opendir: fail_opendir:
//clear_list(&stack);
fail:
return NULL; return NULL;
success: 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; return name;
} }
char *get_interface_name(char *usb_path, char *sub_dir_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); 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) int get_devices(void)
...@@ -317,6 +344,7 @@ int get_devices(void) ...@@ -317,6 +344,7 @@ int get_devices(void)
continue; continue;
} }
name = get_interface_name(usb_path, de->d_name); name = get_interface_name(usb_path, de->d_name);
printf("returned name %s!\n", name);
if (!name) if (!name)
continue; continue;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment