mirror of
https://github.com/troglobit/finit.git
synced 2026-10-01 05:22:48 +07:00
tmpfiles: refactor code into new is_dir_empty function
The existing implementation seemed useful enough to warrant a new helper function. Care was taken to ensure the revised code no longer suffers any memory leaks.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#define FINIT_HELPERS_H_
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
@@ -206,6 +207,22 @@ static inline char *fgetval(const char *line, const char *key, char *sep)
|
||||
return realloc(copy, len);
|
||||
}
|
||||
|
||||
static inline int is_dir_empty(const char *path)
|
||||
{
|
||||
struct dirent **namelist;
|
||||
int num;
|
||||
|
||||
num = scandir(path, &namelist, NULL, NULL);
|
||||
if (num < 0)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < num; i++)
|
||||
free(namelist[i]);
|
||||
free(namelist);
|
||||
|
||||
return num >= 3;
|
||||
}
|
||||
|
||||
#endif /* FINIT_HELPERS_H_ */
|
||||
|
||||
/**
|
||||
|
||||
+2
-12
@@ -23,7 +23,6 @@
|
||||
|
||||
#include "config.h" /* Generated by configure script */
|
||||
|
||||
#include <dirent.h>
|
||||
#include <ftw.h>
|
||||
#include <getopt.h>
|
||||
#include <glob.h>
|
||||
@@ -394,17 +393,8 @@ static void tmpfiles(char *line)
|
||||
paste(buf, sizeof(buf), "/usr/share/factory", path);
|
||||
arg = buf;
|
||||
}
|
||||
if (!strc) {
|
||||
struct dirent **namelist;
|
||||
int num;
|
||||
|
||||
if (!S_ISDIR(st.st_mode))
|
||||
break;
|
||||
num = scandir(path, &namelist, NULL, NULL);
|
||||
free(namelist);
|
||||
if (num >= 3)
|
||||
break; /* not empty */
|
||||
}
|
||||
if (fisdir(path) && !is_dir_empty(path))
|
||||
break;
|
||||
if (fisdir(arg) && !fisslashdir(arg)) {
|
||||
size_t len = strlen(arg) + 2;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user