From 72199c06c8a110a7706ea2752ad713564918f94b Mon Sep 17 00:00:00 2001
From: Ryota Ozaki <ozaki.ryota@gmail.com>
Date: Mon, 9 Mar 2009 08:08:56 +0900
Subject: [PATCH] storage: fix disregarding specified owner/group of permissions

---
 src/storage_conf.c |   98 +++++++++++++++++----------------------------------
 1 files changed, 33 insertions(+), 65 deletions(-)

diff --git a/src/storage_conf.c b/src/storage_conf.c
index 9c13e07..d01665c 100644
--- a/src/storage_conf.c
+++ b/src/storage_conf.c
@@ -371,13 +371,24 @@ virStoragePoolDefParseAuthChap(virConnectPtr conn,
 
 
 static int
-virStoragePoolDefParsePerms(virConnectPtr conn,
-                            xmlXPathContextPtr ctxt,
-                            virStoragePermsPtr perms) {
+virStorageDefParsePerms(virConnectPtr conn,
+                        xmlXPathContextPtr ctxt,
+                        virStoragePermsPtr perms,
+                        const char const *permxpath) {
     char *mode;
     long v;
+    int ret = -1;
+    xmlNodePtr relnode;
+    xmlNodePtr node;
 
-    mode = virXPathString(conn, "string(/pool/permissions/mode)", ctxt);
+    node = virXPathNode(conn, permxpath, ctxt);
+    if (node == NULL)
+        return -1;
+
+    relnode = ctxt->node;
+    ctxt->node = node;
+
+    mode = virXPathString(conn, "string(./mode)", ctxt);
     if (!mode) {
         perms->mode = 0700;
     } else {
@@ -387,36 +398,39 @@ virStoragePoolDefParsePerms(virConnectPtr conn,
         if (*end || perms->mode < 0 || perms->mode > 0777) {
             virStorageReportError(conn, VIR_ERR_XML_ERROR,
                                   "%s", _("malformed octal mode"));
-            return -1;
+            goto error;
         }
     }
 
-    if (virXPathNode(conn, "/pool/permissions/owner", ctxt) == NULL) {
+    if (virXPathNode(conn, "./owner", ctxt) == NULL) {
         perms->uid = getuid();
     } else {
-        if (virXPathLong(conn, "number(/pool/permissions/owner)", ctxt, &v) < 0) {
+        if (virXPathLong(conn, "number(./owner)", ctxt, &v) < 0) {
             virStorageReportError(conn, VIR_ERR_XML_ERROR,
                                   "%s", _("malformed owner element"));
-            return -1;
+            goto error;
         }
         perms->uid = (int)v;
     }
 
-    if (virXPathNode(conn, "/pool/permissions/group", ctxt) == NULL) {
+    if (virXPathNode(conn, "./group", ctxt) == NULL) {
         perms->gid = getgid();
     } else {
-        if (virXPathLong(conn, "number(/pool/permissions/group)", ctxt, &v) < 0) {
+        if (virXPathLong(conn, "number(./group)", ctxt, &v) < 0) {
             virStorageReportError(conn, VIR_ERR_XML_ERROR,
                                   "%s", _("malformed group element"));
-            return -1;
+            goto error;
         }
         perms->gid = (int)v;
     }
 
     /* NB, we're ignoring missing labels here - they'll simply inherit */
-    perms->label = virXPathString(conn, "string(/pool/permissions/label)", ctxt);
+    perms->label = virXPathString(conn, "string(./label)", ctxt);
 
-    return 0;
+    ret = 0;
+error:
+    ctxt->node = relnode;
+    return ret;
 }
 
 
@@ -579,7 +593,8 @@ virStoragePoolDefParseDoc(virConnectPtr conn,
         goto cleanup;
     }
 
-    if (virStoragePoolDefParsePerms(conn, ctxt, &ret->target.perms) < 0)
+    if (virStorageDefParsePerms(conn, ctxt, &ret->target.perms,
+                                    "/pool/target/permissions") < 0)
         goto cleanup;
 
     return ret;
@@ -801,55 +816,6 @@ virStoragePoolDefFormat(virConnectPtr conn,
 
 
 static int
-virStorageVolDefParsePerms(virConnectPtr conn,
-                           xmlXPathContextPtr ctxt,
-                           virStoragePermsPtr perms) {
-    char *mode;
-    long v;
-
-    mode = virXPathString(conn, "string(/volume/permissions/mode)", ctxt);
-    if (!mode) {
-        perms->mode = 0600;
-    } else {
-        char *end = NULL;
-        perms->mode = strtol(mode, &end, 8);
-        VIR_FREE(mode);
-        if (*end || perms->mode < 0 || perms->mode > 0777) {
-            virStorageReportError(conn, VIR_ERR_XML_ERROR,
-                                  "%s", _("malformed octal mode"));
-            return -1;
-        }
-    }
-
-    if (virXPathNode(conn, "/volume/permissions/owner", ctxt) == NULL) {
-        perms->uid = getuid();
-    } else {
-        if (virXPathLong(conn, "number(/volume/permissions/owner)", ctxt, &v) < 0) {
-            virStorageReportError(conn, VIR_ERR_XML_ERROR,
-                                  "%s", _("missing owner element"));
-            return -1;
-        }
-        perms->uid = (int)v;
-    }
-    if (virXPathNode(conn, "/volume/permissions/group", ctxt) == NULL) {
-        perms->gid = getgid();
-    } else {
-        if (virXPathLong(conn, "number(/volume/permissions/group)", ctxt, &v) < 0) {
-            virStorageReportError(conn, VIR_ERR_XML_ERROR,
-                                  "%s", _("missing group element"));
-            return -1;
-        }
-        perms->gid = (int)v;
-    }
-
-    /* NB, we're ignoring missing labels here - they'll simply inherit */
-    perms->label = virXPathString(conn, "string(/volume/permissions/label)", ctxt);
-
-    return 0;
-}
-
-
-static int
 virStorageSize(virConnectPtr conn,
                const char *unit,
                const char *val,
@@ -997,7 +963,8 @@ virStorageVolDefParseDoc(virConnectPtr conn,
         VIR_FREE(format);
     }
 
-    if (virStorageVolDefParsePerms(conn, ctxt, &ret->target.perms) < 0)
+    if (virStorageDefParsePerms(conn, ctxt, &ret->target.perms,
+                                    "/volume/target/permissions") < 0)
         goto cleanup;
 
 
@@ -1019,7 +986,8 @@ virStorageVolDefParseDoc(virConnectPtr conn,
         VIR_FREE(format);
     }
 
-    if (virStorageVolDefParsePerms(conn, ctxt, &ret->backingStore.perms) < 0)
+    if (virStorageDefParsePerms(conn, ctxt, &ret->backingStore.perms,
+                                    "/volume/backingStore/permissions") < 0)
         goto cleanup;
 
     return ret;
-- 
1.6.0.6

