summaryrefslogtreecommitdiffstats
path: root/fs/tftp.c
diff options
context:
space:
mode:
authorAndrey Smirnov <andrew.smirnov@gmail.com>2019-01-28 22:55:37 -0800
committerSascha Hauer <s.hauer@pengutronix.de>2019-01-29 09:27:03 +0100
commitc44997e621cfe190fe44523e6cbcb03fea21eb8c (patch)
treebfd907f6c2fcd6ee6073f1d48864df6644aa559a /fs/tftp.c
parent6811cc0dbc04c71cdc1ff12efe0edc8660200563 (diff)
downloadbarebox-c44997e621cfe190fe44523e6cbcb03fea21eb8c.tar.gz
barebox-c44997e621cfe190fe44523e6cbcb03fea21eb8c.tar.xz
fs: Update FILE position in lseek()
Instead on relying on driver callbacks to update 'pos' in FILE, do it as a part of lseek() code. This allows us to drop a bit of repeating code as well as making lseek() implementation consistent with write() and read(). Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'fs/tftp.c')
-rw-r--r--fs/tftp.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/fs/tftp.c b/fs/tftp.c
index 1b50ba84f9..f9e204db59 100644
--- a/fs/tftp.c
+++ b/fs/tftp.c
@@ -576,12 +576,14 @@ 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)
{
/* We cannot seek backwards without reloading or caching the file */
- if (pos >= f->pos) {
+ loff_t f_pos = f->pos;
+
+ 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);
+ while (pos > f_pos) {
+ size_t len = min_t(size_t, 1024, pos - f_pos);
ret = tftp_read(dev, f, buf, len);
@@ -591,13 +593,20 @@ static loff_t tftp_lseek(struct device_d *dev, FILE *f, loff_t pos)
if (ret < 0)
goto out_free;
- f->pos += ret;
+ f_pos += ret;
}
ret = pos;
out_free:
free(buf);
+ if (ret < 0) {
+ /*
+ * Update f->pos even if the overall request
+ * failed since we can't move backwards
+ */
+ f->pos = f_pos;
+ }
return ret;
}