[PATCH v4 1/4] cutils: Introduce bundle mechanism

Akihiko Odaki posted 4 patches 2 years, 3 months ago
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, Thomas Huth <thuth@redhat.com>, Wainer dos Santos Moschetta <wainersm@redhat.com>, Beraldo Leal <bleal@redhat.com>, Jason Wang <jasowang@redhat.com>, Alexander Bulekov <alxndr@bu.edu>, Paolo Bonzini <pbonzini@redhat.com>, Bandan Das <bsd@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Laurent Vivier <lvivier@redhat.com>, Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>, Ed Maste <emaste@freebsd.org>, Li-Wen Hsu <lwhsu@freebsd.org>, Peter Maydell <peter.maydell@linaro.org>, Akihiko Odaki <akihiko.odaki@gmail.com>, Gerd Hoffmann <kraxel@redhat.com>
There is a newer version of this series
[PATCH v4 1/4] cutils: Introduce bundle mechanism
Posted by Akihiko Odaki 2 years, 3 months ago
Developers often run QEMU without installing. The bundle mechanism
allows to look up files which should be present in installation even in
such a situation.

It is a general mechanism and can find any files located relative
to the installation tree. The build tree must have a new directory,
qemu-bundle, to represent what files the installation tree would
have for reference by the executables.

Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>
---
 include/qemu/cutils.h | 19 +++++++++++++++++++
 util/cutils.c         | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 40e10e19a7e..3b66026cd3c 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -213,6 +213,25 @@ const char *qemu_get_exec_dir(void);
  */
 char *get_relocated_path(const char *dir);
 
+/**
+ * find_bundle:
+ * @path: Relative path
+ *
+ * Returns a path for the specified directory or file bundled in QEMU. It uses
+ * the directory of the running executable as the prefix first. See
+ * get_relocated_path() for the details. The next candidate is "qemu-bundle"
+ * directory in the directory of the running executable. "qemu-bundle"
+ * directory is typically present in the build tree.
+ *
+ * The returned string should be freed by the caller.
+ *
+ * Returns: a path that can access the bundle, or NULL if no matching bundle
+ * exists.
+ */
+char *find_bundle(const char *path);
+
+void list_bundle_candidates(const char *path);
+
 static inline const char *yes_no(bool b)
 {
      return b ? "yes" : "no";
diff --git a/util/cutils.c b/util/cutils.c
index a58bcfd80e7..fe3bbb1c4eb 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -1086,3 +1086,36 @@ char *get_relocated_path(const char *dir)
     }
     return g_string_free(result, false);
 }
+
+static const char * const bundle_formats[] = {
+    "%s" G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S "%s",
+    "%s" G_DIR_SEPARATOR_S "qemu-bundle" G_DIR_SEPARATOR_S "%s"
+};
+
+char *find_bundle(const char *path)
+{
+    const char *dir = qemu_get_exec_dir();
+    char *candidate;
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(bundle_formats); i++) {
+        candidate = g_strdup_printf(bundle_formats[i], dir, path);
+        if (access(candidate, R_OK) == 0) {
+            return candidate;
+        }
+        g_free(candidate);
+    }
+
+    return NULL;
+}
+
+void list_bundle_candidates(const char *path)
+{
+    const char *dir = qemu_get_exec_dir();
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(bundle_formats); i++) {
+        printf(bundle_formats[i], dir, path);
+        putc('\n', stdout);
+    }
+}
-- 
2.32.1 (Apple Git-133)
Re: [PATCH v4 1/4] cutils: Introduce bundle mechanism
Posted by Paolo Bonzini 2 years, 3 months ago
On 6/14/22 23:07, Akihiko Odaki wrote:
> diff --git a/util/cutils.c b/util/cutils.c
> index a58bcfd80e7..fe3bbb1c4eb 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -1086,3 +1086,36 @@ char *get_relocated_path(const char *dir)
>       }
>       return g_string_free(result, false);
>   }
> +
> +static const char * const bundle_formats[] = {
> +    "%s" G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S "%s",
> +    "%s" G_DIR_SEPARATOR_S "qemu-bundle" G_DIR_SEPARATOR_S "%s"
> +};

Why do you need both?

Paolo
Re: [PATCH v4 1/4] cutils: Introduce bundle mechanism
Posted by Akihiko Odaki 2 years, 3 months ago
On 2022/06/15 17:19, Paolo Bonzini wrote:
> On 6/14/22 23:07, Akihiko Odaki wrote:
>> diff --git a/util/cutils.c b/util/cutils.c
>> index a58bcfd80e7..fe3bbb1c4eb 100644
>> --- a/util/cutils.c
>> +++ b/util/cutils.c
>> @@ -1086,3 +1086,36 @@ char *get_relocated_path(const char *dir)
>>       }
>>       return g_string_free(result, false);
>>   }
>> +
>> +static const char * const bundle_formats[] = {
>> +    "%s" G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S "%s",
>> +    "%s" G_DIR_SEPARATOR_S "qemu-bundle" G_DIR_SEPARATOR_S "%s"
>> +};
> 
> Why do you need both?
> 
> Paolo

The earlier one is used when QEMU is installed. The latter one is used 
in the build tree.

Actually the order was problematic as Daniel pointed out. It is fixed in 
the v5, which I have just sent out.
On 2022/06/15 17:16, Daniel P. Berrangé wrote:
 > This is flawed because it looks at the installed paths first, and
 > falls back to uninstalled paths afterwards. So if you're building
 > and running QEMU 7.1.0 from git, and have QEMU 5.0.0 installed,
 > your QEMU 7.1.0 will end up finding files from the 5.0.0 install.

Regards,
Akihiko Odaki