From 67d79ad7ffc6f81c2d61d23c71d30c475cc1a45d Mon Sep 17 00:00:00 2001
From: Osier Yang <jyang@redhat.com>
Date: Thu, 21 Jun 2012 15:11:20 +0800
Subject: [PATCH] util: Use current uid and gid if they are passed as -1 for
 virDirCreate

All the callers of virDirCreate are updated incidentally.
---
 src/storage/storage_backend_fs.c | 23 ++++++-----------------
 src/util/util.c                  |  6 ++++++
 2 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
index 5eb486ef70..4894994649 100644
--- a/src/storage/storage_backend_fs.c
+++ b/src/storage/storage_backend_fs.c
@@ -786,17 +786,10 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
     /* Now create the final dir in the path with the uid/gid/mode
      * requested in the config. If the dir already exists, just set
      * the perms. */
-    uid_t uid;
-    gid_t gid;
-
-    uid = (pool->def->target.perms.uid == (uid_t) -1)
-        ? getuid() : pool->def->target.perms.uid;
-    gid = (pool->def->target.perms.gid == (gid_t) -1)
-        ? getgid() : pool->def->target.perms.gid;
-
     if ((err = virDirCreate(pool->def->target.path,
                             pool->def->target.perms.mode,
-                            uid, gid,
+                            pool->def->target.perms.uid,
+                            pool->def->target.perms.gid,
                             VIR_DIR_CREATE_FORCE_PERMS |
                             VIR_DIR_CREATE_ALLOW_EXIST |
                             (pool->def->type == VIR_STORAGE_POOL_NETFS
@@ -808,9 +801,9 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
 
     /* Reflect the actual uid and gid to the config. */
     if (pool->def->target.perms.uid == (uid_t) -1)
-        pool->def->target.perms.uid = uid;
+        pool->def->target.perms.uid = getuid();
     if (pool->def->target.perms.gid == (gid_t) -1)
-        pool->def->target.perms.gid = gid;
+        pool->def->target.perms.gid = getgid();
 
     if (flags != 0) {
         ret = virStorageBackendMakeFileSystem(pool, flags);
@@ -1050,13 +1043,9 @@ static int createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED,
         return -1;
     }
 
-    uid_t uid = (vol->target.perms.uid == -1)
-        ? getuid() : vol->target.perms.uid;
-    gid_t gid = (vol->target.perms.gid == -1)
-        ? getgid() : vol->target.perms.gid;
-
     if ((err = virDirCreate(vol->target.path, vol->target.perms.mode,
-                            uid, gid,
+                            vol->target.perms.uid,
+                            vol->target.perms.gid,
                             VIR_DIR_CREATE_FORCE_PERMS |
                             (pool->def->type == VIR_STORAGE_POOL_NETFS
                              ? VIR_DIR_CREATE_AS_UID : 0))) < 0) {
diff --git a/src/util/util.c b/src/util/util.c
index 47b13662f3..07dc70461f 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -1126,6 +1126,12 @@ int virDirCreate(const char *path, mode_t mode,
     int waitret;
     int status, ret = 0;
 
+    /* allow using -1 to mean "current value" */
+    if (uid == (uid_t) -1)
+        uid = getuid();
+    if (gid == (gid_t) -1)
+        gid = getgid();
+
     if ((!(flags & VIR_DIR_CREATE_AS_UID))
         || (getuid() != 0)
         || ((uid == 0) && (gid == 0))