From dc6afe5c8e807fd6103749497dc3244a6ab7ef28 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 25 Aug 2023 13:21:08 +0000 Subject: [PATCH] pakfire: Remove using FTS when reading repository configuration Signed-off-by: Michael Tremer --- src/libpakfire/pakfire.c | 79 ++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index c60eef691..77b383fba 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -19,8 +19,8 @@ #############################################################################*/ #include +#include #include -#include #include #include #include @@ -444,7 +444,32 @@ static int pakfire_safety_checks(struct pakfire* pakfire) { return 0; } +static int pakfire_read_one_repo_config(struct pakfire* pakfire, DIR* dir, const char* path) { + FILE* f = NULL; + int fd = -1; + int r; + + // Open the file + fd = openat(dirfd(dir), path, O_CLOEXEC); + if (fd < 0) + return -errno; + + // Re-open as file handle + f = fdopen(fd, "r"); + if (!f) + return -errno; + + // Read the configuration + r = pakfire_config_read(pakfire->config, f); + + // Cleanup + fclose(f); + + return r; +} + static int pakfire_read_repo_config(struct pakfire* pakfire) { + struct dirent* entry = NULL; char path[PATH_MAX]; int r; @@ -455,47 +480,45 @@ static int pakfire_read_repo_config(struct pakfire* pakfire) { DEBUG(pakfire, "Reading repository configuration from %s\n", path); - char* paths[2] = { - path, NULL, - }; - r = 1; + // Open path + DIR* dir = opendir(path); + if (!dir) { + switch (errno) { + case ENOENT: + return 0; - FTS* d = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, NULL); - if (!d) - goto ERROR; + default: + ERROR(pakfire, "Could not open %s: %m\n", path); + return -errno; + } + } for (;;) { - FTSENT* fent = fts_read(d); - if (!fent) + entry = readdir(dir); + if (!entry) break; - // Only handle files - if (fent->fts_info & FTS_F) { - // Skip everything that doesn't end in .repo - if (!pakfire_string_endswith(fent->fts_name, ".repo")) - continue; - - DEBUG(pakfire, "Reading %s\n", fent->fts_path); + // Skip anything that isn't a regular file + if (entry->d_type & DT_REG) + continue; - FILE* f = fopen(fent->fts_path, "r"); - if (!f) - goto ERROR; + // Skip any files that don't end on .repo + if (!pakfire_path_match("*.repo", entry->d_name)) + continue; - // Parse the configuration file - r = pakfire_config_read(pakfire->config, f); - fclose(f); + // Read the configuration + r = pakfire_read_one_repo_config(pakfire, dir, entry->d_name); + if (r) + goto ERROR; - if (r) - goto ERROR; - } } // Success r = 0; ERROR: - if (d) - fts_close(d); + if (dir) + closedir(dir); return r; } -- 2.39.5