secret: Use virFileRewrite instead of replaceFile

Use the common API instead of essentially open coding same functionality.

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2016-02-23 13:53:32 -05:00
parent d44f561824
commit 558a61a3d0

View File

@ -172,52 +172,15 @@ secretFindByUsage(int usageType,
"$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, secretRewriteFile(int fd,
void *data, void *opaque)
size_t size)
{ {
char *tmp_path = NULL; char *data = opaque;
int fd = -1, ret = -1;
if (virAsprintf(&tmp_path, "%sXXXXXX", filename) < 0) if (safewrite(fd, data, strlen(data)) < 0)
goto cleanup; return -1;
fd = mkostemp(tmp_path, O_CLOEXEC);
if (fd == -1) {
virReportSystemError(errno, _("mkostemp('%s') failed"), tmp_path);
goto cleanup;
}
if (fchmod(fd, S_IRUSR | S_IWUSR) != 0) {
virReportSystemError(errno, _("fchmod('%s') failed"), tmp_path);
goto cleanup;
}
ret = safewrite(fd, data, size); return 0;
if (ret < 0) {
virReportSystemError(errno, _("error writing to '%s'"),
tmp_path);
goto cleanup;
}
if (VIR_CLOSE(fd) < 0) {
virReportSystemError(errno, _("error closing '%s'"), tmp_path);
goto cleanup;
}
fd = -1;
if (rename(tmp_path, filename) < 0) {
virReportSystemError(errno, _("rename(%s, %s) failed"), tmp_path,
filename);
goto cleanup;
}
VIR_FREE(tmp_path);
ret = 0;
cleanup:
VIR_FORCE_CLOSE(fd);
if (tmp_path != NULL) {
unlink(tmp_path);
VIR_FREE(tmp_path);
}
return ret;
} }
static char * static char *
@ -272,7 +235,8 @@ secretSaveDef(const virSecretEntry *secret)
if (!(xml = virSecretDefFormat(secret->def))) if (!(xml = virSecretDefFormat(secret->def)))
goto cleanup; goto cleanup;
if (replaceFile(filename, xml, strlen(xml)) < 0) if (virFileRewrite(filename, S_IRUSR | S_IWUSR,
secretRewriteFile, xml) < 0)
goto cleanup; goto cleanup;
ret = 0; ret = 0;
@ -305,7 +269,8 @@ secretSaveValue(const virSecretEntry *secret)
goto cleanup; goto cleanup;
} }
if (replaceFile(filename, base64, strlen(base64)) < 0) if (virFileRewrite(filename, S_IRUSR | S_IWUSR,
secretRewriteFile, base64) < 0)
goto cleanup; goto cleanup;
ret = 0; ret = 0;