]> git.sesse.net Git - vlc/blob - modules/access/file.c
Win32 compile fix
[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     vlc_bool_t          b_stdin;
157
158     file_entry_t *      p_file;
159
160     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
161
162     if( !b_stdin )
163     {
164         if( psz_name[0] == '~' && psz_name[1] == '/' )
165         {
166             /* This is incomplete : we should also support the ~cmassiot/
167              * syntax. */
168             asprintf( &psz, "%s/%s", p_access->p_vlc->psz_homedir, psz_name + 2 );
169             free( psz_name );
170             psz_name = psz;
171         }
172 #if defined(WIN32)
173         else if( !strcasecmp( p_access->psz_access, "file" )
174                 && ('/' == psz_name[0]) && psz_name[1]
175                 && (':' == psz_name[2]) && ('/' == psz_name[3]) )
176         {
177             /*
178             ** explorer can open path such as file:/C:/ or file:///C:/...
179             ** hence remove leading / if found
180             */
181             ++psz_name;
182         }
183 #endif
184
185 #ifdef HAVE_SYS_STAT_H
186         if( utf8_stat( psz_name, &stat_info ) )
187         {
188             msg_Warn( p_access, "%s: %s", psz_name, strerror( errno ) );
189             free( psz_name );
190             return VLC_EGENERIC;
191         }
192 #endif
193     }
194
195     p_access->pf_read = Read;
196     p_access->pf_block = NULL;
197     p_access->pf_seek = Seek;
198     p_access->pf_control = Control;
199     p_access->info.i_update = 0;
200     p_access->info.i_size = 0;
201     p_access->info.i_pos = 0;
202     p_access->info.b_eof = VLC_FALSE;
203     p_access->info.i_title = 0;
204     p_access->info.i_seekpoint = 0;
205     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
206     p_sys->i_nb_reads = 0;
207     p_sys->b_kfir = VLC_FALSE;
208     p_sys->file = NULL;
209     p_sys->i_file = 0;
210     p_sys->i_index = 0;
211 #ifndef UNDER_CE
212     p_sys->fd = -1;
213 #endif
214
215     if( !strcasecmp( p_access->psz_access, "stream" ) )
216     {
217         p_sys->b_seekable = VLC_FALSE;
218         p_sys->b_pace_control = VLC_FALSE;
219     }
220     else if( !strcasecmp( p_access->psz_access, "kfir" ) )
221     {
222         p_sys->b_seekable = VLC_FALSE;
223         p_sys->b_pace_control = VLC_FALSE;
224         p_sys->b_kfir = VLC_TRUE;
225     }
226     else
227     {
228         /* file:%s or %s */
229         p_sys->b_pace_control = VLC_TRUE;
230
231         if( b_stdin )
232         {
233             p_sys->b_seekable = VLC_FALSE;
234         }
235 #ifdef UNDER_CE
236         else if( VLC_TRUE )
237         {
238             /* We'll update i_size after it's been opened */
239             p_sys->b_seekable = VLC_TRUE;
240         }
241 #elif defined( HAVE_SYS_STAT_H )
242         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode) ||
243                  S_ISBLK(stat_info.st_mode) )
244         {
245             p_sys->b_seekable = VLC_TRUE;
246             p_access->info.i_size = stat_info.st_size;
247         }
248         else if( S_ISFIFO(stat_info.st_mode)
249 #   if !defined( SYS_BEOS ) && !defined( WIN32 )
250                   || S_ISSOCK(stat_info.st_mode)
251 #   endif
252                )
253         {
254             p_sys->b_seekable = VLC_FALSE;
255         }
256 #endif
257         else
258         {
259             msg_Err( p_access, "unknown file type for `%s'", psz_name );
260             return VLC_EGENERIC;
261         }
262     }
263
264     msg_Dbg( p_access, "opening file `%s'", psz_name );
265
266     if( b_stdin )
267     {
268         p_sys->fd = 0;
269     }
270     else if( _OpenFile( p_access, psz_name ) )
271     {
272         free( p_sys );
273         free( psz_name );
274         return VLC_EGENERIC;
275     }
276
277     if( p_sys->b_seekable && !p_access->info.i_size )
278     {
279         /* FIXME that's bad because all others access will be probed */
280         msg_Err( p_access, "file %s is empty, aborting", psz_name );
281         free( p_sys );
282         free( psz_name );
283         return VLC_EGENERIC;
284     }
285
286     /* Update default_pts to a suitable value for file access */
287     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
288
289     /*
290      * Get the additional list of files
291      */
292     p_file = malloc( sizeof(file_entry_t) );
293     p_file->i_size = p_access->info.i_size;
294     p_file->psz_name = psz_name;
295     TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
296
297     psz = var_CreateGetString( p_access, "file-cat" );
298     if( *psz )
299     {
300         char *psz_parser = psz_name = psz;
301         int64_t i_size;
302
303         while( psz_name && *psz_name )
304         {
305             psz_parser = strchr( psz_name, ',' );
306             if( psz_parser ) *psz_parser = 0;
307
308             psz_name = strdup( psz_name );
309             if( psz_name )
310             {
311                 msg_Dbg( p_access, "adding file `%s'", psz_name );
312                 i_size = 0;
313
314 #ifdef HAVE_SYS_STAT_H
315                 if( !stat( psz_name, &stat_info ) )
316                 {
317                     p_access->info.i_size += stat_info.st_size;
318                     i_size = stat_info.st_size;
319                 }
320                 else
321                 {
322                     msg_Dbg( p_access, "cannot stat() file `%s'", psz_name );
323                 }
324 #endif
325                 p_file = malloc( sizeof(file_entry_t) );
326                 p_file->i_size = i_size;
327                 p_file->psz_name = psz_name;
328
329                 TAB_APPEND( p_sys->i_file, p_sys->file, p_file );
330             }
331
332             psz_name = psz_parser;
333             if( psz_name ) psz_name++;
334         }
335     }
336     free( psz );
337
338     return VLC_SUCCESS;
339 }
340
341 /*****************************************************************************
342  * Close: close the target
343  *****************************************************************************/
344 static void Close( vlc_object_t * p_this )
345 {
346     access_t     *p_access = (access_t*)p_this;
347     access_sys_t *p_sys = p_access->p_sys;
348     int i;
349
350     close( p_sys->fd );
351
352     for( i = 0; i < p_sys->i_file; i++ )
353     {
354         free( p_sys->file[i]->psz_name );
355         free( p_sys->file[i] );
356     }
357     free( p_sys->file );
358
359     free( p_sys );
360 }
361
362 /*****************************************************************************
363  * Read: standard read on a file descriptor.
364  *****************************************************************************/
365 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
366 {
367     access_sys_t *p_sys = p_access->p_sys;
368     int i_ret;
369
370 #if !defined(WIN32) && !defined(UNDER_CE)
371     if( !p_sys->b_pace_control )
372     {
373         if( !p_sys->b_kfir )
374         {
375             /* Find if some data is available. This won't work under Windows. */
376             do
377             {
378                 struct pollfd ufd;
379
380                 if( p_access->b_die )
381                     return 0;
382
383                 memset (&ufd, 0, sizeof (ufd));
384                 ufd.fd = p_sys->fd;
385                 ufd.events = POLLIN;
386
387                 i_ret = poll( &ufd, 1, 500 );
388                 if( i_ret == -1 )
389                 {
390                     if( errno != EINTR )
391                     {
392                         msg_Err( p_access, "poll error: %s",
393                                  strerror( errno ) );
394                         return -1;
395                     }
396                     i_ret = 0;
397                 }
398             }
399             while( i_ret == 0 );
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
600 #ifdef UNDER_CE
601     p_sys->fd = utf8_fopen( psz_name, "rb" );
602     if ( !p_sys->fd )
603     {
604         msg_Err( p_access, "cannot open file %s", psz_name );
605         return VLC_EGENERIC;
606     }
607
608     fseek( p_sys->fd, 0, SEEK_END );
609     p_access->info.i_size = ftell( p_sys->fd );
610     p_access->info.i_update |= INPUT_UPDATE_SIZE;
611     fseek( p_sys->fd, 0, SEEK_SET );
612 #else
613     const char *psz_localname = ToLocale( psz_name );
614     if( psz_localname == NULL )
615     {
616         msg_Err( p_access, "incorrect file name %s", psz_name );
617         return VLC_EGENERIC;
618     }
619
620     // FIXME: support non-ANSI filenames on Win32
621     p_sys->fd = open( psz_localname, O_NONBLOCK /*| O_LARGEFILE*/ );
622     LocaleFree( psz_localname );
623
624     if ( p_sys->fd == -1 )
625     {
626         msg_Err( p_access, "cannot open file %s (%s)", psz_name,
627                  strerror(errno) );
628         return VLC_EGENERIC;
629     }
630
631 #if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
632     /* We'd rather use any available memory for reading ahead
633      * than for caching what we've already seen/heard */
634     fcntl(p_sys->fd, F_RDAHEAD, 1);
635     fcntl(p_sys->fd, F_NOCACHE, 1);
636 #endif
637
638 #endif
639
640     return VLC_SUCCESS;
641 }