]> git.sesse.net Git - vlc/blob - modules/access/file.c
fix [23367]
[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  * Copyright © 2006-2007 Rémi Denis-Courmont
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Rémi Denis-Courmont <rem # videolan # org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc_input.h>
31 #include <vlc_access.h>
32 #include <vlc_interface.h>
33
34 #include <assert.h>
35 #include <errno.h>
36 #ifdef HAVE_SYS_TYPES_H
37 #   include <sys/types.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 #if defined( WIN32 ) && !defined( UNDER_CE )
47 #   include <io.h>
48 #else
49 #   include <unistd.h>
50 #   include <poll.h>
51 #endif
52 #ifdef HAVE_MMAP
53 #   include <sys/mman.h>
54 #endif
55
56 #if defined( WIN32 ) && !defined( UNDER_CE )
57 #   ifdef lseek
58 #      undef lseek
59 #   endif
60 #   define lseek _lseeki64
61 #elif defined( UNDER_CE )
62 #   ifdef read
63 #      undef read
64 #   endif
65 #   define read(a,b,c) fread(b,1,c,a)
66 #   define close(a) fclose(a)
67 #   ifdef lseek
68 #      undef lseek
69 #   endif
70 #   define lseek fseek
71 #endif
72
73 #include <vlc_charset.h>
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 static int  Open ( vlc_object_t * );
79 static void Close( vlc_object_t * );
80
81 #define CACHING_TEXT N_("Caching value in ms")
82 #define CACHING_LONGTEXT N_( \
83     "Caching value for files. This " \
84     "value should be set in milliseconds." )
85 #define CAT_TEXT N_("Concatenate with additional files")
86 #define CAT_LONGTEXT N_( \
87     "Play split files as if they were part of a unique file. " \
88     "You need to specify a comma-separated list of files." )
89
90 vlc_module_begin();
91     set_description( _("File input") );
92     set_shortname( _("File") );
93     set_category( CAT_INPUT );
94     set_subcategory( SUBCAT_INPUT_ACCESS );
95     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
96     add_obsolete_string( "file-cat" );
97     set_capability( "access2", 50 );
98     add_shortcut( "file" );
99     add_shortcut( "stream" );
100     add_shortcut( "kfir" );
101     set_callbacks( Open, Close );
102 vlc_module_end();
103
104
105 /*****************************************************************************
106  * Exported prototypes
107  *****************************************************************************/
108 static int  Seek( access_t *, int64_t );
109 static ssize_t Read( access_t *, uint8_t *, size_t );
110 static int  Control( access_t *, int, va_list );
111 #ifdef HAVE_MMAP
112 static block_t *mmapBlock( access_t * );
113 #endif
114
115 static int  open_file( access_t *, const char * );
116
117 struct access_sys_t
118 {
119     unsigned int i_nb_reads;
120     vlc_bool_t   b_kfir;
121
122     int fd;
123
124     /* */
125     vlc_bool_t b_seekable;
126     vlc_bool_t b_pace_control;
127 };
128
129 /*****************************************************************************
130  * Open: open the file
131  *****************************************************************************/
132 static int Open( vlc_object_t *p_this )
133 {
134     access_t     *p_access = (access_t*)p_this;
135     access_sys_t *p_sys;
136
137     vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
138
139     /* Update default_pts to a suitable value for file access */
140     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
141
142     STANDARD_READ_ACCESS_INIT;
143     p_sys->i_nb_reads = 0;
144     p_sys->b_kfir = VLC_FALSE;
145     int fd = p_sys->fd = -1;
146
147     if (!strcasecmp (p_access->psz_access, "stream"))
148     {
149         p_sys->b_seekable = VLC_FALSE;
150         p_sys->b_pace_control = VLC_FALSE;
151     }
152     else if (!strcasecmp (p_access->psz_access, "kfir"))
153     {
154         p_sys->b_seekable = VLC_FALSE;
155         p_sys->b_pace_control = VLC_FALSE;
156         p_sys->b_kfir = VLC_TRUE;
157     }
158     else
159     {
160         p_sys->b_seekable = VLC_TRUE;
161         p_sys->b_pace_control = VLC_TRUE;
162     }
163
164     /* Open file */
165     msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
166
167     if (b_stdin)
168         fd = dup (0);
169     else
170         fd = open_file (p_access, p_access->psz_path);
171
172 #ifdef HAVE_SYS_STAT_H
173     struct stat st;
174
175     while (fd != -1)
176     {
177         if (fstat (fd, &st))
178             msg_Err (p_access, "fstat(%d): %m", fd);
179         else
180         if (S_ISDIR (st.st_mode))
181             /* The directory plugin takes care of that */
182             msg_Dbg (p_access, "file is a directory, aborting");
183         else
184             break; // success
185
186         close (fd);
187         fd = -1;
188     }
189 #endif
190
191     if (fd == -1)
192     {
193         free (p_sys);
194         return VLC_EGENERIC;
195     }
196     p_sys->fd = fd;
197
198 #ifdef HAVE_SYS_STAT_H
199     p_access->info.i_size = st.st_size;
200     if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
201      && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
202         p_sys->b_seekable = VLC_FALSE;
203
204 # ifdef HAVE_MMAP
205     /* Autodetect mmap() support */
206     if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
207     {
208         void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0);
209         if (addr != MAP_FAILED)
210         {
211             /* Does the file system support mmap? */
212             munmap (addr, 1);
213             p_access->pf_read = NULL;
214             p_access->pf_block = mmapBlock;
215             msg_Dbg (p_this, "mmap enabled");
216         }
217         else
218             msg_Dbg (p_this, "mmap disabled (%m)");
219     }
220     else
221         msg_Dbg (p_this, "mmap disabled (non regular file)");
222 # endif
223 #else
224     p_sys->b_seekable = !b_stdin;
225 # warning File size not known!
226 #endif
227
228     if (p_sys->b_seekable && (p_access->info.i_size == 0))
229     {
230         /* FIXME that's bad because all others access will be probed */
231         msg_Err (p_access, "file is empty, aborting");
232         Close (p_this);
233         return VLC_EGENERIC;
234     }
235
236     return VLC_SUCCESS;
237 }
238
239 /*****************************************************************************
240  * Close: close the target
241  *****************************************************************************/
242 static void Close (vlc_object_t * p_this)
243 {
244     access_t     *p_access = (access_t*)p_this;
245     access_sys_t *p_sys = p_access->p_sys;
246
247     close (p_sys->fd);
248     free (p_sys);
249 }
250
251 /*****************************************************************************
252  * Read: standard read on a file descriptor.
253  *****************************************************************************/
254 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
255 {
256     access_sys_t *p_sys = p_access->p_sys;
257     ssize_t i_ret;
258     int fd = p_sys->fd;
259
260 #if !defined(WIN32) && !defined(UNDER_CE)
261     if( !p_sys->b_pace_control )
262     {
263         if( !p_sys->b_kfir )
264         {
265             /* Find if some data is available. This won't work under Windows. */
266             do
267             {
268                 struct pollfd ufd;
269
270                 if( p_access->b_die )
271                     return 0;
272
273                 memset (&ufd, 0, sizeof (ufd));
274                 ufd.fd = fd;
275                 ufd.events = POLLIN;
276
277                 i_ret = poll (&ufd, 1, 500);
278             }
279             while (i_ret <= 0);
280
281             i_ret = read (fd, p_buffer, i_len);
282         }
283         else
284         {
285             /* b_kfir ; work around a buggy poll() driver implementation */
286             while (((i_ret = read (fd, p_buffer, i_len)) == 0)
287                 && !p_access->b_die)
288             {
289                 msleep( INPUT_ERROR_SLEEP );
290             }
291         }
292     }
293     else
294 #endif /* WIN32 || UNDER_CE */
295         /* b_pace_control || WIN32 */
296         i_ret = read( fd, p_buffer, i_len );
297
298     if( i_ret < 0 )
299     {
300         switch (errno)
301         {
302             case EINTR:
303             case EAGAIN:
304                 break;
305
306             default:
307                 msg_Err (p_access, "read failed (%m)");
308                 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
309                                 _("VLC could not read the file."));
310         }
311
312         /* Delay a bit to avoid consuming all the CPU. This is particularly
313          * useful when reading from an unconnected FIFO. */
314         msleep( INPUT_ERROR_SLEEP );
315     }
316
317     p_sys->i_nb_reads++;
318
319 #ifdef HAVE_SYS_STAT_H
320     if( p_access->info.i_size != 0 &&
321         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
322     {
323         struct stat st;
324
325         if ((fstat (fd, &st) == 0)
326          && (p_access->info.i_size != st.st_size))
327         {
328             p_access->info.i_size = st.st_size;
329             p_access->info.i_update |= INPUT_UPDATE_SIZE;
330         }
331     }
332 #endif
333
334     if( i_ret > 0 )
335         p_access->info.i_pos += i_ret;
336     else if( i_ret == 0 )
337         p_access->info.b_eof = VLC_TRUE;
338
339     return i_ret;
340 }
341
342 #ifdef HAVE_MMAP
343 # define MMAP_SIZE (1 << 18)
344
345 struct block_sys_t
346 {
347     block_t self;
348     //vlc_object_t *owner;
349     void *base_addr;
350     size_t length;
351 };
352
353 static void mmapRelease (block_t *block)
354 {
355     block_sys_t *p_sys = (block_sys_t *)block;
356
357     munmap (p_sys->base_addr, p_sys->length);
358     //vlc_object_release (p_sys->owner);
359     free (p_sys);
360 }
361
362 static block_t *mmapBlock (access_t *p_access)
363 {
364     access_sys_t *p_sys = p_access->p_sys;
365
366     const int flags = MAP_SHARED;
367     const size_t pagesize = sysconf (_SC_PAGE_SIZE);
368     off_t offset = p_access->info.i_pos & ~(pagesize - 1);
369     size_t align = p_access->info.i_pos & (pagesize - 1);
370     size_t length = (MMAP_SIZE > pagesize) ? MMAP_SIZE : pagesize;
371     void *addr;
372
373     if (p_access->info.i_pos >= p_access->info.i_size)
374     {
375         /* End of file - check that file size hasn't change... */
376         struct stat st;
377
378         if ((fstat (p_sys->fd, &st) == 0)
379          && (st.st_size != p_access->info.i_size))
380         {
381             p_access->info.i_size = st.st_size;
382             p_access->info.i_update |= INPUT_UPDATE_SIZE;
383         }
384
385         /* Really at end of file then */
386         if (p_access->info.i_pos >= p_access->info.i_size)
387         {
388             p_access->info.b_eof = VLC_TRUE;
389             msg_Dbg (p_access, "at end of memory mapped file");
390             return NULL;
391         }
392     }
393
394     if (offset + length > p_access->info.i_size)
395         /* Don't mmap paste end of file */
396         length = p_access->info.i_size - offset;
397
398     assert (offset <= p_access->info.i_pos);               /* and */
399     assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
400     assert (offset < p_access->info.i_size);               /* imply */
401     assert (length > 0);
402
403     addr = mmap (NULL, length, PROT_READ, flags, p_sys->fd, offset);
404     if (addr == MAP_FAILED)
405     {
406         msg_Err (p_access, "memory mapping failed (%m)");
407         intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
408                         _("VLC could not read the file."));
409         msleep( INPUT_ERROR_SLEEP );
410         return NULL;
411     }
412
413     p_access->info.i_pos = offset + length;
414
415     msg_Dbg (p_access, "mapped %lu bytes at %p from offset %lu",
416              (unsigned long)length, addr, (unsigned long)offset);
417     block_sys_t *block = malloc (sizeof (*block));
418     if (block == NULL)
419     {
420         munmap (addr, length);
421         return NULL;
422     }
423
424     block_Init (&block->self, ((uint8_t *)addr) + align, length - align);
425     block->self.pf_release = mmapRelease;
426     block->base_addr = addr;
427     block->length = length;
428     //vlc_object_yield (block->owner = VLC_OBJECT (p_access));
429
430     return &block->self;
431 }
432 #endif
433
434 /*****************************************************************************
435  * Seek: seek to a specific location in a file
436  *****************************************************************************/
437 static int Seek (access_t *p_access, int64_t i_pos)
438 {
439     if (i_pos > p_access->info.i_size)
440     {
441         msg_Err (p_access, "seeking too far");
442         i_pos = p_access->info.i_size;
443     }
444     else if (i_pos < 0)
445     {
446         msg_Err (p_access, "seeking too early");
447         i_pos = 0;
448     }
449
450     p_access->info.i_pos = i_pos;
451     p_access->info.b_eof = VLC_FALSE;
452
453 #ifdef HAVE_MMAP
454     if (p_access->pf_block == NULL)
455 #endif
456         lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
457     return VLC_SUCCESS;
458 }
459
460 /*****************************************************************************
461  * Control:
462  *****************************************************************************/
463 static int Control( access_t *p_access, int i_query, va_list args )
464 {
465     access_sys_t *p_sys = p_access->p_sys;
466     vlc_bool_t   *pb_bool;
467     int          *pi_int;
468     int64_t      *pi_64;
469
470     switch( i_query )
471     {
472         /* */
473         case ACCESS_CAN_SEEK:
474         case ACCESS_CAN_FASTSEEK:
475             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
476             *pb_bool = p_sys->b_seekable;
477             break;
478
479         case ACCESS_CAN_PAUSE:
480         case ACCESS_CAN_CONTROL_PACE:
481             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
482             *pb_bool = p_sys->b_pace_control;
483             break;
484
485         /* */
486         case ACCESS_GET_MTU:
487             pi_int = (int*)va_arg( args, int * );
488             *pi_int = 0;
489             break;
490
491         case ACCESS_GET_PTS_DELAY:
492             pi_64 = (int64_t*)va_arg( args, int64_t * );
493             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
494             break;
495
496         /* */
497         case ACCESS_SET_PAUSE_STATE:
498             /* Nothing to do */
499             break;
500
501         case ACCESS_GET_TITLE_INFO:
502         case ACCESS_SET_TITLE:
503         case ACCESS_SET_SEEKPOINT:
504         case ACCESS_SET_PRIVATE_ID_STATE:
505         case ACCESS_GET_META:
506         case ACCESS_GET_CONTENT_TYPE:
507             return VLC_EGENERIC;
508
509         default:
510             msg_Warn( p_access, "unimplemented query in control" );
511             return VLC_EGENERIC;
512
513     }
514     return VLC_SUCCESS;
515 }
516
517
518 static char *expand_path (const access_t *p_access, const char *path)
519 {
520     if (strncmp (path, "~/", 2) == 0)
521     {
522         char *res;
523
524          // TODO: we should also support the ~cmassiot/ syntax
525          if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
526              return NULL;
527          return res;
528     }
529
530 #if defined(WIN32)
531     if (!strcasecmp (p_access->psz_access, "file")
532       && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
533         // Explorer can open path such as file:/C:/ or file:///C:/
534         // hence remove leading / if found
535         return strdup (path + 1);
536 #endif
537
538     return strdup (path);
539 }
540
541
542 /*****************************************************************************
543  * open_file: Opens a specific file
544  *****************************************************************************/
545 static int open_file (access_t *p_access, const char *psz_name)
546 {
547     char *path = expand_path (p_access, psz_name);
548
549 #ifdef UNDER_CE
550     p_sys->fd = utf8_fopen( path, "rb" );
551     if ( !p_sys->fd )
552     {
553         msg_Err( p_access, "cannot open file %s", psz_name );
554         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
555                         _("VLC could not open the file \"%s\"."), psz_name );
556         free (path);
557         return VLC_EGENERIC;
558     }
559
560     fseek( p_sys->fd, 0, SEEK_END );
561     p_access->info.i_size = ftell( p_sys->fd );
562     p_access->info.i_update |= INPUT_UPDATE_SIZE;
563     fseek( p_sys->fd, 0, SEEK_SET );
564 #else
565     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
566     free (path);
567     if (fd == -1)
568     {
569         msg_Err (p_access, "cannot open file %s (%m)", psz_name);
570         intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
571                         _("VLC could not open the file \"%s\"."), psz_name);
572         return -1;
573     }
574
575 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
576     /* We'd rather use any available memory for reading ahead
577      * than for caching what we've already seen/heard */
578     fcntl (fd, F_RDAHEAD, 1);
579     fcntl (fd, F_NOCACHE, 1);
580 # endif
581 #endif
582
583     return fd;
584 }