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