]> git.ipfire.org Git - pakfire.git/commitdiff
path: Implement joining two paths
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 6 Oct 2023 13:50:44 +0000 (13:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 6 Oct 2023 13:50:44 +0000 (13:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
12 files changed:
src/libpakfire/build.c
src/libpakfire/cgroup.c
src/libpakfire/compress.c
src/libpakfire/include/pakfire/path.h
src/libpakfire/include/pakfire/util.h
src/libpakfire/jail.c
src/libpakfire/mount.c
src/libpakfire/pakfire.c
src/libpakfire/path.c
src/libpakfire/repo.c
src/libpakfire/util.c
tests/libpakfire/path.c

index a69c60aa8ea8603924a194ae72f944abbf34f2f0..30df7ac9e0173b2107d8df1882f68cbda7a5961c 100644 (file)
@@ -40,6 +40,7 @@
 #include <pakfire/package.h>
 #include <pakfire/packager.h>
 #include <pakfire/parser.h>
+#include <pakfire/path.h>
 #include <pakfire/private.h>
 #include <pakfire/problem.h>
 #include <pakfire/repo.h>
index a63a7c211807d6fa0a2c361257424500d963d6aa..8b76cbc67643413dde568e2efee5325f76fbda10 100644 (file)
@@ -31,6 +31,7 @@
 #include <pakfire/cgroup.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/path.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
 
index f850f72943ed899068a377abf70957dc8578574e..4e8a0928db9896d17bff567d5f9247922bf53a28 100644 (file)
@@ -31,6 +31,7 @@
 #include <pakfire/file.h>
 #include <pakfire/filelist.h>
 #include <pakfire/logging.h>
+#include <pakfire/path.h>
 #include <pakfire/progress.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
index 4390becbe2d7d42cabedfb68eadcdef28f04729c..e9b533c758dbc517dfa4f005af24a82b14ea12f8 100644 (file)
@@ -27,4 +27,8 @@
        __pakfire_path_normalize(path, sizeof(path))
 int __pakfire_path_normalize(char* p, const size_t length);
 
+#define pakfire_path_join(path, s1, s2) \
+       __pakfire_path_join(path, sizeof(path), s1, s2)
+int __pakfire_path_join(char* buffer, const size_t length, const char* s1, const char* s2);
+
 #endif /* PAKFIRE_PATH_H */
index 95b2383c8b4fbb197fe03ab0f52840fbe33307f3..7af64b5f65e0a9d9fd8e77735f0f32e6196da9e5 100644 (file)
@@ -63,10 +63,6 @@ char* __pakfire_hexlify(const unsigned char* digest, const size_t length);
 #define pakfire_unhexlify(dst, src) __pakfire_unhexlify(dst, sizeof(dst), src)
 int __pakfire_unhexlify(unsigned char* dst, const size_t l, const char* src);
 
-#define pakfire_path_join(dest, first, second) \
-       __pakfire_path_join(dest, sizeof(dest), first, second)
-int __pakfire_path_join(char* dest, const size_t length,
-       const char* first, const char* second);
 int pakfire_path_is_absolute(const char* path);
 const char* pakfire_path_relpath(const char* root, const char* path);
 
index 7b861d92935b52cc13e4a614b10f423cdaabe9f3..4a7af06e8edd6161def756a3f21849467455b8e7 100644 (file)
@@ -56,6 +56,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/mount.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/path.h>
 #include <pakfire/private.h>
 #include <pakfire/pwd.h>
 #include <pakfire/string.h>
index 403d421448e8488b656f46defd60c7f17061b013..ec9537a60b1698e244a77096c2632c596e483efe 100644 (file)
@@ -32,6 +32,7 @@
 #include <pakfire/arch.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/path.h>
 #include <pakfire/mount.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
index cd2614f860f1faadeb3a3ed979a0a57944e779d1..099ee927913e9a646c3d6882aa9a44bf75ec2681 100644 (file)
@@ -55,6 +55,7 @@
 #include <pakfire/packagelist.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/parser.h>
+#include <pakfire/path.h>
 #include <pakfire/private.h>
 #include <pakfire/pwd.h>
 #include <pakfire/repo.h>
index 2fa08cf28d469c9088ee0293666eb725a087a96a..16ac09b5698b8a2a910c361ce24c2b604e4cdf5e 100644 (file)
@@ -244,3 +244,42 @@ ERROR:
 
        return r;
 }
+
+int __pakfire_path_join(char* buffer, const size_t length, const char* s1, const char* s2) {
+       struct pakfire_path* path = NULL;
+       int r;
+
+       // Check inputs
+       if (!buffer || !length || !s1 || !s2)
+               return -EINVAL;
+
+       // Parse the path
+       r = pakfire_path_parse(&path, s1);
+       if (r)
+               goto ERROR;
+
+       // Skip any leading slashes
+       while (*s2 == '/')
+               s2++;
+
+       // Add the second part
+       r = pakfire_path_import_segments(path, s2);
+       if (r)
+               goto ERROR;
+
+       // Normalize the path
+       r = pakfire_path_do_normalize(path);
+       if (r)
+               goto ERROR;
+
+       // Write back the path
+       r = pakfire_path_to_string(path, buffer, length);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       if (path)
+               pakfire_path_free(path);
+
+       return r;
+}
index bc8833e6e19e034955e38294aadc7f5e0e5cfa83..05465fadc2a14b75c1d0092c480ae42e6b9e4a67 100644 (file)
@@ -42,6 +42,7 @@
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/path.h>
 #include <pakfire/private.h>
 #include <pakfire/progress.h>
 #include <pakfire/repo.h>
index c7b84dd8b4518cab747e7dcee58c849e55fc3c07..c06bf59ee7b38d8f4624ea7526d7fb9a431c54e0 100644 (file)
@@ -79,21 +79,6 @@ char* pakfire_unquote_in_place(char* s) {
        return s;
 }
 
-int __pakfire_path_join(char* dest, const size_t length,
-               const char* first, const char* second) {
-       if (!first)
-               return __pakfire_string_format(dest, length, "%s", second);
-
-       if (!second)
-               return __pakfire_string_format(dest, length, "%s", first);
-
-       // Remove leading slashes from second argument
-       while (*second == '/')
-               second++;
-
-       return __pakfire_string_format(dest, length, "%s/%s", first, second);
-}
-
 int pakfire_path_is_absolute(const char* path) {
        if (!path) {
                errno = EINVAL;
index 4c9218d635e6c5eca5ec60269e7cea6907a3054c..b7ba771c7183c6cff9a99fb89a024af8699243c5 100644 (file)
@@ -58,8 +58,27 @@ FAIL:
        return EXIT_FAILURE;
 }
 
+static int test_path_join(const struct test* t) {
+       char path[PATH_MAX];
+
+       ASSERT_SUCCESS(pakfire_path_join(path, "/usr/bin", "bash"));
+       ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
+
+       ASSERT_SUCCESS(pakfire_path_join(path, "/usr/bin", "/bash"));
+       ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
+
+       ASSERT_SUCCESS(pakfire_path_join(path, "/usr/bin/sh", "../bash"));
+       ASSERT_STRING_EQUALS(path, "/usr/bin/bash");
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
 int main(int argc, const char* argv[]) {
        testsuite_add_test(test_path_normalize);
+       testsuite_add_test(test_path_join);
 
        return testsuite_run(argc, argv);
 }