mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 2/3] scripts: bareboximd: Use mmap when possible
Date: Fri, 11 Feb 2022 10:42:29 +0100	[thread overview]
Message-ID: <20220211094230.1807262-2-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20220211094230.1807262-1-s.hauer@pengutronix.de>

Using mmap() in read_file_2 was dropped in the last patch, bring it back
in a bareboximd specific function here.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/imd.c         |  7 ++++++-
 scripts/bareboximd.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/common/imd.c b/common/imd.c
index 0295d84d34..9ca0248523 100644
--- a/common/imd.c
+++ b/common/imd.c
@@ -21,6 +21,11 @@ static inline void read_file_2_free(void *buf)
 {
 	free(buf);
 }
+
+static int imd_read_file(const char *filename, size_t *size, void **outbuf)
+{
+	return read_file_2(filename, size, outbuf, 0x100000);
+}
 #endif
 
 /*
@@ -473,7 +478,7 @@ int imd_command(int argc, char *argv[])
 
 	filename = argv[optind];
 
-	ret = read_file_2(filename, &size, &buf, 0x100000);
+	ret = imd_read_file(filename, &size, &buf);
 	if (ret && ret != -EFBIG)
 		return -errno;
 
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index a734399aa5..2d4750d7fb 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -55,6 +55,40 @@ static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int ba
 	return strtoul(cp, endp, base);
 }
 
+static int imd_read_file(const char *filename, size_t *size, void **outbuf)
+{
+	void *buf;
+	int fd, ret;
+	size_t fsize;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
+		return -errno;
+	}
+
+	fsize = lseek(fd, 0, SEEK_END);
+	if (fsize == -1) {
+		fprintf(stderr, "Cannot get size %s: %s\n", filename, strerror(errno));
+		ret = -errno;
+		goto close;
+	}
+
+	buf = mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+	if (buf == MAP_FAILED) {
+		close(fd);
+		return read_file_2(filename, size, outbuf, 0x100000);
+	}
+
+	*outbuf = buf;
+	*size = fsize;
+
+	return 0;
+close:
+	close(fd);
+	return ret;
+}
+
 #include "../include/xfuncs.h"
 #include "../crypto/crc32.c"
 #include "../common/imd.c"
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  reply	other threads:[~2022-02-11  9:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-11  9:42 [PATCH 1/3] scripts/common: Do not mmap in read_file_2() Sascha Hauer
2022-02-11  9:42 ` Sascha Hauer [this message]
2022-02-11  9:42 ` [PATCH 3/3] Revert "scripts/common: fix write_file when opened with mmap" Sascha Hauer
2022-02-24 13:22   ` Andrej Picej
2022-02-28 10:16     ` Sascha Hauer
2022-02-28 11:40       ` Andrej Picej

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=20220211094230.1807262-2-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox