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/mount.h>
56 # define HAVE_FSTATFS 1
68 #if defined( WIN32 ) && !defined( UNDER_CE )
72 # define lseek _lseeki64
73 #elif defined( UNDER_CE )
74 /* FIXME the commandline on wince is a mess */
76 # define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
79 #include <vlc_charset.h>
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
87 #define CACHING_TEXT N_("Caching value (ms)")
88 #define CACHING_LONGTEXT N_( \
89 "Caching value for files, in milliseconds." )
91 #define NETWORK_CACHING_TEXT N_("Extra network caching value (ms)")
92 #define NETWORK_CACHING_LONGTEXT N_( \
93 "Supplementary caching value for remote files, in milliseconds." )
96 set_description( N_("File input") )
97 set_shortname( N_("File") )
98 set_category( CAT_INPUT )
99 set_subcategory( SUBCAT_INPUT_ACCESS )
100 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL,
101 CACHING_TEXT, CACHING_LONGTEXT, true )
103 add_integer( "network-caching", 3 * DEFAULT_PTS_DELAY / 1000, NULL,
104 NETWORK_CACHING_TEXT, NETWORK_CACHING_LONGTEXT, true )
106 add_obsolete_string( "file-cat" )
107 set_capability( "access", 50 )
108 add_shortcut( "file" )
110 add_shortcut( "stream" )
111 set_callbacks( Open, Close )
115 /*****************************************************************************
116 * Exported prototypes
117 *****************************************************************************/
118 static int Seek( access_t *, int64_t );
119 static int NoSeek( access_t *, int64_t );
120 static ssize_t Read( access_t *, uint8_t *, size_t );
121 static int Control( access_t *, int, va_list );
123 static int open_file( access_t *, const char * );
127 unsigned int i_nb_reads;
136 static bool IsRemote (int fd)
141 if (fstatfs (fd, &stf))
144 #if defined(MNT_LOCAL)
145 return !(stf.f_flags & MNT_LOCAL);
147 #elif defined (__linux__)
150 case AFS_SUPER_MAGIC:
151 case CODA_SUPER_MAGIC:
152 case NCP_SUPER_MAGIC:
153 case NFS_SUPER_MAGIC:
154 case SMB_SUPER_MAGIC:
155 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
161 #else /* !HAVE_FSTATFS */
168 #ifndef HAVE_POSIX_FADVISE
169 # define posix_fadvise(fd, off, len, adv) (0)
172 /*****************************************************************************
173 * Open: open the file
174 *****************************************************************************/
175 static int Open( vlc_object_t *p_this )
177 access_t *p_access = (access_t*)p_this;
180 wchar_t wpath[MAX_PATH+1];
181 bool is_remote = false;
184 STANDARD_READ_ACCESS_INIT;
185 p_sys->i_nb_reads = 0;
186 p_sys->b_pace_control = true;
191 if (!strcasecmp (p_access->psz_access, "fd"))
192 fd = dup (atoi (p_access->psz_path));
193 else if (!strcmp (p_access->psz_path, "-"))
197 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
198 fd = open_file (p_access, p_access->psz_path);
200 if (MultiByteToWideChar (CP_UTF8, 0, p_access->psz_path, -1,
202 && PathIsNetworkPathW (wpath))
204 # define IsRemote( fd ) ((void)fd, is_remote)
210 #ifdef HAVE_SYS_STAT_H
215 msg_Err (p_access, "failed to read (%m)");
218 /* Directories can be opened and read from, but only readdir() knows
219 * how to parse the data. The directory plugin will do it. */
220 if (S_ISDIR (st.st_mode))
222 msg_Dbg (p_access, "ignoring directory");
225 if (S_ISREG (st.st_mode))
226 p_access->info.i_size = st.st_size;
227 else if (!S_ISBLK (st.st_mode))
229 p_access->pf_seek = NoSeek;
230 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
233 # warning File size not known!
236 p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
238 p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
242 if (p_access->pf_seek != NoSeek)
244 /* Demuxers will need the beginning of the file for probing. */
245 posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
246 /* In most cases, we only read the file once. */
247 posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
258 /*****************************************************************************
259 * Close: close the target
260 *****************************************************************************/
261 static void Close (vlc_object_t * p_this)
263 access_t *p_access = (access_t*)p_this;
264 access_sys_t *p_sys = p_access->p_sys;
271 #include <vlc_network.h>
273 /*****************************************************************************
274 * Read: standard read on a file descriptor.
275 *****************************************************************************/
276 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
278 access_sys_t *p_sys = p_access->p_sys;
283 if (p_access->pf_seek == NoSeek)
284 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
287 i_ret = read (fd, p_buffer, i_len);
298 msg_Err (p_access, "failed to read (%m)");
299 dialog_Fatal (p_access, _("File reading failed"), "%s",
300 _("VLC could not read the file."));
301 p_access->info.b_eof = true;
306 p_access->info.i_pos += i_ret;
308 p_access->info.b_eof = true;
312 if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
313 || (p_access->info.i_pos > p_access->info.i_size))
315 #ifdef HAVE_SYS_STAT_H
318 if ((fstat (fd, &st) == 0)
319 && (p_access->info.i_size != st.st_size))
321 p_access->info.i_size = st.st_size;
322 p_access->info.i_update |= INPUT_UPDATE_SIZE;
330 /*****************************************************************************
331 * Seek: seek to a specific location in a file
332 *****************************************************************************/
333 static int Seek (access_t *p_access, int64_t i_pos)
335 p_access->info.i_pos = i_pos;
336 p_access->info.b_eof = false;
338 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
342 static int NoSeek (access_t *p_access, int64_t i_pos)
345 (void) p_access; (void) i_pos;
349 /*****************************************************************************
351 *****************************************************************************/
352 static int Control( access_t *p_access, int i_query, va_list args )
354 access_sys_t *p_sys = p_access->p_sys;
361 case ACCESS_CAN_SEEK:
362 case ACCESS_CAN_FASTSEEK:
363 pb_bool = (bool*)va_arg( args, bool* );
364 *pb_bool = (p_access->pf_seek != NoSeek);
367 case ACCESS_CAN_PAUSE:
368 case ACCESS_CAN_CONTROL_PACE:
369 pb_bool = (bool*)va_arg( args, bool* );
370 *pb_bool = p_sys->b_pace_control;
374 case ACCESS_GET_PTS_DELAY:
375 pi_64 = (int64_t*)va_arg( args, int64_t * );
376 *pi_64 = p_sys->caching * INT64_C(1000);
380 case ACCESS_SET_PAUSE_STATE:
384 case ACCESS_GET_TITLE_INFO:
385 case ACCESS_SET_TITLE:
386 case ACCESS_SET_SEEKPOINT:
387 case ACCESS_SET_PRIVATE_ID_STATE:
388 case ACCESS_GET_META:
389 case ACCESS_GET_PRIVATE_ID_STATE:
390 case ACCESS_GET_CONTENT_TYPE:
394 msg_Warn( p_access, "unimplemented query %d in control", i_query );
401 /*****************************************************************************
402 * open_file: Opens a specific file
403 *****************************************************************************/
404 static int open_file (access_t *p_access, const char *path)
407 if (!strcasecmp (p_access->psz_access, "file")
408 && ('/' == path[0]) && isalpha (path[1])
409 && (':' == path[2]) && ('/' == path[3]))
410 /* Explorer can open path such as file:/C:/ or file:///C:/
411 * hence remove leading / if found */
415 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
418 msg_Err (p_access, "cannot open file %s (%m)", path);
419 dialog_Fatal (p_access, _("File reading failed"),
420 _("VLC could not open the file \"%s\"."), path);
424 #if defined(HAVE_FCNTL)
425 /* We'd rather use any available memory for reading ahead
426 * than for caching what we've already seen/heard */
427 # if defined(F_RDAHEAD)
428 fcntl (fd, F_RDAHEAD, 1);
430 # if defined(F_NOCACHE)
431 fcntl (fd, F_NOCACHE, 1);