aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc-André Lureau <marcandre.lureau@redhat.com>2019-09-18 12:24:10 +0400
committerPaolo Bonzini <pbonzini@redhat.com>2019-12-17 19:32:47 +0100
commit6dd2dacedd83d12328afa8559bffb2b9ec5c89ed (patch)
tree572c5a0cf38a9bb23a043e1ee62ede1bc6557ba9
parent3819af6e6a8fabe90957fa28bff0d9af56481662 (diff)
os-posix: simplify os_find_datadir
Use g_build_filename instead of sprintf, and g_autofree instead of manual freeing. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--os-posix.c41
1 files changed, 13 insertions, 28 deletions
diff --git a/os-posix.c b/os-posix.c
index 86cffd2c7d..3cd52e1e70 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -80,41 +80,26 @@ void os_setup_signal_handling(void)
sigaction(SIGTERM, &act, NULL);
}
-/* Find a likely location for support files using the location of the binary.
- For installed binaries this will be "$bindir/../share/qemu". When
- running from the build tree this will be "$bindir/../pc-bios". */
-#define SHARE_SUFFIX "/share/qemu"
-#define BUILD_SUFFIX "/pc-bios"
+/*
+ * Find a likely location for support files using the location of the binary.
+ * When running from the build tree this will be "$bindir/../pc-bios".
+ * Otherwise, this is CONFIG_QEMU_DATADIR.
+ */
char *os_find_datadir(void)
{
- char *dir, *exec_dir;
- char *res;
- size_t max_len;
+ g_autofree char *exec_dir = NULL;
+ g_autofree char *dir = NULL;
exec_dir = qemu_get_exec_dir();
- if (exec_dir == NULL) {
- return NULL;
- }
- dir = g_path_get_dirname(exec_dir);
-
- max_len = strlen(dir) +
- MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
- res = g_malloc0(max_len);
- snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
- if (access(res, R_OK)) {
- snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
- if (access(res, R_OK)) {
- g_free(res);
- res = NULL;
- }
+ g_return_val_if_fail(exec_dir != NULL, NULL);
+
+ dir = g_build_filename(exec_dir, "..", "pc-bios", NULL);
+ if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
+ return g_steal_pointer(&dir);
}
- g_free(dir);
- g_free(exec_dir);
- return res;
+ return g_strdup(CONFIG_QEMU_DATADIR);
}
-#undef SHARE_SUFFIX
-#undef BUILD_SUFFIX
void os_set_proc_name(const char *s)
{