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(a)redhat.com>
---
src/conf/virsecretobj.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++
src/conf/virsecretobj.h | 14 +++++
2 files changed, 154 insertions(+)
diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c
index 905e138..1fd9f57 100644
--- a/src/conf/virsecretobj.c
+++ b/src/conf/virsecretobj.c
@@ -136,3 +136,143 @@ virSecretObjListDispose(void *obj)
virHashFree(secrets->objs);
}
+
+
+/**
+ * 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)
+{
+ char uuidstr[VIR_UUID_STRING_BUFLEN];
+
+ virUUIDFormat(uuid, uuidstr);
+
+ return virObjectRef(virHashLookup(secrets->objs, uuidstr));
+}
+
+
+/**
+ * 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;
+}
diff --git a/src/conf/virsecretobj.h b/src/conf/virsecretobj.h
index 3fc0844..0effcd6 100644
--- a/src/conf/virsecretobj.h
+++ b/src/conf/virsecretobj.h
@@ -46,4 +46,18 @@ 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);
+
#endif /* __VIRSECRETOBJ_H__ */
--
2.5.5