diff mbox

[24/25] Add a fallback bios file search, if -L fails.

Message ID acfe7fdf533c832a1428360895fa7868a5a15d4d.1347561356.git.jbaron@redhat.com
State New
Headers show

Commit Message

Jason Baron Sept. 13, 2012, 8:12 p.m. UTC
If -L <dir> is specified, and qemu does not find the bios file in <dir>, then
the search fails. Add infrastructure such that the search will continue in
the default paths, if not found in the -L path.

Signed-off-by: Jason Baron <jbaron@redhat.com>
---
 vl.c |   28 +++++++++++++++++++++-------
 1 files changed, 21 insertions(+), 7 deletions(-)

Comments

Paolo Bonzini Sept. 14, 2012, 7:09 a.m. UTC | #1
Il 13/09/2012 22:12, Jason Baron ha scritto:
> If -L <dir> is specified, and qemu does not find the bios file in <dir>, then
> the search fails. Add infrastructure such that the search will continue in
> the default paths, if not found in the -L path.
> 
> Signed-off-by: Jason Baron <jbaron@redhat.com>

What is this useful for?

> +static char *__qemu_find_file(int type, const char *name, const char *dir)

No __ names in userspace, use qemu_find_file_in_dir.

Paolo
Peter Maydell Sept. 14, 2012, 10:54 a.m. UTC | #2
On 13 September 2012 21:12, Jason Baron <jbaron@redhat.com> wrote:
> If -L <dir> is specified, and qemu does not find the bios file in <dir>, then
> the search fails. Add infrastructure such that the search will continue in
> the default paths, if not found in the -L path.

