mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 03:21:19 +00:00
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: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
b379c44c35
commit
b9b664c5a8
@ -692,20 +692,6 @@ virSecretObjDeleteData(virSecretObjPtr secret)
|
|||||||
has virSecretDef stored as XML in "$basename.xml". If a value of the
|
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
|
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. */
|
||||||
|
|
||||||
static int
|
|
||||||
virSecretRewriteFile(int fd,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
char *data = opaque;
|
|
||||||
|
|
||||||
if (safewrite(fd, data, strlen(data)) < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
virSecretObjSaveConfig(virSecretObjPtr secret)
|
virSecretObjSaveConfig(virSecretObjPtr secret)
|
||||||
{
|
{
|
||||||
@ -715,8 +701,7 @@ virSecretObjSaveConfig(virSecretObjPtr secret)
|
|||||||
if (!(xml = virSecretDefFormat(secret->def)))
|
if (!(xml = virSecretDefFormat(secret->def)))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (virFileRewrite(secret->configFile, S_IRUSR | S_IWUSR,
|
if (virFileRewriteStr(secret->configFile, S_IRUSR | S_IWUSR, xml) < 0)
|
||||||
virSecretRewriteFile, xml) < 0)
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
@ -739,8 +724,7 @@ virSecretObjSaveData(virSecretObjPtr secret)
|
|||||||
if (!(base64 = virStringEncodeBase64(secret->value, secret->value_size)))
|
if (!(base64 = virStringEncodeBase64(secret->value, secret->value_size)))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (virFileRewrite(secret->base64File, S_IRUSR | S_IWUSR,
|
if (virFileRewriteStr(secret->base64File, S_IRUSR | S_IWUSR, base64) < 0)
|
||||||
virSecretRewriteFile, base64) < 0)
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
@ -1601,6 +1601,7 @@ virFileRemoveLastComponent;
|
|||||||
virFileResolveAllLinks;
|
virFileResolveAllLinks;
|
||||||
virFileResolveLink;
|
virFileResolveLink;
|
||||||
virFileRewrite;
|
virFileRewrite;
|
||||||
|
virFileRewriteStr;
|
||||||
virFileSanitizePath;
|
virFileSanitizePath;
|
||||||
virFileSkipRoot;
|
virFileSkipRoot;
|
||||||
virFileStripSuffix;
|
virFileStripSuffix;
|
||||||
|
@ -64,17 +64,6 @@ usage(int status)
|
|||||||
exit(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 */
|
/* Flags denoting actions for a lease */
|
||||||
enum virLeaseActionFlags {
|
enum virLeaseActionFlags {
|
||||||
VIR_LEASE_ACTION_ADD, /* Create new lease */
|
VIR_LEASE_ACTION_ADD, /* Create new lease */
|
||||||
@ -252,8 +241,7 @@ main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Write to file */
|
/* Write to file */
|
||||||
if (virFileRewrite(custom_lease_file, 0644,
|
if (virFileRewriteStr(custom_lease_file, 0644, leases_str) < 0)
|
||||||
customLeaseRewriteFile, &leases_str) < 0)
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -443,7 +443,7 @@ int
|
|||||||
virFileRewrite(const char *path,
|
virFileRewrite(const char *path,
|
||||||
mode_t mode,
|
mode_t mode,
|
||||||
virFileRewriteFunc rewrite,
|
virFileRewriteFunc rewrite,
|
||||||
void *opaque)
|
const void *opaque)
|
||||||
{
|
{
|
||||||
char *newfile = NULL;
|
char *newfile = NULL;
|
||||||
int fd = -1;
|
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 virFileTouch(const char *path, mode_t mode)
|
||||||
{
|
{
|
||||||
int fd = -1;
|
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 virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock);
|
||||||
int virFileUnlock(int fd, off_t start, off_t len);
|
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,
|
int virFileRewrite(const char *path,
|
||||||
mode_t mode,
|
mode_t mode,
|
||||||
virFileRewriteFunc rewrite,
|
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);
|
int virFileTouch(const char *path, mode_t mode);
|
||||||
|
|
||||||
|
@ -871,9 +871,9 @@ struct virXMLRewriteFileData {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static int
|
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 (data->warnCommand) {
|
||||||
if (virXMLEmitWarning(fd, data->warnName, data->warnCommand) < 0)
|
if (virXMLEmitWarning(fd, data->warnName, data->warnCommand) < 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user