[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 12/10] secret: Introduce virSecretObjSaveConfig and virSecretObjSaveData



Move and rename the secretRewriteFile, secretSaveDef, and secretSaveValue
from secret_driver to secret_conf

Need to make some slight adjustments since the secretSave* functions
called secretEnsureDirectory, but otherwise mostly just a move of code.

Signed-off-by: John Ferlan <jferlan redhat com>
---
 src/conf/secret_conf.c     | 69 +++++++++++++++++++++++++++++++++++
 src/conf/secret_conf.h     |  4 +++
 src/libvirt_private.syms   |  2 ++
 src/secret/secret_driver.c | 90 +++++++---------------------------------------
 4 files changed, 87 insertions(+), 78 deletions(-)

diff --git a/src/conf/secret_conf.c b/src/conf/secret_conf.c
index 52f78bd..3528288 100644
--- a/src/conf/secret_conf.c
+++ b/src/conf/secret_conf.c
@@ -706,6 +706,75 @@ virSecretObjDeleteData(virSecretObjPtr secret)
 }
 
 
+/* Permament secret storage */
+
+/* Secrets are stored in virSecretDriverStatePtr->configDir.  Each secret
+   has virSecretDef stored as XML in "$basename.xml".  If a value of the
+   secret is defined, it is stored as base64 (with no formatting) in
+   "$basename.base64".  "$basename" is in both cases the base64-encoded UUID. */
+
+static int
+virSecretRewriteFile(int fd,
+                     void *opaque)
+{
+    char *data = opaque;
+
+    if (safewrite(fd, data, strlen(data)) < 0)
+        return -1;
+
+    return 0;
+}
+
+
+int
+virSecretObjSaveConfig(virSecretObjPtr secret)
+{
+    char *xml = NULL;
+    int ret = -1;
+
+    if (!(xml = virSecretDefFormat(secret->def)))
+        goto cleanup;
+
+    if (virFileRewrite(secret->configFile, S_IRUSR | S_IWUSR,
+                       virSecretRewriteFile, xml) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(xml);
+    return ret;
+}
+
+
+int
+virSecretObjSaveData(virSecretObjPtr secret)
+{
+    char *base64 = NULL;
+    int ret = -1;
+
+    if (!secret->value)
+        return 0;
+
+    base64_encode_alloc((const char *)secret->value, secret->value_size,
+                        &base64);
+    if (base64 == NULL) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    if (virFileRewrite(secret->base64File, S_IRUSR | S_IWUSR,
+                       virSecretRewriteFile, base64) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(base64);
+    return ret;
+}
+
+
 void
 virSecretDefFree(virSecretDefPtr def)
 {
diff --git a/src/conf/secret_conf.h b/src/conf/secret_conf.h
index e2f69b5..d40b510 100644
--- a/src/conf/secret_conf.h
+++ b/src/conf/secret_conf.h
@@ -118,6 +118,10 @@ int virSecretObjDeleteConfig(virSecretObjPtr secret);
 
 void virSecretObjDeleteData(virSecretObjPtr secret);
 
+int virSecretObjSaveConfig(virSecretObjPtr secret);
+
+int virSecretObjSaveData(virSecretObjPtr secret);
+
 void virSecretDefFree(virSecretDefPtr def);
 virSecretDefPtr virSecretDefParseString(const char *xml);
 virSecretDefPtr virSecretDefParseFile(const char *filename);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2437b0b..9e1a09e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -798,6 +798,8 @@ virSecretObjListGetUUIDs;
 virSecretObjListNew;
 virSecretObjListNumOfSecrets;
 virSecretObjListRemove;
+virSecretObjSaveConfig;
+virSecretObjSaveData;
 virSecretUsageIDForDef;
 virSecretUsageTypeFromString;
 virSecretUsageTypeToString;
diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c
index e4315f3..1b4dfea 100644
--- a/src/secret/secret_driver.c
+++ b/src/secret/secret_driver.c
@@ -91,26 +91,6 @@ secretObjFromSecret(virSecretPtr secret)
 }
 
 
-/* Permament secret storage */
-
-/* Secrets are stored in virSecretDriverStatePtr->configDir.  Each secret
-   has virSecretDef stored as XML in "$basename.xml".  If a value of the
-   secret is defined, it is stored as base64 (with no formatting) in
-   "$basename.base64".  "$basename" is in both cases the base64-encoded UUID. */
-
-static int
-secretRewriteFile(int fd,
-                  void *opaque)
-{
-    char *data = opaque;
-
-    if (safewrite(fd, data, strlen(data)) < 0)
-        return -1;
-
-    return 0;
-}
-
-
 static int
 secretEnsureDirectory(void)
 {
@@ -122,59 +102,6 @@ secretEnsureDirectory(void)
     return 0;
 }
 
-static int
-secretSaveDef(const virSecretObj *secret)
-{
-    char *xml = NULL;
-    int ret = -1;
-
-    if (secretEnsureDirectory() < 0)
-        goto cleanup;
-
-    if (!(xml = virSecretDefFormat(secret->def)))
-        goto cleanup;
-
-    if (virFileRewrite(secret->configFile, S_IRUSR | S_IWUSR,
-                       secretRewriteFile, xml) < 0)
-        goto cleanup;
-
-    ret = 0;
-
- cleanup:
-    VIR_FREE(xml);
-    return ret;
-}
-
-static int
-secretSaveValue(const virSecretObj *secret)
-{
-    char *base64 = NULL;
-    int ret = -1;
-
-    if (secret->value == NULL)
-        return 0;
-
-    if (secretEnsureDirectory() < 0)
-        goto cleanup;
-
-    base64_encode_alloc((const char *)secret->value, secret->value_size,
-                        &base64);
-    if (base64 == NULL) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
-    if (virFileRewrite(secret->base64File, S_IRUSR | S_IWUSR,
-                       secretRewriteFile, base64) < 0)
-        goto cleanup;
-
-    ret = 0;
-
- cleanup:
-    VIR_FREE(base64);
-    return ret;
-}
-
 /* Driver functions */
 
 static int
@@ -300,14 +227,18 @@ secretDefineXML(virConnectPtr conn,
         goto cleanup;
 
     if (!new_attrs->ephemeral) {
+        if (secretEnsureDirectory() < 0)
+            goto cleanup;
+
         if (backup && backup->ephemeral) {
-            if (secretSaveValue(secret) < 0)
+            if (virSecretObjSaveData(secret) < 0)
                 goto restore_backup;
         }
-        if (secretSaveDef(secret) < 0) {
+
+        if (virSecretObjSaveConfig(secret) < 0) {
             if (backup && backup->ephemeral) {
-                /* Undo the secretSaveValue() above; ignore errors */
-                (void)unlink(secret->base64File);
+                /* Undo the virSecretObjSaveData() above; ignore errors */
+                virSecretObjDeleteData(secret);
             }
             goto restore_backup;
         }
@@ -396,7 +327,10 @@ secretSetValue(virSecretPtr obj,
     secret->value = new_value;
     secret->value_size = value_size;
     if (!secret->def->ephemeral) {
-        if (secretSaveValue(secret) < 0)
+        if (secretEnsureDirectory() < 0)
+            goto cleanup;
+
+        if (virSecretObjSaveData(secret) < 0)
             goto restore_backup;
     }
     /* Saved successfully - drop old value */
-- 
2.5.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]