qemu-trivial
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-trivial] [PATCH] pc: Use fstat in get_file_size()


From: Michael Tokarev
Subject: Re: [Qemu-trivial] [PATCH] pc: Use fstat in get_file_size()
Date: Sun, 5 Jun 2016 10:15:07 +0300
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.8.0

Please direct all patches to qemu-devel@ too, don't use ONLY
qemu-trivial.  Thanks.

01.06.2016 17:23, Fabien Siron wrote:
> As mentioned in the comment, fstat is quite simpler than playing with
> ftell() and fseek().
> 
> Signed-off-by: Fabien Siron <address@hidden>
> ---
>  hw/i386/pc.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/i386/pc.c b/hw/i386/pc.c
> index 99437e0..fecb067 100644
> --- a/hw/i386/pc.c
> +++ b/hw/i386/pc.c
> @@ -801,16 +801,13 @@ static FWCfgState *bochs_bios_init(AddressSpace *as, 
> PCMachineState *pcms)
>  
>  static long get_file_size(FILE *f)
>  {
> -    long where, size;
> +    struct stat stat;
> +    int fd;
>  
> -    /* XXX: on Unix systems, using fstat() probably makes more sense */
> +    fd = fileno(f);
> +    fstat(fd, &stat);

There's no need to introduce fd variable here, it is perfectly
sane to use fstat(fileno(f), &stat) here.

> -    where = ftell(f);
> -    fseek(f, 0, SEEK_END);
> -    size = ftell(f);
> -    fseek(f, where, SEEK_SET);
> -
> -    return size;
> +    return stat.st_size;

Note there's no error checking in this function, neither in the
original nor in the new implementation.  But old function, in case
of error, will most likely return -1 (error result of ftell()),
while new function will return some random value.

So the body should look something like:

  struct stat st;
  return fstat(fileno(f), &st) < 0 ? -1 : st.st_size;

Thanks,

/mjt




reply via email to

[Prev in Thread] Current Thread [Next in Thread]