diff options
author | Rajesh Amaravathi <rajesh@redhat.com> | 2012-05-10 16:24:45 +0530 |
---|---|---|
committer | Anand Avati <avati@redhat.com> | 2012-05-22 00:53:12 -0700 |
commit | fa287178ac714071ceacf8697bd36cc8a8a8da00 (patch) | |
tree | 684f2e2fc6527c8873f395704d5b799146383837 /libglusterfs | |
parent | bb2fc0a23e46413a14baef1846a9873681d32fe2 (diff) |
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 <rajesh@redhat.com>
Reviewed-on: http://review.gluster.com/3315
Tested-by: Gluster Build System <jenkins@build.gluster.com>
Reviewed-by: Anand Avati <avati@redhat.com>
Diffstat (limited to 'libglusterfs')
-rw-r--r-- | libglusterfs/src/common-utils.c | 52 | ||||
-rw-r--r-- | libglusterfs/src/common-utils.h | 1 |
2 files changed, 48 insertions, 5 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index 737487d1d72..c9c396762ab 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -1953,11 +1953,6 @@ out: * 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. - */ - inline int32_t gf_roundup_next_power_of_two (uint32_t nr) { @@ -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; +} diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index b2533ae175d..9903edad456 100644 --- a/libglusterfs/src/common-utils.h +++ b/libglusterfs/src/common-utils.h @@ -486,4 +486,5 @@ char *get_path_name (char *word, char **path); void gf_path_strip_trailing_slashes (char *path); uint64_t get_mem_size (); int gf_strip_whitespace (char *str, int len); +int gf_canonicalize_path (char *path); #endif /* _COMMON_UTILS_H */ |