diff options
author | wm4 <wm4@nowhere> | 2020-02-16 16:03:03 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2020-02-16 16:03:03 +0100 |
commit | 74a2c0fb7f61ded74b1a66baff61783907433efa (patch) | |
tree | 45dfbf186dd2f2f4b6f0b9f7e786913ac042a928 | |
parent | a19d918816866180a4ddc8be371dfb8b9394fc28 (diff) | |
download | mpv-stream_size_fstat.tar.bz2 mpv-stream_size_fstat.tar.xz |
stream_file: use fstat() instead of lseek() to determine file sizestream_size_fstat
It appears using lseek() to seek to the end and back to determine file
size is inefficient in some cases (like with CIFS, see #7408).
Even Windows supports fstat() (well, almost, but we already have a
wrapper), so use that. It's unknown whether that will work better.
Although I like it more, because it doesn't mess with the file position.
-rw-r--r-- | stream/stream_file.c | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/stream/stream_file.c b/stream/stream_file.c index 6e69f33c94..9f83b73dd1 100644 --- a/stream/stream_file.c +++ b/stream/stream_file.c @@ -77,9 +77,14 @@ static int64_t get_size(stream_t *s) { struct priv *p = s->priv; if (p->cached_size == -2) { - off_t size = lseek(p->fd, 0, SEEK_END); - lseek(p->fd, s->pos, SEEK_SET); - p->cached_size = size < 0 ? -1 : size; + int64_t size = -1; + struct stat st; + if (fstat(p->fd, &st) == 0) { + if (st.st_size <= 0 && !s->seekable) + st.st_size = -1; + size = st.st_size < 0 ? -1 : st.st_size; + } + p->cached_size = size; } return p->cached_size; } |