Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor walk_dir into a library function #402

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 32 additions & 0 deletions include/xbps_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*-
* Copyright (c) 2021 Érico Nogueira Rolim.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*-
*/

#include <dirent.h>

#include "xbps_api_impl.h"

int xbps_walk_dir(const char *,
int (*fn) (const char *, const struct stat *, const struct dirent *));
1 change: 1 addition & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ OBJS += repo.o repo_sync.o
OBJS += rpool.o cb_util.o proplib_wrapper.o
OBJS += package_alternatives.o
OBJS += conf.o log.o
OBJS += walk_dir.o
OBJS += $(EXTOBJS) $(COMPAT_OBJS)
# unnecessary unless pkgdb format changes
# OBJS += pkgdb_conversion.o
Expand Down
83 changes: 83 additions & 0 deletions lib/walk_dir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*-
* Copyright (c) 2021 Érico Nogueira Rolim.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>

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

int
xbps_walk_dir(const char *path,
int (*fn) (const char *, const struct stat *, const struct dirent *))
{
int rv, i, count;
struct dirent **list;
char tmp_path[PATH_MAX] = { 0 };
struct stat sb;

count = scandir(path, &list, NULL, alphasort);
for (i = count - 1; i >= 0; i--) {
if (strcmp(list[i]->d_name, ".") == 0 || strcmp(list[i]->d_name, "..") == 0)
goto cleanup;
if (strlen(path) + strlen(list[i]->d_name) + 1 >= PATH_MAX - 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>, as terminating null is already subtracted from PATH_MAX?

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 (xbps_walk_dir(tmp_path, fn) < 0) {
rv = -1;
break;
}
}

rv = fn(tmp_path, &sb, list[i]);
if (rv != 0) {
break;

cleanup:
free(list[i]);
}

}
for (; i >= 0; i--) {
free(list[i]);
}
Comment on lines +73 to +80
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this doesn't double free, should check first before merging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you refer to breaking out of previous loop: it is not.

free(list);
return rv;
}