diff --git a/meson.build b/meson.build index c59efc08645f..5c8ec2cf9cea 100644 --- a/meson.build +++ b/meson.build @@ -427,6 +427,9 @@ endif features += {'ppoll': cc.has_function('ppoll', args: '-D_GNU_SOURCE', prefix: '#include ')} +features += {'seek-data': cc.has_header_symbol('errno.h', 'ENXIO') and + cc.has_header_symbol('unistd.h', 'SEEK_DATA', args: '-D_GNU_SOURCE')} + cd_devices = { 'windows': 'D:', 'cygwin': 'D:', diff --git a/stream/stream_file.c b/stream/stream_file.c index 89e7a2d9f109..e44357ef5ee1 100644 --- a/stream/stream_file.c +++ b/stream/stream_file.c @@ -332,6 +332,17 @@ static int open_f(stream_t *stream, const struct stream_open_args *args) setmode(p->fd, O_BINARY); #endif +#if HAVE_SEEK_DATA + if (stream->mode == STREAM_READ) { + off_t first_data = lseek(p->fd, 0, SEEK_DATA); + if (first_data == (off_t)-1 && errno == ENXIO) { + MP_ERR(stream, "File is empty or all sparse (has no data).\n"); + s_close(stream); + return STREAM_ERROR; + } + } +#endif + off_t len = lseek(p->fd, 0, SEEK_END); lseek(p->fd, 0, SEEK_SET); if (len != (off_t)-1) {