提交 b9b664c5 编写于 作者: M Michal Privoznik

util: Introduce virFileRewriteStr

There are couple of places where we have a string and want to
save it to a file. Atomically. In all those places we use
virFileRewrite() but also implement the very same callback which
takes the string and write it into temp file. This makes no
sense. Unify the callbacks and move them to one place.
Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
上级 b379c44c
......@@ -692,20 +692,6 @@ virSecretObjDeleteData(virSecretObjPtr secret)
has virSecretDef stored as XML in "$basename.xml". If a value of the
secret is defined, it is stored as base64 (with no formatting) in
"$basename.base64". "$basename" is in both cases the base64-encoded UUID. */
static int
virSecretRewriteFile(int fd,
void *opaque)
{
char *data = opaque;
if (safewrite(fd, data, strlen(data)) < 0)
return -1;
return 0;
}
int
virSecretObjSaveConfig(virSecretObjPtr secret)
{
......@@ -715,8 +701,7 @@ virSecretObjSaveConfig(virSecretObjPtr secret)
if (!(xml = virSecretDefFormat(secret->def)))
goto cleanup;
if (virFileRewrite(secret->configFile, S_IRUSR | S_IWUSR,
virSecretRewriteFile, xml) < 0)
if (virFileRewriteStr(secret->configFile, S_IRUSR | S_IWUSR, xml) < 0)
goto cleanup;
ret = 0;
......@@ -739,8 +724,7 @@ virSecretObjSaveData(virSecretObjPtr secret)
if (!(base64 = virStringEncodeBase64(secret->value, secret->value_size)))
goto cleanup;
if (virFileRewrite(secret->base64File, S_IRUSR | S_IWUSR,
virSecretRewriteFile, base64) < 0)
if (virFileRewriteStr(secret->base64File, S_IRUSR | S_IWUSR, base64) < 0)
goto cleanup;
ret = 0;
......
......@@ -1601,6 +1601,7 @@ virFileRemoveLastComponent;
virFileResolveAllLinks;
virFileResolveLink;
virFileRewrite;
virFileRewriteStr;
virFileSanitizePath;
virFileSkipRoot;
virFileStripSuffix;
......
......@@ -64,17 +64,6 @@ usage(int status)
exit(status);
}
static int
customLeaseRewriteFile(int fd, void *opaque)
{
char **data = opaque;
if (safewrite(fd, *data, strlen(*data)) < 0)
return -1;
return 0;
}
/* Flags denoting actions for a lease */
enum virLeaseActionFlags {
VIR_LEASE_ACTION_ADD, /* Create new lease */
......@@ -252,8 +241,7 @@ main(int argc, char **argv)
}
/* Write to file */
if (virFileRewrite(custom_lease_file, 0644,
customLeaseRewriteFile, &leases_str) < 0)
if (virFileRewriteStr(custom_lease_file, 0644, leases_str) < 0)
goto cleanup;
break;
......
......@@ -443,7 +443,7 @@ int
virFileRewrite(const char *path,
mode_t mode,
virFileRewriteFunc rewrite,
void *opaque)
const void *opaque)
{
char *newfile = NULL;
int fd = -1;
......@@ -494,6 +494,28 @@ virFileRewrite(const char *path,
}
static int
virFileRewriteStrHelper(int fd, const void *opaque)
{
const char *data = opaque;
if (safewrite(fd, data, strlen(data)) < 0)
return -1;
return 0;
}
int
virFileRewriteStr(const char *path,
mode_t mode,
const char *str)
{
return virFileRewrite(path, mode,
virFileRewriteStrHelper, str);
}
int virFileTouch(const char *path, mode_t mode)
{
int fd = -1;
......
......@@ -101,11 +101,14 @@ void virFileWrapperFdFree(virFileWrapperFdPtr dfd);
int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock);
int virFileUnlock(int fd, off_t start, off_t len);
typedef int (*virFileRewriteFunc)(int fd, void *opaque);
typedef int (*virFileRewriteFunc)(int fd, const void *opaque);
int virFileRewrite(const char *path,
mode_t mode,
virFileRewriteFunc rewrite,
void *opaque);
const void *opaque);
int virFileRewriteStr(const char *path,
mode_t mode,
const char *str);
int virFileTouch(const char *path, mode_t mode);
......
......@@ -871,9 +871,9 @@ struct virXMLRewriteFileData {
};
static int
virXMLRewriteFile(int fd, void *opaque)
virXMLRewriteFile(int fd, const void *opaque)
{
struct virXMLRewriteFileData *data = opaque;
const struct virXMLRewriteFileData *data = opaque;
if (data->warnCommand) {
if (virXMLEmitWarning(fd, data->warnName, data->warnCommand) < 0)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册