From fa287178ac714071ceacf8697bd36cc8a8a8da00 Mon Sep 17 00:00:00 2001 From: Rajesh Amaravathi Date: Thu, 10 May 2012 16:24:45 +0530 Subject: core: canonicalize paths canonicalize paths during add-brick, creation of volume, setting nfs.export-dir in volgen BUG: 789870 Change-Id: I1d3788ac850359b0def0457113831e825a475d58 Signed-off-by: Rajesh Amaravathi Reviewed-on: http://review.gluster.com/3315 Tested-by: Gluster Build System Reviewed-by: Anand Avati --- libglusterfs/src/common-utils.c | 52 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'libglusterfs/src/common-utils.c') diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index 737487d1d..c9c396762 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -1948,11 +1948,6 @@ out: return result; } -/* - * rounds up nr to next power of two. If nr is already a power of two, next - * power of two is returned. - */ - /* * rounds up nr to next power of two. If nr is already a power of two, next * power of two is returned. @@ -2092,3 +2087,50 @@ gf_strip_whitespace (char *str, int len) GF_FREE (new_str); return new_len; } + +/* If the path exists use realpath(3) to handle extra slashes and to resolve + * symlinks else strip the extra slashes in the path and return */ + +int +gf_canonicalize_path (char *path) +{ + int ret = -1; + int path_len = 0; + int dir_path_len = 0; + char *tmppath = NULL; + char *dir = NULL; + char *tmpstr = NULL; + + if (!path || *path != '/') + goto out; + + tmppath = gf_strdup (path); + if (!tmppath) + goto out; + + /* Strip the extra slashes and return */ + bzero (path, strlen(path)); + path[0] = '/'; + dir = strtok_r(tmppath, "/", &tmpstr); + + while (dir) { + dir_path_len = strlen(dir); + strncpy ((path + path_len + 1), dir, dir_path_len); + path_len += dir_path_len + 1; + dir = strtok_r (NULL, "/", &tmpstr); + if (dir) + strncpy ((path + path_len), "/", 1); + } + path[path_len] = '\0'; + ret = 0; + + out: + if (ret) + gf_log ("common-utils", GF_LOG_ERROR, + "Path manipulation failed"); + + if (tmppath) + GF_FREE(tmppath); + + return ret; +} -- cgit