Skip to content

Commit 6363562

Browse files
authored
Merge pull request #1331 from jan-cerny/refactor_fsdev
Refactor and remove dead code from FSDEV module
2 parents c4c4e3c + 95bb51c commit 6363562

File tree

4 files changed

+21
-141
lines changed

4 files changed

+21
-141
lines changed

src/OVAL/probes/findfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ int find_files(SEXP_t * spath, SEXP_t * sfilename, SEXP_t * behaviors,
122122
stmp = probe_ent_getattrval(behaviors, "recurse_file_system");
123123

124124
if (stmp && !SEXP_strncmp(stmp, "local", 6)) {
125-
if ((setting->dev_list = fsdev_init(NULL, 0)) == NULL) {
125+
if ((setting->dev_list = fsdev_init()) == NULL) {
126126
SEXP_free(stmp);
127127
goto error;
128128
}

src/OVAL/probes/fsdev.c

Lines changed: 18 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -73,45 +73,6 @@ static int fsdev_cmp(const void *a, const void *b)
7373
return memcmp(a, b, sizeof(dev_t));
7474
}
7575

76-
/**
77-
* Compare two strings.
78-
*/
79-
static int fsname_cmp(const void *a, const void *b)
80-
{
81-
return strcmp(a, b);
82-
}
83-
84-
/**
85-
* Search for a filesystem name in a sorted array using binary search.
86-
* @param fsname name
87-
* @param fs_arr sorted array of filesystem names
88-
* @param fs_cnt number of names in the array
89-
* @retval 1 if found
90-
* @retval 0 otherwise
91-
*/
92-
static int match_fs(const char *fsname, const char **fs_arr, size_t fs_cnt)
93-
{
94-
size_t w, s;
95-
int cmp;
96-
97-
w = fs_cnt;
98-
s = 0;
99-
100-
while (w > 0) {
101-
cmp = fsname_cmp(fsname, fs_arr[s + w / 2]);
102-
if (cmp > 0) {
103-
s += w / 2 + 1;
104-
w = w - w / 2 - 1;
105-
} else if (cmp < 0) {
106-
w = w / 2;
107-
} else {
108-
return (1);
109-
}
110-
}
111-
112-
return (0);
113-
}
114-
11576
#if defined(__linux__) || defined(_AIX)
11677

11778
#define DEVID_ARRAY_SIZE 16
@@ -197,7 +158,7 @@ int is_local_fs(struct mntent *ment)
197158

198159
#endif /* _AIX */
199160

200-
static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
161+
static fsdev_t *__fsdev_init(fsdev_t *lfs)
201162
{
202163
int e;
203164
FILE *fp;
@@ -228,12 +189,8 @@ static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
228189
i = 0;
229190

230191
while ((ment = getmntent(fp)) != NULL) {
231-
if (fs == NULL) {
232-
if (!is_local_fs(ment))
233-
continue;
234-
} else if (!match_fs(ment->mnt_type, fs, fs_cnt)) {
235-
continue;
236-
}
192+
if (!is_local_fs(ment))
193+
continue;
237194
if (stat(ment->mnt_dir, &st) != 0)
238195
continue;
239196
if (i >= lfs->cnt) {
@@ -251,7 +208,7 @@ static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
251208
return (lfs);
252209
}
253210
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
254-
static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
211+
static fsdev_t *__fsdev_init(fsdev_t *lfs)
255212
{
256213
struct statfs *mntbuf = NULL;
257214
struct stat st;
@@ -260,20 +217,11 @@ static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
260217
lfs->cnt = getmntinfo(&mntbuf, (fs == NULL ? MNT_LOCAL : 0) | MNT_NOWAIT);
261218
lfs->ids = malloc(sizeof(dev_t) * lfs->cnt);
262219

263-
if (fs == NULL) {
264-
for (i = 0; i < lfs->cnt; ++i) {
265-
if (stat(mntbuf[i].f_mntonname, &st) != 0)
266-
continue;
267-
268-
memcpy(&(lfs->ids[i]), &st.st_dev, sizeof(dev_t));
269-
}
270-
} else {
271-
for (i = 0; i < lfs->cnt; ++i) {
272-
if (!match_fs(mntbuf[i].f_fstypename, fs, fs_cnt))
273-
continue;
220+
for (i = 0; i < lfs->cnt; ++i) {
221+
if (stat(mntbuf[i].f_mntonname, &st) != 0)
222+
continue;
274223

275-
memcpy(&(lfs->ids[i]), &st.st_dev, sizeof(dev_t));
276-
}
224+
memcpy(&(lfs->ids[i]), &st.st_dev, sizeof(dev_t));
277225
}
278226

279227
if (i != lfs->cnt) {
@@ -288,7 +236,7 @@ static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
288236
#define DEVID_ARRAY_SIZE 16
289237
#define DEVID_ARRAY_ADD 8
290238

291-
static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
239+
static fsdev_t *__fsdev_init(fsdev_t *lfs)
292240
{
293241
int e;
294242
FILE *fp;
@@ -318,31 +266,16 @@ static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
318266
lfs->cnt = DEVID_ARRAY_SIZE;
319267
i = 0;
320268

321-
if (fs == NULL) {
322-
while ((getmntent(fp, &mentbuf)) == 0) {
323-
/* TODO: Is this check reliable? */
324-
if (stat (mentbuf.mnt_special, &st) == 0 && (st.st_mode & S_IFCHR)) {
325-
326-
if (i >= lfs->cnt) {
327-
lfs->cnt += DEVID_ARRAY_ADD;
328-
lfs->ids = realloc(lfs->ids, sizeof(dev_t) * lfs->cnt);
329-
}
269+
while ((getmntent(fp, &mentbuf)) == 0) {
270+
/* TODO: Is this check reliable? */
271+
if (stat(mentbuf.mnt_special, &st) == 0 && (st.st_mode & S_IFCHR)) {
330272

331-
memcpy(&(lfs->ids[i++]), &st.st_dev, sizeof(dev_t));
273+
if (i >= lfs->cnt) {
274+
lfs->cnt += DEVID_ARRAY_ADD;
275+
lfs->ids = realloc(lfs->ids, sizeof(dev_t) * lfs->cnt);
332276
}
333-
}
334-
} else {
335-
while ((getmntent(fp, &mentbuf)) == 0) {
336-
337-
if (match_fs(mentbuf.mnt_fstype, fs, fs_cnt)) {
338-
339-
if (i >= lfs->cnt) {
340-
lfs->cnt += DEVID_ARRAY_ADD;
341-
lfs->ids = realloc(lfs->ids, sizeof(dev_t) * lfs->cnt);
342-
}
343277

344-
memcpy(&(lfs->ids[i++]), &st.st_dev, sizeof(dev_t));
345-
}
278+
memcpy(&(lfs->ids[i++]), &st.st_dev, sizeof(dev_t));
346279
}
347280
}
348281

@@ -355,7 +288,7 @@ static fsdev_t *__fsdev_init(fsdev_t * lfs, const char **fs, size_t fs_cnt)
355288
}
356289
#endif
357290

358-
fsdev_t *fsdev_init(const char **fs, size_t fs_cnt)
291+
fsdev_t *fsdev_init()
359292
{
360293
fsdev_t *lfs;
361294

@@ -364,7 +297,7 @@ fsdev_t *fsdev_init(const char **fs, size_t fs_cnt)
364297
if (lfs == NULL)
365298
return (NULL);
366299

367-
if (__fsdev_init(lfs, fs, fs_cnt) == NULL)
300+
if (__fsdev_init(lfs) == NULL)
368301
return (NULL);
369302

370303
if (lfs->ids != NULL && lfs->cnt > 1)
@@ -378,53 +311,6 @@ static inline int isfschar(int c)
378311
return (isalpha(c) || isdigit(c) || c == '-' || c == '_');
379312
}
380313

381-
fsdev_t *fsdev_strinit(const char *fs_names)
382-
{
383-
fsdev_t *lfs;
384-
char *pstr, **fs_arr;
385-
size_t fs_cnt;
386-
int state, e;
387-
388-
pstr = strdup(fs_names);
389-
state = 0;
390-
fs_arr = NULL;
391-
fs_cnt = 0;
392-
393-
while (*pstr != '\0') {
394-
switch (state) {
395-
case 0:
396-
if (isfschar(*pstr)) {
397-
state = 1;
398-
++fs_cnt;
399-
fs_arr = realloc(fs_arr, sizeof(char *) * fs_cnt);
400-
fs_arr[fs_cnt - 1] = pstr;
401-
}
402-
403-
++pstr;
404-
405-
break;
406-
case 1:
407-
if (!isfschar(*pstr) && *pstr != '\0') {
408-
state = 0;
409-
*pstr = '\0';
410-
++pstr;
411-
}
412-
break;
413-
}
414-
}
415-
416-
if (fs_arr != NULL && fs_cnt > 0)
417-
qsort(fs_arr, fs_cnt, sizeof(char *), fsname_cmp);
418-
419-
lfs = fsdev_init((const char **)fs_arr, fs_cnt);
420-
e = errno;
421-
free(fs_arr);
422-
errno = e;
423-
free(pstr);
424-
425-
return (lfs);
426-
}
427-
428314
void fsdev_free(fsdev_t * lfs)
429315
{
430316
if (lfs != NULL) {

src/OVAL/probes/oval_fts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ OVAL_FTS *oval_fts_open_prefixed(const char *prefix, SEXP_t *path, SEXP_t *filen
878878
#if defined(__SVR4) && defined(__sun)
879879
ofts->localdevs = NULL;
880880
#else
881-
ofts->localdevs = fsdev_init(NULL, 0);
881+
ofts->localdevs = fsdev_init();
882882
if (ofts->localdevs == NULL) {
883883
dE("fsdev_init() failed.");
884884
/* One dummy read to get rid of an uninitialized

src/OVAL/probes/public/fsdev.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ typedef struct {
5252
* Initialize the fsdev_t structure from an array of filesystem
5353
* names.
5454
*/
55-
fsdev_t *fsdev_init(const char **fs, size_t fs_cnt);
56-
57-
/**
58-
* Initialize the fsdev_t structure from a string containing filesystem
59-
* names.
60-
*/
61-
fsdev_t *fsdev_strinit(const char *fs_names);
55+
fsdev_t *fsdev_init(void);
6256

6357
/**
6458
* Free the fsdev_t structure.

0 commit comments

Comments
 (0)