Skip to content

Commit

Permalink
xbps-uchroot,xbps-create: use xbps_walk_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ericonr committed May 12, 2021
1 parent a499ae9 commit bd6cd1d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 93 deletions.
45 changes: 2 additions & 43 deletions bin/xbps-create/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <dirent.h>

#include <xbps.h>
#include "xbps_utils.h"
#include "queue.h"

#ifdef __clang__
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 */
Expand Down
53 changes: 3 additions & 50 deletions bin/xbps-uchroot/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@
#include <stdlib.h>
#include <sched.h>
#include <limits.h> /* PATH_MAX */
#include <ftw.h>
#include <signal.h>
#include <getopt.h>
#include <dirent.h>

#include <xbps.h>
#include "xbps_utils.h"
#include "queue.h"

#ifndef SECBIT_NOROOT
Expand Down Expand Up @@ -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;

Expand All @@ -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)
{
Expand All @@ -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);
Expand Down

0 comments on commit bd6cd1d

Please sign in to comment.