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

improvements to dfs for device name

parent ac41f384
Branches
No related tags found
No related merge requests found
......@@ -200,7 +200,7 @@ fail:
char *get_device_name(char *dir_name)
{
char path[PATH_MAX], *name;
char path[PATH_MAX], *name = NULL;
struct directory *dr, *sub_dr;
struct dirent *de;
struct stat st;
......@@ -215,65 +215,55 @@ char *get_device_name(char *dir_name)
dr->path = strdup(dir_name);
enqueue(dr, &stack);
while (!list_empty(&stack)) {
while (!list_empty(&stack) && !name) {
dr = dequeue(&stack);
dir = opendir(dr->path);
if (!dir) {
perror("opendir");
goto fail_opendir;
}
if (!dir)
continue;
if (!search(dr->path, &visited)) {
enqueue(dr, &visited);
if (!search(dr->path, &visited))
continue;
while ((de = readdir(dir)) != NULL) {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
enqueue(dr, &visited);
if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
perror("fstatat");
continue;
}
while ((de = readdir(dir)) != NULL) {
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
if (!S_ISDIR(st.st_mode))
continue;
if (fstatat(dirfd(dir), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
perror("fstatat");
continue;
}
if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb")) {
snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name);
if (!S_ISDIR(st.st_mode))
continue;
if (!search(path, &visited)) {
sub_dr = malloc(sizeof(struct directory));
if (!sub_dr) {
perror("malloc");
goto fail_malloc;
}
if (!strstr(dr->path, "net")) {
snprintf(path, PATH_MAX, "%s%s/", dr->path, de->d_name);
sub_dr->path = strdup(path);
enqueue(sub_dr, &stack);
}
continue;
sub_dr = malloc(sizeof(struct directory));
if (!sub_dr) {
perror("malloc");
goto fail_malloc;
}
if (strstr(dr->path, "net")) {
name = strdup(de->d_name);
goto success;
}
sub_dr->path = strdup(path);
enqueue(sub_dr, &stack);
continue;
}
if (!strstr(de->d_name, "eth") && !strstr(de->d_name, "usb"))
break;
name = strdup(de->d_name);
break;
}
closedir(dir);
}
fail_malloc:
clear_list(&visited);
closedir(dir);
fail_opendir:
clear_list(&stack);
fail:
return NULL;
success:
clear_list(&visited);
clear_list(&stack);
closedir(dir);
return name;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment