From 2c7108c40abfb1f175391aa59cf1b07ab203e690 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 16 Jun 2010 01:56:00 +0200 Subject: [PATCH] util: introduce readlink_and_make_absolute() --- src/load-fragment.c | 12 +++--------- src/util.c | 20 ++++++++++++++++++++ src/util.h | 1 + 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/load-fragment.c b/src/load-fragment.c index 79b0893a..793c9525 100644 --- a/src/load-fragment.c +++ b/src/load-fragment.c @@ -1160,7 +1160,7 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) { * reached by a symlink. The old string will be freed. */ for (;;) { - char *target, *k, *name; + char *target, *name; if (c++ >= FOLLOW_MAX) return -ELOOP; @@ -1189,17 +1189,11 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) { return -errno; /* Hmm, so this is a symlink. Let's read the name, and follow it manually */ - if ((r = readlink_malloc(*filename, &target)) < 0) + if ((r = readlink_and_make_absolute(*filename, &target)) < 0) return r; - k = file_in_same_dir(*filename, target); - free(target); - - if (!k) - return -ENOMEM; - free(*filename); - *filename = k; + *filename = target; } if (!(f = fdopen(fd, "r"))) { diff --git a/src/util.c b/src/util.c index 7664df59..2bc90da3 100644 --- a/src/util.c +++ b/src/util.c @@ -582,6 +582,26 @@ int readlink_malloc(const char *p, char **r) { } } +int readlink_and_make_absolute(const char *p, char **r) { + char *target, *k; + int j; + + assert(p); + assert(r); + + if ((j = readlink_malloc(p, &target)) < 0) + return j; + + k = file_in_same_dir(p, target); + free(target); + + if (!k) + return -ENOMEM; + + *r = k; + return 0; +} + char *file_name_from_path(const char *p) { char *r; diff --git a/src/util.h b/src/util.h index efc993c3..cacc3969 100644 --- a/src/util.h +++ b/src/util.h @@ -139,6 +139,7 @@ int read_one_line_file(const char *fn, char **line); char *strappend(const char *s, const char *suffix); int readlink_malloc(const char *p, char **r); +int readlink_and_make_absolute(const char *p, char **r); char *file_name_from_path(const char *p); bool is_path(const char *p); -- 2.39.5