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