]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
util: Create a function to create relative symlinks
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 2 Dec 2023 12:36:19 +0000 (12:36 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 2 Dec 2023 12:36:19 +0000 (12:36 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/util.c

index 2fea79211cc1a63f66fe7efd6ca3e98320dae2a2..ff552ed92792a7d49a89e7671624e0793ece3e56 100644 (file)
@@ -69,6 +69,7 @@ int pakfire_mkparentdir(const char* path, mode_t mode);
 int pakfire_mkdir(const char* path, mode_t mode);
 FILE* pakfire_mktemp(char* path, const mode_t mode);
 char* pakfire_mkdtemp(char* path);
+int pakfire_symlink(struct pakfire_ctx* ctx, const char* target, const char* linkpath);
 int pakfire_rmtree(const char* path, int flags);
 
 #define pakfire_which(pakfire, path, what) \
index 2f0c8ef881f4ace4e0b32df50b74f62c93680832..6bb22195a9825e2f98a008099cf91c6e9a347fff 100644 (file)
@@ -550,6 +550,32 @@ char* pakfire_mkdtemp(char* path) {
        return mkdtemp(path);
 }
 
+int pakfire_symlink(struct pakfire_ctx* ctx, const char* target, const char* linkpath) {
+       char path[PATH_MAX];
+       int r;
+
+       // Find the dirname of the target
+       r = pakfire_path_dirname(path, target);
+       if (r)
+               return r;
+
+       // Make the symlink relative
+       r = pakfire_path_relative(path, path, linkpath);
+       if (r)
+               return r;
+
+       CTX_DEBUG(ctx, "Creating symlink %s -> %s (%s)\n", target, linkpath, path);
+
+       // Create the symlink
+       r = symlink(target, path);
+       if (r) {
+               CTX_ERROR(ctx, "Could not create symlink %s (%s)\n", target, path);
+               return r;
+       }
+
+       return 0;
+}
+
 static int _unlink(const char* path, const struct stat* stat,
                const int type, struct FTW* ftwbuf) {
        // Delete directories using rmdir()