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