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>
51 #include <sys/mount.h>
62 #if defined( WIN32 ) && !defined( UNDER_CE )
66 # define lseek _lseeki64
67 #elif defined( UNDER_CE )
68 /* FIXME the commandline on wince is a mess */
72 #include <vlc_charset.h>
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
80 #define CACHING_TEXT N_("Caching value in ms")
81 #define CACHING_LONGTEXT N_( \
82 "Caching value for files. This " \
83 "value should be set in milliseconds." )
86 set_description( N_("File input") )
87 set_shortname( N_("File") )
88 set_category( CAT_INPUT )
89 set_subcategory( SUBCAT_INPUT_ACCESS )
90 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
92 add_obsolete_string( "file-cat" )
93 set_capability( "access", 50 )
94 add_shortcut( "file" )
96 add_shortcut( "stream" )
97 set_callbacks( Open, Close )
101 /*****************************************************************************
102 * Exported prototypes
103 *****************************************************************************/
104 static int Seek( access_t *, int64_t );
105 static int NoSeek( access_t *, int64_t );
106 static ssize_t Read( access_t *, uint8_t *, size_t );
107 static int Control( access_t *, int, va_list );
109 static int open_file( access_t *, const char * );
113 unsigned int i_nb_reads;
121 /*****************************************************************************
122 * Open: open the file
123 *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
126 access_t *p_access = (access_t*)p_this;
129 /* Update default_pts to a suitable value for file access */
130 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
132 STANDARD_READ_ACCESS_INIT;
133 p_sys->i_nb_reads = 0;
134 p_sys->b_pace_control = true;
139 if (!strcasecmp (p_access->psz_access, "fd"))
140 fd = dup (atoi (p_access->psz_path));
141 else if (!strcmp (p_access->psz_path, "-"))
145 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
146 fd = open_file (p_access, p_access->psz_path);
151 #ifdef HAVE_SYS_STAT_H
156 msg_Err (p_access, "failed to read (%m)");
159 /* Directories can be opened and read from, but only readdir() knows
160 * how to parse the data. The directory plugin will do it. */
161 if (S_ISDIR (st.st_mode))
163 msg_Dbg (p_access, "ignoring directory");
166 if (S_ISREG (st.st_mode))
167 p_access->info.i_size = st.st_size;
168 else if (!S_ISBLK (st.st_mode))
170 p_access->pf_seek = NoSeek;
171 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
174 # warning File size not known!
177 #if defined(HAVE_SYS_MOUNT_H) && defined(MNT_LOCAL)
179 if ((fstatfs (fd, &stat) == 0) && !(stat.f_flags & MNT_LOCAL) ) {
180 int i_cache = var_GetInteger (p_access, "file-caching") + 700;
181 var_SetInteger (p_access, "file-caching", i_cache);
182 msg_Warn (p_access, "Opening non-local file, use more caching: %d", i_cache);
196 /*****************************************************************************
197 * Close: close the target
198 *****************************************************************************/
199 static void Close (vlc_object_t * p_this)
201 access_t *p_access = (access_t*)p_this;
202 access_sys_t *p_sys = p_access->p_sys;
209 #include <vlc_network.h>
211 /*****************************************************************************
212 * Read: standard read on a file descriptor.
213 *****************************************************************************/
214 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
216 access_sys_t *p_sys = p_access->p_sys;
221 if (p_access->pf_seek == NoSeek)
222 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
225 i_ret = read (fd, p_buffer, i_len);
236 msg_Err (p_access, "failed to read (%m)");
237 dialog_Fatal (p_access, _("File reading failed"), "%s",
238 _("VLC could not read the file."));
239 p_access->info.b_eof = true;
244 p_access->info.i_pos += i_ret;
246 p_access->info.b_eof = true;
250 #ifdef HAVE_SYS_STAT_H
251 if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
252 || (p_access->info.i_pos > p_access->info.i_size))
256 if ((fstat (fd, &st) == 0)
257 && (p_access->info.i_size != st.st_size))
259 p_access->info.i_size = st.st_size;
260 p_access->info.i_update |= INPUT_UPDATE_SIZE;
268 /*****************************************************************************
269 * Seek: seek to a specific location in a file
270 *****************************************************************************/
271 static int Seek (access_t *p_access, int64_t i_pos)
273 p_access->info.i_pos = i_pos;
274 p_access->info.b_eof = false;
276 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
280 static int NoSeek (access_t *p_access, int64_t i_pos)
283 (void) p_access; (void) i_pos;
287 /*****************************************************************************
289 *****************************************************************************/
290 static int Control( access_t *p_access, int i_query, va_list args )
292 access_sys_t *p_sys = p_access->p_sys;
299 case ACCESS_CAN_SEEK:
300 case ACCESS_CAN_FASTSEEK:
301 pb_bool = (bool*)va_arg( args, bool* );
302 *pb_bool = (p_access->pf_seek != NoSeek);
305 case ACCESS_CAN_PAUSE:
306 case ACCESS_CAN_CONTROL_PACE:
307 pb_bool = (bool*)va_arg( args, bool* );
308 *pb_bool = p_sys->b_pace_control;
312 case ACCESS_GET_PTS_DELAY:
313 pi_64 = (int64_t*)va_arg( args, int64_t * );
314 *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
318 case ACCESS_SET_PAUSE_STATE:
322 case ACCESS_GET_TITLE_INFO:
323 case ACCESS_SET_TITLE:
324 case ACCESS_SET_SEEKPOINT:
325 case ACCESS_SET_PRIVATE_ID_STATE:
326 case ACCESS_GET_META:
327 case ACCESS_GET_PRIVATE_ID_STATE:
328 case ACCESS_GET_CONTENT_TYPE:
332 msg_Warn( p_access, "unimplemented query %d in control", i_query );
339 /*****************************************************************************
340 * open_file: Opens a specific file
341 *****************************************************************************/
342 static int open_file (access_t *p_access, const char *path)
345 if (!strcasecmp (p_access->psz_access, "file")
346 && ('/' == path[0]) && isalpha (path[1])
347 && (':' == path[2]) && ('/' == path[3]))
348 /* Explorer can open path such as file:/C:/ or file:///C:/
349 * hence remove leading / if found */
353 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
356 msg_Err (p_access, "cannot open file %s (%m)", path);
357 dialog_Fatal (p_access, _("File reading failed"),
358 _("VLC could not open the file \"%s\"."), path);
362 #if defined(HAVE_FCNTL)
363 /* We'd rather use any available memory for reading ahead
364 * than for caching what we've already seen/heard */
365 # if defined(F_RDAHEAD)
366 fcntl (fd, F_RDAHEAD, 1);
368 # if defined(F_NOCACHE)
369 fcntl (fd, F_NOCACHE, 1);