mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2] tftp: implement forward seeking
@ 2017-03-03  8:48 Uwe Kleine-König
  2017-03-03 10:52 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Uwe Kleine-König @ 2017-03-03  8:48 UTC (permalink / raw)
  To: barebox

Just abuse tftp_read to step forward.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
Hello,

this is v2 which compared to (implicit) v1 uses xmalloc and no debugging
any more.

When tftp_lseek is entered with f->pos == pos, the only thing that the
function does is xmalloc, free, return. Is it worth to optimize that?
I thought it's not worth the additional (even if trivial) complication,
but want to at least point that out.

Best regards
Uwe

 fs/tftp.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/fs/tftp.c b/fs/tftp.c
index 56d4365d773a..4bacd2ed7fcf 100644
--- a/fs/tftp.c
+++ b/fs/tftp.c
@@ -591,7 +591,32 @@ static int tftp_read(struct device_d *dev, FILE *f, void *buf, size_t insize)
 
 static loff_t tftp_lseek(struct device_d *dev, FILE *f, loff_t pos)
 {
-	/* not implemented in tftp protocol */
+	/* We cannot seek backwards without reloading or caching the file */
+	if (pos >= f->pos) {
+		loff_t ret;
+		char *buf = xmalloc(1024);
+
+		while (pos > f->pos) {
+			size_t len = min_t(size_t, 1024, pos - f->pos);
+
+			ret = tftp_read(dev, f, buf, len);
+
+			if (!ret)
+				/* EOF, so the desired pos is invalid. */
+				ret = -EINVAL;
+			if (ret < 0)
+				goto out_free;
+
+			f->pos += ret;
+		}
+
+		ret = pos;
+
+out_free:
+		free(buf);
+		return ret;
+	}
+
 	return -ENOSYS;
 }
 
-- 
2.11.0


_______________________________________________
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 v2] tftp: implement forward seeking
  2017-03-03  8:48 [PATCH v2] tftp: implement forward seeking Uwe Kleine-König
@ 2017-03-03 10:52 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2017-03-03 10:52 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Fri, Mar 03, 2017 at 09:48:41AM +0100, Uwe Kleine-König wrote:
> Just abuse tftp_read to step forward.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
> Hello,
> 
> this is v2 which compared to (implicit) v1 uses xmalloc and no debugging
> any more.
> 
> When tftp_lseek is entered with f->pos == pos, the only thing that the
> function does is xmalloc, free, return. Is it worth to optimize that?
> I thought it's not worth the additional (even if trivial) complication,
> but want to at least point that out.

I don' think that's worth optimizing.

Applied, thanks

Sascha

> 
> Best regards
> Uwe
> 
>  fs/tftp.c | 27 ++++++++++++++++++++++++++-
>  1 file changed, 26 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/tftp.c b/fs/tftp.c
> index 56d4365d773a..4bacd2ed7fcf 100644
> --- a/fs/tftp.c
> +++ b/fs/tftp.c
> @@ -591,7 +591,32 @@ static int tftp_read(struct device_d *dev, FILE *f, void *buf, size_t insize)
>  
>  static loff_t tftp_lseek(struct device_d *dev, FILE *f, loff_t pos)
>  {
> -	/* not implemented in tftp protocol */
> +	/* We cannot seek backwards without reloading or caching the file */
> +	if (pos >= f->pos) {
> +		loff_t ret;
> +		char *buf = xmalloc(1024);
> +
> +		while (pos > f->pos) {
> +			size_t len = min_t(size_t, 1024, pos - f->pos);
> +
> +			ret = tftp_read(dev, f, buf, len);
> +
> +			if (!ret)
> +				/* EOF, so the desired pos is invalid. */
> +				ret = -EINVAL;
> +			if (ret < 0)
> +				goto out_free;
> +
> +			f->pos += ret;
> +		}
> +
> +		ret = pos;
> +
> +out_free:
> +		free(buf);
> +		return ret;
> +	}
> +
>  	return -ENOSYS;
>  }
>  
> -- 
> 2.11.0
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

-- 
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] 2+ messages in thread

end of thread, other threads:[~2017-03-03 10:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-03  8:48 [PATCH v2] tftp: implement forward seeking Uwe Kleine-König
2017-03-03 10:52 ` Sascha Hauer

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