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