From c4b88bf23ffa6b906226f4474ff2bfdbd4f033ad Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 27 Apr 2016 14:47:59 +0200 Subject: [PATCH] utils: add nm_utils_read_link_absolute() --- src/nm-core-utils.c | 33 +++++++++++++++++++++++++++++++++ src/nm-core-utils.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 6581d6b711..90031646f1 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -1147,6 +1147,39 @@ nm_utils_find_helper(const char *progname, const char *try_first, GError **error /******************************************************************************************/ +/** + * nm_utils_read_link_absolute: + * @link_file: file name of the symbolic link + * @error: error reason in case of failure + * + * Uses to g_file_read_link()/readlink() to read the symlink + * and returns the result as absolute path. + **/ +char * +nm_utils_read_link_absolute (const char *link_file, GError **error) +{ + char *ln, *dirname, *ln_abs; + + ln = g_file_read_link (link_file, error); + if (!ln) + return NULL; + if (g_path_is_absolute (ln)) + return ln; + + dirname = g_path_get_dirname (link_file); + if (!g_path_is_absolute (link_file)) { + gs_free char *dirname_rel = dirname; + + dirname = g_build_filename (g_get_current_dir (), dirname_rel, NULL); + } + ln_abs = g_build_filename (dirname, ln, NULL); + g_free (dirname); + g_free (ln); + return ln_abs; +} + +/******************************************************************************************/ + #define MAC_TAG "mac:" #define INTERFACE_NAME_TAG "interface-name:" #define DEVICE_TYPE_TAG "type:" diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 3033c33a5f..7d9cc44b77 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -134,6 +134,8 @@ const char *nm_utils_find_helper (const char *progname, const char *try_first, GError **error); +char *nm_utils_read_link_absolute (const char *link_file, GError **error); + typedef enum { NM_MATCH_SPEC_NO_MATCH = 0, NM_MATCH_SPEC_MATCH = 1,