]> git.sesse.net Git - vlc/blob - modules/access/file.c
Use UTF-8 internally (closes #16 and introduces lots of yet to be uncovered bugs)
[vlc] / modules / access / file.c
1 /*****************************************************************************
2  * file.c: file input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #ifdef HAVE_SYS_TYPES_H
34 #   include <sys/types.h>
35 #endif
36 #ifdef HAVE_SYS_TIME_H
37 #   include <sys/time.h>
38 #endif
39 #ifdef HAVE_SYS_STAT_H
40 #   include <sys/stat.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #   include <fcntl.h>
44 #endif
45
46 #ifdef HAVE_UNISTD_H
47 #   include <unistd.h>
48 #elif defined( WIN32 ) && !defined( UNDER_CE )
49 #   include <io.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     "Allows you to modify the default caching value for file streams. This " \
83     "value should be set in millisecond units." )
84 #define CAT_TEXT N_("Concatenate with additional files")
85 #define CAT_LONGTEXT N_( \
86     "Allows you to play split files as if they were part of a unique file. " \
87     "Specify a comma-separated list of files." )
88
89 vlc_module_begin();
90     set_description( _("Standard filesystem 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 = p_access->psz_path;
152     char *psz;
153
154 #ifdef HAVE_SYS_STAT_H
155     int                 i_stat;
156     struct stat         stat_info;
157 #endif
158     vlc_bool_t          b_stdin;
159
160     file_entry_t *      p_file;
161
162
163     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
164
165 #ifdef HAVE_SYS_STAT_H
166     if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
167     {
168         msg_Warn( p_access, "cannot stat() file `%s' (%s)",
169                   psz_name, strerror(errno));
170         return VLC_EGENERIC;
171     }
172 #endif
173
174     p_access->pf_read = Read;
175     p_access->pf_block = NULL;
176     p_access->pf_seek = Seek;
177     p_access->pf_control = Control;
178     p_access->info.i_update = 0;
179     p_access->info.i_size = 0;
180     p_access->info.i_pos = 0;
181     p_access->info.b_eof = VLC_FALSE;
182     p_access->info.i_title = 0;
183     p_access->info.i_seekpoint = 0;
184     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
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     if( !strcasecmp( p_access->psz_access, "stream" ) )
195     {
196         p_sys->b_seekable = VLC_FALSE;
197         p_sys->b_pace_control = VLC_FALSE;
198     }
199     else if( !strcasecmp( p_access->psz_access, "kfir" ) )
200     {
201         p_sys->b_seekable = VLC_FALSE;
202         p_sys->b_pace_control = VLC_FALSE;
203         p_sys->b_kfir = VLC_TRUE;
204     }
205     else
206     {
207         /* file:%s or %s */
208         p_sys->b_pace_control = VLC_TRUE;
209
210         if( b_stdin )
211         {
212             p_sys->b_seekable = VLC_FALSE;
213         }
214 #ifdef UNDER_CE
215         else if( VLC_TRUE )
216         {
217             /* We'll update i_size after it's been opened */
218             p_sys->b_seekable = VLC_TRUE;
219         }
220 #elif defined( HAVE_SYS_STAT_H )
221         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
222                  S_ISBLK(stat_info.st_mode) )
223         {
224             p_sys->b_seekable = VLC_TRUE;
225             p_access->info.i_size = stat_info.st_size;
226         }
227         else if( S_ISFIFO(stat_info.st_mode)
228 #   if !defined( SYS_BEOS ) && !defined( WIN32 )
229                   || S_ISSOCK(stat_info.st_mode)
230 #   endif
231                )
232         {
233             p_sys->b_seekable = VLC_FALSE;
234         }
235 #endif
236         else
237         {
238             msg_Err( p_access, "unknown file type for `%s'", psz_name );
239             return VLC_EGENERIC;
240         }
241     }
242
243     msg_Dbg( p_access, "opening file `%s'", psz_name );
244
245     if( b_stdin )
246     {
247         p_sys->fd = 0;
248     }
249     else if( _OpenFile( p_access, psz_name ) )
250     {
251         free( p_sys );
252         return VLC_EGENERIC;
253     }
254
255     if( p_sys->b_seekable && !p_access->info.i_size )
256     {
257         /* FIXME that's bad because all others access will be probed */
258         msg_Err( p_access, "file %s is empty, aborting", psz_name );
259         free( p_sys );
260         return VLC_EGENERIC;
261     }
262
263     /* Update default_pts to a suitable value for file access */
264     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
265
266     /*
267      * Get the additional list of files
268      */
269     p_file = malloc( sizeof(file_entry_t) );
270     p_file->i_size = p_access->info.i_size;
271     p_file->psz_name = strdup( psz_name );
272     TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
273
274     psz = var_CreateGetString( p_access, "file-cat" );
275     if( *psz )
276     {
277         char *psz_parser = psz_name = psz;
278         int64_t i_size;
279
280         while( psz_name && *psz_name )
281         {
282             psz_parser = strchr( psz_name, ',' );
283             if( psz_parser ) *psz_parser = 0;
284
285             psz_name = strdup( psz_name );
286             if( psz_name )
287             {
288                 msg_Dbg( p_access, "adding file `%s'", psz_name );
289                 i_size = 0;
290
291 #ifdef HAVE_SYS_STAT_H
292                 if( !stat( psz_name, &stat_info ) )
293                 {
294                     p_access->info.i_size += stat_info.st_size;
295                     i_size = stat_info.st_size;
296                 }
297                 else
298                 {
299                     msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
300                 }
301 #endif
302                 p_file = malloc( sizeof(file_entry_t) );
303                 p_file->i_size = i_size;
304                 p_file->psz_name = psz_name;
305
306                 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
307             }
308
309             psz_name = psz_parser;
310             if( psz_name ) psz_name++;
311         }
312     }
313     free( psz );
314
315     return VLC_SUCCESS;
316 }
317
318 /*****************************************************************************
319  * Close: close the target
320  *****************************************************************************/
321 static void Close( vlc_object_t * p_this )
322 {
323     access_t     *p_access = (access_t*)p_this;
324     access_sys_t *p_sys = p_access->p_sys;
325     int i;
326
327     close( p_sys->fd );
328
329     for( i = 0; i < p_sys->i_file; i++ )
330     {
331         free( p_sys->file[i]->psz_name );
332         free( p_sys->file[i] );
333     }
334     free( p_sys->file );
335
336     free( p_sys );
337 }
338
339 /*****************************************************************************
340  * Read: standard read on a file descriptor.
341  *****************************************************************************/
342 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
343 {
344     access_sys_t *p_sys = p_access->p_sys;
345     int i_ret;
346
347 #if !defined(WIN32) && !defined(UNDER_CE)
348     if( !p_sys->b_pace_control )
349     {
350         if( !p_sys->b_kfir )
351         {
352             /* Find if some data is available. This won't work under Windows. */
353             struct timeval  timeout;
354             fd_set          fds;
355
356             /* Initialize file descriptor set */
357             FD_ZERO( &fds );
358             FD_SET( p_sys->fd, &fds );
359
360             /* We'll wait 0.5 second if nothing happens */
361             timeout.tv_sec = 0;
362             timeout.tv_usec = 500000;
363
364             /* Find if some data is available */
365             while( (i_ret = select( p_sys->fd + 1, &fds, NULL, NULL, &timeout )) == 0
366                     || (i_ret < 0 && errno == EINTR) )
367             {
368                 FD_ZERO( &fds );
369                 FD_SET( p_sys->fd, &fds );
370                 timeout.tv_sec = 0;
371                 timeout.tv_usec = 500000;
372
373                 if( p_access->b_die )
374                     return 0;
375             }
376
377             if( i_ret < 0 )
378             {
379                 msg_Err( p_access, "select error (%s)", strerror(errno) );
380                 return -1;
381             }
382
383             i_ret = read( p_sys->fd, p_buffer, i_len );
384         }
385         else
386         {
387             /* b_kfir ; work around a buggy poll() driver implementation */
388             while ( (i_ret = read( p_sys->fd, p_buffer, i_len )) == 0 &&
389                     !p_access->b_die )
390             {
391                 msleep( INPUT_ERROR_SLEEP );
392             }
393         }
394     }
395     else
396 #endif /* WIN32 || UNDER_CE */
397     {
398         /* b_pace_control || WIN32 */
399         i_ret = read( p_sys->fd, p_buffer, i_len );
400     }
401
402     if( i_ret < 0 )
403     {
404         if( errno != EINTR && errno != EAGAIN )
405             msg_Err( p_access, "read failed (%s)", strerror(errno) );
406
407         /* Delay a bit to avoid consuming all the CPU. This is particularly
408          * useful when reading from an unconnected FIFO. */
409         msleep( INPUT_ERROR_SLEEP );
410     }
411
412     p_sys->i_nb_reads++;
413 #ifdef HAVE_SYS_STAT_H
414     if( p_access->info.i_size != 0 &&
415         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
416     {
417         struct stat stat_info;
418         int i_file = p_sys->i_index;
419
420         if ( fstat( p_sys->fd, &stat_info ) == -1 )
421         {
422             msg_Warn( p_access, "couldn't stat again the file (%s)", strerror(errno) );
423         }
424         else if ( p_sys->file[i_file]->i_size != stat_info.st_size )
425         {
426             p_access->info.i_size += (stat_info.st_size - p_sys->file[i_file]->i_size );
427             p_sys->file[i_file]->i_size = stat_info.st_size;
428             p_access->info.i_update |= INPUT_UPDATE_SIZE;
429         }
430     }
431 #endif
432
433     /* If we reached an EOF then switch to the next file in the list */
434     if ( i_ret == 0 && p_sys->i_index + 1 < p_sys->i_file )
435     {
436         char *psz_name = p_sys->file[++p_sys->i_index]->psz_name;
437         p_sys->fd_backup = p_sys->fd;
438
439         msg_Dbg( p_access, "opening file `%s'", psz_name );
440
441         if ( _OpenFile( p_access, psz_name ) )
442         {
443             p_sys->fd = p_sys->fd_backup;
444             return 0;
445         }
446
447         close( p_sys->fd_backup );
448
449         /* We have to read some data */
450         return Read( p_access, p_buffer, i_len );
451     }
452
453     if( i_ret > 0 )
454         p_access->info.i_pos += i_ret;
455     else if( i_ret == 0 )
456         p_access->info.b_eof = VLC_TRUE;
457
458     return i_ret;
459 }
460
461 /*****************************************************************************
462  * Seek: seek to a specific location in a file
463  *****************************************************************************/
464 static int Seek( access_t *p_access, int64_t i_pos )
465 {
466     access_sys_t *p_sys = p_access->p_sys;
467     int64_t i_size = 0;
468
469     /* Check which file we need to access */
470     if( p_sys->i_file > 1 )
471     {
472         int i;
473         char *psz_name;
474         p_sys->fd_backup = p_sys->fd;
475
476         for( i = 0; i < p_sys->i_file - 1; i++ )
477         {
478             if( i_pos < p_sys->file[i]->i_size + i_size )
479                 break;
480             i_size += p_sys->file[i]->i_size;
481         }
482         psz_name = p_sys->file[i]->psz_name;
483
484         msg_Dbg( p_access, "opening file `%s'", psz_name );
485
486         if ( i != p_sys->i_index && !_OpenFile( p_access, psz_name ) )
487         {
488             /* Close old file */
489             close( p_sys->fd_backup );
490             p_sys->i_index = i;
491         }
492         else
493         {
494             p_sys->fd = p_sys->fd_backup;
495         }
496     }
497
498     lseek( p_sys->fd, i_pos - i_size, SEEK_SET );
499
500     p_access->info.i_pos = i_pos;
501     if( p_access->info.i_size < p_access->info.i_pos )
502     {
503         msg_Err( p_access, "seeking too far" );
504         p_access->info.i_pos = p_access->info.i_size;
505     }
506     else if( p_access->info.i_pos < 0 )
507     {
508         msg_Err( p_access, "seeking too early" );
509         p_access->info.i_pos = 0;
510     }
511     /* Reset eof */
512     p_access->info.b_eof = VLC_FALSE;
513
514     /* FIXME */
515     return VLC_SUCCESS;
516 }
517
518 /*****************************************************************************
519  * Control:
520  *****************************************************************************/
521 static int Control( access_t *p_access, int i_query, va_list args )
522 {
523     access_sys_t *p_sys = p_access->p_sys;
524     vlc_bool_t   *pb_bool;
525     int          *pi_int;
526     int64_t      *pi_64;
527
528     switch( i_query )
529     {
530         /* */
531         case ACCESS_CAN_SEEK:
532         case ACCESS_CAN_FASTSEEK:
533             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
534             *pb_bool = p_sys->b_seekable;
535             break;
536
537         case ACCESS_CAN_PAUSE:
538         case ACCESS_CAN_CONTROL_PACE:
539             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
540             *pb_bool = p_sys->b_pace_control;
541             break;
542
543         /* */
544         case ACCESS_GET_MTU:
545             pi_int = (int*)va_arg( args, int * );
546             *pi_int = 0;
547             break;
548
549         case ACCESS_GET_PTS_DELAY:
550             pi_64 = (int64_t*)va_arg( args, int64_t * );
551             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
552             break;
553
554         /* */
555         case ACCESS_SET_PAUSE_STATE:
556             /* Nothing to do */
557             break;
558
559         case ACCESS_GET_TITLE_INFO:
560         case ACCESS_SET_TITLE:
561         case ACCESS_SET_SEEKPOINT:
562         case ACCESS_SET_PRIVATE_ID_STATE:
563         case ACCESS_GET_META:
564             return VLC_EGENERIC;
565
566         default:
567             msg_Warn( p_access, "unimplemented query in control" );
568             return VLC_EGENERIC;
569
570     }
571     return VLC_SUCCESS;
572 }
573
574
575 /*****************************************************************************
576  * OpenFile: Opens a specific file
577  *****************************************************************************/
578 static int _OpenFile( access_t * p_access, const char * psz_name )
579 {
580     access_sys_t *p_sys = p_access->p_sys;
581     const char *psz_localname;
582
583     psz_localname = ToLocale( psz_name );
584
585 #ifdef UNDER_CE
586     p_sys->fd = fopen( psz_localname, "rb" );
587     LocaleFree( psz_localname );
588     if ( !p_sys->fd )
589     {
590         msg_Err( p_access, "cannot open file %s", psz_name );
591         return VLC_EGENERIC;
592     }
593
594     fseek( p_sys->fd, 0, SEEK_END );
595     p_access->info.i_size = ftell( p_sys->fd );
596     p_access->info.i_update |= INPUT_UPDATE_SIZE;
597     fseek( p_sys->fd, 0, SEEK_SET );
598 #else
599
600     p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
601     LocaleFree( psz_localname );
602     if ( p_sys->fd == -1 )
603     {
604         msg_Err( p_access, "cannot open file %s (%s)", psz_name,
605                  strerror(errno) );
606         return VLC_EGENERIC;
607     }
608 #endif
609
610     return VLC_SUCCESS;
611 }