From: Michael Tremer Date: Sat, 2 Dec 2023 12:36:19 +0000 (+0000) Subject: util: Create a function to create relative symlinks X-Git-Tag: 0.9.30~1294 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c98867a2bf994dc0736e6774868a642607e69a7a;p=pakfire.git util: Create a function to create relative symlinks Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 2fea79211..ff552ed92 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -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) \ diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 2f0c8ef88..6bb22195a 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -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()