Skip to content

Commit

Permalink
Create xbps_walk_dir function.
Browse files Browse the repository at this point in the history
It's used in both xbps-create and xbps-uchroot. Even though neither
uses the "dirent" arg in fn, leave it in for possible future use.

This implementation also fixes a bug with the version of walk_dir in
those binaries where it leaks all the list[i] items. This version frees
them after using them and does a final cleanup in case the for loop
ended before going through all of them.

xbps-create requires this function to use alphasort, but xbps-uchroot
wouldn't. For implemenation simplicity, use alphasort in all cases.
  • Loading branch information
ericonr committed May 12, 2021
1 parent 4a5eb8d commit a499ae9
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
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 HIDDEN 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) {
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]);
}
free(list);
return rv;
}

0 comments on commit a499ae9

Please sign in to comment.