> @@ -1872,12 +1873,15 @@ static int balloon_parse(const char *arg)
>      return -1;
>  }
>
> -char *qemu_find_file(int type, const char *name)
> +static char *__qemu_find_file(int type, const char *name, const char *dir)
>  {
>      int len;
>      const char *subdir;
>      char *buf;
>
> +    if (!dir)
> +        return NULL;
> +
>      /* Try the name as a straight path first */
>      if (access(name, R_OK) == 0) {
>          return g_strdup(name);

This means that every try with a different directory will
check the name as a straight path again, which is a bit
pointless. The "just try the name" code shouldn't be in
this function (which as Paolo suggests should be named
qemu_find_file_in_dir()), it should be pulled out into
your new qemu_find_file(), which will then look like:
 * try as plain pathname
 * try in directory 1
 * try in directory 2

-- PMM
Blue Swirl Sept. 14, 2012, 7:15 p.m. UTC | #3
On Thu, Sep 13, 2012 at 8:12 PM, Jason Baron <jbaron@redhat.com> wrote:
> If -L <dir> is specified, and qemu does not find the bios file in <dir>, then
> the search fails. Add infrastructure such that the search will continue in
> the default paths, if not found in the -L path.
>
> Signed-off-by: Jason Baron <jbaron@redhat.com>
> ---
>  vl.c |   28 +++++++++++++++++++++-------
>  1 files changed, 21 insertions(+), 7 deletions(-)
>
> diff --git a/vl.c b/vl.c
> index 7c577fa..656a210 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -177,6 +177,7 @@ int main(int argc, char **argv)
>  #define MAX_VIRTIO_CONSOLES 1
>
>  static const char *data_dir;
> +static const char *data_dir_fallback;
>  const char *bios_name = NULL;
>  enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
>  DisplayType display_type = DT_DEFAULT;
> @@ -1872,12 +1873,15 @@ static int balloon_parse(const char *arg)
>      return -1;
>  }
>
> -char *qemu_find_file(int type, const char *name)
> +static char *__qemu_find_file(int type, const char *name, const char *dir)
>  {
>      int len;
>      const char *subdir;
>      char *buf;
>
> +    if (!dir)
> +        return NULL;

Missing braces.

> +
>      /* Try the name as a straight path first */
>      if (access(name, R_OK) == 0) {
>          return g_strdup(name);
> @@ -1892,9 +1896,9 @@ char *qemu_find_file(int type, const char *name)
>      default:
>          abort();
>      }
> -    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
> +    len = strlen(dir) + strlen(name) + strlen(subdir) + 2;
>      buf = g_malloc0(len);
> -    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
> +    snprintf(buf, len, "%s/%s%s", dir, subdir, name);
>      if (access(buf, R_OK)) {
>          g_free(buf);
>          return NULL;
> @@ -1902,6 +1906,18 @@ char *qemu_find_file(int type, const char *name)
>      return buf;
>  }
>
> +char *qemu_find_file(int type, const char *name)
> +{
> +    char *filename;
> +
> +    filename = __qemu_find_file(type, name, data_dir);
> +    if (!filename) {
> +        filename = __qemu_find_file(type, name, data_dir_fallback);
> +    }
> +
> +    return filename;
> +}
> +
>  static int device_help_func(QemuOpts *opts, void *opaque)
>  {
>      return qdev_device_help(opts);
> @@ -3338,11 +3354,9 @@ int main(int argc, char **argv, char **envp)
>
>      /* If no data_dir is specified then try to find it relative to the
>         executable path.  */
> -    if (!data_dir) {
> -        data_dir = os_find_datadir(argv[0]);
> -    }
> +    data_dir_fallback = os_find_datadir(argv[0]);
>      /* If all else fails use the install path specified when building. */
> -    if (!data_dir) {
> +    if (!data_dir_fallback) {
>          data_dir = CONFIG_QEMU_DATADIR;
>      }
>
> --
> 1.7.1
>
>
diff mbox

Patch

diff --git a/vl.c b/vl.c
index 7c577fa..656a210 100644
--- a/vl.c
+++ b/vl.c
@@ -177,6 +177,7 @@  int main(int argc, char **argv)
 #define MAX_VIRTIO_CONSOLES 1
 
 static const char *data_dir;
+static const char *data_dir_fallback;
 const char *bios_name = NULL;
 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
 DisplayType display_type = DT_DEFAULT;
@@ -1872,12 +1873,15 @@  static int balloon_parse(const char *arg)
     return -1;
 }
 
-char *qemu_find_file(int type, const char *name)
+static char *__qemu_find_file(int type, const char *name, const char *dir)
 {
     int len;
     const char *subdir;
     char *buf;
 
+    if (!dir)
+        return NULL;
+
     /* Try the name as a straight path first */
     if (access(name, R_OK) == 0) {
         return g_strdup(name);
@@ -1892,9 +1896,9 @@  char *qemu_find_file(int type, const char *name)
     default:
         abort();
     }
-    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
+    len = strlen(dir) + strlen(name) + strlen(subdir) + 2;
     buf = g_malloc0(len);
-    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
+    snprintf(buf, len, "%s/%s%s", dir, subdir, name);
     if (access(buf, R_OK)) {
         g_free(buf);
         return NULL;
@@ -1902,6 +1906,18 @@  char *qemu_find_file(int type, const char *name)
     return buf;
 }
 
+char *qemu_find_file(int type, const char *name)
+{
+    char *filename;
+
+    filename = __qemu_find_file(type, name, data_dir);
+    if (!filename) {
+        filename = __qemu_find_file(type, name, data_dir_fallback);
+    }
+
+    return filename;
+}
+
 static int device_help_func(QemuOpts *opts, void *opaque)
 {
     return qdev_device_help(opts);
@@ -3338,11 +3354,9 @@  int main(int argc, char **argv, char **envp)
 
     /* If no data_dir is specified then try to find it relative to the
        executable path.  */
-    if (!data_dir) {
-        data_dir = os_find_datadir(argv[0]);
-    }
+    data_dir_fallback = os_find_datadir(argv[0]);
     /* If all else fails use the install path specified when building. */
-    if (!data_dir) {
+    if (!data_dir_fallback) {
         data_dir = CONFIG_QEMU_DATADIR;
     }