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 *****************************************************************************/
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_interface.h>
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
50 #if defined( WIN32 ) && !defined( UNDER_CE )
58 #if defined( WIN32 ) && !defined( UNDER_CE )
62 # define lseek _lseeki64
63 #elif defined( UNDER_CE )
67 # define read(a,b,c) fread(b,1,c,a)
68 # define close(a) fclose(a)
75 #include <vlc_charset.h>
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open ( vlc_object_t * );
81 static void Close( vlc_object_t * );
83 #define CACHING_TEXT N_("Caching value in ms")
84 #define CACHING_LONGTEXT N_( \
85 "Caching value for files. This " \
86 "value should be set in milliseconds." )
89 set_description( _("File input") );
90 set_shortname( _("File") );
91 set_category( CAT_INPUT );
92 set_subcategory( SUBCAT_INPUT_ACCESS );
93 add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true );
94 add_obsolete_string( "file-cat" );
95 set_capability( "access", 50 );
96 add_shortcut( "file" );
97 add_shortcut( "stream" );
98 add_shortcut( "kfir" );
99 set_callbacks( Open, Close );
103 /*****************************************************************************
104 * Exported prototypes
105 *****************************************************************************/
106 static int Seek( access_t *, int64_t );
107 static ssize_t Read( access_t *, uint8_t *, size_t );
108 static int Control( access_t *, int, va_list );
110 static int open_file( access_t *, const char * );
114 unsigned int i_nb_reads;
124 /*****************************************************************************
125 * Open: open the file
126 *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
129 access_t *p_access = (access_t*)p_this;
132 bool b_stdin = !strcmp (p_access->psz_path, "-");
134 /* Update default_pts to a suitable value for file access */
135 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
137 STANDARD_READ_ACCESS_INIT;
138 p_sys->i_nb_reads = 0;
139 p_sys->b_kfir = false;
140 int fd = p_sys->fd = -1;
142 if (!strcasecmp (p_access->psz_access, "stream"))
144 p_sys->b_seekable = false;
145 p_sys->b_pace_control = false;
147 else if (!strcasecmp (p_access->psz_access, "kfir"))
149 p_sys->b_seekable = false;
150 p_sys->b_pace_control = false;
151 p_sys->b_kfir = true;
155 p_sys->b_seekable = true;
156 p_sys->b_pace_control = true;
160 msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
165 fd = open_file (p_access, p_access->psz_path);
167 #ifdef HAVE_SYS_STAT_H
173 msg_Err (p_access, "fstat(%d): %m", fd);
175 if (S_ISDIR (st.st_mode))
176 /* The directory plugin takes care of that */
177 msg_Dbg (p_access, "file is a directory, aborting");
193 #ifdef HAVE_SYS_STAT_H
194 p_access->info.i_size = st.st_size;
195 if (!S_ISREG (st.st_mode)
196 && !S_ISBLK (st.st_mode)
197 && !S_ISCHR (st.st_mode))
198 p_sys->b_seekable = false;
200 p_sys->b_seekable = !b_stdin;
201 # warning File size not known!
207 /*****************************************************************************
208 * Close: close the target
209 *****************************************************************************/
210 static void Close (vlc_object_t * p_this)
212 access_t *p_access = (access_t*)p_this;
213 access_sys_t *p_sys = p_access->p_sys;
219 /*****************************************************************************
220 * Read: standard read on a file descriptor.
221 *****************************************************************************/
222 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
224 access_sys_t *p_sys = p_access->p_sys;
228 #if !defined(WIN32) && !defined(UNDER_CE)
229 if( !p_sys->b_pace_control )
233 /* Find if some data is available. This won't work under Windows. */
238 if( p_access->b_die )
241 memset (&ufd, 0, sizeof (ufd));
245 i_ret = poll (&ufd, 1, 500);
249 i_ret = read (fd, p_buffer, i_len);
253 /* b_kfir ; work around a buggy poll() driver implementation */
254 while (((i_ret = read (fd, p_buffer, i_len)) == 0)
257 msleep( INPUT_ERROR_SLEEP );
262 #endif /* WIN32 || UNDER_CE */
263 /* b_pace_control || WIN32 */
264 i_ret = read( fd, p_buffer, i_len );
275 msg_Err (p_access, "read failed (%m)");
276 intf_UserFatal (p_access, false, _("File reading failed"),
277 _("VLC could not read the file."));
280 /* Delay a bit to avoid consuming all the CPU. This is particularly
281 * useful when reading from an unconnected FIFO. */
282 msleep( INPUT_ERROR_SLEEP );
287 #ifdef HAVE_SYS_STAT_H
288 if( p_access->info.i_size != 0 &&
289 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
293 if ((fstat (fd, &st) == 0)
294 && (p_access->info.i_size != st.st_size))
296 p_access->info.i_size = st.st_size;
297 p_access->info.i_update |= INPUT_UPDATE_SIZE;
303 p_access->info.i_pos += i_ret;
304 else if( i_ret == 0 )
305 p_access->info.b_eof = true;
311 /*****************************************************************************
312 * Seek: seek to a specific location in a file
313 *****************************************************************************/
314 static int Seek (access_t *p_access, int64_t i_pos)
316 p_access->info.i_pos = i_pos;
317 p_access->info.b_eof = false;
319 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
323 /*****************************************************************************
325 *****************************************************************************/
326 static int Control( access_t *p_access, int i_query, va_list args )
328 access_sys_t *p_sys = p_access->p_sys;
336 case ACCESS_CAN_SEEK:
337 case ACCESS_CAN_FASTSEEK:
338 pb_bool = (bool*)va_arg( args, bool* );
339 *pb_bool = p_sys->b_seekable;
342 case ACCESS_CAN_PAUSE:
343 case ACCESS_CAN_CONTROL_PACE:
344 pb_bool = (bool*)va_arg( args, bool* );
345 *pb_bool = p_sys->b_pace_control;
350 pi_int = (int*)va_arg( args, int * );
354 case ACCESS_GET_PTS_DELAY:
355 pi_64 = (int64_t*)va_arg( args, int64_t * );
356 *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
360 case ACCESS_SET_PAUSE_STATE:
364 case ACCESS_GET_TITLE_INFO:
365 case ACCESS_SET_TITLE:
366 case ACCESS_SET_SEEKPOINT:
367 case ACCESS_SET_PRIVATE_ID_STATE:
368 case ACCESS_GET_META:
369 case ACCESS_GET_PRIVATE_ID_STATE:
370 case ACCESS_GET_CONTENT_TYPE:
374 msg_Warn( p_access, "unimplemented query %d in control", i_query );
381 /*****************************************************************************
382 * open_file: Opens a specific file
383 *****************************************************************************/
384 static int open_file (access_t *p_access, const char *path)
387 if (!strcasecmp (p_access->psz_access, "file")
388 && ('/' == path[0]) && isalpha (path[1])
389 && (':' == path[2]) && ('/' == path[3]))
390 /* Explorer can open path such as file:/C:/ or file:///C:/
391 * hence remove leading / if found */
396 p_sys->fd = utf8_fopen( path, "rb" );
399 msg_Err( p_access, "cannot open file %s", path );
400 intf_UserFatal( p_access, false, _("File reading failed"),
401 _("VLC could not open the file \"%s\"."), path );
405 fseek( p_sys->fd, 0, SEEK_END );
406 p_access->info.i_size = ftell( p_sys->fd );
407 p_access->info.i_update |= INPUT_UPDATE_SIZE;
408 fseek( p_sys->fd, 0, SEEK_SET );
410 int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
413 msg_Err (p_access, "cannot open file %s (%m)", path);
414 intf_UserFatal (p_access, false, _("File reading failed"),
415 _("VLC could not open the file \"%s\"."), path);
419 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
420 /* We'd rather use any available memory for reading ahead
421 * than for caching what we've already seen/heard */
422 fcntl (fd, F_RDAHEAD, 1);
423 fcntl (fd, F_NOCACHE, 1);