mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] libfile: implement new pread_full
@ 2022-03-18 14:49 Ahmad Fatoum
  2022-03-28  8:56 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2022-03-18 14:49 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We already have pwrite_full, add pread_full for symmetry.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/libfile.h |  1 +
 lib/libfile.c     | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/include/libfile.h b/include/libfile.h
index 3c2fe1714dcb..a353ccfa9ea9 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -2,6 +2,7 @@
 #ifndef __LIBFILE_H
 #define __LIBFILE_H
 
+int pread_full(int fd, void *buf, size_t size, loff_t offset);
 int pwrite_full(int fd, const void *buf, size_t size, loff_t offset);
 int write_full(int fd, const void *buf, size_t size);
 int read_full(int fd, void *buf, size_t size);
diff --git a/lib/libfile.c b/lib/libfile.c
index 6b373f05ca72..1f533133c593 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -75,6 +75,31 @@ int write_full(int fd, const void *buf, size_t size)
 }
 EXPORT_SYMBOL(write_full);
 
+/*
+ * pread_full - read to filedescriptor at offset
+ *
+ * Like pread, but this function only returns less bytes than
+ * requested when the end of file is reached.
+ */
+int pread_full(int fd, void *buf, size_t size, loff_t offset)
+{
+	size_t insize = size;
+	int now;
+
+	while (size) {
+		now = pread(fd, buf, size, offset);
+		if (now == 0)
+			break;
+		if (now < 0)
+			return now;
+		size -= now;
+		buf += now;
+	}
+
+	return insize - size;
+}
+EXPORT_SYMBOL(pread_full);
+
 /*
  * read_full - read from filedescriptor
  *
-- 
2.30.2


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


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

* Re: [PATCH] libfile: implement new pread_full
  2022-03-18 14:49 [PATCH] libfile: implement new pread_full Ahmad Fatoum
@ 2022-03-28  8:56 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-03-28  8:56 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Fri, Mar 18, 2022 at 03:49:42PM +0100, Ahmad Fatoum wrote:
> We already have pwrite_full, add pread_full for symmetry.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  include/libfile.h |  1 +
>  lib/libfile.c     | 25 +++++++++++++++++++++++++
>  2 files changed, 26 insertions(+)

Applied, thanks

Sascha

> 
> diff --git a/include/libfile.h b/include/libfile.h
> index 3c2fe1714dcb..a353ccfa9ea9 100644
> --- a/include/libfile.h
> +++ b/include/libfile.h
> @@ -2,6 +2,7 @@
>  #ifndef __LIBFILE_H
>  #define __LIBFILE_H
>  
> +int pread_full(int fd, void *buf, size_t size, loff_t offset);
>  int pwrite_full(int fd, const void *buf, size_t size, loff_t offset);
>  int write_full(int fd, const void *buf, size_t size);
>  int read_full(int fd, void *buf, size_t size);
> diff --git a/lib/libfile.c b/lib/libfile.c
> index 6b373f05ca72..1f533133c593 100644
> --- a/lib/libfile.c
> +++ b/lib/libfile.c
> @@ -75,6 +75,31 @@ int write_full(int fd, const void *buf, size_t size)
>  }
>  EXPORT_SYMBOL(write_full);
>  
> +/*
> + * pread_full - read to filedescriptor at offset
> + *
> + * Like pread, but this function only returns less bytes than
> + * requested when the end of file is reached.
> + */
> +int pread_full(int fd, void *buf, size_t size, loff_t offset)
> +{
> +	size_t insize = size;
> +	int now;
> +
> +	while (size) {
> +		now = pread(fd, buf, size, offset);
> +		if (now == 0)
> +			break;
> +		if (now < 0)
> +			return now;
> +		size -= now;
> +		buf += now;
> +	}
> +
> +	return insize - size;
> +}
> +EXPORT_SYMBOL(pread_full);
> +
>  /*
>   * read_full - read from filedescriptor
>   *
> -- 
> 2.30.2
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
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] 2+ messages in thread

end of thread, other threads:[~2022-03-28  8:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-18 14:49 [PATCH] libfile: implement new pread_full Ahmad Fatoum
2022-03-28  8:56 ` Sascha Hauer

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