diff --git a/fs/tftp.c b/fs/tftp.c index ebb81e9..847921a 100644 --- a/fs/tftp.c +++ b/fs/tftp.c @@ -589,7 +589,32 @@ 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; }