1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006-2007 Rémi Denis-Courmont
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
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 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 General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
50 #if defined (__linux__)
52 # include <linux/magic.h>
53 # define HAVE_FSTATFS 1
54 #elif defined (HAVE_SYS_MOUNT_H)
55 # include <sys/param.h>
56 # include <sys/mount.h>
57 # define HAVE_FSTATFS 1
69 #if defined( WIN32 ) && !defined( UNDER_CE )
73 # define lseek _lseeki64
74 #elif defined( UNDER_CE )
75 /* FIXME the commandline on wince is a mess */
77 # define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
80 #include <vlc_charset.h>
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Open ( vlc_object_t * );
86 static void Close( vlc_object_t * );
88 #define CACHING_TEXT N_("Caching value (ms)")
89 #define CACHING_LONGTEXT N_( \
90 "Caching value for files, in milliseconds." )
92 #define NETWORK_CACHING_TEXT N_("Extra network caching value (ms)")
93 #define NETWORK_CACHING_LONGTEXT N_( \
94 "Supplementary caching value for remote files, in milliseconds." )
97 set_description( N_("File input") )
98 set_shortname( N_("File") )
99 set_category( CAT_INPUT )
100 set_subcategory( SUBCAT_INPUT_ACCESS )
101 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL,
102 CACHING_TEXT, CACHING_LONGTEXT, true )
104 add_integer( "network-caching", 3 * DEFAULT_PTS_DELAY / 1000, NULL,
105 NETWORK_CACHING_TEXT, NETWORK_CACHING_LONGTEXT, true )
107 add_obsolete_string( "file-cat" )
108 set_capability( "access", 50 )
109 add_shortcut( "file" )
111 add_shortcut( "stream" )
112 set_callbacks( Open, Close )
116 /*****************************************************************************
117 * Exported prototypes
118 *****************************************************************************/
119 static int Seek( access_t *, int64_t );
120 static int NoSeek( access_t *, int64_t );
121 static ssize_t Read( access_t *, uint8_t *, size_t );
122 static int Control( access_t *, int, va_list );
124 static int open_file( access_t *, const char * );
128 unsigned int i_nb_reads;
137 static bool IsRemote (int fd)
142 if (fstatfs (fd, &stf))
145 #if defined(MNT_LOCAL)
146 return !(stf.f_flags & MNT_LOCAL);
148 #elif defined (__linux__)
151 case AFS_SUPER_MAGIC:
152 case CODA_SUPER_MAGIC:
153 case NCP_SUPER_MAGIC:
154 case NFS_SUPER_MAGIC:
155 case SMB_SUPER_MAGIC:
156 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
162 #else /* !HAVE_FSTATFS */
169 #ifndef HAVE_POSIX_FADVISE
170 # define posix_fadvise(fd, off, len, adv) (0)
173 /*****************************************************************************
174 * Open: open the file
175 *****************************************************************************/
176 static int Open( vlc_object_t *p_this )
178 access_t *p_access = (access_t*)p_this;
181 wchar_t wpath[MAX_PATH+1];
182 bool is_remote = false;
185 STANDARD_READ_ACCESS_INIT;
186 p_sys->i_nb_reads = 0;
187 p_sys->b_pace_control = true;
192 if (!strcasecmp (p_access->psz_access, "fd"))
193 fd = dup (atoi (p_access->psz_path));
194 else if (!strcmp (p_access->psz_path, "-"))
198 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
199 fd = open_file (p_access, p_access->psz_path);
201 if (MultiByteToWideChar (CP_UTF8, 0, p_access->psz_path, -1,
203 && PathIsNetworkPathW (wpath))
205 # define IsRemote( fd ) ((void)fd, is_remote)
211 #ifdef HAVE_SYS_STAT_H
216 msg_Err (p_access, "failed to read (%m)");
219 /* Directories can be opened and read from, but only readdir() knows
220 * how to parse the data. The directory plugin will do it. */
221 if (S_ISDIR (st.st_mode))
223 msg_Dbg (p_access, "ignoring directory");
226 if (S_ISREG (st.st_mode))
227 p_access->info.i_size = st.st_size;
228 else if (!S_ISBLK (st.st_mode))
230 p_access->pf_seek = NoSeek;
231 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
234 # warning File size not known!
237 p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
239 p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
243 if (p_access->pf_seek != NoSeek)
245 /* Demuxers will need the beginning of the file for probing. */
246 posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
247 /* In most cases, we only read the file once. */
248 posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
259 /*****************************************************************************
260 * Close: close the target
261 *****************************************************************************/
262 static void Close (vlc_object_t * p_this)
264 access_t *p_access = (access_t*)p_this;
265 access_sys_t *p_sys = p_access->p_sys;
272 #include <vlc_network.h>
274 /*****************************************************************************
275 * Read: standard read on a file descriptor.
276 *****************************************************************************/
277 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
279 access_sys_t *p_sys = p_access->p_sys;
284 if (p_access->pf_seek == NoSeek)
285 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
288 i_ret = read (fd, p_buffer, i_len);
299 msg_Err (p_access, "failed to read (%m)");
300 dialog_Fatal (p_access, _("File reading failed"), "%s",
301 _("VLC could not read the file."));
302 p_access->info.b_eof = true;
307 p_access->info.i_pos += i_ret;
309 p_access->info.b_eof = true;
313 if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
314 || (p_access->info.i_pos > p_access->info.i_size))
316 #ifdef HAVE_SYS_STAT_H
319 if ((fstat (fd, &st) == 0)
320 && (p_access->info.i_size != st.st_size))
322 p_access->info.i_size = st.st_size;
323 p_access->info.i_update |= INPUT_UPDATE_SIZE;
331 /*****************************************************************************
332 * Seek: seek to a specific location in a file
333 *****************************************************************************/
334 static int Seek (access_t *p_access, int64_t i_pos)
336 p_access->info.i_pos = i_pos;
337 p_access->info.b_eof = false;
339 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
343 static int NoSeek (access_t *p_access, int64_t i_pos)
346 (void) p_access; (void) i_pos;
350 /*****************************************************************************
352 *****************************************************************************/
353 static int Control( access_t *p_access, int i_query, va_list args )
355 access_sys_t *p_sys = p_access->p_sys;
362 case ACCESS_CAN_SEEK:
363 case ACCESS_CAN_FASTSEEK:
364 pb_bool = (bool*)va_arg( args, bool* );
365 *pb_bool = (p_access->pf_seek != NoSeek);
368 case ACCESS_CAN_PAUSE:
369 case ACCESS_CAN_CONTROL_PACE:
370 pb_bool = (bool*)va_arg( args, bool* );
371 *pb_bool = p_sys->b_pace_control;
375 case ACCESS_GET_PTS_DELAY:
376 pi_64 = (int64_t*)va_arg( args, int64_t * );
377 *pi_64 = p_sys->caching * INT64_C(1000);
381 case ACCESS_SET_PAUSE_STATE:
385 case ACCESS_GET_TITLE_INFO:
386 case ACCESS_SET_TITLE:
387 case ACCESS_SET_SEEKPOINT:
388 case ACCESS_SET_PRIVATE_ID_STATE:
389 case ACCESS_GET_META:
390 case ACCESS_GET_PRIVATE_ID_STATE:
391 case ACCESS_GET_CONTENT_TYPE:
395 msg_Warn( p_access, "unimplemented query %d in control", i_query );
402 /*****************************************************************************
403 * open_file: Opens a specific file
404 *****************************************************************************/
405 static int open_file (access_t *p_access, const char *path)
408 if (!strcasecmp (p_access->psz_access, "file")
409 && ('/' == path[0]) && isalpha (path[1])
410 && (':' == path[2]) && ('/' == path[3]))
411 /* Explorer can open path such as file:/C:/ or file:///C:/
412 * hence remove leading / if found */
416 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
419 msg_Err (p_access, "cannot open file %s (%m)", path);
420 dialog_Fatal (p_access, _("File reading failed"),
421 _("VLC could not open the file \"%s\"."), path);
425 #if defined(HAVE_FCNTL)
426 /* We'd rather use any available memory for reading ahead
427 * than for caching what we've already seen/heard */
428 # if defined(F_RDAHEAD)
429 fcntl (fd, F_RDAHEAD, 1);
431 # if defined(F_NOCACHE)
432 fcntl (fd, F_NOCACHE, 1);