aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmy Griffis <amy.griffis@hp.com>2009-10-08 16:55:58 +0200
committerDaniel Veillard <veillard@redhat.com>2009-10-08 16:55:58 +0200
commit2e812c89e92b0a67d5569ca524ff798eb23fdc7f (patch)
tree5455d1c6a804b1c3648df3fcf4ee118a021304eb
parentDocumentation and examples for SVirt Apparmor driver (diff)
downloadlibvirt-2e812c89e92b0a67d5569ca524ff798eb23fdc7f.tar.gz
libvirt-2e812c89e92b0a67d5569ca524ff798eb23fdc7f.tar.bz2
libvirt-2e812c89e92b0a67d5569ca524ff798eb23fdc7f.zip
Add virFileAbsPath() utility
* 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
-rw-r--r--src/libvirt_private.syms1
-rw-r--r--src/util/util.c36
-rw-r--r--src/util/util.h3
3 files changed, 40 insertions, 0 deletions
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 37c729c72..57a6a6a98 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -445,6 +445,7 @@ virFileExists;
virFileHasSuffix;
virFileLinkPointsTo;
virFileMakePath;
+virFileAbsPath;
virFileOpenTty;
virFileReadLimFD;
virFilePid;
diff --git a/src/util/util.c b/src/util/util.c
index f474ead9d..81b743c95 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -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.
diff --git a/src/util/util.h b/src/util/util.h
index 77f50edca..8679636bd 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -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);