Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mdmngr
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
IOPSYS
mdmngr
Commits
6deda5c1
Commit
6deda5c1
authored
7 years ago
by
Jakob Olsson
Browse files
Options
Downloads
Patches
Plain Diff
dfs refactor and clean up
parent
05a048e0
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
dfs.c
+26
-29
26 additions, 29 deletions
dfs.c
dfs.h
+5
-7
5 additions, 7 deletions
dfs.h
dongle_infrastructure.c
+55
-27
55 additions, 27 deletions
dongle_infrastructure.c
with
86 additions
and
63 deletions
dfs.c
+
26
−
29
View file @
6deda5c1
#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
*
v
is
ited
)
void
clear_list
(
struct
list_head
*
l
is
t
)
{
{
struct
node
*
n
,
*
tmp
;
struct
directory
*
dr
,
*
tmp
;
print_list_dfs
(
list
);
if
(
list_empty
(
list
))
return
;
list_for_each_entry_safe
(
n
,
tmp
,
v
is
ited
,
list
)
{
list_for_each_entry_safe
(
dr
,
tmp
,
l
is
t
,
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
This diff is collapsed.
Click to expand it.
dfs.h
+
5
−
7
View file @
6deda5c1
...
@@ -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
This diff is collapsed.
Click to expand it.
dongle_infrastructure.c
+
55
−
27
View file @
6deda5c1
...
@@ -219,34 +219,39 @@ fail:
...
@@ -219,34 +219,39 @@ fail:
return
NULL
;
return
NULL
;
}
}
char
*
dfs_get_path
_name
(
char
*
folde
r_name
)
char
*
get_device
_name
(
char
*
di
r_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
(
d
i
r
))
!=
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
(
d
i
r
),
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
(
str
cmp
(
reverse_lexer
(
&
path
,
'/'
)
,
"net"
)
==
0
)
{
if
(
str
str
(
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
(
d
i
r
);
}
}
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
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment