]> git.sesse.net Git - vlc/blob - modules/access/file.c
For consistency, remove references to vlc from libvlc
[vlc] / modules / access / file.c
1 /*****************************************************************************
2  * file.c: file input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          RĂ©mi Denis-Courmont <rem # videolan # org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/input.h>
30 #include <vlc_interaction.h>
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #ifdef HAVE_SYS_TYPES_H
36 #   include <sys/types.h>
37 #endif
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #   include <fcntl.h>
43 #endif
44
45 #if defined( WIN32 ) && !defined( UNDER_CE )
46 #   include <io.h>
47 #else
48 #   include <unistd.h>
49 #   include <poll.h>
50 #endif
51
52 #if defined( WIN32 ) && !defined( UNDER_CE )
53 /* stat() support for large files on win32 */
54 #   define stat _stati64
55 #   define fstat(a,b) _fstati64(a,b)
56 #   ifdef lseek
57 #      undef lseek
58 #   endif
59 #   define lseek _lseeki64
60 #elif defined( UNDER_CE )
61 #   ifdef read
62 #      undef read
63 #   endif
64 #   define read(a,b,c) fread(b,1,c,a)
65 #   define close(a) fclose(a)
66 #   ifdef lseek
67 #      undef lseek
68 #   endif
69 #   define lseek fseek
70 #endif
71
72 #include "charset.h"
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 static int  Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
79
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." )
84 #define CAT_TEXT N_("Concatenate with additional files")
85 #define CAT_LONGTEXT N_( \
86     "Play split files as if they were part of a unique file. " \
87     "You need to specify a comma-separated list of files." )
88
89 vlc_module_begin();
90     set_description( _("File input") );
91     set_shortname( _("File") );
92     set_category( CAT_INPUT );
93     set_subcategory( SUBCAT_INPUT_ACCESS );
94     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
95     add_string( "file-cat", NULL, NULL, CAT_TEXT, CAT_LONGTEXT, VLC_TRUE );
96     set_capability( "access2", 50 );
97     add_shortcut( "file" );
98     add_shortcut( "stream" );
99     add_shortcut( "kfir" );
100     set_callbacks( Open, Close );
101 vlc_module_end();
102
103
104 /*****************************************************************************
105  * Exported prototypes
106  *****************************************************************************/
107 static int  Seek( access_t *, int64_t );
108 static int  Read( access_t *, uint8_t *, int );
109 static int  Control( access_t *, int, va_list );
110
111 static int  _OpenFile( access_t *, const char * );
112
113 typedef struct
114 {
115     char     *psz_name;
116     int64_t  i_size;
117
118 } file_entry_t;
119
120 struct access_sys_t
121 {
122     unsigned int i_nb_reads;
123     vlc_bool_t   b_kfir;
124
125     /* Files list */
126     int          i_file;
127     file_entry_t **file;
128
129     /* Current file */
130     int  i_index;
131 #ifndef UNDER_CE
132     int  fd;
133     int  fd_backup;
134 #else
135     FILE *fd;
136     FILE *fd_backup;
137 #endif
138
139     /* */
140     vlc_bool_t b_seekable;
141     vlc_bool_t b_pace_control;
142 };
143
144 /*****************************************************************************
145  * Open: open the file
146  *****************************************************************************/
147 static int Open( vlc_object_t *p_this )
148 {
149     access_t     *p_access = (access_t*)p_this;
150     access_sys_t *p_sys;
151     char *psz_name = strdup( p_access->psz_path );
152     char *psz;
153
154 #ifdef HAVE_SYS_STAT_H
155     struct stat         stat_info;
156 #endif
157     file_entry_t *      p_file;
158     vlc_bool_t          b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
159
160     if( !b_stdin )
161     {
162         if( psz_name[0] == '~' && psz_name[1] == '/' )
163         {
164             /* This is incomplete : we should also support the ~cmassiot/
165              * syntax. */
166             asprintf( &psz, "%s/%s", p_access->p_libvlc->psz_homedir, psz_name + 2 );
167             free( psz_name );
168             psz_name = psz;
169         }
170 #if defined(WIN32)
171         else if( !strcasecmp( p_access->psz_access, "file" )
172                 && ('/' == psz_name[0]) && psz_name[1]
173                 && (':' == psz_name[2]) && ('/' == psz_name[3]) )
174         {
175             /*
176             ** explorer can open path such as file:/C:/ or file:///C:/...
177             ** hence remove leading / if found
178             */
179             strcpy( psz_name, p_access->psz_path + 1 );
180         }
181 #endif
182
183 #ifdef HAVE_SYS_STAT_H
184         if( utf8_stat( psz_name, &stat_info ) )
185         {
186             msg_Warn( p_access, "%s: %s", psz_name, strerror( errno ) );
187             free( psz_name );
188             return VLC_EGENERIC;
189         }
190 #endif
191     }
192
193     STANDARD_READ_ACCESS_INIT;
194     p_sys->i_nb_reads = 0;
195     p_sys->b_kfir = VLC_FALSE;
196     p_sys->file = NULL;
197     p_sys->i_file = 0;
198     p_sys->i_index = 0;
199 #ifndef UNDER_CE
200     p_sys->fd = -1;
201 #endif
202
203     if( !strcasecmp( p_access->psz_access, "stream" ) )
204     {
205         p_sys->b_seekable = VLC_FALSE;
206         p_sys->b_pace_control = VLC_FALSE;
207     }
208     else if( !strcasecmp( p_access->psz_access, "kfir" ) )
209     {
210         p_sys->b_seekable = VLC_FALSE;
211         p_sys->b_pace_control = VLC_FALSE;
212         p_sys->b_kfir = VLC_TRUE;
213     }
214     else
215     {
216         /* file:%s or %s */
217         p_sys->b_pace_control = VLC_TRUE;
218
219         if( b_stdin )
220         {
221             p_sys->b_seekable = VLC_FALSE;
222         }
223 #ifdef UNDER_CE
224         else if( VLC_TRUE )
225         {
226             /* We'll update i_size after it's been opened */
227             p_sys->b_seekable = VLC_TRUE;
228         }
229 #elif defined( HAVE_SYS_STAT_H )
230         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
231                  S_ISBLK(stat_info.st_mode) )
232         {
233             p_sys->b_seekable = VLC_TRUE;
234             p_access->info.i_size = stat_info.st_size;
235         }
236         else if( S_ISFIFO(stat_info.st_mode)
237 #   if !defined( SYS_BEOS ) && !defined( WIN32 )
238                   || S_ISSOCK(stat_info.st_mode)
239 #   endif
240                )
241         {
242             p_sys->b_seekable = VLC_FALSE;
243         }
244 #endif
245         else
246         {
247             msg_Err( p_access, "unknown file type for `%s'", psz_name );
248             intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
249                             _("\"%s\"'s file type is unknown."),
250                             psz_name );
251             free( psz_name );
252             return VLC_EGENERIC;
253         }
254     }
255
256     msg_Dbg( p_access, "opening file `%s'", psz_name );
257
258     if( b_stdin )
259     {
260         p_sys->fd = 0;
261     }
262     else if( _OpenFile( p_access, psz_name ) )
263     {
264         free( p_sys );
265         free( psz_name );
266         return VLC_EGENERIC;
267     }
268
269     if( p_sys->b_seekable && !p_access->info.i_size )
270     {
271         /* FIXME that's bad because all others access will be probed */
272         msg_Err( p_access, "file %s is empty, aborting", psz_name );
273         free( p_sys );
274         free( psz_name );
275         return VLC_EGENERIC;
276     }
277
278     /* Update default_pts to a suitable value for file access */
279     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
280
281     /*
282      * Get the additional list of files
283      */
284     p_file = malloc( sizeof(file_entry_t) );
285     p_file->i_size = p_access->info.i_size;
286     p_file->psz_name = psz_name;
287     TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
288
289     psz = var_CreateGetString( p_access, "file-cat" );
290     if( *psz )
291     {
292         char *psz_parser = psz_name = psz;
293         int64_t i_size;
294
295         while( psz_name && *psz_name )
296         {
297             psz_parser = strchr( psz_name, ',' );
298             if( psz_parser ) *psz_parser = 0;
299
300             psz_name = strdup( psz_name );
301             if( psz_name )
302             {
303                 msg_Dbg( p_access, "adding file `%s'", psz_name );
304                 i_size = 0;
305
306 #ifdef HAVE_SYS_STAT_H
307                 if( !stat( psz_name, &stat_info ) )
308                 {
309                     p_access->info.i_size += stat_info.st_size;
310                     i_size = stat_info.st_size;
311                 }
312                 else
313                 {
314                     msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
315                 }
316 #endif
317                 p_file = malloc( sizeof(file_entry_t) );
318                 p_file->i_size = i_size;
319                 p_file->psz_name = psz_name;
320
321                 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
322             }
323
324             psz_name = psz_parser;
325             if( psz_name ) psz_name++;
326         }
327     }
328     free( psz );
329
330     return VLC_SUCCESS;
331 }
332
333 /*****************************************************************************
334  * Close: close the target
335  *****************************************************************************/
336 static void Close( vlc_object_t * p_this )
337 {
338     access_t     *p_access = (access_t*)p_this;
339     access_sys_t *p_sys = p_access->p_sys;
340     int i;
341
342     close( p_sys->fd );
343
344     for( i = 0; i < p_sys->i_file; i++ )
345     {
346         free( p_sys->file[i]->psz_name );
347         free( p_sys->file[i] );
348     }
349     free( p_sys->file );
350
351     free( p_sys );
352 }
353
354 /*****************************************************************************
355  * Read: standard read on a file descriptor.
356  *****************************************************************************/
357 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
358 {
359     access_sys_t *p_sys = p_access->p_sys;
360     int i_ret;
361
362 #if !defined(WIN32) && !defined(UNDER_CE)
363     if( !p_sys->b_pace_control )
364     {
365         if( !p_sys->b_kfir )
366         {
367             /* Find if some data is available. This won't work under Windows. */
368             do
369             {
370                 struct pollfd ufd;
371
372                 if( p_access->b_die )
373                     return 0;
374
375                 memset (&ufd, 0, sizeof (ufd));
376                 ufd.fd = p_sys->fd;
377                 ufd.events = POLLIN;
378
379                 i_ret = poll( &ufd, 1, 500 );
380                 if( i_ret == -1 )
381                 {
382                     if( errno != EINTR )
383                     {
384                         msg_Err( p_access, "poll error: %s",
385                                  strerror( errno ) );
386                         return -1;
387                     }
388                     i_ret = 0;
389                 }
390             }
391             while( i_ret == 0 );
392
393             i_ret = read( p_sys->fd, p_buffer, i_len );
394         }
395         else
396         {
397             /* b_kfir ; work around a buggy poll() driver implementation */
398             while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
399                     !p_access->b_die )
400             {
401                 msleep( INPUT_ERROR_SLEEP );
402             }
403         }
404     }
405     else
406 #endif /* WIN32 || UNDER_CE */
407     {
408         /* b_pace_control || WIN32 */
409         i_ret = read( p_sys->fd, p_buffer, i_len );
410     }
411
412     if( i_ret < 0 )
413     {
414         if( errno != EINTR && errno != EAGAIN )
415         {
416             msg_Err( p_access, "read failed (%s)", strerror(errno) );
417             intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
418                             _("VLC could not read file \"%s\"."),
419                             strerror(errno) );
420         }
421
422         /* Delay a bit to avoid consuming all the CPU. This is particularly
423          * useful when reading from an unconnected FIFO. */
424         msleep( INPUT_ERROR_SLEEP );
425     }
426
427     p_sys->i_nb_reads++;
428 #ifdef HAVE_SYS_STAT_H
429     if( p_access->info.i_size != 0 &&
430         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
431     {
432         struct stat stat_info;
433         int i_file = p_sys->i_index;
434
435         if ( fstat( p_sys->fd, &stat_info ) == -1 )
436         {
437             msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
438         }
439         else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
440         {
441             p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
442             p_sys->file[i_file]->i_size = stat_info.st_size;
443             p_access->info.i_update |= INPUT_UPDATE_SIZE;
444         }
445     }
446 #endif
447
448     /* If we reached an EOF then switch to the next file in the list */
449     if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
450     {
451         char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
452         p_sys->fd_backup = p_sys->fd;
453
454         msg_Dbg( p_access, "opening file `%s'", psz_name );
455
456         if ( _OpenFile( p_access, psz_name ) )
457         {
458             p_sys->fd = p_sys->fd_backup;
459             return 0;
460         }
461
462         close( p_sys->fd_backup );
463
464         /* We have to read some data */
465         return Read( p_access, p_buffer, i_len );
466     }
467
468     if( i_ret > 0 )
469         p_access->info.i_pos += i_ret;
470     else if( i_ret == 0 )
471         p_access->info.b_eof = VLC_TRUE;
472
473     return i_ret;
474 }
475
476 /*****************************************************************************
477  * Seek: seek to a specific location in a file
478  *****************************************************************************/
479 static int Seek( access_t *p_access, int64_t i_pos )
480 {
481     access_sys_t *p_sys = p_access->p_sys;
482     int64_t i_size = 0;
483
484     /* Check which file we need to access */
485     if( p_sys->i_file > 1 )
486     {
487         int i;
488         char *psz_name;
489         p_sys->fd_backup = p_sys->fd;
490
491         for( i = 0; i < p_sys->i_file - 1; i++ )
492         {
493             if( i_pos < p_sys->file[i]->i_size + i_size )
494                 break;
495             i_size += p_sys->file[i]->i_size;
496         }
497         psz_name = p_sys->file[i]->psz_name;
498
499         msg_Dbg( p_access, "opening file `%s'", psz_name );
500
501         if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
502         {
503             /* Close old file */
504             close( p_sys->fd_backup );
505             p_sys->i_index = i;
506         }
507         else
508         {
509             p_sys->fd = p_sys->fd_backup;
510         }
511     }
512
513     lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
514
515     p_access->info.i_pos = i_pos;
516     if( p_access->info.i_size < p_access->info.i_pos )
517     {
518         msg_Err( p_access, "seeking too far" );
519         p_access->info.i_pos = p_access->info.i_size;
520     }
521     else if( p_access->info.i_pos < 0 )
522     {
523         msg_Err( p_access, "seeking too early" );
524         p_access->info.i_pos = 0;
525     }
526     /* Reset eof */
527     p_access->info.b_eof = VLC_FALSE;
528
529     /* FIXME */
530     return VLC_SUCCESS;
531 }
532
533 /*****************************************************************************
534  * Control:
535  *****************************************************************************/
536 static int Control( access_t *p_access, int i_query, va_list args )
537 {
538     access_sys_t *p_sys = p_access->p_sys;
539     vlc_bool_t   *pb_bool;
540     int          *pi_int;
541     int64_t      *pi_64;
542
543     switch( i_query )
544     {
545         /* */
546         case ACCESS_CAN_SEEK:
547         case ACCESS_CAN_FASTSEEK:
548             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
549             *pb_bool = p_sys->b_seekable;
550             break;
551
552         case ACCESS_CAN_PAUSE:
553         case ACCESS_CAN_CONTROL_PACE:
554             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
555             *pb_bool = p_sys->b_pace_control;
556             break;
557
558         /* */
559         case ACCESS_GET_MTU:
560             pi_int = (int*)va_arg( args, int * );
561             *pi_int = 0;
562             break;
563
564         case ACCESS_GET_PTS_DELAY:
565             pi_64 = (int64_t*)va_arg( args, int64_t * );
566             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
567             break;
568
569         /* */
570         case ACCESS_SET_PAUSE_STATE:
571             /* Nothing to do */
572             break;
573
574         case ACCESS_GET_TITLE_INFO:
575         case ACCESS_SET_TITLE:
576         case ACCESS_SET_SEEKPOINT:
577         case ACCESS_SET_PRIVATE_ID_STATE:
578         case ACCESS_GET_META:
579             return VLC_EGENERIC;
580
581         default:
582             msg_Warn( p_access, "unimplemented query in control" );
583             return VLC_EGENERIC;
584
585     }
586     return VLC_SUCCESS;
587 }
588
589
590 /*****************************************************************************
591  * OpenFile: Opens a specific file
592  *****************************************************************************/
593 static int _OpenFile( access_t * p_access, const char * psz_name )
594 {
595     access_sys_t *p_sys = p_access->p_sys;
596
597 #ifdef UNDER_CE
598     p_sys->fd = utf8_fopen( psz_name, "rb" );
599     if ( !p_sys->fd )
600     {
601         msg_Err( p_access, "cannot open file %s", psz_name );
602         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
603                         _("VLC could not open file \"%s\"."), psz_name );
604         return VLC_EGENERIC;
605     }
606
607     fseek( p_sys->fd, 0, SEEK_END );
608     p_access->info.i_size = ftell( p_sys->fd );
609     p_access->info.i_update |= INPUT_UPDATE_SIZE;
610     fseek( p_sys->fd, 0, SEEK_SET );
611 #else
612     const char *psz_localname = ToLocale( psz_name );
613     if( psz_localname == NULL )
614     {
615         msg_Err( p_access, "incorrect file name %s", psz_name );
616         return VLC_EGENERIC;
617     }
618
619     // FIXME: support non-ANSI filenames on Win32
620     p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
621     LocaleFree( psz_localname );
622
623     if ( p_sys->fd == -1 )
624     {
625         msg_Err( p_access, "cannot open file %s (%s)", psz_name,
626                  strerror(errno) );
627         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
628                         _("VLC could not open file \"%s\" (%s)."),
629                         psz_name, strerror(errno) );
630         return VLC_EGENERIC;
631     }
632
633 #if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
634     /* We'd rather use any available memory for reading ahead
635      * than for caching what we've already seen/heard */
636     fcntl(p_sys->fd, F_RDAHEAD, 1);
637     fcntl(p_sys->fd, F_NOCACHE, 1);
638 #endif
639
640 #endif
641
642     return VLC_SUCCESS;
643 }