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

[libvirt] [PATCH 04/10] secret: Introduce virSecretObjListFindBy{UUID|Usage} support



New API's including unlocked and Locked versions in order to be able
to use in either manner.

Support for searching hash object lists instead of linked lists will
replace existing secret_driver functions secretFindByUUID and
secretFindByUsage

Signed-off-by: John Ferlan <jferlan redhat com>
---
 src/conf/secret_conf.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/conf/secret_conf.h |  14 +++++
 2 files changed, 158 insertions(+)

diff --git a/src/conf/secret_conf.c b/src/conf/secret_conf.c
index 675fc3f..d082ad8 100644
--- a/src/conf/secret_conf.c
+++ b/src/conf/secret_conf.c
@@ -152,6 +152,150 @@ virSecretObjListDispose(void *obj)
 }
 
 
+/**
+ * virSecretObjFindByUUIDLocked:
+ * @secrets: list of secret objects
+ * @uuid: secret uuid to find
+ *
+ * This functions requires @secrets to be locked already!
+ *
+ * Returns: not locked, but ref'd secret object.
+ */
+virSecretObjPtr
+virSecretObjListFindByUUIDLocked(virSecretObjListPtr secrets,
+                                 const unsigned char *uuid)
+{
+    virSecretObjPtr ret = NULL;
+    char uuidstr[VIR_UUID_STRING_BUFLEN];
+
+    virUUIDFormat(uuid, uuidstr);
+
+    ret = virHashLookup(secrets->objs, uuidstr);
+    if (ret)
+        virObjectRef(ret);
+    return ret;
+}
+
+
+/**
+ * virSecretObjFindByUUID:
+ * @secrets: list of secret objects
+ * @uuid: secret uuid to find
+ *
+ * This function locks @secrets and finds the secret object which
+ * corresponds to @uuid.
+ *
+ * Returns: locked and ref'd secret object.
+ */
+virSecretObjPtr
+virSecretObjListFindByUUID(virSecretObjListPtr secrets,
+                           const unsigned char *uuid)
+{
+    virSecretObjPtr ret;
+
+    virObjectLock(secrets);
+    ret = virSecretObjListFindByUUIDLocked(secrets, uuid);
+    virObjectUnlock(secrets);
+    if (ret)
+        virObjectLock(ret);
+    return ret;
+}
+
+
+static int
+virSecretObjSearchName(const void *payload,
+                       const void *name ATTRIBUTE_UNUSED,
+                       const void *opaque)
+{
+    virSecretObjPtr secret = (virSecretObjPtr) payload;
+    struct virSecretSearchData *data = (struct virSecretSearchData *) opaque;
+    int found = 0;
+
+    virObjectLock(secret);
+
+    if (secret->def->usage_type != data->usageType)
+        goto cleanup;
+
+    switch (data->usageType) {
+    case VIR_SECRET_USAGE_TYPE_NONE:
+    /* never match this */
+        break;
+
+    case VIR_SECRET_USAGE_TYPE_VOLUME:
+        if (STREQ(secret->def->usage.volume, data->usageID))
+            found = 1;
+        break;
+
+    case VIR_SECRET_USAGE_TYPE_CEPH:
+        if (STREQ(secret->def->usage.ceph, data->usageID))
+            found = 1;
+        break;
+
+    case VIR_SECRET_USAGE_TYPE_ISCSI:
+        if (STREQ(secret->def->usage.target, data->usageID))
+            found = 1;
+        break;
+    }
+
+ cleanup:
+    virObjectUnlock(secret);
+    return found;
+}
+
+
+/**
+ * virSecretObjFindByUsageLocked:
+ * @secrets: list of secret objects
+ * @usageType: secret usageType to find
+ * @usageID: secret usage string
+ *
+ * This functions requires @secrets to be locked already!
+ *
+ * Returns: not locked, but ref'd secret object.
+ */
+virSecretObjPtr
+virSecretObjListFindByUsageLocked(virSecretObjListPtr secrets,
+                                  int usageType,
+                                  const char *usageID)
+{
+    virSecretObjPtr ret = NULL;
+    struct virSecretSearchData data = { .usageType = usageType,
+                                        .usageID = usageID };
+
+    ret = virHashSearch(secrets->objs, virSecretObjSearchName, &data);
+    if (ret)
+        virObjectRef(ret);
+    return ret;
+}
+
+
+/**
+ * virSecretObjFindByUsage:
+ * @secrets: list of secret objects
+ * @usageType: secret usageType to find
+ * @usageID: secret usage string
+ *
+ * This function locks @secrets and finds the secret object which
+ * corresponds to @usageID of @usageType.
+ *
+ * Returns: locked and ref'd secret object.
+ */
+virSecretObjPtr
+virSecretObjListFindByUsage(virSecretObjListPtr secrets,
+                            int usageType,
+                            const char *usageID)
+{
+    virSecretObjPtr ret;
+
+    virObjectLock(secrets);
+    ret = virSecretObjListFindByUsageLocked(secrets, usageType, usageID);
+    virObjectUnlock(secrets);
+    if (ret)
+        virObjectLock(ret);
+    return ret;
+}
+
+
 void
 virSecretDefFree(virSecretDefPtr def)
 {
diff --git a/src/conf/secret_conf.h b/src/conf/secret_conf.h
index 76b805c..53e03fe 100644
--- a/src/conf/secret_conf.h
+++ b/src/conf/secret_conf.h
@@ -63,6 +63,20 @@ typedef virSecretObjList *virSecretObjListPtr;
 
 virSecretObjListPtr virSecretObjListNew(void);
 
+virSecretObjPtr virSecretObjListFindByUUIDLocked(virSecretObjListPtr secrets,
+                                                 const unsigned char *uuid);
+
+virSecretObjPtr virSecretObjListFindByUUID(virSecretObjListPtr secrets,
+                                           const unsigned char *uuid);
+
+virSecretObjPtr virSecretObjListFindByUsageLocked(virSecretObjListPtr secrets,
+                                                  int usageType,
+                                                  const char *usageID);
+
+virSecretObjPtr virSecretObjListFindByUsage(virSecretObjListPtr secrets,
+                                            int usageType,
+                                            const char *usageID);
+
 void virSecretDefFree(virSecretDefPtr def);
 virSecretDefPtr virSecretDefParseString(const char *xml);
 virSecretDefPtr virSecretDefParseFile(const char *filename);
-- 
2.5.0


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