From: Michael Tremer Date: Thu, 18 Aug 2022 16:16:17 +0000 (+0000) Subject: pakfire: Replace pakfire_make_path with something easier X-Git-Tag: 0.9.28~447 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=77e26129d0a10fe32c1e7e61af20d7edca5ebc75;p=pakfire.git pakfire: Replace pakfire_make_path with something easier Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index fe4c96c74..92a6fde8d 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -127,9 +127,9 @@ static int pakfire_build_enable_repos(struct pakfire_build* build) { } // Make path for configuration file - r = pakfire_make_path(build->pakfire, repopath, - PAKFIRE_CONFIG_DIR "/repos/" PAKFIRE_REPO_LOCAL ".repo"); - if (r < 0) + r = pakfire_path(build->pakfire, repopath, + "%s", PAKFIRE_CONFIG_DIR "/repos/" PAKFIRE_REPO_LOCAL ".repo"); + if (r) goto ERROR; // Write repository configuration @@ -206,8 +206,8 @@ static int pakfire_build_find_dependencies(struct pakfire_build* build, char path[PATH_MAX]; // Allocate path to write the filelist to - int r = pakfire_make_path(build->pakfire, path, "/var/tmp/.pakfire-filelist.XXXXXX"); - if (r < 0) + int r = pakfire_path(build->pakfire, path, "%s", "/var/tmp/.pakfire-filelist.XXXXXX"); + if (r) return 1; // Create a temporary file @@ -302,7 +302,6 @@ static int pakfire_build_package_add_files(struct pakfire_build* build, struct pakfire_parser* makefile, const char* buildroot, const char* namespace, struct pakfire_package* pkg, struct pakfire_packager* packager) { struct pakfire_filelist* filelist = NULL; - char path[PATH_MAX]; int r = 1; char** files = NULL; @@ -316,9 +315,6 @@ static int pakfire_build_package_add_files(struct pakfire_build* build, if (!files) return 0; - // Convert to absolute path - pakfire_make_path(build->pakfire, path, buildroot); - // Split into includes and excludes for (char** file = files; *file; file++) { if (**file == '!') @@ -335,7 +331,7 @@ static int pakfire_build_package_add_files(struct pakfire_build* build, goto ERROR; // Scan for files - r = pakfire_filelist_scan(filelist, path, includes, excludes); + r = pakfire_filelist_scan(filelist, build->buildroot, includes, excludes); if (r) goto ERROR; @@ -404,8 +400,8 @@ static int pakfire_build_add_scriptlet_requires(struct pakfire_build* build, const char* root = pakfire_get_path(build->pakfire); // Make filename - r = pakfire_make_path(build->pakfire, path, "/var/tmp/.pakfire-scriptlet.XXXXXX"); - if (r < 0) + r = pakfire_path(build->pakfire, path, "%s", "/var/tmp/.pakfire-scriptlet.XXXXXX"); + if (r) return r; // Fetch scriptlet payload @@ -1141,7 +1137,6 @@ static int pakfire_build_init(struct pakfire_build* build) { static int pakfire_build_read_makefile(struct pakfire_build* build, struct pakfire_parser** parser, struct pakfire_package* package) { - char makefile[PATH_MAX]; char path[PATH_MAX]; int r; @@ -1151,12 +1146,7 @@ static int pakfire_build_read_makefile(struct pakfire_build* build, const char* name = pakfire_package_get_name(package); // Compose path to makefile - r = pakfire_string_format(makefile, "/usr/src/packages/%s/%s.nm", nevra, name); - if (r) - return r; - - // Make it absolute - r = pakfire_make_path(build->pakfire, path, makefile); + r = pakfire_path(build->pakfire, path, "/usr/src/packages/%s/%s.nm", nevra, name); if (r < 0) return 1; @@ -1233,7 +1223,9 @@ PAKFIRE_EXPORT int pakfire_build_exec(struct pakfire_build* build, const char* p int r; // Set buildroot - pakfire_make_path(build->pakfire, build->buildroot, "/var/tmp/.pakfire-buildroot.XXXXXX"); + r = pakfire_path(build->pakfire, build->buildroot, "%s", "/var/tmp/.pakfire-buildroot.XXXXXX"); + if (r) + goto ERROR; // Open source archive r = pakfire_archive_open(&archive, build->pakfire, path); diff --git a/src/libpakfire/compress.c b/src/libpakfire/compress.c index 4d467147f..0eeb8b32b 100644 --- a/src/libpakfire/compress.c +++ b/src/libpakfire/compress.c @@ -651,12 +651,15 @@ int pakfire_extract(struct pakfire* pakfire, struct archive* archive, }; // Set prefix (including pakfire path) - pakfire_make_path(pakfire, data.prefix, prefix); + r = pakfire_path(pakfire, data.prefix, "%s", prefix); + if (r) + goto ERROR; // Allocate writer data.writer = pakfire_make_archive_disk_writer(pakfire, 1); if (!data.writer) { ERROR(pakfire, "Could not create disk writer: %m\n"); + r = 1; goto ERROR; } diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index 587d62b58..a9f0342b0 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -647,8 +647,8 @@ int pakfire_db_open(struct pakfire_db** db, struct pakfire* pakfire, int flags) } // Make the filename - r = pakfire_make_path(o->pakfire, o->path, DATABASE_PATH); - if (r < 0) + r = pakfire_path(o->pakfire, o->path, "%s", DATABASE_PATH); + if (r) goto END; // Try to open the sqlite3 database file @@ -2112,8 +2112,8 @@ static int pakfire_db_load_file(struct pakfire_db* db, struct pakfire_filelist* pakfire_file_set_path(file, path); // Abspath - r = pakfire_make_path(db->pakfire, abspath, path); - if (r < 0) + r = pakfire_path(db->pakfire, abspath, "%s", path); + if (r) goto ERROR; pakfire_file_set_abspath(file, abspath); diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index 630ca1000..e626affae 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -160,9 +160,10 @@ const char* pakfire_get_distro_version_id(struct pakfire* pakfire); const char* pakfire_get_keystore_path(struct pakfire* pakfire); -#define pakfire_make_path(pakfire, dst, path) \ - __pakfire_make_path(pakfire, dst, sizeof(dst) - 1, path) -int __pakfire_make_path(struct pakfire* pakfire, char* dst, size_t length, const char* path); +#define pakfire_path(pakfire, path, format, ...) \ + __pakfire_path(pakfire, path, sizeof(path), format, __VA_ARGS__) +int __pakfire_path(struct pakfire* pakfire, char* path, const size_t length, + const char* format, ...) __attribute__((format(printf, 4, 5))); const char* pakfire_relpath(struct pakfire* pakfire, const char* path); diff --git a/src/libpakfire/include/pakfire/string.h b/src/libpakfire/include/pakfire/string.h index d2b83ed26..aa94eddaa 100644 --- a/src/libpakfire/include/pakfire/string.h +++ b/src/libpakfire/include/pakfire/string.h @@ -23,6 +23,8 @@ #ifdef PAKFIRE_PRIVATE +#include + /* Formats a string and stores it in the given buffer. */ diff --git a/src/libpakfire/jail.c b/src/libpakfire/jail.c index 1b3f23c2e..1de30827f 100644 --- a/src/libpakfire/jail.c +++ b/src/libpakfire/jail.c @@ -1632,9 +1632,9 @@ int pakfire_jail_ldconfig(struct pakfire* pakfire) { const char* ldconfig = "/sbin/ldconfig"; // Check if ldconfig exists before calling it to avoid overhead - int r = pakfire_make_path(pakfire, path, ldconfig); - if (r < 0) - return 1; + int r = pakfire_path(pakfire, path, "%s", ldconfig); + if (r) + return r; // Check if ldconfig is executable r = access(path, X_OK); diff --git a/src/libpakfire/keystore.c b/src/libpakfire/keystore.c index 38247b5b8..bb55e2fa2 100644 --- a/src/libpakfire/keystore.c +++ b/src/libpakfire/keystore.c @@ -58,8 +58,8 @@ static int pakfire_keystore_import(struct pakfire* pakfire, gpgme_ctx_t ctx) { char path[PATH_MAX]; // Make path - int r = pakfire_make_path(pakfire, path, "/etc/pakfire/trusted.keys.d"); - if (r < 0) + int r = pakfire_path(pakfire, path, "%s", "/etc/pakfire/trusted.keys.d"); + if (r) return r; DEBUG(pakfire, "Loading keys from %s\n", path); diff --git a/src/libpakfire/mount.c b/src/libpakfire/mount.c index b256a6759..a94ebce53 100644 --- a/src/libpakfire/mount.c +++ b/src/libpakfire/mount.c @@ -242,9 +242,9 @@ static int pakfire_populate_dev(struct pakfire* pakfire) { for (const struct pakfire_devnode* devnode = devnodes; devnode->path; devnode++) { DEBUG(pakfire, "Creating device node %s\n", devnode->path); - int r = pakfire_make_path(pakfire, path, devnode->path); - if (r < 0) - return 1; + int r = pakfire_path(pakfire, path, "%s", devnode->path); + if (r) + return r; dev_t dev = makedev(devnode->major, devnode->minor); @@ -282,9 +282,9 @@ MOUNT: for (const struct pakfire_symlink* s = symlinks; s->target; s++) { DEBUG(pakfire, "Creating symlink %s -> %s\n", s->path, s->target); - int r = pakfire_make_path(pakfire, path, s->path); - if (r < 0) - return 1; + int r = pakfire_path(pakfire, path, "%s", s->path); + if (r) + return r; r = symlink(s->target, path); if (r) { @@ -312,8 +312,8 @@ static int pakfire_mount_interpreter(struct pakfire* pakfire) { DEBUG(pakfire, "Mounting interpreter %s for %s\n", interpreter, arch); // Where to mount this? - int r = pakfire_make_path(pakfire, target, interpreter); - if (r < 0) + int r = pakfire_path(pakfire, target, "%s", interpreter); + if (r) return r; // Create directory @@ -389,9 +389,9 @@ int pakfire_bind(struct pakfire* pakfire, const char* src, const char* dst, int if (!dst) dst = src; - int r = pakfire_make_path(pakfire, mountpoint, dst); - if (r < 0) - return 1; + int r = pakfire_path(pakfire, mountpoint, "%s", dst); + if (r) + return r; DEBUG(pakfire, "Bind-mounting %s to %s\n", src, mountpoint); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 8fcd7a09f..347cf4202 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -391,10 +391,12 @@ static int pakfire_safety_checks(struct pakfire* pakfire) { static int pakfire_read_repo_config(struct pakfire* pakfire) { char path[PATH_MAX]; + int r; - int r = pakfire_make_path(pakfire, path, PAKFIRE_CONFIG_DIR "/repos"); - if (r < 0) - return 1; + // Make path absolute + r = pakfire_path(pakfire, path, "%s", PAKFIRE_CONFIG_DIR "/repos"); + if (r) + return r; DEBUG(pakfire, "Reading repository configuration from %s\n", path); @@ -529,12 +531,13 @@ static int pakfire_config_import_distro(struct pakfire* pakfire) { static int pakfire_read_config(struct pakfire* pakfire, const char* path) { char default_path[PATH_MAX]; + int r; // Use default path if none set if (!path) { - int r = pakfire_make_path(pakfire, default_path, PAKFIRE_CONFIG_DIR "/general.conf"); - if (r < 0) - return 1; + r = pakfire_path(pakfire, default_path, "%s", PAKFIRE_CONFIG_DIR "/general.conf"); + if (r) + return r; path = default_path; } @@ -552,7 +555,7 @@ static int pakfire_read_config(struct pakfire* pakfire, const char* path) { } // Read configuration from file - int r = pakfire_config_read(pakfire->config, f); + r = pakfire_config_read(pakfire->config, f); if (r) { ERROR(pakfire, "Could not parse configuration file %s: %m\n", path); goto ERROR; @@ -579,9 +582,9 @@ static int pakfire_read_os_release(struct pakfire* pakfire) { char* line = NULL; size_t l = 0; - int r = pakfire_make_path(pakfire, path, "/etc/os-release"); - if (r < 0) - return 1; + int r = pakfire_path(pakfire, path, "%s", "/etc/os-release"); + if (r) + return r; r = 1; @@ -656,6 +659,7 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, const char* arch, const char* conf, int flags, int loglevel, pakfire_log_callback log_callback, void* log_data) { char tempdir[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-root-XXXXXX"; + char private_dir[PATH_MAX]; int r = 1; // Reset pakfire pointer @@ -788,8 +792,8 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, DEBUG(p, " slogan = %s\n", p->distro.slogan); // Set lock path - r = pakfire_make_path(p, p->lock_path, LOCK_PATH); - if (r < 0) { + r = pakfire_path(p, p->lock_path, "%s", LOCK_PATH); + if (r) { ERROR(p, "Could not set lock path: %m\n"); goto ERROR; } @@ -811,19 +815,16 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, } // Set keystore path - r = pakfire_make_path(p, p->keystore_path, KEYSTORE_DIR); - if (r < 0) { + r = pakfire_path(p, p->keystore_path, "%s", KEYSTORE_DIR); + if (r) { ERROR(p, "Could not set keystore path: %m\n"); goto ERROR; } // Make path for private files - char private_dir[PATH_MAX]; - r = pakfire_make_path(p, private_dir, PAKFIRE_PRIVATE_DIR); - if (r < 0) { - r = 1; + r = pakfire_path(p, private_dir, "%s", PAKFIRE_PRIVATE_DIR); + if (r) goto ERROR; - } // Make sure that our private directory exists r = pakfire_mkdir(private_dir, 0755); @@ -946,12 +947,23 @@ const char* pakfire_get_keystore_path(struct pakfire* pakfire) { return pakfire->keystore_path; } -int __pakfire_make_path(struct pakfire* pakfire, char* dst, size_t length, const char* path) { - // Make sure that path never starts with / - while (path && *path == '/') - path++; +int __pakfire_path(struct pakfire* pakfire, char* path, const size_t length, + const char* format, ...) { + char buffer[PATH_MAX]; + va_list args; + int r; + + // Format input into buffer + va_start(args, format); + r = __pakfire_string_vformat(buffer, sizeof(buffer), format, args); + va_end(args); + + // Break on any errors + if (r) + return r; - return __pakfire_path_join(dst, length, pakfire->path, path); + // Join paths together + return __pakfire_path_join(path, length, pakfire->path, buffer); } const char* pakfire_relpath(struct pakfire* pakfire, const char* path) { @@ -1147,29 +1159,33 @@ ERROR: } PAKFIRE_EXPORT int pakfire_copy_in(struct pakfire* pakfire, const char* src, const char* dst) { + char path[PATH_MAX]; + int r; + if (pakfire_on_root(pakfire)) { errno = ENOTSUP; return 1; } - char path[PATH_MAX]; - int r = pakfire_make_path(pakfire, path, dst); - if (r < 0) - return 1; + r = pakfire_path(pakfire, path, "%s", dst); + if (r) + return r; return pakfire_copy(pakfire, src, path, PAKFIRE_COPY_IN); } PAKFIRE_EXPORT int pakfire_copy_out(struct pakfire* pakfire, const char* src, const char* dst) { + char path[PATH_MAX]; + int r; + if (pakfire_on_root(pakfire)) { errno = ENOTSUP; return 1; } - char path[PATH_MAX]; - int r = pakfire_make_path(pakfire, path, src); - if (r < 0) - return 1; + r = pakfire_path(pakfire, path, "%s", src); + if (r) + return r; return pakfire_copy(pakfire, path, dst, PAKFIRE_COPY_OUT); } diff --git a/src/libpakfire/pwd.c b/src/libpakfire/pwd.c index b3ce65635..3adef05e8 100644 --- a/src/libpakfire/pwd.c +++ b/src/libpakfire/pwd.c @@ -37,10 +37,11 @@ static struct passwd* pakfire_getpwent(struct pakfire* pakfire, int(*cmp)(struct passwd* entry, const void* value), const void* value) { struct passwd* entry = NULL; char path[PATH_MAX]; + int r; // Get path to /etc/passwd - int r = pakfire_make_path(pakfire, path, "/etc/passwd"); - if (r < 0) + r = pakfire_path(pakfire, path, "%s", "/etc/passwd"); + if (r) return NULL; FILE* f = fopen(path, "r"); @@ -97,10 +98,11 @@ static struct group* pakfire_getgrent(struct pakfire* pakfire, int(*cmp)(struct group* entry, const void* value), const void* value) { struct group* entry = NULL; char path[PATH_MAX]; + int r; // Get path to /etc/group - int r = pakfire_make_path(pakfire, path, "/etc/group"); - if (r < 0) + r = pakfire_path(pakfire, path, "%s", "/etc/group"); + if (r) return NULL; FILE* f = fopen(path, "r"); diff --git a/tests/libpakfire/archive.c b/tests/libpakfire/archive.c index eac2ee931..fd90dd77a 100644 --- a/tests/libpakfire/archive.c +++ b/tests/libpakfire/archive.c @@ -225,7 +225,7 @@ static int test_extract(const struct test* t) { ASSERT_SUCCESS(pakfire_archive_extract(archive)); // Check if test file from the archive exists - pakfire_make_path(t->pakfire, path, "/usr/bin/beep"); + pakfire_path(t->pakfire, path, "%s", "/usr/bin/beep"); ASSERT_SUCCESS(access(path, F_OK)); r = EXIT_SUCCESS;