All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Tokarev <mjt@tls.msk.ru>
To: Fabien Siron <fabien.siron@epita.fr>, qemu-trivial@nongnu.org
Cc: qemu-devel <qemu-devel@nongnu.org>, Fabien Siron <fabien.siron@epita.fr>
Subject: Re: [Qemu-trivial] [PATCH] pc: Use fstat in get_file_size()
Date: Sun, 5 Jun 2016 10:15:34 +0300	[thread overview]
Message-ID: <5753D196.1040307@msgid.tls.msk.ru> (raw)
In-Reply-To: <1464790984-8123-1-git-send-email-fabien.siron@epita.fr>

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 <fabien.siron@epita.fr>
> ---
>  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



WARNING: multiple messages have this Message-ID (diff)
From: Michael Tokarev <mjt@tls.msk.ru>
To: Fabien Siron <fabien.siron@epita.fr>, qemu-trivial@nongnu.org
Cc: qemu-devel <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH] pc: Use fstat in get_file_size()
Date: Sun, 5 Jun 2016 10:15:34 +0300	[thread overview]
Message-ID: <5753D196.1040307@msgid.tls.msk.ru> (raw)
In-Reply-To: <1464790984-8123-1-git-send-email-fabien.siron@epita.fr>

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 <fabien.siron@epita.fr>
> ---
>  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

  parent reply	other threads:[~2016-06-05  7:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-01 14:23 [Qemu-trivial] [PATCH] pc: Use fstat in get_file_size() Fabien Siron
2016-06-05  7:15 ` Michael Tokarev
2016-06-05  7:15   ` [Qemu-devel] " Michael Tokarev
2016-06-05  7:15 ` Michael Tokarev [this message]
2016-06-05  7:15   ` Michael Tokarev

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=5753D196.1040307@msgid.tls.msk.ru \
    --to=mjt@tls.msk.ru \
    --cc=fabien.siron@epita.fr \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.