X-Git-Url: http://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Futils.c;h=8ae984836595d1271b0a38a8809e2e5f5e0d88b9;hp=b982bd5936149ab7667e64176a9f0c499dfd0207;hb=94dd71b967efa722af734ee067025ef11011a3c8;hpb=dab4fc145b926b13e1d1ba6c6047a1e2a9c46002 diff --git a/src/bin/lttng/utils.c b/src/bin/lttng/utils.c index b982bd593..8ae984836 100644 --- a/src/bin/lttng/utils.c +++ b/src/bin/lttng/utils.c @@ -18,12 +18,61 @@ #define _GNU_SOURCE #include #include +#include #include #include "conf.h" #include "utils.h" +/* + * Return the realpath(3) of the path even if the last directory token does not + * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the + * /tmp/test1 does, the real path is returned. In normal time, realpath(3) + * fails if the end point directory does not exist. + */ +char *expand_full_path(const char *path) +{ + const char *end_path = path; + char *next, *cut_path, *expanded_path; + + /* Find last token delimited by '/' */ + while ((next = strpbrk(end_path + 1, "/"))) { + end_path = next; + } + + /* Cut last token from original path */ + cut_path = strndup(path, end_path - path); + + expanded_path = malloc(PATH_MAX); + if (expanded_path == NULL) { + goto error; + } + + expanded_path = realpath((char *)cut_path, expanded_path); + if (expanded_path == NULL) { + switch (errno) { + case ENOENT: + ERR("%s: No such file or directory", cut_path); + break; + default: + perror("realpath"); + break; + } + goto error; + } + + /* Add end part to expanded path */ + strcat(expanded_path, end_path); + + free(cut_path); + return expanded_path; + +error: + free(cut_path); + return NULL; +} + /* * get_session_name *