access: srt: add support stream encryption
[vlc.git] / modules / access / file.c
blob4a6213b73a5e3425f424083c6dad69965eb613fe
1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5 * Copyright © 2006-2007 Rémi Denis-Courmont
6 * $Id$
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan # org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <assert.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #ifdef HAVE_FSTATVFS
36 # include <sys/statvfs.h>
37 # if defined (HAVE_SYS_MOUNT_H)
38 # include <sys/param.h>
39 # include <sys/mount.h>
40 # endif
41 #endif
42 #ifdef HAVE_LINUX_MAGIC_H
43 # include <sys/vfs.h>
44 # include <linux/magic.h>
45 #endif
47 #if defined( _WIN32 )
48 # include <io.h>
49 # include <ctype.h>
50 # include <shlwapi.h>
51 #else
52 # include <unistd.h>
53 #endif
54 #include <dirent.h>
56 #include <vlc_common.h>
57 #include "fs.h"
58 #include <vlc_input.h>
59 #include <vlc_access.h>
60 #ifdef _WIN32
61 # include <vlc_charset.h>
62 #endif
63 #include <vlc_fs.h>
64 #include <vlc_url.h>
65 #include <vlc_interrupt.h>
67 struct access_sys_t
69 int fd;
71 bool b_pace_control;
74 #if !defined (_WIN32) && !defined (__OS2__)
75 static bool IsRemote (int fd)
77 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
78 struct statvfs stf;
80 if (fstatvfs (fd, &stf))
81 return false;
82 /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
83 return !(stf.f_flag & MNT_LOCAL);
85 #elif defined (HAVE_LINUX_MAGIC_H)
86 struct statfs stf;
88 if (fstatfs (fd, &stf))
89 return false;
91 switch ((unsigned long)stf.f_type)
93 case AFS_SUPER_MAGIC:
94 case CODA_SUPER_MAGIC:
95 case NCP_SUPER_MAGIC:
96 case NFS_SUPER_MAGIC:
97 case SMB_SUPER_MAGIC:
98 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
99 return true;
101 return false;
103 #else
104 (void)fd;
105 return false;
107 #endif
109 # define IsRemote(fd,path) IsRemote(fd)
111 #else /* _WIN32 || __OS2__ */
112 static bool IsRemote (const char *path)
114 # if !defined(__OS2__) && !VLC_WINSTORE_APP
115 wchar_t *wpath = ToWide (path);
116 bool is_remote = (wpath != NULL && PathIsNetworkPathW (wpath));
117 free (wpath);
118 return is_remote;
119 # else
120 return (! strncmp(path, "\\\\", 2));
121 # endif
123 # define IsRemote(fd,path) IsRemote(path)
124 #endif
126 #ifndef HAVE_POSIX_FADVISE
127 # define posix_fadvise(fd, off, len, adv)
128 #endif
130 static ssize_t Read (stream_t *, void *, size_t);
131 static int FileSeek (stream_t *, uint64_t);
132 static int NoSeek (stream_t *, uint64_t);
133 static int FileControl (stream_t *, int, va_list);
135 /*****************************************************************************
136 * FileOpen: open the file
137 *****************************************************************************/
138 int FileOpen( vlc_object_t *p_this )
140 stream_t *p_access = (stream_t*)p_this;
142 /* Open file */
143 int fd = -1;
145 if (!strcasecmp (p_access->psz_name, "fd"))
147 char *end;
148 int oldfd = strtol (p_access->psz_location, &end, 10);
150 if (*end == '\0')
151 fd = vlc_dup (oldfd);
152 else if (*end == '/' && end > p_access->psz_location)
154 char *name = vlc_uri_decode_duplicate (end - 1);
155 if (name != NULL)
157 name[0] = '.';
158 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
159 free (name);
163 else
165 if (unlikely(p_access->psz_filepath == NULL))
166 return VLC_EGENERIC;
167 fd = vlc_open (p_access->psz_filepath, O_RDONLY | O_NONBLOCK);
170 if (fd == -1)
172 msg_Err (p_access, "cannot open file %s (%s)",
173 p_access->psz_filepath ? p_access->psz_filepath
174 : p_access->psz_location,
175 vlc_strerror_c(errno));
176 return VLC_EGENERIC;
179 struct stat st;
180 if (fstat (fd, &st))
182 msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
183 goto error;
186 #if O_NONBLOCK
187 /* Force blocking mode back */
188 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) & ~O_NONBLOCK);
189 #endif
191 /* Directories can be opened and read from, but only readdir() knows
192 * how to parse the data. The directory plugin will do it. */
193 if (S_ISDIR (st.st_mode))
195 #ifdef HAVE_FDOPENDIR
196 DIR *p_dir = fdopendir(fd);
197 if (!p_dir) {
198 msg_Err (p_access, "fdopendir error: %s", vlc_strerror_c(errno));
199 goto error;
201 return DirInit (p_access, p_dir);
202 #else
203 msg_Dbg (p_access, "ignoring directory");
204 goto error;
205 #endif
208 access_sys_t *p_sys = vlc_obj_malloc(p_this, sizeof (*p_sys));
209 if (unlikely(p_sys == NULL))
210 goto error;
211 p_access->pf_read = Read;
212 p_access->pf_block = NULL;
213 p_access->pf_control = FileControl;
214 p_access->p_sys = p_sys;
215 p_sys->fd = fd;
217 if (S_ISREG (st.st_mode) || S_ISBLK (st.st_mode))
219 p_access->pf_seek = FileSeek;
220 p_sys->b_pace_control = true;
222 /* Demuxers will need the beginning of the file for probing. */
223 posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
224 /* In most cases, we only read the file once. */
225 posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
226 #ifdef F_NOCACHE
227 fcntl (fd, F_NOCACHE, 0);
228 #endif
229 #ifdef F_RDAHEAD
230 if (IsRemote(fd, p_access->psz_filepath))
231 fcntl (fd, F_RDAHEAD, 0);
232 else
233 fcntl (fd, F_RDAHEAD, 1);
234 #endif
236 else
238 p_access->pf_seek = NoSeek;
239 p_sys->b_pace_control = strcasecmp (p_access->psz_name, "stream");
242 return VLC_SUCCESS;
244 error:
245 vlc_close (fd);
246 return VLC_EGENERIC;
249 /*****************************************************************************
250 * FileClose: close the target
251 *****************************************************************************/
252 void FileClose (vlc_object_t * p_this)
254 stream_t *p_access = (stream_t*)p_this;
256 if (p_access->pf_read == NULL)
258 DirClose (p_this);
259 return;
262 access_sys_t *p_sys = p_access->p_sys;
264 vlc_close (p_sys->fd);
268 static ssize_t Read (stream_t *p_access, void *p_buffer, size_t i_len)
270 access_sys_t *p_sys = p_access->p_sys;
271 int fd = p_sys->fd;
273 ssize_t val = vlc_read_i11e (fd, p_buffer, i_len);
274 if (val < 0)
276 switch (errno)
278 case EINTR:
279 case EAGAIN:
280 return -1;
283 msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
284 val = 0;
287 return val;
290 /*****************************************************************************
291 * Seek: seek to a specific location in a file
292 *****************************************************************************/
293 static int FileSeek (stream_t *p_access, uint64_t i_pos)
295 access_sys_t *sys = p_access->p_sys;
297 if (lseek(sys->fd, i_pos, SEEK_SET) == (off_t)-1)
298 return VLC_EGENERIC;
299 return VLC_SUCCESS;
302 static int NoSeek (stream_t *p_access, uint64_t i_pos)
304 /* vlc_assert_unreachable(); ?? */
305 (void) p_access; (void) i_pos;
306 return VLC_EGENERIC;
309 /*****************************************************************************
310 * Control:
311 *****************************************************************************/
312 static int FileControl( stream_t *p_access, int i_query, va_list args )
314 access_sys_t *p_sys = p_access->p_sys;
315 bool *pb_bool;
316 int64_t *pi_64;
318 switch( i_query )
320 case STREAM_CAN_SEEK:
321 case STREAM_CAN_FASTSEEK:
322 pb_bool = va_arg( args, bool * );
323 *pb_bool = (p_access->pf_seek != NoSeek);
324 break;
326 case STREAM_CAN_PAUSE:
327 case STREAM_CAN_CONTROL_PACE:
328 pb_bool = va_arg( args, bool * );
329 *pb_bool = p_sys->b_pace_control;
330 break;
332 case STREAM_GET_SIZE:
334 struct stat st;
336 if (fstat (p_sys->fd, &st) || !S_ISREG(st.st_mode))
337 return VLC_EGENERIC;
338 *va_arg( args, uint64_t * ) = st.st_size;
339 break;
342 case STREAM_GET_PTS_DELAY:
343 pi_64 = va_arg( args, int64_t * );
344 if (IsRemote (p_sys->fd, p_access->psz_filepath))
345 *pi_64 = var_InheritInteger (p_access, "network-caching");
346 else
347 *pi_64 = var_InheritInteger (p_access, "file-caching");
348 *pi_64 *= 1000;
349 break;
351 case STREAM_SET_PAUSE_STATE:
352 /* Nothing to do */
353 break;
355 default:
356 return VLC_EGENERIC;
359 return VLC_SUCCESS;