]> git.sesse.net Git - vlc/blob - modules/access/file.c
Character devices are not seekable.
[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_ISBLK(stat_info.st_mode) )
231         {
232             p_sys->b_seekable = VLC_TRUE;
233             p_access->info.i_size = stat_info.st_size;
234         }
235         else if( S_ISCHR(stat_info.st_mode) || S_ISFIFO(stat_info.st_mode)
236 #   ifdef S_ISSOCK
237                   || S_ISSOCK(stat_info.st_mode)
238 #   endif
239                )
240         {
241             p_sys->b_seekable = VLC_FALSE;
242         }
243 #endif
244         else
245         {
246             msg_Err( p_access, "unknown file type for `%s'", psz_name );
247             intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
248                             _("\"%s\"'s file type is unknown."),
249                             psz_name );
250             free( psz_name );
251             return VLC_EGENERIC;
252         }
253     }
254
255     msg_Dbg( p_access, "opening file `%s'", psz_name );
256
257     if( b_stdin )
258     {
259         p_sys->fd = 0;
260     }
261     else if( _OpenFile( p_access, psz_name ) )
262     {
263         free( p_sys );
264         free( psz_name );
265         return VLC_EGENERIC;
266     }
267
268     if( p_sys->b_seekable && !p_access->info.i_size )
269     {
270         /* FIXME that's bad because all others access will be probed */
271         msg_Err( p_access, "file %s is empty, aborting", psz_name );
272         free( p_sys );
273         free( psz_name );
274         return VLC_EGENERIC;
275     }
276
277     /* Update default_pts to a suitable value for file access */
278     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
279
280     /*
281      * Get the additional list of files
282      */
283     p_file = malloc( sizeof(file_entry_t) );
284     p_file->i_size = p_access->info.i_size;
285     p_file->psz_name = psz_name;
286     TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
287
288     psz = var_CreateGetString( p_access, "file-cat" );
289     if( *psz )
290     {
291         char *psz_parser = psz_name = psz;
292         int64_t i_size;
293
294         while( psz_name && *psz_name )
295         {
296             psz_parser = strchr( psz_name, ',' );
297             if( psz_parser ) *psz_parser = 0;
298
299             psz_name = strdup( psz_name );
300             if( psz_name )
301             {
302                 msg_Dbg( p_access, "adding file `%s'", psz_name );
303                 i_size = 0;
304
305 #ifdef HAVE_SYS_STAT_H
306                 if( !stat( psz_name, &stat_info ) )
307                 {
308                     p_access->info.i_size += stat_info.st_size;
309                     i_size = stat_info.st_size;
310                 }
311                 else
312                 {
313                     msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
314                 }
315 #endif
316                 p_file = malloc( sizeof(file_entry_t) );
317                 p_file->i_size = i_size;
318                 p_file->psz_name = psz_name;
319
320                 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
321             }
322
323             psz_name = psz_parser;
324             if( psz_name ) psz_name++;
325         }
326     }
327     free( psz );
328
329     return VLC_SUCCESS;
330 }
331
332 /*****************************************************************************
333  * Close: close the target
334  *****************************************************************************/
335 static void Close( vlc_object_t * p_this )
336 {
337     access_t     *p_access = (access_t*)p_this;
338     access_sys_t *p_sys = p_access->p_sys;
339     int i;
340
341     close( p_sys->fd );
342
343     for( i = 0; i < p_sys->i_file; i++ )
344     {
345         free( p_sys->file[i]->psz_name );
346         free( p_sys->file[i] );
347     }
348     free( p_sys->file );
349
350     free( p_sys );
351 }
352
353 /*****************************************************************************
354  * Read: standard read on a file descriptor.
355  *****************************************************************************/
356 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
357 {
358     access_sys_t *p_sys = p_access->p_sys;
359     int i_ret;
360
361 #if !defined(WIN32) && !defined(UNDER_CE)
362     if( !p_sys->b_pace_control )
363     {
364         if( !p_sys->b_kfir )
365         {
366             /* Find if some data is available. This won't work under Windows. */
367             do
368             {
369                 struct pollfd ufd;
370
371                 if( p_access->b_die )
372                     return 0;
373
374                 memset (&ufd, 0, sizeof (ufd));
375                 ufd.fd = p_sys->fd;
376                 ufd.events = POLLIN;
377
378                 i_ret = poll( &ufd, 1, 500 );
379                 if( i_ret == -1 )
380                 {
381                     if( errno != EINTR )
382                     {
383                         msg_Err( p_access, "poll error: %s",
384                                  strerror( errno ) );
385                         return -1;
386                     }
387                     i_ret = 0;
388                 }
389             }
390             while( i_ret == 0 );
391
392             i_ret = read( p_sys->fd, p_buffer, i_len );
393         }
394         else
395         {
396             /* b_kfir ; work around a buggy poll() driver implementation */
397             while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
398                     !p_access->b_die )
399             {
400                 msleep( INPUT_ERROR_SLEEP );
401             }
402         }
403     }
404     else
405 #endif /* WIN32 || UNDER_CE */
406     {
407         /* b_pace_control || WIN32 */
408         i_ret = read( p_sys->fd, p_buffer, i_len );
409     }
410
411     if( i_ret < 0 )
412     {
413         if( errno != EINTR && errno != EAGAIN )
414         {
415             msg_Err( p_access, "read failed (%s)", strerror(errno) );
416             intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
417                             _("VLC could not read file \"%s\"."),
418                             strerror(errno) );
419         }
420
421         /* Delay a bit to avoid consuming all the CPU. This is particularly
422          * useful when reading from an unconnected FIFO. */
423         msleep( INPUT_ERROR_SLEEP );
424     }
425
426     p_sys->i_nb_reads++;
427 #ifdef HAVE_SYS_STAT_H
428     if( p_access->info.i_size != 0 &&
429         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
430     {
431         struct stat stat_info;
432         int i_file = p_sys->i_index;
433
434         if ( fstat( p_sys->fd, &stat_info ) == -1 )
435         {
436             msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
437         }
438         else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
439         {
440             p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
441             p_sys->file[i_file]->i_size = stat_info.st_size;
442             p_access->info.i_update |= INPUT_UPDATE_SIZE;
443         }
444     }
445 #endif
446
447     /* If we reached an EOF then switch to the next file in the list */
448     if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
449     {
450         char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
451         p_sys->fd_backup = p_sys->fd;
452
453         msg_Dbg( p_access, "opening file `%s'", psz_name );
454
455         if ( _OpenFile( p_access, psz_name ) )
456         {
457             p_sys->fd = p_sys->fd_backup;
458             return 0;
459         }
460
461         close( p_sys->fd_backup );
462
463         /* We have to read some data */
464         return Read( p_access, p_buffer, i_len );
465     }
466
467     if( i_ret > 0 )
468         p_access->info.i_pos += i_ret;
469     else if( i_ret == 0 )
470         p_access->info.b_eof = VLC_TRUE;
471
472     return i_ret;
473 }
474
475 /*****************************************************************************
476  * Seek: seek to a specific location in a file
477  *****************************************************************************/
478 static int Seek( access_t *p_access, int64_t i_pos )
479 {
480     access_sys_t *p_sys = p_access->p_sys;
481     int64_t i_size = 0;
482
483     /* Check which file we need to access */
484     if( p_sys->i_file > 1 )
485     {
486         int i;
487         char *psz_name;
488         p_sys->fd_backup = p_sys->fd;
489
490         for( i = 0; i < p_sys->i_file - 1; i++ )
491         {
492             if( i_pos < p_sys->file[i]->i_size + i_size )
493                 break;
494             i_size += p_sys->file[i]->i_size;
495         }
496         psz_name = p_sys->file[i]->psz_name;
497
498         msg_Dbg( p_access, "opening file `%s'", psz_name );
499
500         if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
501         {
502             /* Close old file */
503             close( p_sys->fd_backup );
504             p_sys->i_index = i;
505         }
506         else
507         {
508             p_sys->fd = p_sys->fd_backup;
509         }
510     }
511
512     lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
513
514     p_access->info.i_pos = i_pos;
515     if( p_access->info.i_size < p_access->info.i_pos )
516     {
517         msg_Err( p_access, "seeking too far" );
518         p_access->info.i_pos = p_access->info.i_size;
519     }
520     else if( p_access->info.i_pos < 0 )
521     {
522         msg_Err( p_access, "seeking too early" );
523         p_access->info.i_pos = 0;
524     }
525     /* Reset eof */
526     p_access->info.b_eof = VLC_FALSE;
527
528     /* FIXME */
529     return VLC_SUCCESS;
530 }
531
532 /*****************************************************************************
533  * Control:
534  *****************************************************************************/
535 static int Control( access_t *p_access, int i_query, va_list args )
536 {
537     access_sys_t *p_sys = p_access->p_sys;
538     vlc_bool_t   *pb_bool;
539     int          *pi_int;
540     int64_t      *pi_64;
541
542     switch( i_query )
543     {
544         /* */
545         case ACCESS_CAN_SEEK:
546         case ACCESS_CAN_FASTSEEK:
547             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
548             *pb_bool = p_sys->b_seekable;
549             break;
550
551         case ACCESS_CAN_PAUSE:
552         case ACCESS_CAN_CONTROL_PACE:
553             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
554             *pb_bool = p_sys->b_pace_control;
555             break;
556
557         /* */
558         case ACCESS_GET_MTU:
559             pi_int = (int*)va_arg( args, int * );
560             *pi_int = 0;
561             break;
562
563         case ACCESS_GET_PTS_DELAY:
564             pi_64 = (int64_t*)va_arg( args, int64_t * );
565             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
566             break;
567
568         /* */
569         case ACCESS_SET_PAUSE_STATE:
570             /* Nothing to do */
571             break;
572
573         case ACCESS_GET_TITLE_INFO:
574         case ACCESS_SET_TITLE:
575         case ACCESS_SET_SEEKPOINT:
576         case ACCESS_SET_PRIVATE_ID_STATE:
577         case ACCESS_GET_META:
578             return VLC_EGENERIC;
579
580         default:
581             msg_Warn( p_access, "unimplemented query in control" );
582             return VLC_EGENERIC;
583
584     }
585     return VLC_SUCCESS;
586 }
587
588
589 /*****************************************************************************
590  * OpenFile: Opens a specific file
591  *****************************************************************************/
592 static int _OpenFile( access_t * p_access, const char * psz_name )
593 {
594     access_sys_t *p_sys = p_access->p_sys;
595
596 #ifdef UNDER_CE
597     p_sys->fd = utf8_fopen( psz_name, "rb" );
598     if ( !p_sys->fd )
599     {
600         msg_Err( p_access, "cannot open file %s", psz_name );
601         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
602                         _("VLC could not open file \"%s\"."), psz_name );
603         return VLC_EGENERIC;
604     }
605
606     fseek( p_sys->fd, 0, SEEK_END );
607     p_access->info.i_size = ftell( p_sys->fd );
608     p_access->info.i_update |= INPUT_UPDATE_SIZE;
609     fseek( p_sys->fd, 0, SEEK_SET );
610 #else
611     const char *psz_localname = ToLocale( psz_name );
612     if( psz_localname == NULL )
613     {
614         msg_Err( p_access, "incorrect file name %s", psz_name );
615         return VLC_EGENERIC;
616     }
617
618     // FIXME: support non-ANSI filenames on Win32
619     p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
620     LocaleFree( psz_localname );
621
622     if ( p_sys->fd == -1 )
623     {
624         msg_Err( p_access, "cannot open file %s (%s)", psz_name,
625                  strerror(errno) );
626         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
627                         _("VLC could not open file \"%s\" (%s)."),
628                         psz_name, strerror(errno) );
629         return VLC_EGENERIC;
630     }
631
632 #if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
633     /* We'd rather use any available memory for reading ahead
634      * than for caching what we've already seen/heard */
635     fcntl(p_sys->fd, F_RDAHEAD, 1);
636     fcntl(p_sys->fd, F_NOCACHE, 1);
637 #endif
638
639 #endif
640
641     return VLC_SUCCESS;
642 }