[coreboot-gerrit] New patch to review for coreboot: 11b6118 util: replace fseek/ftell/rewind with fstat

Patrick Georgi (patrick@georgi-clan.de) gerrit at coreboot.org
Mon Aug 11 09:31:58 CEST 2014


Patrick Georgi (patrick at georgi-clan.de) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/6594

-gerrit

commit 11b6118dc118aa4c4d38e371044d3bc651d42749
Author: Patrick Georgi <patrick at georgi-clan.de>
Date:   Mon Aug 11 09:27:18 2014 +0200

    util: replace fseek/ftell/rewind with fstat
    
    It's a more direct approach to get the file size.
    
    Change-Id: If49df26bf4996bd556c675f3a673d0003b4adf89
    Signed-off-by: Patrick Georgi <patrick at georgi-clan.de>
---
 util/arm_boot_tools/mksunxiboot/mksunxiboot.c | 18 ++++++++++++------
 util/cbfstool/common.c                        | 20 +++++++++++++++++---
 util/romcc/romcc.c                            | 15 ++++++++++++---
 3 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/util/arm_boot_tools/mksunxiboot/mksunxiboot.c b/util/arm_boot_tools/mksunxiboot/mksunxiboot.c
index af8450c..cb8e8f9 100644
--- a/util/arm_boot_tools/mksunxiboot/mksunxiboot.c
+++ b/util/arm_boot_tools/mksunxiboot/mksunxiboot.c
@@ -11,6 +11,9 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include <errno.h>
 
 /* boot head definition from sun4i boot code */
@@ -108,12 +111,11 @@ static void fill_header(struct boot_file_head *hdr, size_t load_size)
 
 static long int fsize(FILE *file)
 {
-	long int size;
-
-	fseek(file, 0L, SEEK_END);
-	size = ftell(file);
-	fseek(file, 0L, SEEK_SET);
-	return size;
+	struct stat s;
+	int fd = fileno(file);
+	if (fd == -1) return -1;
+	if (fstat(fd, &s) == -1) return -1;
+	return s.st_size;
 }
 
 int main(int argc, char *argv[])
@@ -145,6 +147,10 @@ int main(int argc, char *argv[])
 
 	/* Get input file size */
 	file_size = fsize(fd_in);
+	if (file_size == -1) {
+		fprintf(stderr, "can't determine file size\n");
+		return EXIT_FAILURE;
+	}
 	if ((file_data = malloc(file_size)) == NULL) {
 		fprintf(stderr, "Cannot allocate memory\n");
 		return EXIT_FAILURE;
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index b746c86..6778eb9 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -22,6 +22,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include <libgen.h>
 #include "common.h"
 #include "cbfs.h"
@@ -40,6 +43,14 @@ int is_big_endian(void)
 	return 0;
 }
 
+static off_t get_file_size(FILE *f)
+{
+	struct stat s;
+	int fd = fileno(f);
+	if (fd == -1) return -1;
+	if (fstat(fd, &s) == -1) return -1;
+	return s.st_size;
+}
 /* Buffer and file I/O */
 
 int buffer_create(struct buffer *buffer, size_t size, const char *name) {
@@ -59,10 +70,13 @@ int buffer_from_file(struct buffer *buffer, const char *filename) {
 		perror(filename);
 		return -1;
 	}
-	fseek(fp, 0, SEEK_END);
-	buffer->size = ftell(fp);
+	buffer->size = get_file_size(fp);
+	if (buffer->size == -1) {
+		fprintf(stderr, "could not determine size of %s\n", filename);
+		fclose(fp);
+		return -1;
+	}
 	buffer->name = strdup(filename);
-	rewind(fp);
 	buffer->data = (char *)malloc(buffer->size);
 	assert(buffer->data);
 	if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
diff --git a/util/romcc/romcc.c b/util/romcc/romcc.c
index 49b4dd4..01d836c 100644
--- a/util/romcc/romcc.c
+++ b/util/romcc/romcc.c
@@ -223,6 +223,14 @@ static int exists(const char *dirname, const char *filename)
 	return does_exist;
 }
 
+static off_t get_file_size(FILE *f)
+{
+	struct stat s;
+	int fd = fileno(f);
+	if (fd == -1) return -1;
+	if (fstat(fd, &s) == -1) return -1;
+	return s.st_size;
+}
 
 static char *slurp_file(const char *dirname, const char *filename, off_t *r_size)
 {
@@ -246,9 +254,10 @@ static char *slurp_file(const char *dirname, const char *filename, off_t *r_size
 		die("Cannot open '%s' : %s\n",
 			filename, strerror(errno));
 	}
-	fseek(file, 0, SEEK_END);
-	size = ftell(file);
-	fseek(file, 0, SEEK_SET);
+	size = get_file_size(file);
+	if (size == -1) {
+		die("Could not fetch size of '%s': %s\n", filename, strerror(errno));
+	}
 	*r_size = size +1;
 	buf = xmalloc(size +2, filename);
 	buf[size] = '\n'; /* Make certain the file is newline terminated */



More information about the coreboot-gerrit mailing list