[PATCH v2] tftp: implement forward seeking

Uwe Kleine-König u.kleine-koenig at pengutronix.de
Fri Mar 3 00:48:41 PST 2017


Just abuse tftp_read to step forward.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig at 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




More information about the barebox mailing list