secret: Use consistent naming for variables

When processing a virSecretPtr use 'secret' as a variable name.

When processing a virSecretObjPtr use 'obj' as a variable name.

When processing a virSecretDefPtr use 'def' as a variable name,
unless a distinction needs to be made with a 'newdef' such as
virSecretObjListAddLocked (which also used the VIR_STEAL_PTR macro
for the configFile and base64File).

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-04-01 11:46:36 -04:00
parent 6de89f33fa
commit a1b568cdbd
3 changed files with 211 additions and 212 deletions

View File

@ -89,29 +89,29 @@ VIR_ONCE_GLOBAL_INIT(virSecretObj)
static virSecretObjPtr static virSecretObjPtr
virSecretObjNew(void) virSecretObjNew(void)
{ {
virSecretObjPtr secret; virSecretObjPtr obj;
if (virSecretObjInitialize() < 0) if (virSecretObjInitialize() < 0)
return NULL; return NULL;
if (!(secret = virObjectLockableNew(virSecretObjClass))) if (!(obj = virObjectLockableNew(virSecretObjClass)))
return NULL; return NULL;
virObjectLock(secret); virObjectLock(obj);
return secret; return obj;
} }
void void
virSecretObjEndAPI(virSecretObjPtr *secret) virSecretObjEndAPI(virSecretObjPtr *obj)
{ {
if (!*secret) if (!*obj)
return; return;
virObjectUnlock(*secret); virObjectUnlock(*obj);
virObjectUnref(*secret); virObjectUnref(*obj);
*secret = NULL; *obj = NULL;
} }
@ -136,18 +136,18 @@ virSecretObjListNew(void)
static void static void
virSecretObjDispose(void *obj) virSecretObjDispose(void *opaque)
{ {
virSecretObjPtr secret = obj; virSecretObjPtr obj = opaque;
virSecretDefFree(secret->def); virSecretDefFree(obj->def);
if (secret->value) { if (obj->value) {
/* Wipe before free to ensure we don't leave a secret on the heap */ /* Wipe before free to ensure we don't leave a secret on the heap */
memset(secret->value, 0, secret->value_size); memset(obj->value, 0, obj->value_size);
VIR_FREE(secret->value); VIR_FREE(obj->value);
} }
VIR_FREE(secret->configFile); VIR_FREE(obj->configFile);
VIR_FREE(secret->base64File); VIR_FREE(obj->base64File);
} }
@ -195,14 +195,14 @@ virSecretObjPtr
virSecretObjListFindByUUID(virSecretObjListPtr secrets, virSecretObjListFindByUUID(virSecretObjListPtr secrets,
const unsigned char *uuid) const unsigned char *uuid)
{ {
virSecretObjPtr ret; virSecretObjPtr obj;
virObjectLock(secrets); virObjectLock(secrets);
ret = virSecretObjListFindByUUIDLocked(secrets, uuid); obj = virSecretObjListFindByUUIDLocked(secrets, uuid);
virObjectUnlock(secrets); virObjectUnlock(secrets);
if (ret) if (obj)
virObjectLock(ret); virObjectLock(obj);
return ret; return obj;
} }
@ -211,21 +211,21 @@ virSecretObjSearchName(const void *payload,
const void *name ATTRIBUTE_UNUSED, const void *name ATTRIBUTE_UNUSED,
const void *opaque) const void *opaque)
{ {
virSecretObjPtr secret = (virSecretObjPtr) payload; virSecretObjPtr obj = (virSecretObjPtr) payload;
struct virSecretSearchData *data = (struct virSecretSearchData *) opaque; struct virSecretSearchData *data = (struct virSecretSearchData *) opaque;
int found = 0; int found = 0;
virObjectLock(secret); virObjectLock(obj);
if (secret->def->usage_type != data->usageType) if (obj->def->usage_type != data->usageType)
goto cleanup; goto cleanup;
if (data->usageType != VIR_SECRET_USAGE_TYPE_NONE && if (data->usageType != VIR_SECRET_USAGE_TYPE_NONE &&
STREQ(secret->def->usage_id, data->usageID)) STREQ(obj->def->usage_id, data->usageID))
found = 1; found = 1;
cleanup: cleanup:
virObjectUnlock(secret); virObjectUnlock(obj);
return found; return found;
} }
@ -245,14 +245,14 @@ virSecretObjListFindByUsageLocked(virSecretObjListPtr secrets,
int usageType, int usageType,
const char *usageID) const char *usageID)
{ {
virSecretObjPtr ret = NULL; virSecretObjPtr obj = NULL;
struct virSecretSearchData data = { .usageType = usageType, struct virSecretSearchData data = { .usageType = usageType,
.usageID = usageID }; .usageID = usageID };
ret = virHashSearch(secrets->objs, virSecretObjSearchName, &data); obj = virHashSearch(secrets->objs, virSecretObjSearchName, &data);
if (ret) if (obj)
virObjectRef(ret); virObjectRef(obj);
return ret; return obj;
} }
@ -272,14 +272,14 @@ virSecretObjListFindByUsage(virSecretObjListPtr secrets,
int usageType, int usageType,
const char *usageID) const char *usageID)
{ {
virSecretObjPtr ret; virSecretObjPtr obj;
virObjectLock(secrets); virObjectLock(secrets);
ret = virSecretObjListFindByUsageLocked(secrets, usageType, usageID); obj = virSecretObjListFindByUsageLocked(secrets, usageType, usageID);
virObjectUnlock(secrets); virObjectUnlock(secrets);
if (ret) if (obj)
virObjectLock(ret); virObjectLock(obj);
return ret; return obj;
} }
@ -294,22 +294,22 @@ virSecretObjListFindByUsage(virSecretObjListPtr secrets,
*/ */
void void
virSecretObjListRemove(virSecretObjListPtr secrets, virSecretObjListRemove(virSecretObjListPtr secrets,
virSecretObjPtr secret) virSecretObjPtr obj)
{ {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
if (!obj) if (!obj)
return; return;
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(obj->def->uuid, uuidstr);
virObjectRef(secret); virObjectRef(obj);
virObjectUnlock(secret); virObjectUnlock(obj);
virObjectLock(secrets); virObjectLock(secrets);
virObjectLock(secret); virObjectLock(obj);
virHashRemoveEntry(secrets->objs, uuidstr); virHashRemoveEntry(secrets->objs, uuidstr);
virObjectUnlock(secret); virObjectUnlock(obj);
virObjectUnref(secret); virObjectUnref(obj);
virObjectUnlock(secrets); virObjectUnlock(secrets);
} }
@ -317,11 +317,11 @@ virSecretObjListRemove(virSecretObjListPtr secrets,
/* /*
* virSecretObjListAddLocked: * virSecretObjListAddLocked:
* @secrets: list of secret objects * @secrets: list of secret objects
* @def: new secret definition * @newdef: new secret definition
* @configDir: directory to place secret config files * @configDir: directory to place secret config files
* @oldDef: Former secret def (e.g. a reload path perhaps) * @oldDef: Former secret def (e.g. a reload path perhaps)
* *
* Add the new def to the secret obj table hash * Add the new @newdef to the secret obj table hash
* *
* This functions requires @secrets to be locked already! * This functions requires @secrets to be locked already!
* *
@ -329,11 +329,11 @@ virSecretObjListRemove(virSecretObjListPtr secrets,
*/ */
static virSecretObjPtr static virSecretObjPtr
virSecretObjListAddLocked(virSecretObjListPtr secrets, virSecretObjListAddLocked(virSecretObjListPtr secrets,
virSecretDefPtr def, virSecretDefPtr newdef,
const char *configDir, const char *configDir,
virSecretDefPtr *oldDef) virSecretDefPtr *oldDef)
{ {
virSecretObjPtr secret; virSecretObjPtr obj;
virSecretObjPtr ret = NULL; virSecretObjPtr ret = NULL;
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
char *configFile = NULL, *base64File = NULL; char *configFile = NULL, *base64File = NULL;
@ -342,71 +342,69 @@ virSecretObjListAddLocked(virSecretObjListPtr secrets,
*oldDef = NULL; *oldDef = NULL;
/* Is there a secret already matching this UUID */ /* Is there a secret already matching this UUID */
if ((secret = virSecretObjListFindByUUIDLocked(secrets, def->uuid))) { if ((obj = virSecretObjListFindByUUIDLocked(secrets, newdef->uuid))) {
virObjectLock(secret); virObjectLock(obj);
if (STRNEQ_NULLABLE(secret->def->usage_id, def->usage_id)) { if (STRNEQ_NULLABLE(obj->def->usage_id, newdef->usage_id)) {
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(obj->def->uuid, uuidstr);
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("a secret with UUID %s is already defined for " _("a secret with UUID %s is already defined for "
"use with %s"), "use with %s"),
uuidstr, secret->def->usage_id); uuidstr, obj->def->usage_id);
goto cleanup; goto cleanup;
} }
if (secret->def->isprivate && !def->isprivate) { if (obj->def->isprivate && !newdef->isprivate) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("cannot change private flag on existing secret")); _("cannot change private flag on existing secret"));
goto cleanup; goto cleanup;
} }
if (oldDef) if (oldDef)
*oldDef = secret->def; *oldDef = obj->def;
else else
virSecretDefFree(secret->def); virSecretDefFree(obj->def);
secret->def = def; obj->def = newdef;
} else { } else {
/* No existing secret with same UUID, /* No existing secret with same UUID,
* try look for matching usage instead */ * try look for matching usage instead */
if ((secret = virSecretObjListFindByUsageLocked(secrets, if ((obj = virSecretObjListFindByUsageLocked(secrets,
def->usage_type, newdef->usage_type,
def->usage_id))) { newdef->usage_id))) {
virObjectLock(secret); virObjectLock(obj);
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(obj->def->uuid, uuidstr);
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("a secret with UUID %s already defined for " _("a secret with UUID %s already defined for "
"use with %s"), "use with %s"),
uuidstr, def->usage_id); uuidstr, newdef->usage_id);
goto cleanup; goto cleanup;
} }
/* Generate the possible configFile and base64File strings /* Generate the possible configFile and base64File strings
* using the configDir, uuidstr, and appropriate suffix * using the configDir, uuidstr, and appropriate suffix
*/ */
virUUIDFormat(def->uuid, uuidstr); virUUIDFormat(newdef->uuid, uuidstr);
if (!(configFile = virFileBuildPath(configDir, uuidstr, ".xml")) || if (!(configFile = virFileBuildPath(configDir, uuidstr, ".xml")) ||
!(base64File = virFileBuildPath(configDir, uuidstr, ".base64"))) !(base64File = virFileBuildPath(configDir, uuidstr, ".base64")))
goto cleanup; goto cleanup;
if (!(secret = virSecretObjNew())) if (!(obj = virSecretObjNew()))
goto cleanup; goto cleanup;
if (virHashAddEntry(secrets->objs, uuidstr, secret) < 0) if (virHashAddEntry(secrets->objs, uuidstr, obj) < 0)
goto cleanup; goto cleanup;
secret->def = def; obj->def = newdef;
secret->configFile = configFile; VIR_STEAL_PTR(obj->configFile, configFile);
secret->base64File = base64File; VIR_STEAL_PTR(obj->base64File, base64File);
configFile = NULL; virObjectRef(obj);
base64File = NULL;
virObjectRef(secret);
} }
ret = secret; ret = obj;
secret = NULL; obj = NULL;
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
VIR_FREE(configFile); VIR_FREE(configFile);
VIR_FREE(base64File); VIR_FREE(base64File);
return ret; return ret;
@ -415,16 +413,16 @@ virSecretObjListAddLocked(virSecretObjListPtr secrets,
virSecretObjPtr virSecretObjPtr
virSecretObjListAdd(virSecretObjListPtr secrets, virSecretObjListAdd(virSecretObjListPtr secrets,
virSecretDefPtr def, virSecretDefPtr newdef,
const char *configDir, const char *configDir,
virSecretDefPtr *oldDef) virSecretDefPtr *oldDef)
{ {
virSecretObjPtr ret; virSecretObjPtr obj;
virObjectLock(secrets); virObjectLock(secrets);
ret = virSecretObjListAddLocked(secrets, def, configDir, oldDef); obj = virSecretObjListAddLocked(secrets, newdef, configDir, oldDef);
virObjectUnlock(secrets); virObjectUnlock(secrets);
return ret; return obj;
} }
@ -496,23 +494,23 @@ virSecretObjListNumOfSecrets(virSecretObjListPtr secrets,
#define MATCH(FLAG) (flags & (FLAG)) #define MATCH(FLAG) (flags & (FLAG))
static bool static bool
virSecretObjMatchFlags(virSecretObjPtr secret, virSecretObjMatchFlags(virSecretObjPtr obj,
unsigned int flags) unsigned int flags)
{ {
/* filter by whether it's ephemeral */ /* filter by whether it's ephemeral */
if (MATCH(VIR_CONNECT_LIST_SECRETS_FILTERS_EPHEMERAL) && if (MATCH(VIR_CONNECT_LIST_SECRETS_FILTERS_EPHEMERAL) &&
!((MATCH(VIR_CONNECT_LIST_SECRETS_EPHEMERAL) && !((MATCH(VIR_CONNECT_LIST_SECRETS_EPHEMERAL) &&
secret->def->isephemeral) || obj->def->isephemeral) ||
(MATCH(VIR_CONNECT_LIST_SECRETS_NO_EPHEMERAL) && (MATCH(VIR_CONNECT_LIST_SECRETS_NO_EPHEMERAL) &&
!secret->def->isephemeral))) !obj->def->isephemeral)))
return false; return false;
/* filter by whether it's private */ /* filter by whether it's private */
if (MATCH(VIR_CONNECT_LIST_SECRETS_FILTERS_PRIVATE) && if (MATCH(VIR_CONNECT_LIST_SECRETS_FILTERS_PRIVATE) &&
!((MATCH(VIR_CONNECT_LIST_SECRETS_PRIVATE) && !((MATCH(VIR_CONNECT_LIST_SECRETS_PRIVATE) &&
secret->def->isprivate) || obj->def->isprivate) ||
(MATCH(VIR_CONNECT_LIST_SECRETS_NO_PRIVATE) && (MATCH(VIR_CONNECT_LIST_SECRETS_NO_PRIVATE) &&
!secret->def->isprivate))) !obj->def->isprivate)))
return false; return false;
return true; return true;
@ -640,12 +638,12 @@ virSecretObjListGetUUIDs(virSecretObjListPtr secrets,
int int
virSecretObjDeleteConfig(virSecretObjPtr secret) virSecretObjDeleteConfig(virSecretObjPtr obj)
{ {
if (!secret->def->isephemeral && if (!obj->def->isephemeral &&
unlink(secret->configFile) < 0 && errno != ENOENT) { unlink(obj->configFile) < 0 && errno != ENOENT) {
virReportSystemError(errno, _("cannot unlink '%s'"), virReportSystemError(errno, _("cannot unlink '%s'"),
secret->configFile); obj->configFile);
return -1; return -1;
} }
@ -654,11 +652,11 @@ virSecretObjDeleteConfig(virSecretObjPtr secret)
void void
virSecretObjDeleteData(virSecretObjPtr secret) virSecretObjDeleteData(virSecretObjPtr obj)
{ {
/* The configFile will already be removed, so secret won't be /* The configFile will already be removed, so secret won't be
* loaded again if this fails */ * loaded again if this fails */
(void)unlink(secret->base64File); (void)unlink(obj->base64File);
} }
@ -669,15 +667,15 @@ virSecretObjDeleteData(virSecretObjPtr secret)
secret is defined, it is stored as base64 (with no formatting) in secret is defined, it is stored as base64 (with no formatting) in
"$basename.base64". "$basename" is in both cases the base64-encoded UUID. */ "$basename.base64". "$basename" is in both cases the base64-encoded UUID. */
int int
virSecretObjSaveConfig(virSecretObjPtr secret) virSecretObjSaveConfig(virSecretObjPtr obj)
{ {
char *xml = NULL; char *xml = NULL;
int ret = -1; int ret = -1;
if (!(xml = virSecretDefFormat(secret->def))) if (!(xml = virSecretDefFormat(obj->def)))
goto cleanup; goto cleanup;
if (virFileRewriteStr(secret->configFile, S_IRUSR | S_IWUSR, xml) < 0) if (virFileRewriteStr(obj->configFile, S_IRUSR | S_IWUSR, xml) < 0)
goto cleanup; goto cleanup;
ret = 0; ret = 0;
@ -689,18 +687,18 @@ virSecretObjSaveConfig(virSecretObjPtr secret)
int int
virSecretObjSaveData(virSecretObjPtr secret) virSecretObjSaveData(virSecretObjPtr obj)
{ {
char *base64 = NULL; char *base64 = NULL;
int ret = -1; int ret = -1;
if (!secret->value) if (!obj->value)
return 0; return 0;
if (!(base64 = virStringEncodeBase64(secret->value, secret->value_size))) if (!(base64 = virStringEncodeBase64(obj->value, obj->value_size)))
goto cleanup; goto cleanup;
if (virFileRewriteStr(secret->base64File, S_IRUSR | S_IWUSR, base64) < 0) if (virFileRewriteStr(obj->base64File, S_IRUSR | S_IWUSR, base64) < 0)
goto cleanup; goto cleanup;
ret = 0; ret = 0;
@ -712,36 +710,36 @@ virSecretObjSaveData(virSecretObjPtr secret)
virSecretDefPtr virSecretDefPtr
virSecretObjGetDef(virSecretObjPtr secret) virSecretObjGetDef(virSecretObjPtr obj)
{ {
return secret->def; return obj->def;
} }
void void
virSecretObjSetDef(virSecretObjPtr secret, virSecretObjSetDef(virSecretObjPtr obj,
virSecretDefPtr def) virSecretDefPtr def)
{ {
secret->def = def; obj->def = def;
} }
unsigned char * unsigned char *
virSecretObjGetValue(virSecretObjPtr secret) virSecretObjGetValue(virSecretObjPtr obj)
{ {
unsigned char *ret = NULL; unsigned char *ret = NULL;
if (!secret->value) { if (!obj->value) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(obj->def->uuid, uuidstr);
virReportError(VIR_ERR_NO_SECRET, virReportError(VIR_ERR_NO_SECRET,
_("secret '%s' does not have a value"), uuidstr); _("secret '%s' does not have a value"), uuidstr);
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(ret, secret->value_size) < 0) if (VIR_ALLOC_N(ret, obj->value_size) < 0)
goto cleanup; goto cleanup;
memcpy(ret, secret->value, secret->value_size); memcpy(ret, obj->value, obj->value_size);
cleanup: cleanup:
return ret; return ret;
@ -749,7 +747,7 @@ virSecretObjGetValue(virSecretObjPtr secret)
int int
virSecretObjSetValue(virSecretObjPtr secret, virSecretObjSetValue(virSecretObjPtr obj,
const unsigned char *value, const unsigned char *value,
size_t value_size) size_t value_size)
{ {
@ -759,14 +757,14 @@ virSecretObjSetValue(virSecretObjPtr secret,
if (VIR_ALLOC_N(new_value, value_size) < 0) if (VIR_ALLOC_N(new_value, value_size) < 0)
return -1; return -1;
old_value = secret->value; old_value = obj->value;
old_value_size = secret->value_size; old_value_size = obj->value_size;
memcpy(new_value, value, value_size); memcpy(new_value, value, value_size);
secret->value = new_value; obj->value = new_value;
secret->value_size = value_size; obj->value_size = value_size;
if (!secret->def->isephemeral && virSecretObjSaveData(secret) < 0) if (!obj->def->isephemeral && virSecretObjSaveData(obj) < 0)
goto error; goto error;
/* Saved successfully - drop old value */ /* Saved successfully - drop old value */
@ -779,8 +777,8 @@ virSecretObjSetValue(virSecretObjPtr secret,
error: error:
/* Error - restore previous state and free new value */ /* Error - restore previous state and free new value */
secret->value = old_value; obj->value = old_value;
secret->value_size = old_value_size; obj->value_size = old_value_size;
memset(new_value, 0, value_size); memset(new_value, 0, value_size);
VIR_FREE(new_value); VIR_FREE(new_value);
return -1; return -1;
@ -788,17 +786,17 @@ virSecretObjSetValue(virSecretObjPtr secret,
size_t size_t
virSecretObjGetValueSize(virSecretObjPtr secret) virSecretObjGetValueSize(virSecretObjPtr obj)
{ {
return secret->value_size; return obj->value_size;
} }
void void
virSecretObjSetValueSize(virSecretObjPtr secret, virSecretObjSetValueSize(virSecretObjPtr obj,
size_t value_size) size_t value_size)
{ {
secret->value_size = value_size; obj->value_size = value_size;
} }
@ -822,33 +820,33 @@ virSecretLoadValidateUUID(virSecretDefPtr def,
static int static int
virSecretLoadValue(virSecretObjPtr secret) virSecretLoadValue(virSecretObjPtr obj)
{ {
int ret = -1, fd = -1; int ret = -1, fd = -1;
struct stat st; struct stat st;
char *contents = NULL, *value = NULL; char *contents = NULL, *value = NULL;
size_t value_size; size_t value_size;
if ((fd = open(secret->base64File, O_RDONLY)) == -1) { if ((fd = open(obj->base64File, O_RDONLY)) == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
ret = 0; ret = 0;
goto cleanup; goto cleanup;
} }
virReportSystemError(errno, _("cannot open '%s'"), virReportSystemError(errno, _("cannot open '%s'"),
secret->base64File); obj->base64File);
goto cleanup; goto cleanup;
} }
if (fstat(fd, &st) < 0) { if (fstat(fd, &st) < 0) {
virReportSystemError(errno, _("cannot stat '%s'"), virReportSystemError(errno, _("cannot stat '%s'"),
secret->base64File); obj->base64File);
goto cleanup; goto cleanup;
} }
if ((size_t)st.st_size != st.st_size) { if ((size_t)st.st_size != st.st_size) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("'%s' file does not fit in memory"), _("'%s' file does not fit in memory"),
secret->base64File); obj->base64File);
goto cleanup; goto cleanup;
} }
@ -857,7 +855,7 @@ virSecretLoadValue(virSecretObjPtr secret)
if (saferead(fd, contents, st.st_size) != st.st_size) { if (saferead(fd, contents, st.st_size) != st.st_size) {
virReportSystemError(errno, _("cannot read '%s'"), virReportSystemError(errno, _("cannot read '%s'"),
secret->base64File); obj->base64File);
goto cleanup; goto cleanup;
} }
@ -866,15 +864,15 @@ virSecretLoadValue(virSecretObjPtr secret)
if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) { if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid base64 in '%s'"), _("invalid base64 in '%s'"),
secret->base64File); obj->base64File);
goto cleanup; goto cleanup;
} }
if (value == NULL) if (value == NULL)
goto cleanup; goto cleanup;
secret->value = (unsigned char *)value; obj->value = (unsigned char *)value;
value = NULL; value = NULL;
secret->value_size = value_size; obj->value_size = value_size;
ret = 0; ret = 0;
@ -899,7 +897,8 @@ virSecretLoad(virSecretObjListPtr secrets,
const char *configDir) const char *configDir)
{ {
virSecretDefPtr def = NULL; virSecretDefPtr def = NULL;
virSecretObjPtr secret = NULL, ret = NULL; virSecretObjPtr obj = NULL;
virSecretObjPtr ret = NULL;
if (!(def = virSecretDefParseFile(path))) if (!(def = virSecretDefParseFile(path)))
goto cleanup; goto cleanup;
@ -907,18 +906,18 @@ virSecretLoad(virSecretObjListPtr secrets,
if (virSecretLoadValidateUUID(def, file) < 0) if (virSecretLoadValidateUUID(def, file) < 0)
goto cleanup; goto cleanup;
if (!(secret = virSecretObjListAdd(secrets, def, configDir, NULL))) if (!(obj = virSecretObjListAdd(secrets, def, configDir, NULL)))
goto cleanup; goto cleanup;
def = NULL; def = NULL;
if (virSecretLoadValue(secret) < 0) if (virSecretLoadValue(obj) < 0)
goto cleanup; goto cleanup;
ret = secret; ret = obj;
secret = NULL; obj = NULL;
cleanup: cleanup:
virSecretObjListRemove(secrets, secret); virSecretObjListRemove(secrets, obj);
virSecretDefFree(def); virSecretDefFree(def);
return ret; return ret;
} }
@ -939,7 +938,7 @@ virSecretLoadAllConfigs(virSecretObjListPtr secrets,
* loop (if any). It's better to keep the secrets we managed to find. */ * loop (if any). It's better to keep the secrets we managed to find. */
while (virDirRead(dir, &de, NULL) > 0) { while (virDirRead(dir, &de, NULL) > 0) {
char *path; char *path;
virSecretObjPtr secret; virSecretObjPtr obj;
if (!virFileHasSuffix(de->d_name, ".xml")) if (!virFileHasSuffix(de->d_name, ".xml"))
continue; continue;
@ -947,7 +946,7 @@ virSecretLoadAllConfigs(virSecretObjListPtr secrets,
if (!(path = virFileBuildPath(configDir, de->d_name, NULL))) if (!(path = virFileBuildPath(configDir, de->d_name, NULL)))
continue; continue;
if (!(secret = virSecretLoad(secrets, de->d_name, path, configDir))) { if (!(obj = virSecretLoad(secrets, de->d_name, path, configDir))) {
VIR_ERROR(_("Error reading secret: %s"), VIR_ERROR(_("Error reading secret: %s"),
virGetLastErrorMessage()); virGetLastErrorMessage());
VIR_FREE(path); VIR_FREE(path);
@ -955,7 +954,7 @@ virSecretLoadAllConfigs(virSecretObjListPtr secrets,
} }
VIR_FREE(path); VIR_FREE(path);
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
} }
VIR_DIR_CLOSE(dir); VIR_DIR_CLOSE(dir);

View File

@ -30,7 +30,7 @@ typedef struct _virSecretObj virSecretObj;
typedef virSecretObj *virSecretObjPtr; typedef virSecretObj *virSecretObjPtr;
void void
virSecretObjEndAPI(virSecretObjPtr *secret); virSecretObjEndAPI(virSecretObjPtr *obj);
typedef struct _virSecretObjList virSecretObjList; typedef struct _virSecretObjList virSecretObjList;
typedef virSecretObjList *virSecretObjListPtr; typedef virSecretObjList *virSecretObjListPtr;
@ -49,11 +49,11 @@ virSecretObjListFindByUsage(virSecretObjListPtr secrets,
void void
virSecretObjListRemove(virSecretObjListPtr secrets, virSecretObjListRemove(virSecretObjListPtr secrets,
virSecretObjPtr secret); virSecretObjPtr obj);
virSecretObjPtr virSecretObjPtr
virSecretObjListAdd(virSecretObjListPtr secrets, virSecretObjListAdd(virSecretObjListPtr secrets,
virSecretDefPtr def, virSecretDefPtr newdef,
const char *configDir, const char *configDir,
virSecretDefPtr *oldDef); virSecretDefPtr *oldDef);
@ -81,37 +81,37 @@ virSecretObjListGetUUIDs(virSecretObjListPtr secrets,
virConnectPtr conn); virConnectPtr conn);
int int
virSecretObjDeleteConfig(virSecretObjPtr secret); virSecretObjDeleteConfig(virSecretObjPtr obj);
void void
virSecretObjDeleteData(virSecretObjPtr secret); virSecretObjDeleteData(virSecretObjPtr obj);
int int
virSecretObjSaveConfig(virSecretObjPtr secret); virSecretObjSaveConfig(virSecretObjPtr obj);
int int
virSecretObjSaveData(virSecretObjPtr secret); virSecretObjSaveData(virSecretObjPtr obj);
virSecretDefPtr virSecretDefPtr
virSecretObjGetDef(virSecretObjPtr secret); virSecretObjGetDef(virSecretObjPtr obj);
void void
virSecretObjSetDef(virSecretObjPtr secret, virSecretObjSetDef(virSecretObjPtr obj,
virSecretDefPtr def); virSecretDefPtr def);
unsigned char * unsigned char *
virSecretObjGetValue(virSecretObjPtr secret); virSecretObjGetValue(virSecretObjPtr obj);
int int
virSecretObjSetValue(virSecretObjPtr secret, virSecretObjSetValue(virSecretObjPtr obj,
const unsigned char *value, const unsigned char *value,
size_t value_size); size_t value_size);
size_t size_t
virSecretObjGetValueSize(virSecretObjPtr secret); virSecretObjGetValueSize(virSecretObjPtr obj);
void void
virSecretObjSetValueSize(virSecretObjPtr secret, virSecretObjSetValueSize(virSecretObjPtr obj,
size_t value_size); size_t value_size);
int int

View File

@ -156,10 +156,10 @@ secretLookupByUUID(virConnectPtr conn,
const unsigned char *uuid) const unsigned char *uuid)
{ {
virSecretPtr ret = NULL; virSecretPtr ret = NULL;
virSecretObjPtr secret; virSecretObjPtr obj;
virSecretDefPtr def; virSecretDefPtr def;
if (!(secret = virSecretObjListFindByUUID(driver->secrets, uuid))) { if (!(obj = virSecretObjListFindByUUID(driver->secrets, uuid))) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(uuid, uuidstr); virUUIDFormat(uuid, uuidstr);
virReportError(VIR_ERR_NO_SECRET, virReportError(VIR_ERR_NO_SECRET,
@ -167,7 +167,7 @@ secretLookupByUUID(virConnectPtr conn,
goto cleanup; goto cleanup;
} }
def = virSecretObjGetDef(secret); def = virSecretObjGetDef(obj);
if (virSecretLookupByUUIDEnsureACL(conn, def) < 0) if (virSecretLookupByUUIDEnsureACL(conn, def) < 0)
goto cleanup; goto cleanup;
@ -177,7 +177,7 @@ secretLookupByUUID(virConnectPtr conn,
def->usage_id); def->usage_id);
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
return ret; return ret;
} }
@ -188,17 +188,17 @@ secretLookupByUsage(virConnectPtr conn,
const char *usageID) const char *usageID)
{ {
virSecretPtr ret = NULL; virSecretPtr ret = NULL;
virSecretObjPtr secret; virSecretObjPtr obj;
virSecretDefPtr def; virSecretDefPtr def;
if (!(secret = virSecretObjListFindByUsage(driver->secrets, if (!(obj = virSecretObjListFindByUsage(driver->secrets,
usageType, usageID))) { usageType, usageID))) {
virReportError(VIR_ERR_NO_SECRET, virReportError(VIR_ERR_NO_SECRET,
_("no secret with matching usage '%s'"), usageID); _("no secret with matching usage '%s'"), usageID);
goto cleanup; goto cleanup;
} }
def = virSecretObjGetDef(secret); def = virSecretObjGetDef(obj);
if (virSecretLookupByUsageEnsureACL(conn, def) < 0) if (virSecretLookupByUsageEnsureACL(conn, def) < 0)
goto cleanup; goto cleanup;
@ -208,7 +208,7 @@ secretLookupByUsage(virConnectPtr conn,
def->usage_id); def->usage_id);
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
return ret; return ret;
} }
@ -219,74 +219,74 @@ secretDefineXML(virConnectPtr conn,
unsigned int flags) unsigned int flags)
{ {
virSecretPtr ret = NULL; virSecretPtr ret = NULL;
virSecretObjPtr secret = NULL; virSecretObjPtr obj = NULL;
virSecretDefPtr backup = NULL; virSecretDefPtr backup = NULL;
virSecretDefPtr new_attrs; virSecretDefPtr def;
virObjectEventPtr event = NULL; virObjectEventPtr event = NULL;
virCheckFlags(0, NULL); virCheckFlags(0, NULL);
if (!(new_attrs = virSecretDefParseString(xml))) if (!(def = virSecretDefParseString(xml)))
return NULL; return NULL;
if (virSecretDefineXMLEnsureACL(conn, new_attrs) < 0) if (virSecretDefineXMLEnsureACL(conn, def) < 0)
goto cleanup; goto cleanup;
if (!(secret = virSecretObjListAdd(driver->secrets, new_attrs, if (!(obj = virSecretObjListAdd(driver->secrets, def,
driver->configDir, &backup))) driver->configDir, &backup)))
goto cleanup; goto cleanup;
if (!new_attrs->isephemeral) { if (!def->isephemeral) {
if (secretEnsureDirectory() < 0) if (secretEnsureDirectory() < 0)
goto cleanup; goto cleanup;
if (backup && backup->isephemeral) { if (backup && backup->isephemeral) {
if (virSecretObjSaveData(secret) < 0) if (virSecretObjSaveData(obj) < 0)
goto restore_backup; goto restore_backup;
} }
if (virSecretObjSaveConfig(secret) < 0) { if (virSecretObjSaveConfig(obj) < 0) {
if (backup && backup->isephemeral) { if (backup && backup->isephemeral) {
/* Undo the virSecretObjSaveData() above; ignore errors */ /* Undo the virSecretObjSaveData() above; ignore errors */
virSecretObjDeleteData(secret); virSecretObjDeleteData(obj);
} }
goto restore_backup; goto restore_backup;
} }
} else if (backup && !backup->isephemeral) { } else if (backup && !backup->isephemeral) {
if (virSecretObjDeleteConfig(secret) < 0) if (virSecretObjDeleteConfig(obj) < 0)
goto restore_backup; goto restore_backup;
virSecretObjDeleteData(secret); virSecretObjDeleteData(obj);
} }
/* Saved successfully - drop old values */ /* Saved successfully - drop old values */
virSecretDefFree(backup); virSecretDefFree(backup);
event = virSecretEventLifecycleNew(new_attrs->uuid, event = virSecretEventLifecycleNew(def->uuid,
new_attrs->usage_type, def->usage_type,
new_attrs->usage_id, def->usage_id,
VIR_SECRET_EVENT_DEFINED, VIR_SECRET_EVENT_DEFINED,
0); 0);
ret = virGetSecret(conn, ret = virGetSecret(conn,
new_attrs->uuid, def->uuid,
new_attrs->usage_type, def->usage_type,
new_attrs->usage_id); def->usage_id);
new_attrs = NULL; def = NULL;
goto cleanup; goto cleanup;
restore_backup: restore_backup:
/* If we have a backup, then secret was defined before, so just restore /* If we have a backup, then secret was defined before, so just restore
* the backup. The current (new_attrs) will be handled below. * the backup. The current def will be handled below.
* Otherwise, this is a new secret, thus remove it. * Otherwise, this is a new secret, thus remove it.
*/ */
if (backup) if (backup)
virSecretObjSetDef(secret, backup); virSecretObjSetDef(obj, backup);
else else
virSecretObjListRemove(driver->secrets, secret); virSecretObjListRemove(driver->secrets, obj);
cleanup: cleanup:
virSecretDefFree(new_attrs); virSecretDefFree(def);
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
if (event) if (event)
virObjectEventStateQueue(driver->secretEventState, event); virObjectEventStateQueue(driver->secretEventState, event);
@ -294,54 +294,54 @@ secretDefineXML(virConnectPtr conn,
} }
static char * static char *
secretGetXMLDesc(virSecretPtr obj, secretGetXMLDesc(virSecretPtr secret,
unsigned int flags) unsigned int flags)
{ {
char *ret = NULL; char *ret = NULL;
virSecretObjPtr secret; virSecretObjPtr obj;
virSecretDefPtr def; virSecretDefPtr def;
virCheckFlags(0, NULL); virCheckFlags(0, NULL);
if (!(secret = secretObjFromSecret(obj))) if (!(obj = secretObjFromSecret(secret)))
goto cleanup; goto cleanup;
def = virSecretObjGetDef(secret); def = virSecretObjGetDef(obj);
if (virSecretGetXMLDescEnsureACL(obj->conn, def) < 0) if (virSecretGetXMLDescEnsureACL(secret->conn, def) < 0)
goto cleanup; goto cleanup;
ret = virSecretDefFormat(def); ret = virSecretDefFormat(def);
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
return ret; return ret;
} }
static int static int
secretSetValue(virSecretPtr obj, secretSetValue(virSecretPtr secret,
const unsigned char *value, const unsigned char *value,
size_t value_size, size_t value_size,
unsigned int flags) unsigned int flags)
{ {
int ret = -1; int ret = -1;
virSecretObjPtr secret; virSecretObjPtr obj;
virSecretDefPtr def; virSecretDefPtr def;
virObjectEventPtr event = NULL; virObjectEventPtr event = NULL;
virCheckFlags(0, -1); virCheckFlags(0, -1);
if (!(secret = secretObjFromSecret(obj))) if (!(obj = secretObjFromSecret(secret)))
goto cleanup; goto cleanup;
def = virSecretObjGetDef(secret); def = virSecretObjGetDef(obj);
if (virSecretSetValueEnsureACL(obj->conn, def) < 0) if (virSecretSetValueEnsureACL(secret->conn, def) < 0)
goto cleanup; goto cleanup;
if (secretEnsureDirectory() < 0) if (secretEnsureDirectory() < 0)
goto cleanup; goto cleanup;
if (virSecretObjSetValue(secret, value, value_size) < 0) if (virSecretObjSetValue(obj, value, value_size) < 0)
goto cleanup; goto cleanup;
event = virSecretEventValueChangedNew(def->uuid, event = virSecretEventValueChangedNew(def->uuid,
@ -350,7 +350,7 @@ secretSetValue(virSecretPtr obj,
ret = 0; ret = 0;
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
if (event) if (event)
virObjectEventStateQueue(driver->secretEventState, event); virObjectEventStateQueue(driver->secretEventState, event);
@ -358,22 +358,22 @@ secretSetValue(virSecretPtr obj,
} }
static unsigned char * static unsigned char *
secretGetValue(virSecretPtr obj, secretGetValue(virSecretPtr secret,
size_t *value_size, size_t *value_size,
unsigned int flags, unsigned int flags,
unsigned int internalFlags) unsigned int internalFlags)
{ {
unsigned char *ret = NULL; unsigned char *ret = NULL;
virSecretObjPtr secret; virSecretObjPtr obj;
virSecretDefPtr def; virSecretDefPtr def;
virCheckFlags(0, NULL); virCheckFlags(0, NULL);
if (!(secret = secretObjFromSecret(obj))) if (!(obj = secretObjFromSecret(secret)))
goto cleanup; goto cleanup;
def = virSecretObjGetDef(secret); def = virSecretObjGetDef(obj);
if (virSecretGetValueEnsureACL(obj->conn, def) < 0) if (virSecretGetValueEnsureACL(secret->conn, def) < 0)
goto cleanup; goto cleanup;
if ((internalFlags & VIR_SECRET_GET_VALUE_INTERNAL_CALL) == 0 && if ((internalFlags & VIR_SECRET_GET_VALUE_INTERNAL_CALL) == 0 &&
@ -383,33 +383,33 @@ secretGetValue(virSecretPtr obj,
goto cleanup; goto cleanup;
} }
if (!(ret = virSecretObjGetValue(secret))) if (!(ret = virSecretObjGetValue(obj)))
goto cleanup; goto cleanup;
*value_size = virSecretObjGetValueSize(secret); *value_size = virSecretObjGetValueSize(obj);
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
return ret; return ret;
} }
static int static int
secretUndefine(virSecretPtr obj) secretUndefine(virSecretPtr secret)
{ {
int ret = -1; int ret = -1;
virSecretObjPtr secret; virSecretObjPtr obj;
virSecretDefPtr def; virSecretDefPtr def;
virObjectEventPtr event = NULL; virObjectEventPtr event = NULL;
if (!(secret = secretObjFromSecret(obj))) if (!(obj = secretObjFromSecret(secret)))
goto cleanup; goto cleanup;
def = virSecretObjGetDef(secret); def = virSecretObjGetDef(obj);
if (virSecretUndefineEnsureACL(obj->conn, def) < 0) if (virSecretUndefineEnsureACL(secret->conn, def) < 0)
goto cleanup; goto cleanup;
if (virSecretObjDeleteConfig(secret) < 0) if (virSecretObjDeleteConfig(obj) < 0)
goto cleanup; goto cleanup;
event = virSecretEventLifecycleNew(def->uuid, event = virSecretEventLifecycleNew(def->uuid,
@ -418,14 +418,14 @@ secretUndefine(virSecretPtr obj)
VIR_SECRET_EVENT_UNDEFINED, VIR_SECRET_EVENT_UNDEFINED,
0); 0);
virSecretObjDeleteData(secret); virSecretObjDeleteData(obj);
virSecretObjListRemove(driver->secrets, secret); virSecretObjListRemove(driver->secrets, obj);
ret = 0; ret = 0;
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&obj);
if (event) if (event)
virObjectEventStateQueue(driver->secretEventState, event); virObjectEventStateQueue(driver->secretEventState, event);