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