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