summaryrefslogtreecommitdiffstats
path: root/cli/src/cli.c
diff options
context:
space:
mode:
authorSachidananda <sac@gluster.com>2011-07-16 12:54:00 +0000
committerAnand Avati <avati@gluster.com>2011-07-17 07:45:58 -0700
commit273f898364463cd10165fc82c8c5250a0962c452 (patch)
tree6678fa75c2dcf17c84146c972b635ecee1e80d3b /cli/src/cli.c
parent260edb905171ebed3692eaeca404d13202cd5fd0 (diff)
Canonicalize path names while creating volumes.
When a volume is created resolve symbolic links, delete duplicate slashes in the path name. Signed-off-by: Sachidananda Urs <sac@gluster.com> Signed-off-by: Anand Avati <avati@gluster.com> BUG: 3183 (When creating volumes brick paths are not handled properly.) URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=3183
Diffstat (limited to 'cli/src/cli.c')
-rw-r--r--cli/src/cli.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/cli/src/cli.c b/cli/src/cli.c
index 8d37405c614..8a06c8abd76 100644
--- a/cli/src/cli.c
+++ b/cli/src/cli.c
@@ -568,6 +568,70 @@ cli_local_wipe (cli_local_t *local)
return;
}
+/* 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
+cli_canonicalize_path (char *path)
+{
+ struct stat sb = {0};
+ int ret = -1;
+ char *tmppath = NULL;
+ char *dir = NULL;
+ char *tmpstr = NULL;
+ int path_len = 0;
+
+ if (!path)
+ return ret;
+
+ ret = stat (path, &sb);
+ if (ret == -1) {
+ /* Strip the extra slashes and return */
+ tmppath = gf_strdup (path);
+ if (tmppath == NULL) {
+ ret = -1;
+ gf_log ("cli", GF_LOG_ERROR, "Out of memory.");
+ goto out;
+ }
+ bzero (path, strlen(path));
+ path[0] = '/';
+ dir = strtok_r(tmppath, "/", &tmpstr);
+ while (dir) {
+ strncpy ((path + path_len + 1), dir, strlen(dir));
+ path_len = strlen (path);
+ dir = strtok_r(NULL, "/", &tmpstr);
+ if (dir)
+ strncpy((path + path_len), "/", 1);
+ }
+ if (path_len == 0)
+ path[1] = '\0';
+ else
+ path[path_len] = '\0';
+ ret = 0;
+ goto out;
+ } else {
+ tmppath = gf_strdup(path);
+ if (tmppath == NULL) {
+ ret = -1;
+ gf_log ("cli", GF_LOG_ERROR, "Out of memory.");
+ goto out;
+ }
+ if (realpath (tmppath, path) == NULL) {
+ cli_out ("Path manipulation failed: %s",
+ strerror(errno));
+ gf_log ("cli", GF_LOG_ERROR, "Path manipulation "
+ "failed: %s", strerror(errno));
+ ret = -1;
+ goto out;
+ }
+ ret = 0;
+ }
+out:
+ if (tmppath)
+ GF_FREE(tmppath);
+ return ret;
+}
+
void
cli_path_strip_trailing_slashes (char *path)
{