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 */
78 #include <vlc_charset.h>
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
86 #define CACHING_TEXT N_("Caching value (ms)")
87 #define CACHING_LONGTEXT N_( \
88 "Caching value for files, in milliseconds." )
90 #define NETWORK_CACHING_TEXT N_("Extra network caching value (ms)")
91 #define NETWORK_CACHING_LONGTEXT N_( \
92 "Supplementary caching value for remote files, in milliseconds." )
95 set_description( N_("File input") )
96 set_shortname( N_("File") )
97 set_category( CAT_INPUT )
98 set_subcategory( SUBCAT_INPUT_ACCESS )
99 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL,
100 CACHING_TEXT, CACHING_LONGTEXT, true )
102 add_integer( "network-caching", 3 * DEFAULT_PTS_DELAY / 1000, NULL,
103 NETWORK_CACHING_TEXT, NETWORK_CACHING_LONGTEXT, true )
105 add_obsolete_string( "file-cat" )
106 set_capability( "access", 50 )
107 add_shortcut( "file" )
109 add_shortcut( "stream" )
110 set_callbacks( Open, Close )
114 /*****************************************************************************
115 * Exported prototypes
116 *****************************************************************************/
117 static int Seek( access_t *, int64_t );
118 static int NoSeek( access_t *, int64_t );
119 static ssize_t Read( access_t *, uint8_t *, size_t );
120 static int Control( access_t *, int, va_list );
122 static int open_file( access_t *, const char * );
126 unsigned int i_nb_reads;
135 static bool IsRemote (int fd)
140 if (fstatfs (fd, &stf))
143 #if defined(MNT_LOCAL)
144 return !(stf.f_flags & MNT_LOCAL);
146 #elif defined (__linux__)
149 case AFS_SUPER_MAGIC:
150 case CODA_SUPER_MAGIC:
151 case NCP_SUPER_MAGIC:
152 case NFS_SUPER_MAGIC:
153 case SMB_SUPER_MAGIC:
154 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
160 #else /* !HAVE_FSTATFS */
168 /*****************************************************************************
169 * Open: open the file
170 *****************************************************************************/
171 static int Open( vlc_object_t *p_this )
173 access_t *p_access = (access_t*)p_this;
176 wchar_t wpath[MAX_PATH+1];
177 bool is_remote = false;
180 STANDARD_READ_ACCESS_INIT;
181 p_sys->i_nb_reads = 0;
182 p_sys->b_pace_control = true;
187 if (!strcasecmp (p_access->psz_access, "fd"))
188 fd = dup (atoi (p_access->psz_path));
189 else if (!strcmp (p_access->psz_path, "-"))
193 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
194 fd = open_file (p_access, p_access->psz_path);
196 if (MultiByteToWideChar (CP_UTF8, 0, p_access->psz_path, -1,
198 && PathIsNetworkPathW (wpath))
200 # define IsRemote( fd ) ((void)fd, is_remote)
206 #ifdef HAVE_SYS_STAT_H
211 msg_Err (p_access, "failed to read (%m)");
214 /* Directories can be opened and read from, but only readdir() knows
215 * how to parse the data. The directory plugin will do it. */
216 if (S_ISDIR (st.st_mode))
218 msg_Dbg (p_access, "ignoring directory");
221 if (S_ISREG (st.st_mode))
222 p_access->info.i_size = st.st_size;
223 else if (!S_ISBLK (st.st_mode))
225 p_access->pf_seek = NoSeek;
226 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
229 # warning File size not known!
232 p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
234 p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
246 /*****************************************************************************
247 * Close: close the target
248 *****************************************************************************/
249 static void Close (vlc_object_t * p_this)
251 access_t *p_access = (access_t*)p_this;
252 access_sys_t *p_sys = p_access->p_sys;
259 #include <vlc_network.h>
261 /*****************************************************************************
262 * Read: standard read on a file descriptor.
263 *****************************************************************************/
264 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
266 access_sys_t *p_sys = p_access->p_sys;
271 if (p_access->pf_seek == NoSeek)
272 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
275 i_ret = read (fd, p_buffer, i_len);
286 msg_Err (p_access, "failed to read (%m)");
287 dialog_Fatal (p_access, _("File reading failed"), "%s",
288 _("VLC could not read the file."));
289 p_access->info.b_eof = true;
294 p_access->info.i_pos += i_ret;
296 p_access->info.b_eof = true;
300 #ifdef HAVE_SYS_STAT_H
301 if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
302 || (p_access->info.i_pos > p_access->info.i_size))
306 if ((fstat (fd, &st) == 0)
307 && (p_access->info.i_size != st.st_size))
309 p_access->info.i_size = st.st_size;
310 p_access->info.i_update |= INPUT_UPDATE_SIZE;
318 /*****************************************************************************
319 * Seek: seek to a specific location in a file
320 *****************************************************************************/
321 static int Seek (access_t *p_access, int64_t i_pos)
323 p_access->info.i_pos = i_pos;
324 p_access->info.b_eof = false;
326 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
330 static int NoSeek (access_t *p_access, int64_t i_pos)
333 (void) p_access; (void) i_pos;
337 /*****************************************************************************
339 *****************************************************************************/
340 static int Control( access_t *p_access, int i_query, va_list args )
342 access_sys_t *p_sys = p_access->p_sys;
349 case ACCESS_CAN_SEEK:
350 case ACCESS_CAN_FASTSEEK:
351 pb_bool = (bool*)va_arg( args, bool* );
352 *pb_bool = (p_access->pf_seek != NoSeek);
355 case ACCESS_CAN_PAUSE:
356 case ACCESS_CAN_CONTROL_PACE:
357 pb_bool = (bool*)va_arg( args, bool* );
358 *pb_bool = p_sys->b_pace_control;
362 case ACCESS_GET_PTS_DELAY:
363 pi_64 = (int64_t*)va_arg( args, int64_t * );
364 *pi_64 = p_sys->caching * INT64_C(1000);
368 case ACCESS_SET_PAUSE_STATE:
372 case ACCESS_GET_TITLE_INFO:
373 case ACCESS_SET_TITLE:
374 case ACCESS_SET_SEEKPOINT:
375 case ACCESS_SET_PRIVATE_ID_STATE:
376 case ACCESS_GET_META:
377 case ACCESS_GET_PRIVATE_ID_STATE:
378 case ACCESS_GET_CONTENT_TYPE:
382 msg_Warn( p_access, "unimplemented query %d in control", i_query );
389 /*****************************************************************************
390 * open_file: Opens a specific file
391 *****************************************************************************/
392 static int open_file (access_t *p_access, const char *path)
395 if (!strcasecmp (p_access->psz_access, "file")
396 && ('/' == path[0]) && isalpha (path[1])
397 && (':' == path[2]) && ('/' == path[3]))
398 /* Explorer can open path such as file:/C:/ or file:///C:/
399 * hence remove leading / if found */
403 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
406 msg_Err (p_access, "cannot open file %s (%m)", path);
407 dialog_Fatal (p_access, _("File reading failed"),
408 _("VLC could not open the file \"%s\"."), path);
412 #if defined(HAVE_FCNTL)
413 /* We'd rather use any available memory for reading ahead
414 * than for caching what we've already seen/heard */
415 # if defined(F_RDAHEAD)
416 fcntl (fd, F_RDAHEAD, 1);
418 # if defined(F_NOCACHE)
419 fcntl (fd, F_NOCACHE, 1);