提交 d44f5618 编写于 作者: J John Ferlan

secret: Various formatting cleanups

Rather than having it interspersed with other changes, do it once.

Remove a couple ^L, 1 argument per line for functions, less than 80 chars
per line, use of spacing between logical groups of code, use of one line
if statements when doing fetch followed by comparison, use direct return
when no cleanup to be done.
Signed-off-by: NJohn Ferlan <jferlan@redhat.com>
上级 85d74806
/* /*
* secret_driver.c: local driver for secret manipulation API * secret_driver.c: local driver for secret manipulation API
* *
* Copyright (C) 2009-2014 Red Hat, Inc. * Copyright (C) 2009-2016 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -50,7 +50,7 @@ VIR_LOG_INIT("secret.secret_driver"); ...@@ -50,7 +50,7 @@ VIR_LOG_INIT("secret.secret_driver");
enum { SECRET_MAX_XML_FILE = 10*1024*1024 }; enum { SECRET_MAX_XML_FILE = 10*1024*1024 };
/* Internal driver state */ /* Internal driver state */
typedef struct _virSecretEntry virSecretEntry; typedef struct _virSecretEntry virSecretEntry;
typedef virSecretEntry *virSecretEntryPtr; typedef virSecretEntry *virSecretEntryPtr;
...@@ -94,7 +94,8 @@ listUnlink(virSecretEntryPtr *pptr) ...@@ -94,7 +94,8 @@ listUnlink(virSecretEntryPtr *pptr)
} }
static void static void
listInsert(virSecretEntryPtr *pptr, virSecretEntryPtr secret) listInsert(virSecretEntryPtr *pptr,
virSecretEntryPtr secret)
{ {
secret->next = *pptr; secret->next = *pptr;
*pptr = secret; *pptr = secret;
...@@ -128,7 +129,8 @@ secretFindByUUID(const unsigned char *uuid) ...@@ -128,7 +129,8 @@ secretFindByUUID(const unsigned char *uuid)
} }
static virSecretEntryPtr static virSecretEntryPtr
secretFindByUsage(int usageType, const char *usageID) secretFindByUsage(int usageType,
const char *usageID)
{ {
virSecretEntryPtr *pptr, s; virSecretEntryPtr *pptr, s;
...@@ -170,7 +172,9 @@ secretFindByUsage(int usageType, const char *usageID) ...@@ -170,7 +172,9 @@ secretFindByUsage(int usageType, const char *usageID)
"$basename.base64". "$basename" is in both cases the base64-encoded UUID. */ "$basename.base64". "$basename" is in both cases the base64-encoded UUID. */
static int static int
replaceFile(const char *filename, void *data, size_t size) replaceFile(const char *filename,
void *data,
size_t size)
{ {
char *tmp_path = NULL; char *tmp_path = NULL;
int fd = -1, ret = -1; int fd = -1, ret = -1;
...@@ -217,14 +221,16 @@ replaceFile(const char *filename, void *data, size_t size) ...@@ -217,14 +221,16 @@ replaceFile(const char *filename, void *data, size_t size)
} }
static char * static char *
secretComputePath(const virSecretEntry *secret, const char *suffix) secretComputePath(const virSecretEntry *secret,
const char *suffix)
{ {
char *ret; char *ret;
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(secret->def->uuid, uuidstr);
ignore_value(virAsprintf(&ret, "%s/%s%s", driver->directory, uuidstr, suffix)); ignore_value(virAsprintf(&ret, "%s/%s%s", driver->directory,
uuidstr, suffix));
return ret; return ret;
} }
...@@ -260,11 +266,10 @@ secretSaveDef(const virSecretEntry *secret) ...@@ -260,11 +266,10 @@ secretSaveDef(const virSecretEntry *secret)
if (secretEnsureDirectory() < 0) if (secretEnsureDirectory() < 0)
goto cleanup; goto cleanup;
filename = secretXMLPath(secret); if (!(filename = secretXMLPath(secret)))
if (filename == NULL)
goto cleanup; goto cleanup;
xml = virSecretDefFormat(secret->def);
if (xml == NULL) if (!(xml = virSecretDefFormat(secret->def)))
goto cleanup; goto cleanup;
if (replaceFile(filename, xml, strlen(xml)) < 0) if (replaceFile(filename, xml, strlen(xml)) < 0)
...@@ -290,9 +295,9 @@ secretSaveValue(const virSecretEntry *secret) ...@@ -290,9 +295,9 @@ secretSaveValue(const virSecretEntry *secret)
if (secretEnsureDirectory() < 0) if (secretEnsureDirectory() < 0)
goto cleanup; goto cleanup;
filename = secretBase64Path(secret); if (!(filename = secretBase64Path(secret)))
if (filename == NULL)
goto cleanup; goto cleanup;
base64_encode_alloc((const char *)secret->value, secret->value_size, base64_encode_alloc((const char *)secret->value, secret->value_size,
&base64); &base64);
if (base64 == NULL) { if (base64 == NULL) {
...@@ -317,11 +322,10 @@ secretDeleteSaved(const virSecretEntry *secret) ...@@ -317,11 +322,10 @@ secretDeleteSaved(const virSecretEntry *secret)
char *xml_filename = NULL, *value_filename = NULL; char *xml_filename = NULL, *value_filename = NULL;
int ret = -1; int ret = -1;
xml_filename = secretXMLPath(secret); if (!(xml_filename = secretXMLPath(secret)))
if (xml_filename == NULL)
goto cleanup; goto cleanup;
value_filename = secretBase64Path(secret);
if (value_filename == NULL) if (!(value_filename = secretBase64Path(secret)))
goto cleanup; goto cleanup;
if (unlink(xml_filename) < 0 && errno != ENOENT) if (unlink(xml_filename) < 0 && errno != ENOENT)
...@@ -364,12 +368,10 @@ secretLoadValue(virSecretEntryPtr secret) ...@@ -364,12 +368,10 @@ secretLoadValue(virSecretEntryPtr secret)
char *filename = NULL, *contents = NULL, *value = NULL; char *filename = NULL, *contents = NULL, *value = NULL;
size_t value_size; size_t value_size;
filename = secretBase64Path(secret); if (!(filename = secretBase64Path(secret)))
if (filename == NULL)
goto cleanup; goto cleanup;
fd = open(filename, O_RDONLY); if ((fd = open(filename, O_RDONLY)) == -1) {
if (fd == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
ret = 0; ret = 0;
goto cleanup; goto cleanup;
...@@ -377,10 +379,12 @@ secretLoadValue(virSecretEntryPtr secret) ...@@ -377,10 +379,12 @@ secretLoadValue(virSecretEntryPtr secret)
virReportSystemError(errno, _("cannot open '%s'"), filename); virReportSystemError(errno, _("cannot open '%s'"), filename);
goto cleanup; goto cleanup;
} }
if (fstat(fd, &st) < 0) { if (fstat(fd, &st) < 0) {
virReportSystemError(errno, _("cannot stat '%s'"), filename); virReportSystemError(errno, _("cannot stat '%s'"), filename);
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"), filename); _("'%s' file does not fit in memory"), filename);
...@@ -389,10 +393,12 @@ secretLoadValue(virSecretEntryPtr secret) ...@@ -389,10 +393,12 @@ secretLoadValue(virSecretEntryPtr secret)
if (VIR_ALLOC_N(contents, st.st_size) < 0) if (VIR_ALLOC_N(contents, st.st_size) < 0)
goto cleanup; goto cleanup;
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'"), filename); virReportSystemError(errno, _("cannot read '%s'"), filename);
goto cleanup; goto cleanup;
} }
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) { if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) {
...@@ -433,9 +439,10 @@ secretLoad(const char *xml_basename) ...@@ -433,9 +439,10 @@ secretLoad(const char *xml_basename)
if (virAsprintf(&xml_filename, "%s/%s", driver->directory, if (virAsprintf(&xml_filename, "%s/%s", driver->directory,
xml_basename) < 0) xml_basename) < 0)
goto cleanup; goto cleanup;
def = virSecretDefParseFile(xml_filename);
if (def == NULL) if (!(def = virSecretDefParseFile(xml_filename)))
goto cleanup; goto cleanup;
VIR_FREE(xml_filename); VIR_FREE(xml_filename);
if (secretLoadValidateUUID(def, xml_basename) < 0) if (secretLoadValidateUUID(def, xml_basename) < 0)
...@@ -462,29 +469,28 @@ secretLoad(const char *xml_basename) ...@@ -462,29 +469,28 @@ secretLoad(const char *xml_basename)
static int static int
loadSecrets(virSecretEntryPtr *dest) loadSecrets(virSecretEntryPtr *dest)
{ {
int ret = -1;
DIR *dir = NULL; DIR *dir = NULL;
struct dirent *de; struct dirent *de;
virSecretEntryPtr list = NULL; virSecretEntryPtr list = NULL;
dir = opendir(driver->directory); if (!(dir = opendir(driver->directory))) {
if (dir == NULL) {
if (errno == ENOENT) if (errno == ENOENT)
return 0; return 0;
virReportSystemError(errno, _("cannot open '%s'"), virReportSystemError(errno, _("cannot open '%s'"),
driver->directory); driver->directory);
goto cleanup; return -1;
} }
while (virDirRead(dir, &de, NULL) > 0) { while (virDirRead(dir, &de, NULL) > 0) {
virSecretEntryPtr secret; virSecretEntryPtr secret;
if (STREQ(de->d_name, ".") || STREQ(de->d_name, "..")) if (STREQ(de->d_name, ".") || STREQ(de->d_name, ".."))
continue; continue;
if (!virFileHasSuffix(de->d_name, ".xml")) if (!virFileHasSuffix(de->d_name, ".xml"))
continue; continue;
secret = secretLoad(de->d_name); if (!(secret = secretLoad(de->d_name))) {
if (secret == NULL) {
virErrorPtr err = virGetLastError(); virErrorPtr err = virGetLastError();
VIR_ERROR(_("Error reading secret: %s"), VIR_ERROR(_("Error reading secret: %s"),
...@@ -492,6 +498,7 @@ loadSecrets(virSecretEntryPtr *dest) ...@@ -492,6 +498,7 @@ loadSecrets(virSecretEntryPtr *dest)
virResetError(err); virResetError(err);
continue; continue;
} }
listInsert(&list, secret); listInsert(&list, secret);
} }
/* Ignore error reported by readdir, if any. It's better to keep the /* Ignore error reported by readdir, if any. It's better to keep the
...@@ -504,15 +511,11 @@ loadSecrets(virSecretEntryPtr *dest) ...@@ -504,15 +511,11 @@ loadSecrets(virSecretEntryPtr *dest)
listInsert(dest, s); listInsert(dest, s);
} }
ret = 0; closedir(dir);
return 0;
cleanup:
if (dir != NULL)
closedir(dir);
return ret;
} }
/* Driver functions */ /* Driver functions */
static int static int
secretConnectNumOfSecrets(virConnectPtr conn) secretConnectNumOfSecrets(virConnectPtr conn)
...@@ -537,7 +540,9 @@ secretConnectNumOfSecrets(virConnectPtr conn) ...@@ -537,7 +540,9 @@ secretConnectNumOfSecrets(virConnectPtr conn)
} }
static int static int
secretConnectListSecrets(virConnectPtr conn, char **uuids, int maxuuids) secretConnectListSecrets(virConnectPtr conn,
char **uuids,
int maxuuids)
{ {
size_t i; size_t i;
virSecretEntryPtr secret; virSecretEntryPtr secret;
...@@ -679,15 +684,15 @@ secretConnectListAllSecrets(virConnectPtr conn, ...@@ -679,15 +684,15 @@ secretConnectListAllSecrets(virConnectPtr conn,
static virSecretPtr static virSecretPtr
secretLookupByUUID(virConnectPtr conn, const unsigned char *uuid) secretLookupByUUID(virConnectPtr conn,
const unsigned char *uuid)
{ {
virSecretPtr ret = NULL; virSecretPtr ret = NULL;
virSecretEntryPtr secret; virSecretEntryPtr secret;
secretDriverLock(); secretDriverLock();
secret = secretFindByUUID(uuid); if (!(secret = secretFindByUUID(uuid))) {
if (secret == NULL) {
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,
...@@ -710,15 +715,16 @@ secretLookupByUUID(virConnectPtr conn, const unsigned char *uuid) ...@@ -710,15 +715,16 @@ secretLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
static virSecretPtr static virSecretPtr
secretLookupByUsage(virConnectPtr conn, int usageType, const char *usageID) secretLookupByUsage(virConnectPtr conn,
int usageType,
const char *usageID)
{ {
virSecretPtr ret = NULL; virSecretPtr ret = NULL;
virSecretEntryPtr secret; virSecretEntryPtr secret;
secretDriverLock(); secretDriverLock();
secret = secretFindByUsage(usageType, usageID); if (!(secret = secretFindByUsage(usageType, usageID))) {
if (secret == NULL) {
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;
...@@ -739,7 +745,8 @@ secretLookupByUsage(virConnectPtr conn, int usageType, const char *usageID) ...@@ -739,7 +745,8 @@ secretLookupByUsage(virConnectPtr conn, int usageType, const char *usageID)
static virSecretPtr static virSecretPtr
secretDefineXML(virConnectPtr conn, const char *xml, secretDefineXML(virConnectPtr conn,
const char *xml,
unsigned int flags) unsigned int flags)
{ {
virSecretPtr ret = NULL; virSecretPtr ret = NULL;
...@@ -749,8 +756,7 @@ secretDefineXML(virConnectPtr conn, const char *xml, ...@@ -749,8 +756,7 @@ secretDefineXML(virConnectPtr conn, const char *xml,
virCheckFlags(0, NULL); virCheckFlags(0, NULL);
new_attrs = virSecretDefParseString(xml); if (!(new_attrs = virSecretDefParseString(xml)))
if (new_attrs == NULL)
return NULL; return NULL;
secretDriverLock(); secretDriverLock();
...@@ -758,16 +764,16 @@ secretDefineXML(virConnectPtr conn, const char *xml, ...@@ -758,16 +764,16 @@ secretDefineXML(virConnectPtr conn, const char *xml,
if (virSecretDefineXMLEnsureACL(conn, new_attrs) < 0) if (virSecretDefineXMLEnsureACL(conn, new_attrs) < 0)
goto cleanup; goto cleanup;
secret = secretFindByUUID(new_attrs->uuid); if (!(secret = secretFindByUUID(new_attrs->uuid))) {
if (secret == NULL) { /* No existing secret with same UUID,
/* No existing secret with same UUID, try look for matching usage instead */ * try look for matching usage instead */
const char *usageID = secretUsageIDForDef(new_attrs); const char *usageID = secretUsageIDForDef(new_attrs);
secret = secretFindByUsage(new_attrs->usage_type, usageID); if ((secret = secretFindByUsage(new_attrs->usage_type, usageID))) {
if (secret) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(secret->def->uuid, uuidstr);
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("a secret with UUID %s already defined for use with %s"), _("a secret with UUID %s already defined for "
"use with %s"),
uuidstr, usageID); uuidstr, usageID);
goto cleanup; goto cleanup;
} }
...@@ -785,7 +791,8 @@ secretDefineXML(virConnectPtr conn, const char *xml, ...@@ -785,7 +791,8 @@ secretDefineXML(virConnectPtr conn, const char *xml,
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(secret->def->uuid, uuidstr);
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("a secret with UUID %s is already defined for use with %s"), _("a secret with UUID %s is already defined "
"for use with %s"),
uuidstr, oldUsageID); uuidstr, oldUsageID);
goto cleanup; goto cleanup;
} }
...@@ -853,7 +860,8 @@ secretDefineXML(virConnectPtr conn, const char *xml, ...@@ -853,7 +860,8 @@ secretDefineXML(virConnectPtr conn, const char *xml,
} }
static char * static char *
secretGetXMLDesc(virSecretPtr obj, unsigned int flags) secretGetXMLDesc(virSecretPtr obj,
unsigned int flags)
{ {
char *ret = NULL; char *ret = NULL;
virSecretEntryPtr secret; virSecretEntryPtr secret;
...@@ -862,8 +870,7 @@ secretGetXMLDesc(virSecretPtr obj, unsigned int flags) ...@@ -862,8 +870,7 @@ secretGetXMLDesc(virSecretPtr obj, unsigned int flags)
secretDriverLock(); secretDriverLock();
secret = secretFindByUUID(obj->uuid); if (!(secret = secretFindByUUID(obj->uuid))) {
if (secret == NULL) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(obj->uuid, uuidstr); virUUIDFormat(obj->uuid, uuidstr);
virReportError(VIR_ERR_NO_SECRET, virReportError(VIR_ERR_NO_SECRET,
...@@ -883,8 +890,10 @@ secretGetXMLDesc(virSecretPtr obj, unsigned int flags) ...@@ -883,8 +890,10 @@ secretGetXMLDesc(virSecretPtr obj, unsigned int flags)
} }
static int static int
secretSetValue(virSecretPtr obj, const unsigned char *value, secretSetValue(virSecretPtr obj,
size_t value_size, unsigned int flags) const unsigned char *value,
size_t value_size,
unsigned int flags)
{ {
int ret = -1; int ret = -1;
unsigned char *old_value, *new_value; unsigned char *old_value, *new_value;
...@@ -898,8 +907,7 @@ secretSetValue(virSecretPtr obj, const unsigned char *value, ...@@ -898,8 +907,7 @@ secretSetValue(virSecretPtr obj, const unsigned char *value,
secretDriverLock(); secretDriverLock();
secret = secretFindByUUID(obj->uuid); if (!(secret = secretFindByUUID(obj->uuid))) {
if (secret == NULL) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(obj->uuid, uuidstr); virUUIDFormat(obj->uuid, uuidstr);
virReportError(VIR_ERR_NO_SECRET, virReportError(VIR_ERR_NO_SECRET,
...@@ -945,7 +953,9 @@ secretSetValue(virSecretPtr obj, const unsigned char *value, ...@@ -945,7 +953,9 @@ secretSetValue(virSecretPtr obj, const unsigned char *value,
} }
static unsigned char * static unsigned char *
secretGetValue(virSecretPtr obj, size_t *value_size, unsigned int flags, secretGetValue(virSecretPtr obj,
size_t *value_size,
unsigned int flags,
unsigned int internalFlags) unsigned int internalFlags)
{ {
unsigned char *ret = NULL; unsigned char *ret = NULL;
...@@ -955,8 +965,7 @@ secretGetValue(virSecretPtr obj, size_t *value_size, unsigned int flags, ...@@ -955,8 +965,7 @@ secretGetValue(virSecretPtr obj, size_t *value_size, unsigned int flags,
secretDriverLock(); secretDriverLock();
secret = secretFindByUUID(obj->uuid); if (!(secret = secretFindByUUID(obj->uuid))) {
if (secret == NULL) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(obj->uuid, uuidstr); virUUIDFormat(obj->uuid, uuidstr);
virReportError(VIR_ERR_NO_SECRET, virReportError(VIR_ERR_NO_SECRET,
...@@ -1001,8 +1010,7 @@ secretUndefine(virSecretPtr obj) ...@@ -1001,8 +1010,7 @@ secretUndefine(virSecretPtr obj)
secretDriverLock(); secretDriverLock();
secret = secretFindByUUID(obj->uuid); if (!(secret = secretFindByUUID(obj->uuid))) {
if (secret == NULL) {
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
virUUIDFormat(obj->uuid, uuidstr); virUUIDFormat(obj->uuid, uuidstr);
virReportError(VIR_ERR_NO_SECRET, virReportError(VIR_ERR_NO_SECRET,
...@@ -1079,8 +1087,7 @@ secretStateInitialize(bool privileged, ...@@ -1079,8 +1087,7 @@ secretStateInitialize(bool privileged,
if (VIR_STRDUP(base, SYSCONFDIR "/libvirt") < 0) if (VIR_STRDUP(base, SYSCONFDIR "/libvirt") < 0)
goto error; goto error;
} else { } else {
base = virGetUserConfigDirectory(); if (!(base = virGetUserConfigDirectory()))
if (!base)
goto error; goto error;
} }
if (virAsprintf(&driver->directory, "%s/secrets", base) < 0) if (virAsprintf(&driver->directory, "%s/secrets", base) < 0)
...@@ -1113,8 +1120,9 @@ secretStateReload(void) ...@@ -1113,8 +1120,9 @@ secretStateReload(void)
if (loadSecrets(&new_secrets) < 0) if (loadSecrets(&new_secrets) < 0)
goto end; goto end;
/* Keep ephemeral secrets from current state. Discard non-ephemeral secrets /* Keep ephemeral secrets from current state.
that were removed by the secrets directory. */ * Discard non-ephemeral secrets that were removed
* by the secrets directory. */
while (driver->secrets != NULL) { while (driver->secrets != NULL) {
virSecretEntryPtr s; virSecretEntryPtr s;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册