Skip to content

Commit

Permalink
stream: don't try to read from all-sparse/no-data files
Browse files Browse the repository at this point in the history
 ```
 dd if=/dev/zero of=/tmp/10g.empty bs=1 seek=10G count=0
 dd if=/dev/zero of=/tmp/10m.empty bs=1 seek=10M count=0
 time mpv /tmp/10{g,m}.empty
 ```

 I keep files with the name format `${name}-${hash}.${ext}.empty`
 around, where the original is removed, and a sparse file with
 the size of the original is created instead.

 A lot of time is wasted on such files when going through
 playlists/directories that include some of them.

 This admittedly may not be that common of a use-case.

Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>
  • Loading branch information
MoSal committed Apr 6, 2024
1 parent 4ce4bf1 commit 98a5bb8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ endif
features += {'ppoll': cc.has_function('ppoll', args: '-D_GNU_SOURCE',
prefix: '#include <poll.h>')}

features += {'seek-data': cc.has_header_symbol('errno.h', 'ENXIO') and
cc.has_header_symbol('unistd.h', 'SEEK_DATA', prefix: '#define _GNU_SOURCE')}

cd_devices = {
'windows': 'D:',
'cygwin': 'D:',
Expand Down
11 changes: 11 additions & 0 deletions stream/stream_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 98a5bb8

Please sign in to comment.