From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Subject: [PATCH 04/13] cutils: introduce get_relocated_path
Date: Tue, 1 Sep 2020 02:20:11 -0400 [thread overview]
Message-ID: <20200901062020.26660-5-pbonzini@redhat.com> (raw)
In-Reply-To: <20200901062020.26660-1-pbonzini@redhat.com>
Add the function that will compute a relocated version of the
directories in CONFIG_QEMU_*DIR and CONFIG_QEMU_*PATH.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/qemu/cutils.h | 12 +++++++++
meson.build | 4 +--
util/cutils.c | 61 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 75 insertions(+), 2 deletions(-)
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index eb59852dfd..3a86ec0321 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -184,4 +184,16 @@ int uleb128_decode_small(const uint8_t *in, uint32_t *n);
*/
int qemu_pstrcmp0(const char **str1, const char **str2);
+
+/**
+ * get_relocated_path:
+ * @dir: the directory (typically a `CONFIG_*DIR` variable) to be relocated.
+ *
+ * Returns a path for @dir that uses the directory of the running executable
+ * as the prefix. For example, if `bindir` is `/usr/bin` and @dir is
+ * `/usr/share/qemu`, the function will append `../share/qemu` to the
+ * directory that contains the running executable and return the result.
+ */
+char *get_relocated_path(const char *dir);
+
#endif
diff --git a/meson.build b/meson.build
index 10ab189bc5..5ff005b835 100644
--- a/meson.build
+++ b/meson.build
@@ -415,9 +415,9 @@ config_host_data.set('QEMU_VERSION_MINOR', meson.project_version().split('.')[1]
config_host_data.set('QEMU_VERSION_MICRO', meson.project_version().split('.')[2])
arrays = ['CONFIG_AUDIO_DRIVERS', 'CONFIG_BDRV_RW_WHITELIST', 'CONFIG_BDRV_RO_WHITELIST']
-strings = ['HOST_DSOSUF', 'CONFIG_IASL', 'bindir', 'qemu_confdir', 'qemu_datadir',
+strings = ['HOST_DSOSUF', 'CONFIG_IASL', 'bindir', 'prefix', 'qemu_confdir', 'qemu_datadir',
'qemu_moddir', 'qemu_localstatedir', 'qemu_helperdir', 'qemu_localedir',
- 'qemu_icondir', 'qemu_desktopdir', 'qemu_firmwarepath']
+ 'qemu_icondir', 'qemu_desktopdir', 'qemu_firmwarepath', 'sysconfdir']
foreach k, v: config_host
if arrays.contains(k)
if v != ''
diff --git a/util/cutils.c b/util/cutils.c
index 36ce712271..8da34e04b0 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -889,3 +889,64 @@ int qemu_pstrcmp0(const char **str1, const char **str2)
{
return g_strcmp0(*str1, *str2);
}
+
+static inline bool starts_with_prefix(const char *dir)
+{
+ size_t prefix_len = strlen(CONFIG_PREFIX);
+ return !memcmp(dir, CONFIG_PREFIX, prefix_len) &&
+ (!dir[prefix_len] || G_IS_DIR_SEPARATOR(dir[prefix_len]));
+}
+
+/* Return the next path component in dir, and store its length in *p_len. */
+static inline const char *next_component(const char *dir, int *p_len)
+{
+ int len;
+ while (*dir && G_IS_DIR_SEPARATOR(*dir)) {
+ dir++;
+ }
+ len = 0;
+ while (dir[len] && !G_IS_DIR_SEPARATOR(dir[len])) {
+ len++;
+ }
+ *p_len = len;
+ return dir;
+}
+
+char *get_relocated_path(const char *dir)
+{
+ size_t prefix_len = strlen(CONFIG_PREFIX);
+ const char *bindir = CONFIG_BINDIR;
+ const char *exec_dir = qemu_get_exec_dir();
+ GString *result;
+ int len_dir, len_bindir;
+
+ /* Fail if qemu_init_exec_dir was not called. */
+ assert(exec_dir[0]);
+ if (!starts_with_prefix(dir) || !starts_with_prefix(bindir)) {
+ return strdup(dir);
+ }
+
+ result = g_string_new(exec_dir);
+
+ /* Advance over common components. */
+ len_dir = len_bindir = prefix_len;
+ do {
+ dir += len_dir;
+ bindir += len_bindir;
+ dir = next_component(dir, &len_dir);
+ bindir = next_component(bindir, &len_bindir);
+ } while (len_dir == len_bindir && !memcmp(dir, bindir, len_dir));
+
+ /* Ascend from bindir to the common prefix with dir. */
+ while (len_bindir) {
+ bindir += len_bindir;
+ g_string_append(result, "/..");
+ bindir = next_component(bindir, &len_bindir);
+ }
+
+ if (*dir) {
+ assert(G_IS_DIR_SEPARATOR(dir[-1]));
+ g_string_append(result, dir - 1);
+ }
+ return result->str;
+}
--
2.26.2
next prev parent reply other threads:[~2020-09-01 6:27 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-01 6:20 [PATCH 00/13] Make QEMU installation relocatable Paolo Bonzini
2020-09-01 6:20 ` [PATCH 01/13] fuzz: use qemu_get_exec_dir Paolo Bonzini
2020-09-01 9:18 ` Thomas Huth
2020-09-01 14:36 ` Alexander Bulekov
2020-09-01 6:20 ` [PATCH 02/13] oslib: do not call g_strdup from qemu_get_exec_dir Paolo Bonzini
2020-09-02 10:30 ` Thomas Huth
2020-09-01 6:20 ` [PATCH 03/13] oslib-posix: default exec_dir to bindir Paolo Bonzini
2020-09-01 18:04 ` Richard Henderson
2020-09-01 18:40 ` Paolo Bonzini
2020-09-01 6:20 ` Paolo Bonzini [this message]
2020-09-01 6:20 ` [PATCH 05/13] oslib-posix: relocate path to /var Paolo Bonzini
2020-09-02 8:20 ` Philippe Mathieu-Daudé
2020-09-01 6:20 ` [PATCH 06/13] module: relocate path to modules Paolo Bonzini
2020-09-01 6:20 ` [PATCH 07/13] net: relocate paths to helpers and scripts Paolo Bonzini
2020-09-02 8:24 ` Philippe Mathieu-Daudé
2020-09-02 8:40 ` Paolo Bonzini
2020-09-01 6:20 ` [PATCH 08/13] vl: relocate paths to data directories Paolo Bonzini
2020-09-02 8:28 ` Philippe Mathieu-Daudé
2020-09-02 8:35 ` Paolo Bonzini
2020-09-02 8:41 ` Philippe Mathieu-Daudé
2020-09-01 6:20 ` [PATCH 09/13] vl: relocate path to configuration file Paolo Bonzini
2020-09-02 8:28 ` Philippe Mathieu-Daudé
2020-09-01 6:20 ` [PATCH 10/13] qemu-bridge-helper: relocate path to default ACL Paolo Bonzini
2020-09-01 6:20 ` [PATCH 11/13] qga: relocate path to default configuration and hook Paolo Bonzini
2020-09-01 6:20 ` [PATCH 12/13] ui: relocate paths to icons and translations Paolo Bonzini
2020-09-02 8:29 ` Philippe Mathieu-Daudé
2020-09-01 6:20 ` [PATCH 13/13] configure: use a platform-neutral prefix Paolo Bonzini
2020-09-01 21:14 ` [PATCH 00/13] Make QEMU installation relocatable Mark Cave-Ayland
2020-09-01 21:22 ` Paolo Bonzini
2020-09-02 6:09 ` Mark Cave-Ayland
2020-09-02 6:42 ` Paolo Bonzini
2020-09-02 11:42 ` Mark Cave-Ayland
2020-09-02 11:45 ` Mark Cave-Ayland
2020-09-02 12:16 ` Paolo Bonzini
2020-09-02 18:45 ` Mark Cave-Ayland
2020-09-02 20:20 ` Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200901062020.26660-5-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=mark.cave-ayland@ilande.co.uk \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).