mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] libfile: open_and_lseek: don't imply O_RDONLY
@ 2018-08-09  8:33 Uwe Kleine-König
  2018-08-09  8:33 ` [PATCH 2/3] libfile: open_and_lseek: enlarge small files enough to make lseek possible Uwe Kleine-König
  2018-08-09  8:33 ` [PATCH 3/3] commands: teach commands that write to files to also create them Uwe Kleine-König
  0 siblings, 2 replies; 5+ messages in thread
From: Uwe Kleine-König @ 2018-08-09  8:33 UTC (permalink / raw)
  To: barebox

There are several users that pass O_RDWR or O_WRONLY in mode to
open_and_lseek() and use the resulting file descriptor for writing. This
is no real issue becauce O_RDONLY is 0 and so can be dropped without
any side effects.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 lib/libfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/libfile.c b/lib/libfile.c
index d22519b8f4bb..83c6399a5b39 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -501,7 +501,7 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
 {
 	int fd, ret;
 
-	fd = open(filename, mode | O_RDONLY);
+	fd = open(filename, mode);
 	if (fd < 0) {
 		perror("open");
 		return fd;
-- 
2.18.0


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] libfile: open_and_lseek: enlarge small files enough to make lseek possible
  2018-08-09  8:33 [PATCH 1/3] libfile: open_and_lseek: don't imply O_RDONLY Uwe Kleine-König
@ 2018-08-09  8:33 ` Uwe Kleine-König
  2018-08-10  6:22   ` Sascha Hauer
  2018-08-09  8:33 ` [PATCH 3/3] commands: teach commands that write to files to also create them Uwe Kleine-König
  1 sibling, 1 reply; 5+ messages in thread
From: Uwe Kleine-König @ 2018-08-09  8:33 UTC (permalink / raw)
  To: barebox

This makes the following do the expected thing:

	barebox@barebox sandbox:/ ls -l lala
	-rwxrwxrwx              4 lala
	barebox@barebox sandbox:/ mw -d lala 72 0

Without this patch mw dies with

	lseek: Invalid argument

memset, memcpy and probably others benefit in the same way.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 lib/libfile.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/lib/libfile.c b/lib/libfile.c
index 83c6399a5b39..01c59cdb80fe 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -510,6 +510,24 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
 	if (!pos)
 		return fd;
 
+	if (mode & (O_WRONLY | O_RDWR)) {
+		struct stat s;
+
+		ret = fstat(fd, &s);
+		if (ret) {
+			perror("ftruncate");
+			return ret;
+		}
+
+		if (s.st_size < pos) {
+			ret = ftruncate(fd, pos);
+			if (ret) {
+				perror("ftruncate");
+				return ret;
+			}
+		}
+	}
+
 	ret = lseek(fd, pos, SEEK_SET);
 	if (ret == -1) {
 		perror("lseek");
-- 
2.18.0


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] commands: teach commands that write to files to also create them
  2018-08-09  8:33 [PATCH 1/3] libfile: open_and_lseek: don't imply O_RDONLY Uwe Kleine-König
  2018-08-09  8:33 ` [PATCH 2/3] libfile: open_and_lseek: enlarge small files enough to make lseek possible Uwe Kleine-König
@ 2018-08-09  8:33 ` Uwe Kleine-König
  1 sibling, 0 replies; 5+ messages in thread
From: Uwe Kleine-König @ 2018-08-09  8:33 UTC (permalink / raw)
  To: barebox

This allows to use the adapted commands on non-existing files which
failed before with

	open: No such file or directory

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 commands/memset.c | 2 +-
 commands/mm.c     | 2 +-
 commands/mw.c     | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/commands/memset.c b/commands/memset.c
index f871e07c9523..f99bf86c0415 100644
--- a/commands/memset.c
+++ b/commands/memset.c
@@ -56,7 +56,7 @@ static int do_memset(int argc, char *argv[])
 	c = strtoull_suffix(argv[optind + 1], NULL, 0);
 	n = strtoull_suffix(argv[optind + 2], NULL, 0);
 
-	fd = open_and_lseek(file, mode | O_WRONLY, s);
+	fd = open_and_lseek(file, mode | O_WRONLY | O_CREAT, s);
 	if (fd < 0)
 		return 1;
 
diff --git a/commands/mm.c b/commands/mm.c
index 6d2a88789299..c7f62fca54bb 100644
--- a/commands/mm.c
+++ b/commands/mm.c
@@ -53,7 +53,7 @@ static int do_mem_mm(int argc, char *argv[])
 	value = simple_strtoull(argv[optind++], NULL, 0);
 	mask = simple_strtoull(argv[optind++], NULL, 0);
 
-	fd = open_and_lseek(filename, mode | O_RDWR, adr);
+	fd = open_and_lseek(filename, mode | O_RDWR | O_CREAT, adr);
 	if (fd < 0)
 		return 1;
 
diff --git a/commands/mw.c b/commands/mw.c
index 7ff589abb1d4..2912997a31df 100644
--- a/commands/mw.c
+++ b/commands/mw.c
@@ -52,7 +52,7 @@ static int do_mem_mw(int argc, char *argv[])
 
 	adr = strtoull_suffix(argv[optind++], NULL, 0);
 
-	fd = open_and_lseek(filename, mode | O_WRONLY, adr);
+	fd = open_and_lseek(filename, mode | O_WRONLY | O_CREAT, adr);
 	if (fd < 0)
 		return 1;
 
-- 
2.18.0


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/3] libfile: open_and_lseek: enlarge small files enough to make lseek possible
  2018-08-09  8:33 ` [PATCH 2/3] libfile: open_and_lseek: enlarge small files enough to make lseek possible Uwe Kleine-König
@ 2018-08-10  6:22   ` Sascha Hauer
  2018-08-10  7:14     ` Uwe Kleine-König
  0 siblings, 1 reply; 5+ messages in thread
From: Sascha Hauer @ 2018-08-10  6:22 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Thu, Aug 09, 2018 at 10:33:12AM +0200, Uwe Kleine-König wrote:
> This makes the following do the expected thing:
> 
> 	barebox@barebox sandbox:/ ls -l lala
> 	-rwxrwxrwx              4 lala
> 	barebox@barebox sandbox:/ mw -d lala 72 0
> 
> Without this patch mw dies with
> 
> 	lseek: Invalid argument
> 
> memset, memcpy and probably others benefit in the same way.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  lib/libfile.c | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
> 
> diff --git a/lib/libfile.c b/lib/libfile.c
> index 83c6399a5b39..01c59cdb80fe 100644
> --- a/lib/libfile.c
> +++ b/lib/libfile.c
> @@ -510,6 +510,24 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
>  	if (!pos)
>  		return fd;
>  
> +	if (mode & (O_WRONLY | O_RDWR)) {
> +		struct stat s;
> +
> +		ret = fstat(fd, &s);
> +		if (ret) {
> +			perror("ftruncate");

I assume this "ftruncate" did not happen on purpose, right? Replaced
with "fstat" and applied.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/3] libfile: open_and_lseek: enlarge small files enough to make lseek possible
  2018-08-10  6:22   ` Sascha Hauer
@ 2018-08-10  7:14     ` Uwe Kleine-König
  0 siblings, 0 replies; 5+ messages in thread
From: Uwe Kleine-König @ 2018-08-10  7:14 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Fri, Aug 10, 2018 at 08:22:47AM +0200, Sascha Hauer wrote:
> On Thu, Aug 09, 2018 at 10:33:12AM +0200, Uwe Kleine-König wrote:
> > This makes the following do the expected thing:
> > 
> > 	barebox@barebox sandbox:/ ls -l lala
> > 	-rwxrwxrwx              4 lala
> > 	barebox@barebox sandbox:/ mw -d lala 72 0
> > 
> > Without this patch mw dies with
> > 
> > 	lseek: Invalid argument
> > 
> > memset, memcpy and probably others benefit in the same way.
> > 
> > Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> > ---
> >  lib/libfile.c | 18 ++++++++++++++++++
> >  1 file changed, 18 insertions(+)
> > 
> > diff --git a/lib/libfile.c b/lib/libfile.c
> > index 83c6399a5b39..01c59cdb80fe 100644
> > --- a/lib/libfile.c
> > +++ b/lib/libfile.c
> > @@ -510,6 +510,24 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
> >  	if (!pos)
> >  		return fd;
> >  
> > +	if (mode & (O_WRONLY | O_RDWR)) {
> > +		struct stat s;
> > +
> > +		ret = fstat(fd, &s);
> > +		if (ret) {
> > +			perror("ftruncate");
> 
> I assume this "ftruncate" did not happen on purpose, right? Replaced
> with "fstat" and applied.

Right, thanks

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-08-10  7:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-09  8:33 [PATCH 1/3] libfile: open_and_lseek: don't imply O_RDONLY Uwe Kleine-König
2018-08-09  8:33 ` [PATCH 2/3] libfile: open_and_lseek: enlarge small files enough to make lseek possible Uwe Kleine-König
2018-08-10  6:22   ` Sascha Hauer
2018-08-10  7:14     ` Uwe Kleine-König
2018-08-09  8:33 ` [PATCH 3/3] commands: teach commands that write to files to also create them Uwe Kleine-König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox