]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
pakfire: Replace pakfire_make_path with something easier
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Aug 2022 16:16:17 +0000 (16:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Aug 2022 16:16:17 +0000 (16:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/compress.c
src/libpakfire/db.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/include/pakfire/string.h
src/libpakfire/jail.c
src/libpakfire/keystore.c
src/libpakfire/mount.c
src/libpakfire/pakfire.c
src/libpakfire/pwd.c
tests/libpakfire/archive.c

index fe4c96c747636bcc09ef112349e643e6ae5b64cd..92a6fde8dbb9fece5badf520f7b68b23cb61d28d 100644 (file)
@@ -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);
index 4d467147fe41f8ab7afafa937469d4127b9dfe06..0eeb8b32b00fad24a04c3947e2c57e11e733139f 100644 (file)
@@ -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;
        }
 
index 587d62b585194abb971e8abb4d18e3a007c96f73..a9f0342b0a73ea80d693e3992be9e259a99cf7e3 100644 (file)
@@ -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);
index 630ca10002e1ec5fb96b547d27594371c1dbd7a4..e626affaeeb37518037ba5cb2b05c4a316c5d5b6 100644 (file)
@@ -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);
 
index d2b83ed26e7b855c8d992c5bf78a4e4612b0222d..aa94eddaa57df767c56e927742fb0a116a456729 100644 (file)
@@ -23,6 +23,8 @@
 
 #ifdef PAKFIRE_PRIVATE
 
+#include <stdarg.h>
+
 /*
        Formats a string and stores it in the given buffer.
 */
index 1b3f23c2ed67159d071a3983344f6888a767807f..1de30827f025681c4a5d378292ff821dcf6cd15d 100644 (file)
@@ -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);
index 38247b5b8d00764ca2ec6506c4e5261001468ac0..bb55e2fa256c50bfd8c3471c099a6cc87fffd5e4 100644 (file)
@@ -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);
index b256a6759eef52c851d33450c7ac4855f91a7922..a94ebce5335b8432449065aca2fe8d7ff56d9d41 100644 (file)
@@ -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);
 
index 8fcd7a09f335dc190beee3cb25bdf4a7b84dac50..347cf4202343cf21e87f5cc4928a54eb3c8780e0 100644 (file)
@@ -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);
 }
index b3ce65635f42ddc225c2a09567216f792a738dc2..3adef05e8d81d6c27f2c886f94b8f70e91a46e76 100644 (file)
@@ -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");
index eac2ee9315e46f73917853526e812deccbf109c7..fd90dd77adb5b92e7ee676104012429f18ec8dc1 100644 (file)
@@ -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;