Skip to content
This repository was archived by the owner on Oct 23, 2021. It is now read-only.

Commit 19c1df2

Browse files
committed
Faster devs ()
1 parent 80d731b commit 19c1df2

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ add_executable (
2525
say.c
2626
sort.c
2727
)
28-
target_link_libraries (sudodev)
28+
target_link_libraries (sudodev pthread)
2929

src/devs.c

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,37 @@
2222
#include <unistd.h>
2323
#include <dirent.h>
2424
#include <errno.h>
25+
#include <pthread.h>
2526
#include <libgen.h>
2627
#include <sys/types.h>
2728
#include <sys/param.h>
2829
#include "chomp.h"
2930
#include "say.h"
3031
#include "find.h"
32+
#include "readfile.h"
3133
#include "config.h"
3234

35+
static char **list = NULL;
36+
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
37+
38+
/* ========================================================================== *
39+
* Determine whether a devPath is a local device
40+
* ========================================================================== */
3341
int
3442
isLocalDev (const char * const devPath)
3543
{
3644
char *pattern;
3745
char buff[MAXPATHLEN];
3846
saymode_t mode;
39-
int index;
47+
int index, index2;
4048
int status;
4149

4250
sayMode (&mode);
4351

4452
strncpy (buff, devPath, MAXPATHLEN - 1);
4553
pattern = basename (buff);
4654

55+
/* Remove partition number */
4756
for (index = strlen (pattern) - 1; index > -1; --index)
4857
{
4958
if (pattern[index] >= '0' && pattern[index] < '9' + 1)
@@ -52,15 +61,54 @@ isLocalDev (const char * const devPath)
5261
}
5362
}
5463

55-
/* TODO: Use a faster and none-threadsafe way to search FSTAB { */
64+
/* TODO: Use a faster way to search FSTAB { *
5665
if (-1 == (status = find (FSTAB, pattern)))
5766
{
5867
say (mode, MSG_E, "find failed\n");
5968
return -1;
6069
}
6170
6271
return status;
63-
/* } */
72+
* } */
73+
74+
if (!list)
75+
{
76+
pthread_mutex_lock (&mutex);
77+
78+
if (-1 == readfile (FSTAB, &list) || !list)
79+
{
80+
say (mode, MSG_E, "readfile failed\n");
81+
return -1;
82+
}
83+
84+
/* Remove all comments */
85+
for (index = 0; list[index]; ++index)
86+
{
87+
for (index2 = 0;
88+
list[index][index2] && '#' != list[index][index2];
89+
++index2);
90+
list[index][index2] = 0;
91+
}
92+
93+
pthread_mutex_unlock (&mutex);
94+
}
95+
96+
if (!list)
97+
{
98+
return -1;
99+
}
100+
101+
status = 0;
102+
for (index = 0; list[index]; ++index)
103+
{
104+
if (strstr (list[index], pattern))
105+
{
106+
status = 1;
107+
break;
108+
}
109+
}
110+
111+
return status;
64112
}
65113

66114
int
@@ -139,9 +187,20 @@ devs (char ***addr)
139187
uuids[no] = NULL;
140188
}
141189

190+
*addr = uuids;
191+
142192
closedir (dh);
193+
pthread_mutex_destroy (&mutex);
143194

144-
*addr = uuids;
195+
if (list)
196+
{
197+
for (count = 0; list[count]; ++count)
198+
{
199+
free (list[count]);
200+
}
201+
free (list);
202+
list = NULL;
203+
}
145204

146205
return 1;
147206
}

src/sudodev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ del (void)
438438

439439
if (!index)
440440
{
441-
say (mode, MSG_I, "no available device found\n");
441+
say (mode, MSG_I, "No available device found\n");
442442
error = 0;
443443
goto CLEAN;
444444
}

0 commit comments

Comments
 (0)