summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--stream/stream_file.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/stream/stream_file.c b/stream/stream_file.c
index a79ef0e913..6e69f33c94 100644
--- a/stream/stream_file.c
+++ b/stream/stream_file.c
@@ -64,6 +64,7 @@ struct priv {
bool use_poll;
bool regular_file;
bool appending;
+ int64_t cached_size; // -2: invalid, -1: unknown
int64_t orig_size;
struct mp_cancel *cancel;
};
@@ -75,15 +76,20 @@ struct priv {
static int64_t get_size(stream_t *s)
{
struct priv *p = s->priv;
- off_t size = lseek(p->fd, 0, SEEK_END);
- lseek(p->fd, s->pos, SEEK_SET);
- return size == (off_t)-1 ? -1 : size;
+ 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;
+ }
+ return p->cached_size;
}
static int fill_buffer(stream_t *s, void *buffer, int max_len)
{
struct priv *p = s->priv;
+ p->cached_size = -2; // always invalidate cached size
+
#ifndef __MINGW32__
if (p->use_poll) {
int c = mp_cancel_get_fd(p->cancel);
@@ -245,7 +251,8 @@ static int open_f(stream_t *stream)
{
struct priv *p = talloc_ptrtype(stream, p);
*p = (struct priv) {
- .fd = -1
+ .fd = -1,
+ .cached_size = -2,
};
stream->priv = p;
stream->is_local_file = true;