1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan # org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc/input.h>
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
44 #if defined( WIN32 ) && !defined( UNDER_CE )
51 #if defined( WIN32 ) && !defined( UNDER_CE )
52 /* stat() support for large files on win32 */
53 # define stat _stati64
54 # define fstat(a,b) _fstati64(a,b)
58 # define lseek _lseeki64
59 #elif defined( UNDER_CE )
63 # define read(a,b,c) fread(b,1,c,a)
64 # define close(a) fclose(a)
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Open ( vlc_object_t * );
77 static void Close( vlc_object_t * );
79 #define CACHING_TEXT N_("Caching value in ms")
80 #define CACHING_LONGTEXT N_( \
81 "Caching value for files. This " \
82 "value should be set in milliseconds." )
83 #define CAT_TEXT N_("Concatenate with additional files")
84 #define CAT_LONGTEXT N_( \
85 "Play split files as if they were part of a unique file. " \
86 "You need to specify a comma-separated list of files." )
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, VLC_TRUE );
94 add_string( "file-cat", NULL, NULL, CAT_TEXT, CAT_LONGTEXT, VLC_TRUE );
95 set_capability( "access2", 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 int Read( access_t *, uint8_t *, int );
108 static int Control( access_t *, int, va_list );
110 static int _OpenFile( access_t *, const char * );
121 unsigned int i_nb_reads;
139 vlc_bool_t b_seekable;
140 vlc_bool_t b_pace_control;
143 /*****************************************************************************
144 * Open: open the file
145 *****************************************************************************/
146 static int Open( vlc_object_t *p_this )
148 access_t *p_access = (access_t*)p_this;
150 char *psz_name = strdup( p_access->psz_path );
153 #ifdef HAVE_SYS_STAT_H
154 struct stat stat_info;
156 file_entry_t * p_file;
157 vlc_bool_t b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
161 if( psz_name[0] == '~' && psz_name[1] == '/' )
163 /* This is incomplete : we should also support the ~cmassiot/
165 asprintf( &psz, "%s/%s", p_access->p_vlc->psz_homedir, psz_name + 2 );
170 else if( !strcasecmp( p_access->psz_access, "file" )
171 && ('/' == psz_name[0]) && psz_name[1]
172 && (':' == psz_name[2]) && ('/' == psz_name[3]) )
175 ** explorer can open path such as file:/C:/ or file:///C:/...
176 ** hence remove leading / if found
182 #ifdef HAVE_SYS_STAT_H
183 if( utf8_stat( psz_name, &stat_info ) )
185 msg_Warn( p_access, "%s: %s", psz_name, strerror( errno ) );
192 STANDARD_READ_ACCESS_INIT;
193 p_sys->i_nb_reads = 0;
194 p_sys->b_kfir = VLC_FALSE;
202 if( !strcasecmp( p_access->psz_access, "stream" ) )
204 p_sys->b_seekable = VLC_FALSE;
205 p_sys->b_pace_control = VLC_FALSE;
207 else if( !strcasecmp( p_access->psz_access, "kfir" ) )
209 p_sys->b_seekable = VLC_FALSE;
210 p_sys->b_pace_control = VLC_FALSE;
211 p_sys->b_kfir = VLC_TRUE;
216 p_sys->b_pace_control = VLC_TRUE;
220 p_sys->b_seekable = VLC_FALSE;
225 /* We'll update i_size after it's been opened */
226 p_sys->b_seekable = VLC_TRUE;
228 #elif defined( HAVE_SYS_STAT_H )
229 else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
230 S_ISBLK(stat_info.st_mode) )
232 p_sys->b_seekable = VLC_TRUE;
233 p_access->info.i_size = stat_info.st_size;
235 else if( S_ISFIFO(stat_info.st_mode)
236 # if !defined( SYS_BEOS ) && !defined( WIN32 )
237 || S_ISSOCK(stat_info.st_mode)
241 p_sys->b_seekable = VLC_FALSE;
246 msg_Err( p_access, "unknown file type for `%s'", psz_name );
251 msg_Dbg( p_access, "opening file `%s'", psz_name );
257 else if( _OpenFile( p_access, psz_name ) )
264 if( p_sys->b_seekable && !p_access->info.i_size )
266 /* FIXME that's bad because all others access will be probed */
267 msg_Err( p_access, "file %s is empty, aborting", psz_name );
273 /* Update default_pts to a suitable value for file access */
274 var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
277 * Get the additional list of files
279 p_file = malloc( sizeof(file_entry_t) );
280 p_file->i_size = p_access->info.i_size;
281 p_file->psz_name = psz_name;
282 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
284 psz = var_CreateGetString( p_access, "file-cat" );
287 char *psz_parser = psz_name = psz;
290 while( psz_name && *psz_name )
292 psz_parser = strchr( psz_name, ',' );
293 if( psz_parser ) *psz_parser = 0;
295 psz_name = strdup( psz_name );
298 msg_Dbg( p_access, "adding file `%s'", psz_name );
301 #ifdef HAVE_SYS_STAT_H
302 if( !stat( psz_name, &stat_info ) )
304 p_access->info.i_size += stat_info.st_size;
305 i_size = stat_info.st_size;
309 msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
312 p_file = malloc( sizeof(file_entry_t) );
313 p_file->i_size = i_size;
314 p_file->psz_name = psz_name;
316 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
319 psz_name = psz_parser;
320 if( psz_name ) psz_name++;
328 /*****************************************************************************
329 * Close: close the target
330 *****************************************************************************/
331 static void Close( vlc_object_t * p_this )
333 access_t *p_access = (access_t*)p_this;
334 access_sys_t *p_sys = p_access->p_sys;
339 for( i = 0; i < p_sys->i_file; i++ )
341 free( p_sys->file[i]->psz_name );
342 free( p_sys->file[i] );
349 /*****************************************************************************
350 * Read: standard read on a file descriptor.
351 *****************************************************************************/
352 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
354 access_sys_t *p_sys = p_access->p_sys;
357 #if !defined(WIN32) && !defined(UNDER_CE)
358 if( !p_sys->b_pace_control )
362 /* Find if some data is available. This won't work under Windows. */
367 if( p_access->b_die )
370 memset (&ufd, 0, sizeof (ufd));
374 i_ret = poll( &ufd, 1, 500 );
379 msg_Err( p_access, "poll error: %s",
388 i_ret = read( p_sys->fd, p_buffer, i_len );
392 /* b_kfir ; work around a buggy poll() driver implementation */
393 while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
396 msleep( INPUT_ERROR_SLEEP );
401 #endif /* WIN32 || UNDER_CE */
403 /* b_pace_control || WIN32 */
404 i_ret = read( p_sys->fd, p_buffer, i_len );
409 if( errno != EINTR && errno != EAGAIN )
410 msg_Err( p_access, "read failed (%s)", strerror(errno) );
412 /* Delay a bit to avoid consuming all the CPU. This is particularly
413 * useful when reading from an unconnected FIFO. */
414 msleep( INPUT_ERROR_SLEEP );
418 #ifdef HAVE_SYS_STAT_H
419 if( p_access->info.i_size != 0 &&
420 (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
422 struct stat stat_info;
423 int i_file = p_sys->i_index;
425 if ( fstat( p_sys->fd, &stat_info ) == -1 )
427 msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
429 else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
431 p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
432 p_sys->file[i_file]->i_size = stat_info.st_size;
433 p_access->info.i_update |= INPUT_UPDATE_SIZE;
438 /* If we reached an EOF then switch to the next file in the list */
439 if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
441 char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
442 p_sys->fd_backup = p_sys->fd;
444 msg_Dbg( p_access, "opening file `%s'", psz_name );
446 if ( _OpenFile( p_access, psz_name ) )
448 p_sys->fd = p_sys->fd_backup;
452 close( p_sys->fd_backup );
454 /* We have to read some data */
455 return Read( p_access, p_buffer, i_len );
459 p_access->info.i_pos += i_ret;
460 else if( i_ret == 0 )
461 p_access->info.b_eof = VLC_TRUE;
466 /*****************************************************************************
467 * Seek: seek to a specific location in a file
468 *****************************************************************************/
469 static int Seek( access_t *p_access, int64_t i_pos )
471 access_sys_t *p_sys = p_access->p_sys;
474 /* Check which file we need to access */
475 if( p_sys->i_file > 1 )
479 p_sys->fd_backup = p_sys->fd;
481 for( i = 0; i < p_sys->i_file - 1; i++ )
483 if( i_pos < p_sys->file[i]->i_size + i_size )
485 i_size += p_sys->file[i]->i_size;
487 psz_name = p_sys->file[i]->psz_name;
489 msg_Dbg( p_access, "opening file `%s'", psz_name );
491 if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
494 close( p_sys->fd_backup );
499 p_sys->fd = p_sys->fd_backup;
503 lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
505 p_access->info.i_pos = i_pos;
506 if( p_access->info.i_size < p_access->info.i_pos )
508 msg_Err( p_access, "seeking too far" );
509 p_access->info.i_pos = p_access->info.i_size;
511 else if( p_access->info.i_pos < 0 )
513 msg_Err( p_access, "seeking too early" );
514 p_access->info.i_pos = 0;
517 p_access->info.b_eof = VLC_FALSE;
523 /*****************************************************************************
525 *****************************************************************************/
526 static int Control( access_t *p_access, int i_query, va_list args )
528 access_sys_t *p_sys = p_access->p_sys;
536 case ACCESS_CAN_SEEK:
537 case ACCESS_CAN_FASTSEEK:
538 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
539 *pb_bool = p_sys->b_seekable;
542 case ACCESS_CAN_PAUSE:
543 case ACCESS_CAN_CONTROL_PACE:
544 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
545 *pb_bool = p_sys->b_pace_control;
550 pi_int = (int*)va_arg( args, int * );
554 case ACCESS_GET_PTS_DELAY:
555 pi_64 = (int64_t*)va_arg( args, int64_t * );
556 *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
560 case ACCESS_SET_PAUSE_STATE:
564 case ACCESS_GET_TITLE_INFO:
565 case ACCESS_SET_TITLE:
566 case ACCESS_SET_SEEKPOINT:
567 case ACCESS_SET_PRIVATE_ID_STATE:
568 case ACCESS_GET_META:
572 msg_Warn( p_access, "unimplemented query in control" );
580 /*****************************************************************************
581 * OpenFile: Opens a specific file
582 *****************************************************************************/
583 static int _OpenFile( access_t * p_access, const char * psz_name )
585 access_sys_t *p_sys = p_access->p_sys;
588 p_sys->fd = utf8_fopen( psz_name, "rb" );
591 msg_Err( p_access, "cannot open file %s", psz_name );
595 fseek( p_sys->fd, 0, SEEK_END );
596 p_access->info.i_size = ftell( p_sys->fd );
597 p_access->info.i_update |= INPUT_UPDATE_SIZE;
598 fseek( p_sys->fd, 0, SEEK_SET );
600 const char *psz_localname = ToLocale( psz_name );
601 if( psz_localname == NULL )
603 msg_Err( p_access, "incorrect file name %s", psz_name );
607 // FIXME: support non-ANSI filenames on Win32
608 p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
609 LocaleFree( psz_localname );
611 if ( p_sys->fd == -1 )
613 msg_Err( p_access, "cannot open file %s (%s)", psz_name,
618 #if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
619 /* We'd rather use any available memory for reading ahead
620 * than for caching what we've already seen/heard */
621 fcntl(p_sys->fd, F_RDAHEAD, 1);
622 fcntl(p_sys->fd, F_NOCACHE, 1);