vlc/modules/access/file.c

352 lines
9.2 KiB
C
Raw Normal View History

/*****************************************************************************
* file.c: file input (file: access plug-in)
*****************************************************************************
LGPL Re-license almost all the playback modules to LGPLv2.1+ with authorization from their respective contributors (230+) This includes: - access, codec, packetizers, demux - audio filters, audio mixers, audio output - video filters, video chroma, video output - text renderers - XML parser - ARM NEON and SSE2 optimisations (mostly for chromas and filters) Some modules are not concerned: - BDA and DShow access modules because Manol Manolov is AWOL - Real RTSP, because it is derived from Xine - x264 and t140 because they are encoders only - DLL Loader, because it is derived from MPlayer - DTS packetizer, because Jon Lech Johansen is AWOL - Shine and WMAfixed, because they are derived from Rockbox - Real demuxer, as it is derived from MPlayer and Wang Bo is AWOL - MPC demuxer, as Yavor Doganov is AWOL - Tivo demuxer, because it is derived from an MPlayer fork - Playlist demuxer, (WPL and ZPL parts missing), because suheaven is AWOL - iOS audio output and video display, because author refuses the license change - Equalizer and compressor, because Ronald Wright is AWOL - Mono, Headphone and Dolby, because author refuses the license change - hqdn3d and yadif, because they are from MPlayer/libavfilter - remoteosd, because it derives from RealVNC code - MMX optimisations, because Ollie Lho, from SiS, is AWOL - Rotate, because it depends on GPL motion Nota Bene: - Some modules depend on GPL-only libraries, a LGPL module does not mean that the resulting binary module will be LGPL. Libraries affected would include liba52, libdvdcss, libdvdnav, libdvdread, faad2, libdca, libmad, libmpeg2, libpostproc, SRC, sid, zvbi and probably others.
2012-11-06 17:48:33 +01:00
* Copyright (C) 2001-2006 VLC authors and VideoLAN
2007-11-26 21:27:15 +01:00
* Copyright © 2006-2007 Rémi Denis-Courmont
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
2019-09-04 21:10:51 +02:00
* Rémi Denis-Courmont
*
LGPL Re-license almost all the playback modules to LGPLv2.1+ with authorization from their respective contributors (230+) This includes: - access, codec, packetizers, demux - audio filters, audio mixers, audio output - video filters, video chroma, video output - text renderers - XML parser - ARM NEON and SSE2 optimisations (mostly for chromas and filters) Some modules are not concerned: - BDA and DShow access modules because Manol Manolov is AWOL - Real RTSP, because it is derived from Xine - x264 and t140 because they are encoders only - DLL Loader, because it is derived from MPlayer - DTS packetizer, because Jon Lech Johansen is AWOL - Shine and WMAfixed, because they are derived from Rockbox - Real demuxer, as it is derived from MPlayer and Wang Bo is AWOL - MPC demuxer, as Yavor Doganov is AWOL - Tivo demuxer, because it is derived from an MPlayer fork - Playlist demuxer, (WPL and ZPL parts missing), because suheaven is AWOL - iOS audio output and video display, because author refuses the license change - Equalizer and compressor, because Ronald Wright is AWOL - Mono, Headphone and Dolby, because author refuses the license change - hqdn3d and yadif, because they are from MPlayer/libavfilter - remoteosd, because it derives from RealVNC code - MMX optimisations, because Ollie Lho, from SiS, is AWOL - Rotate, because it depends on GPL motion Nota Bene: - Some modules depend on GPL-only libraries, a LGPL module does not mean that the resulting binary module will be LGPL. Libraries affected would include liba52, libdvdcss, libdvdnav, libdvdread, faad2, libdca, libmad, libmpeg2, libpostproc, SRC, sid, zvbi and probably others.
2012-11-06 17:48:33 +01:00
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
LGPL Re-license almost all the playback modules to LGPLv2.1+ with authorization from their respective contributors (230+) This includes: - access, codec, packetizers, demux - audio filters, audio mixers, audio output - video filters, video chroma, video output - text renderers - XML parser - ARM NEON and SSE2 optimisations (mostly for chromas and filters) Some modules are not concerned: - BDA and DShow access modules because Manol Manolov is AWOL - Real RTSP, because it is derived from Xine - x264 and t140 because they are encoders only - DLL Loader, because it is derived from MPlayer - DTS packetizer, because Jon Lech Johansen is AWOL - Shine and WMAfixed, because they are derived from Rockbox - Real demuxer, as it is derived from MPlayer and Wang Bo is AWOL - MPC demuxer, as Yavor Doganov is AWOL - Tivo demuxer, because it is derived from an MPlayer fork - Playlist demuxer, (WPL and ZPL parts missing), because suheaven is AWOL - iOS audio output and video display, because author refuses the license change - Equalizer and compressor, because Ronald Wright is AWOL - Mono, Headphone and Dolby, because author refuses the license change - hqdn3d and yadif, because they are from MPlayer/libavfilter - remoteosd, because it derives from RealVNC code - MMX optimisations, because Ollie Lho, from SiS, is AWOL - Rotate, because it depends on GPL motion Nota Bene: - Some modules depend on GPL-only libraries, a LGPL module does not mean that the resulting binary module will be LGPL. Libraries affected would include liba52, libdvdcss, libdvdnav, libdvdread, faad2, libdca, libmad, libmpeg2, libpostproc, SRC, sid, zvbi and probably others.
2012-11-06 17:48:33 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
LGPL Re-license almost all the playback modules to LGPLv2.1+ with authorization from their respective contributors (230+) This includes: - access, codec, packetizers, demux - audio filters, audio mixers, audio output - video filters, video chroma, video output - text renderers - XML parser - ARM NEON and SSE2 optimisations (mostly for chromas and filters) Some modules are not concerned: - BDA and DShow access modules because Manol Manolov is AWOL - Real RTSP, because it is derived from Xine - x264 and t140 because they are encoders only - DLL Loader, because it is derived from MPlayer - DTS packetizer, because Jon Lech Johansen is AWOL - Shine and WMAfixed, because they are derived from Rockbox - Real demuxer, as it is derived from MPlayer and Wang Bo is AWOL - MPC demuxer, as Yavor Doganov is AWOL - Tivo demuxer, because it is derived from an MPlayer fork - Playlist demuxer, (WPL and ZPL parts missing), because suheaven is AWOL - iOS audio output and video display, because author refuses the license change - Equalizer and compressor, because Ronald Wright is AWOL - Mono, Headphone and Dolby, because author refuses the license change - hqdn3d and yadif, because they are from MPlayer/libavfilter - remoteosd, because it derives from RealVNC code - MMX optimisations, because Ollie Lho, from SiS, is AWOL - Rotate, because it depends on GPL motion Nota Bene: - Some modules depend on GPL-only libraries, a LGPL module does not mean that the resulting binary module will be LGPL. Libraries affected would include liba52, libdvdcss, libdvdnav, libdvdread, faad2, libdca, libmad, libmpeg2, libpostproc, SRC, sid, zvbi and probably others.
2012-11-06 17:48:33 +01:00
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2007-11-26 21:27:15 +01:00
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_FSTATVFS
# include <sys/statvfs.h>
# if defined (HAVE_SYS_MOUNT_H)
# include <sys/param.h>
# include <sys/mount.h>
# endif
#endif
#ifdef HAVE_LINUX_MAGIC_H
# include <sys/vfs.h>
2009-06-25 19:30:57 +02:00
# include <linux/magic.h>
#endif
#if defined( _WIN32 )
2006-04-29 16:23:00 +02:00
# include <io.h>
# include <ctype.h>
# include <shlwapi.h>
2006-04-29 16:23:00 +02:00
#else
# include <unistd.h>
#endif
2010-04-10 17:19:20 +02:00
#include <dirent.h>
#include <vlc_common.h>
#include "fs.h"
#include <vlc_access.h>
#ifdef _WIN32
# include <vlc_charset.h>
#endif
#include <vlc_fs.h>
2010-01-16 17:34:31 +01:00
#include <vlc_url.h>
2015-06-30 22:52:51 +02:00
#include <vlc_interrupt.h>
typedef struct
{
int fd;
bool b_pace_control;
} access_sys_t;
#if !defined (_WIN32) && !defined (__OS2__)
2009-06-25 19:30:57 +02:00
static bool IsRemote (int fd)
{
#if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
struct statvfs stf;
if (fstatvfs (fd, &stf))
return false;
/* fstatvfs() is in POSIX, but MNT_LOCAL is not */
return !(stf.f_flag & MNT_LOCAL);
#elif defined (HAVE_LINUX_MAGIC_H)
2009-06-25 19:30:57 +02:00
struct statfs stf;
if (fstatfs (fd, &stf))
return false;
2010-07-17 20:50:22 +02:00
switch ((unsigned long)stf.f_type)
2009-06-25 19:30:57 +02:00
{
case AFS_SUPER_MAGIC:
case CODA_SUPER_MAGIC:
case NCP_SUPER_MAGIC:
case NFS_SUPER_MAGIC:
case SMB_SUPER_MAGIC:
case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
return true;
}
return false;
#else
2009-07-23 17:05:08 +02:00
(void)fd;
2009-06-25 19:30:57 +02:00
return false;
#endif
}
# define IsRemote(fd,path) IsRemote(fd)
#else /* _WIN32 || __OS2__ */
static bool IsRemote (const char *path)
{
# if !defined(__OS2__) && !VLC_WINSTORE_APP
wchar_t *wpath = ToWide (path);
bool is_remote = (wpath != NULL && PathIsNetworkPathW (wpath));
free (wpath);
return is_remote;
# else
return (! strncmp(path, "\\\\", 2));
# endif
}
# define IsRemote(fd,path) IsRemote(path)
#endif
2009-06-25 19:30:57 +02:00
#ifndef HAVE_POSIX_FADVISE
# define posix_fadvise(fd, off, len, adv)
#endif
2009-06-25 19:30:57 +02:00
2017-07-23 07:43:16 +02:00
static ssize_t Read (stream_t *, void *, size_t);
static int FileSeek (stream_t *, uint64_t);
static int FileControl (stream_t *, int, va_list);
/*****************************************************************************
* FileOpen: open the file
*****************************************************************************/
int FileOpen( vlc_object_t *p_this )
{
2017-07-23 07:43:16 +02:00
stream_t *p_access = (stream_t*)p_this;
/* Open file */
2009-02-05 16:52:52 +01:00
int fd = -1;
if (!strcasecmp (p_access->psz_name, "fd"))
2010-01-16 17:34:31 +01:00
{
char *end;
int oldfd = strtol (p_access->psz_location, &end, 10);
2010-01-16 17:34:31 +01:00
if (*end == '\0')
2010-02-07 14:54:39 +01:00
fd = vlc_dup (oldfd);
else if (*end == '/' && end > p_access->psz_location)
2010-01-16 17:34:31 +01:00
{
char *name = vlc_uri_decode_duplicate (end - 1);
if (name != NULL)
2010-01-16 17:34:31 +01:00
{
name[0] = '.';
fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
2010-01-16 17:34:31 +01:00
free (name);
}
}
}
else
{
if (unlikely(p_access->psz_filepath == NULL))
return VLC_EGENERIC;
fd = vlc_open (p_access->psz_filepath, O_RDONLY | O_NONBLOCK);
}
2009-02-05 16:52:52 +01:00
if (fd == -1)
{
msg_Err (p_access, "cannot open file %s (%s)",
p_access->psz_filepath ? p_access->psz_filepath
: p_access->psz_location,
vlc_strerror_c(errno));
return VLC_EGENERIC;
}
struct stat st;
2009-02-05 16:52:52 +01:00
if (fstat (fd, &st))
{
msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
2009-02-05 16:52:52 +01:00
goto error;
}
#if O_NONBLOCK
2015-06-30 22:52:51 +02:00
/* Force blocking mode back */
2015-08-22 11:08:34 +02:00
fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) & ~O_NONBLOCK);
2012-06-25 16:25:15 +02:00
#endif
2009-02-05 16:52:52 +01:00
/* Directories can be opened and read from, but only readdir() knows
* how to parse the data. The directory plugin will do it. */
if (S_ISDIR (st.st_mode))
{
#ifdef HAVE_FDOPENDIR
DIR *p_dir = fdopendir(fd);
if (!p_dir) {
msg_Err (p_access, "fdopendir error: %s", vlc_strerror_c(errno));
goto error;
}
return DirInit (p_access, p_dir);
#else
2009-02-05 16:52:52 +01:00
msg_Dbg (p_access, "ignoring directory");
goto error;
#endif
}
access_sys_t *p_sys = vlc_obj_malloc(p_this, sizeof (*p_sys));
if (unlikely(p_sys == NULL))
goto error;
2015-08-31 22:01:45 +02:00
p_access->pf_read = Read;
p_access->pf_block = NULL;
p_access->pf_control = FileControl;
p_access->p_sys = p_sys;
p_sys->fd = fd;
if (S_ISREG (st.st_mode) || S_ISBLK (st.st_mode))
{
p_access->pf_seek = FileSeek;
p_sys->b_pace_control = true;
/* Demuxers will need the beginning of the file for probing. */
posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
/* In most cases, we only read the file once. */
posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
#ifdef F_NOCACHE
fcntl (fd, F_NOCACHE, 0);
#endif
#ifdef F_RDAHEAD
if (IsRemote(fd, p_access->psz_filepath))
fcntl (fd, F_RDAHEAD, 0);
else
fcntl (fd, F_RDAHEAD, 1);
#endif
}
else
{
2019-06-18 18:17:26 +02:00
p_access->pf_seek = NULL;
p_sys->b_pace_control = strcasecmp (p_access->psz_name, "stream");
}
return VLC_SUCCESS;
2009-02-05 16:52:52 +01:00
error:
2016-04-21 23:11:43 +02:00
vlc_close (fd);
2009-02-05 16:52:52 +01:00
return VLC_EGENERIC;
}
/*****************************************************************************
* FileClose: close the target
*****************************************************************************/
void FileClose (vlc_object_t * p_this)
{
2017-07-23 07:43:16 +02:00
stream_t *p_access = (stream_t*)p_this;
if (p_access->pf_read == NULL)
{
DirClose (p_this);
return;
}
access_sys_t *p_sys = p_access->p_sys;
2016-04-21 23:11:43 +02:00
vlc_close (p_sys->fd);
}
2008-09-30 17:39:26 +02:00
2017-07-23 07:43:16 +02:00
static ssize_t Read (stream_t *p_access, void *p_buffer, size_t i_len)
{
access_sys_t *p_sys = p_access->p_sys;
int fd = p_sys->fd;
2015-06-30 22:52:51 +02:00
ssize_t val = vlc_read_i11e (fd, p_buffer, i_len);
if (val < 0)
{
switch (errno)
{
case EINTR:
case EAGAIN:
return -1;
}
2015-06-30 22:52:51 +02:00
msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
val = 0;
}
return val;
}
2015-08-31 22:01:45 +02:00
/*****************************************************************************
* Seek: seek to a specific location in a file
*****************************************************************************/
2017-07-23 07:43:16 +02:00
static int FileSeek (stream_t *p_access, uint64_t i_pos)
2015-08-31 22:01:45 +02:00
{
access_sys_t *sys = p_access->p_sys;
if (lseek(sys->fd, i_pos, SEEK_SET) == (off_t)-1)
2015-08-31 22:01:45 +02:00
return VLC_EGENERIC;
return VLC_SUCCESS;
}
/*****************************************************************************
* Control:
*****************************************************************************/
2017-07-23 07:43:16 +02:00
static int FileControl( stream_t *p_access, int i_query, va_list args )
{
access_sys_t *p_sys = p_access->p_sys;
bool *pb_bool;
vlc_tick_t *pi_64;
switch( i_query )
{
case STREAM_CAN_SEEK:
case STREAM_CAN_FASTSEEK:
2017-04-19 19:23:50 +02:00
pb_bool = va_arg( args, bool * );
2019-06-18 18:17:26 +02:00
*pb_bool = (p_access->pf_seek != NULL);
break;
case STREAM_CAN_PAUSE:
case STREAM_CAN_CONTROL_PACE:
2017-04-19 19:23:50 +02:00
pb_bool = va_arg( args, bool * );
*pb_bool = p_sys->b_pace_control;
break;
case STREAM_GET_SIZE:
{
struct stat st;
if (fstat (p_sys->fd, &st) || !S_ISREG(st.st_mode))
return VLC_EGENERIC;
2015-08-31 22:01:45 +02:00
*va_arg( args, uint64_t * ) = st.st_size;
break;
}
case STREAM_GET_PTS_DELAY:
pi_64 = va_arg( args, vlc_tick_t * );
if (IsRemote (p_sys->fd, p_access->psz_filepath))
*pi_64 = VLC_TICK_FROM_MS(
var_InheritInteger (p_access, "network-caching") );
2011-08-30 17:28:20 +02:00
else
*pi_64 = VLC_TICK_FROM_MS(
var_InheritInteger (p_access, "file-caching") );
break;
case STREAM_SET_PAUSE_STATE:
/* Nothing to do */
break;
default:
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}