]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add virFileAbsPath() utility
authorAmy Griffis <amy.griffis@hp.com>
Thu, 8 Oct 2009 14:55:58 +0000 (16:55 +0200)
committerDaniel Veillard <veillard@redhat.com>
Thu, 8 Oct 2009 14:55:58 +0000 (16:55 +0200)
* src/util/util.[ch]: Add virFileAbsPath() function to ensure an
  absolute path for a potentially realtive path.
* src/libvirt_private.syms: add it in libvirt private symbols

src/libvirt_private.syms
src/util/util.c
src/util/util.h

index 37c729c72508f66d58cd59aa4c562dbd717c8a78..57a6a6a9886d6b6be6bc839448be9203dac9d36b 100644 (file)
@@ -445,6 +445,7 @@ virFileExists;
 virFileHasSuffix;
 virFileLinkPointsTo;
 virFileMakePath;
+virFileAbsPath;
 virFileOpenTty;
 virFileReadLimFD;
 virFilePid;
index f474ead9d6bad23ebf64e2de93ef7e4cd563ea91..81b743c95ce57199e7fa6abf52741c2bfa58fb97 100644 (file)
@@ -1402,6 +1402,42 @@ cleanup:
 
 #endif /* PROXY */
 
+/*
+ * Creates an absolute path for a potentialy realtive path.
+ * Return 0 if the path was not relative, or on success.
+ * Return -1 on error.
+ *
+ * You must free the result.
+ */
+int virFileAbsPath(const char *path, char **abspath)
+{
+    char *buf;
+    int cwdlen;
+
+    if (path[0] == '/') {
+        buf = strdup(path);
+        if (buf == NULL)
+            return(-1);
+    } else {
+        buf = getcwd(NULL, 0);
+        if (buf == NULL)
+            return(-1);
+
+        cwdlen = strlen(buf);
+        /* cwdlen includes the null terminator */
+        if (VIR_REALLOC_N(buf, cwdlen + strlen(path) + 1) < 0) {
+            VIR_FREE(buf);
+            errno = ENOMEM;
+            return(-1);
+        }
+
+        buf[cwdlen] = '/';
+        strcpy(&buf[cwdlen + 1], path);
+    }
+
+    *abspath = buf;
+    return 0;
+}
 
 /* Like strtol, but produce an "int" result, and check more carefully.
    Return 0 upon success;  return -1 to indicate failure.
index 77f50edcacb09b17fc885fbf7b92d40828e6e2a1..8679636bde74ad5e0faaa3ea320f380d0b321808 100644 (file)
@@ -115,6 +115,9 @@ int virFileBuildPath(const char *dir,
                      char *buf,
                      unsigned int buflen);
 
+int virFileAbsPath(const char *path,
+                   char **abspath);
+
 int virFileOpenTty(int *ttymaster,
                    char **ttyName,
                    int rawmode);