mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 19:45:21 +00:00
vsh: Add helper for auto-removing temporary file
The vsh helpers for user-editing of contents use temporary files. Introduce 'vshTempFile' type which automatically removes the file. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
a3ef5414ed
commit
8c35dcf9fc
25
tools/vsh.c
25
tools/vsh.c
@ -2378,34 +2378,47 @@ vshAskReedit(vshControl *ctl,
|
|||||||
#endif /* WIN32 */
|
#endif /* WIN32 */
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
vshEditUnlinkTempfile(char *file)
|
||||||
|
{
|
||||||
|
if (!file)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ignore_value(unlink(file));
|
||||||
|
g_free(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Common code for the edit / net-edit / pool-edit functions which follow. */
|
/* Common code for the edit / net-edit / pool-edit functions which follow. */
|
||||||
char *
|
char *
|
||||||
vshEditWriteToTempFile(vshControl *ctl, const char *doc)
|
vshEditWriteToTempFile(vshControl *ctl, const char *doc)
|
||||||
{
|
{
|
||||||
g_autofree char *ret = NULL;
|
g_autofree char *filename = NULL;
|
||||||
|
g_autoptr(vshTempFile) ret = NULL;
|
||||||
const char *tmpdir;
|
const char *tmpdir;
|
||||||
VIR_AUTOCLOSE fd = -1;
|
VIR_AUTOCLOSE fd = -1;
|
||||||
|
|
||||||
tmpdir = getenv("TMPDIR");
|
tmpdir = getenv("TMPDIR");
|
||||||
if (!tmpdir) tmpdir = "/tmp";
|
if (!tmpdir)
|
||||||
ret = g_strdup_printf("%s/virshXXXXXX.xml", tmpdir);
|
tmpdir = "/tmp";
|
||||||
fd = g_mkstemp_full(ret, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
filename = g_strdup_printf("%s/virshXXXXXX.xml", tmpdir);
|
||||||
|
fd = g_mkstemp_full(filename, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
vshError(ctl, _("g_mkstemp_full: failed to create temporary file: %s"),
|
vshError(ctl, _("g_mkstemp_full: failed to create temporary file: %s"),
|
||||||
g_strerror(errno));
|
g_strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = g_steal_pointer(&filename);
|
||||||
|
|
||||||
if (safewrite(fd, doc, strlen(doc)) == -1) {
|
if (safewrite(fd, doc, strlen(doc)) == -1) {
|
||||||
vshError(ctl, _("write: %s: failed to write to temporary file: %s"),
|
vshError(ctl, _("write: %s: failed to write to temporary file: %s"),
|
||||||
ret, g_strerror(errno));
|
ret, g_strerror(errno));
|
||||||
unlink(ret);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (VIR_CLOSE(fd) < 0) {
|
if (VIR_CLOSE(fd) < 0) {
|
||||||
vshError(ctl, _("close: %s: failed to write or close temporary file: %s"),
|
vshError(ctl, _("close: %s: failed to write or close temporary file: %s"),
|
||||||
ret, g_strerror(errno));
|
ret, g_strerror(errno));
|
||||||
unlink(ret);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -341,6 +341,9 @@ void vshSaveLibvirtError(void);
|
|||||||
void vshSaveLibvirtHelperError(void);
|
void vshSaveLibvirtHelperError(void);
|
||||||
|
|
||||||
/* file handling */
|
/* file handling */
|
||||||
|
void vshEditUnlinkTempfile(char *file);
|
||||||
|
typedef char vshTempFile;
|
||||||
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(vshTempFile, vshEditUnlinkTempfile);
|
||||||
char *vshEditWriteToTempFile(vshControl *ctl, const char *doc);
|
char *vshEditWriteToTempFile(vshControl *ctl, const char *doc);
|
||||||
int vshEditFile(vshControl *ctl, const char *filename);
|
int vshEditFile(vshControl *ctl, const char *filename);
|
||||||
char *vshEditReadBackFile(vshControl *ctl, const char *filename);
|
char *vshEditReadBackFile(vshControl *ctl, const char *filename);
|
||||||
|
Loading…
Reference in New Issue
Block a user