From eeedca4b7f4b559d1d3630585a59841c24c7a2c2 Mon Sep 17 00:00:00 2001 From: Lukasz Marek Date: Wed, 5 Jun 2013 12:58:38 +0200 Subject: [PATCH] ftp: fix seeking beyond file size adjust to ff* tools seek nature Signed-off-by: Lukasz Marek --- libavformat/ftp.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libavformat/ftp.c b/libavformat/ftp.c index 3c32fcd852..b59b5b6909 100644 --- a/libavformat/ftp.c +++ b/libavformat/ftp.c @@ -591,7 +591,7 @@ static int64_t ftp_seek(URLContext *h, int64_t pos, int whence) { FTPContext *s = h->priv_data; int err; - int64_t new_pos; + int64_t new_pos, fake_pos; av_dlog(h, "ftp protocol seek %"PRId64" %d\n", pos, whence); @@ -617,13 +617,12 @@ static int64_t ftp_seek(URLContext *h, int64_t pos, int whence) return AVERROR(EIO); new_pos = FFMAX(0, new_pos); - if (s->filesize >= 0) - new_pos = FFMIN(s->filesize, new_pos); + fake_pos = s->filesize != -1 ? FFMIN(new_pos, s->filesize) : new_pos; - if (new_pos != s->position) { + if (fake_pos != s->position) { if ((err = ftp_abort(h)) < 0) return err; - s->position = new_pos; + s->position = fake_pos; } return new_pos; } @@ -652,8 +651,13 @@ static int ftp_read(URLContext *h, unsigned char *buf, int size) if (read >= 0) { s->position += read; if (s->position >= s->filesize) { - if (ftp_abort(h) < 0) + /* server will terminate, but keep current position to avoid madness */ + int64_t pos = s->position; + if (ftp_abort(h) < 0) { + s->position = pos; return AVERROR(EIO); + } + s->position = pos; } } if (read <= 0 && s->position < s->filesize && !h->is_streamed) {