On 14 August 2012 08:49, Olivia Yin <hong-hua....@freescale.com> wrote:
> Sanity check in rom_add_file() could be reused by other image loaders.
>
> Signed-off-by: Olivia Yin <hong-hua....@freescale.com>
> ---
> This patch is based on branch 'ppc-next' of Alex's upstream QEMU repo:
> http://repo.or.cz/r/qemu/agraf.git
>
>  hw/loader.c |   61 +++++++++++++++++++++++++++++++---------------------------
>  1 files changed, 33 insertions(+), 28 deletions(-)
>
> diff --git a/hw/loader.c b/hw/loader.c
> index 33acc2f..f2099b6 100644
> --- a/hw/loader.c
> +++ b/hw/loader.c
> @@ -86,6 +86,36 @@ int load_image(const char *filename, uint8_t *addr)
>      return size;
>  }
>
> +static int file_load(const char *file, uint8_t **data)
> +{
> +    int fd = -1;
> +    ssize_t rc, size;
> +
> +    fd = open(file, O_RDONLY | O_BINARY);
> +    if (fd == -1) {
> +        fprintf(stderr, "Could not open file '%s': %s\n",
> +                file, strerror(errno));
> +        return -1;
> +    }
> +
> +    size = lseek(fd, 0, SEEK_END);
> +    *data = g_malloc0(size);
> +    lseek(fd, 0, SEEK_SET);
> +    rc = read(fd, *data, size);
> +    if (rc != size) {
> +        fprintf(stderr, "file %-20s: read error: rc=%zd (expected %zd)\n",
> +                file, rc, size);
> +        goto err;
> +    }
> +    close(fd);
> +    return size;
> +err:
> +    if (fd != -1)
> +        close(fd);
> +    g_free(*data);
> +    return -1;
> +}

Isn't this function effectively a reimplementation of the glib
g_file_get_contents() function? It would probably be better to
just make the callers use that instead.

-- PMM

Reply via email to