qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] hw/i386/pc: Use fstat in get_file_size()
@ 2016-06-05  8:14 Fabien Siron
  0 siblings, 0 replies; only message in thread
From: Fabien Siron @ 2016-06-05  8:14 UTC (permalink / raw)
  To: qemu-trivial; +Cc: qemu-devel, Fabien Siron

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, 3 insertions(+), 10 deletions(-)

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index e29ccc8..186dfe4 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -802,16 +802,9 @@ static FWCfgState *bochs_bios_init(AddressSpace *as, PCMachineState *pcms)
 
 static long get_file_size(FILE *f)
 {
-    long where, size;
+    struct stat st;
 
-    /* XXX: on Unix systems, using fstat() probably makes more sense */
-
-    where = ftell(f);
-    fseek(f, 0, SEEK_END);
-    size = ftell(f);
-    fseek(f, where, SEEK_SET);
-
-    return size;
+    return fstat(fileno(f), &st) < 0 ? -1: st.st_size;
 }
 
 static void load_linux(PCMachineState *pcms,
@@ -835,7 +828,7 @@ static void load_linux(PCMachineState *pcms,
 
     /* load the kernel header */
     f = fopen(kernel_filename, "rb");
-    if (!f || !(kernel_size = get_file_size(f)) ||
+    if (!f || (kernel_size = get_file_size(f)) <= 0 ||
         fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
         MIN(ARRAY_SIZE(header), kernel_size)) {
         fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
-- 
2.8.3

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-06-05  9:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-05  8:14 [Qemu-devel] [PATCH] hw/i386/pc: Use fstat in get_file_size() Fabien Siron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).