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