diff --git a/bin/xbps-create/main.c b/bin/xbps-create/main.c index 1ff0640cf..888300842 100644 --- a/bin/xbps-create/main.c +++ b/bin/xbps-create/main.c @@ -43,6 +43,7 @@ #include #include +#include "xbps_utils.h" #include "queue.h" #ifdef __clang__ @@ -510,48 +511,6 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED return 0; } -static int -walk_dir(const char *path, - int (*fn) (const char *, const struct stat *sb, const struct dirent *dir)) -{ - int rv, i; - struct dirent **list; - char tmp_path[PATH_MAX] = { 0 }; - struct stat sb; - - rv = scandir(path, &list, NULL, alphasort); - for (i = rv - 1; i >= 0; i--) { - if (strcmp(list[i]->d_name, ".") == 0 || strcmp(list[i]->d_name, "..") == 0) - continue; - if (strlen(path) + strlen(list[i]->d_name) + 1 >= PATH_MAX - 1) { - errno = ENAMETOOLONG; - rv = -1; - break; - } - strncpy(tmp_path, path, PATH_MAX - 1); - strncat(tmp_path, "/", PATH_MAX - 1 - strlen(tmp_path)); - strncat(tmp_path, list[i]->d_name, PATH_MAX - 1 - strlen(tmp_path)); - if (lstat(tmp_path, &sb) < 0) { - break; - } - - if (S_ISDIR(sb.st_mode)) { - if (walk_dir(tmp_path, fn) < 0) { - rv = -1; - break; - } - } - - rv = fn(tmp_path, &sb, list[i]); - if (rv != 0) { - break; - } - - } - free(list); - return rv; -} - static void process_xentry(const char *key, const char *mutable_files) { @@ -620,7 +579,7 @@ process_xentry(const char *key, const char *mutable_files) static void process_destdir(const char *mutable_files) { - if (walk_dir(".", ftw_cb) < 0) + if (xbps_walk_dir(".", ftw_cb) < 0) die("failed to process destdir files (nftw):"); /* Process regular files */ diff --git a/bin/xbps-uchroot/main.c b/bin/xbps-uchroot/main.c index db93e2887..20714ae28 100644 --- a/bin/xbps-uchroot/main.c +++ b/bin/xbps-uchroot/main.c @@ -50,12 +50,11 @@ #include #include #include /* PATH_MAX */ -#include #include #include -#include #include +#include "xbps_utils.h" #include "queue.h" #ifndef SECBIT_NOROOT @@ -115,7 +114,7 @@ die(const char *fmt, ...) } static int -ftw_cb(const char *fpath, const struct stat *sb) +ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED) { int sverrno = 0; @@ -132,52 +131,6 @@ ftw_cb(const char *fpath, const struct stat *sb) return 0; } -static int -walk_dir(const char *path, - int (*fn)(const char *fpath, const struct stat *sb)) -{ - struct dirent **list; - struct stat sb; - const char *p; - char tmp_path[PATH_MAX] = {0}; - int rv = 0, i; - - i = scandir(path, &list, NULL, alphasort); - if (i == -1) { - rv = -1; - goto out; - } - while (i--) { - p = list[i]->d_name; - if (strcmp(p, ".") == 0 || strcmp(p, "..") == 0) - continue; - if (strlen(path) + strlen(p) + 1 >= (PATH_MAX - 1)) { - errno = ENAMETOOLONG; - rv = -1; - break; - } - strncpy(tmp_path, path, PATH_MAX - 1); - strncat(tmp_path, "/", PATH_MAX - 1 - strlen(tmp_path)); - strncat(tmp_path, p, PATH_MAX - 1 - strlen(tmp_path)); - if (lstat(tmp_path, &sb) < 0) { - break; - } - if (S_ISDIR(sb.st_mode)) { - if (walk_dir(tmp_path, fn) < 0) { - rv = -1; - break; - } - } - rv = fn(tmp_path, &sb); - if (rv != 0) { - break; - } - } -out: - free(list); - return rv; -} - static void cleanup_overlayfs(void) { @@ -188,7 +141,7 @@ cleanup_overlayfs(void) goto out; /* recursively remove the temporary dir */ - if (walk_dir(tmpdir, ftw_cb) != 0) { + if (xbps_walk_dir(tmpdir, ftw_cb) != 0) { fprintf(stderr, "Failed to remove directory tree %s: %s\n", tmpdir, strerror(errno)); exit(EXIT_FAILURE);