diff options
author | Sascha Silbe <silbe@linux.vnet.ibm.com> | 2016-08-18 20:46:02 +0200 |
---|---|---|
committer | Peter Maydell <peter.maydell@linaro.org> | 2016-08-19 12:42:40 +0100 |
commit | 50455700092412d90ffaf57ee5d00f38f7d1cc5b (patch) | |
tree | 6a89caa1d4e3a47259a2a89007e6217d60caec8d | |
parent | 60c6b790fc5dc26418dca42a77bab925ca7bac60 (diff) |
glib: add compatibility implementation for g_dir_make_tmp()
We're going to make use of g_dir_make_tmp() in test-logging. Provide a
compatibility implementation of it for glib < 2.30.
May behave differently in some edge cases (e.g. pattern only at the
end of the template, the file name is not part of the error message),
but good enough in practice.
Signed-off-by: Sascha Silbe <silbe@linux.vnet.ibm.com>
Message-id: 1471545963-11720-2-git-send-email-silbe@linux.vnet.ibm.com
[PMM: removed variable "template" which caused compilation failures
when C++ files include glib-compat.h]
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
-rw-r--r-- | include/glib-compat.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/include/glib-compat.h b/include/glib-compat.h index 01aa7b37a5..8d5a7f3801 100644 --- a/include/glib-compat.h +++ b/include/glib-compat.h @@ -48,6 +48,26 @@ static inline gint64 qemu_g_get_monotonic_time(void) gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout); #endif +#if !GLIB_CHECK_VERSION(2, 30, 0) +/* Not a 100% compatible implementation, but good enough for most + * cases. Placeholders are only supported at the end of the + * template. */ +static inline gchar *qemu_g_dir_make_tmp(gchar const *tmpl, GError **error) +{ + gchar *path = g_build_filename(g_get_tmp_dir(), tmpl ?: ".XXXXXX", NULL); + + if (mkdtemp(path) != NULL) { + return path; + } + /* Error occurred, clean up. */ + g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), + "mkdtemp() failed"); + g_free(path); + return NULL; +} +#define g_dir_make_tmp(tmpl, error) qemu_g_dir_make_tmp(tmpl, error) +#endif /* glib 2.30 */ + #if !GLIB_CHECK_VERSION(2, 31, 0) /* before glib-2.31, GMutex and GCond was dynamic-only (there was a separate * GStaticMutex, but it didn't work with condition variables). |