]> git.sesse.net Git - vlc/blob - modules/access/file.c
input options whitelisting, step 2 (refs #1371)
[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         change_safe();
97     add_obsolete_string( "file-cat" );
98     set_capability( "access2", 50 );
99     add_shortcut( "file" );
100     add_shortcut( "stream" );
101     add_shortcut( "kfir" );
102     set_callbacks( Open, Close );
103 vlc_module_end();
104
105
106 /*****************************************************************************
107  * Exported prototypes
108  *****************************************************************************/
109 static int  Seek( access_t *, int64_t );
110 static ssize_t Read( access_t *, uint8_t *, size_t );
111 static int  Control( access_t *, int, va_list );
112 #ifdef HAVE_MMAP
113 static block_t *mmapBlock( access_t * );
114 #endif
115
116 static int  open_file( access_t *, const char * );
117
118 struct access_sys_t
119 {
120     uint64_t     pagemask;
121     unsigned int i_nb_reads;
122     vlc_bool_t   b_kfir;
123
124     int fd;
125
126     /* */
127     vlc_bool_t b_seekable;
128     vlc_bool_t b_pace_control;
129 };
130
131 /*****************************************************************************
132  * Open: open the file
133  *****************************************************************************/
134 static int Open( vlc_object_t *p_this )
135 {
136     access_t     *p_access = (access_t*)p_this;
137     access_sys_t *p_sys;
138
139     vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
140
141     /* Update default_pts to a suitable value for file access */
142     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
143
144     STANDARD_READ_ACCESS_INIT;
145     p_sys->i_nb_reads = 0;
146     p_sys->b_kfir = VLC_FALSE;
147     int fd = p_sys->fd = -1;
148
149     if (!strcasecmp (p_access->psz_access, "stream"))
150     {
151         p_sys->b_seekable = VLC_FALSE;
152         p_sys->b_pace_control = VLC_FALSE;
153     }
154     else if (!strcasecmp (p_access->psz_access, "kfir"))
155     {
156         p_sys->b_seekable = VLC_FALSE;
157         p_sys->b_pace_control = VLC_FALSE;
158         p_sys->b_kfir = VLC_TRUE;
159     }
160     else
161     {
162         p_sys->b_seekable = VLC_TRUE;
163         p_sys->b_pace_control = VLC_TRUE;
164     }
165
166     /* Open file */
167     msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
168
169     if (b_stdin)
170         fd = dup (0);
171     else
172         fd = open_file (p_access, p_access->psz_path);
173
174 #ifdef HAVE_SYS_STAT_H
175     struct stat st;
176
177     while (fd != -1)
178     {
179         if (fstat (fd, &st))
180             msg_Err (p_access, "fstat(%d): %m", fd);
181         else
182         if (S_ISDIR (st.st_mode))
183             /* The directory plugin takes care of that */
184             msg_Dbg (p_access, "file is a directory, aborting");
185         else
186             break; // success
187
188         close (fd);
189         fd = -1;
190     }
191 #endif
192
193     if (fd == -1)
194     {
195         free (p_sys);
196         return VLC_EGENERIC;
197     }
198     p_sys->fd = fd;
199
200 #ifdef HAVE_SYS_STAT_H
201     p_access->info.i_size = st.st_size;
202     if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
203      && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
204         p_sys->b_seekable = VLC_FALSE;
205
206 # ifdef HAVE_MMAP
207     p_sys->pagemask = sysconf (_SC_PAGE_SIZE) - 1;
208
209     /* Autodetect mmap() support */
210     if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
211     {
212         /* TODO: Do not allow PROT_WRITE, we should not need it.
213          * However, this far, "block" ownership seems such that whoever
214          * "receives" a block can freely modify its content. Hence we _may_
215          * need PROT_WRITE not to default memory protection.
216          * NOTE: With MAP_PRIVATE, changes are not committed to the underlying
217          * file, write open permission is not required.
218          */
219         void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
220         if (addr != MAP_FAILED)
221         {
222             /* Does the file system support mmap? */
223             munmap (addr, 1);
224             p_access->pf_read = NULL;
225             p_access->pf_block = mmapBlock;
226             msg_Dbg (p_this, "mmap enabled");
227         }
228         else
229             msg_Dbg (p_this, "mmap disabled (%m)");
230     }
231     else
232         msg_Dbg (p_this, "mmap disabled (non regular file)");
233 # endif
234 #else
235     p_sys->b_seekable = !b_stdin;
236 # warning File size not known!
237 #endif
238
239     if (p_sys->b_seekable && (p_access->info.i_size == 0))
240     {
241         /* FIXME that's bad because all others access will be probed */
242         msg_Err (p_access, "file is empty, aborting");
243         Close (p_this);
244         return VLC_EGENERIC;
245     }
246
247     return VLC_SUCCESS;
248 }
249
250 /*****************************************************************************
251  * Close: close the target
252  *****************************************************************************/
253 static void Close (vlc_object_t * p_this)
254 {
255     access_t     *p_access = (access_t*)p_this;
256     access_sys_t *p_sys = p_access->p_sys;
257
258     close (p_sys->fd);
259     free (p_sys);
260 }
261
262 /*****************************************************************************
263  * Read: standard read on a file descriptor.
264  *****************************************************************************/
265 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
266 {
267     access_sys_t *p_sys = p_access->p_sys;
268     ssize_t i_ret;
269     int fd = p_sys->fd;
270
271 #if !defined(WIN32) && !defined(UNDER_CE)
272     if( !p_sys->b_pace_control )
273     {
274         if( !p_sys->b_kfir )
275         {
276             /* Find if some data is available. This won't work under Windows. */
277             do
278             {
279                 struct pollfd ufd;
280
281                 if( p_access->b_die )
282                     return 0;
283
284                 memset (&ufd, 0, sizeof (ufd));
285                 ufd.fd = fd;
286                 ufd.events = POLLIN;
287
288                 i_ret = poll (&ufd, 1, 500);
289             }
290             while (i_ret <= 0);
291
292             i_ret = read (fd, p_buffer, i_len);
293         }
294         else
295         {
296             /* b_kfir ; work around a buggy poll() driver implementation */
297             while (((i_ret = read (fd, p_buffer, i_len)) == 0)
298                 && !p_access->b_die)
299             {
300                 msleep( INPUT_ERROR_SLEEP );
301             }
302         }
303     }
304     else
305 #endif /* WIN32 || UNDER_CE */
306         /* b_pace_control || WIN32 */
307         i_ret = read( fd, p_buffer, i_len );
308
309     if( i_ret < 0 )
310     {
311         switch (errno)
312         {
313             case EINTR:
314             case EAGAIN:
315                 break;
316
317             default:
318                 msg_Err (p_access, "read failed (%m)");
319                 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
320                                 _("VLC could not read the file."));
321         }
322
323         /* Delay a bit to avoid consuming all the CPU. This is particularly
324          * useful when reading from an unconnected FIFO. */
325         msleep( INPUT_ERROR_SLEEP );
326     }
327
328     p_sys->i_nb_reads++;
329
330 #ifdef HAVE_SYS_STAT_H
331     if( p_access->info.i_size != 0 &&
332         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
333     {
334         struct stat st;
335
336         if ((fstat (fd, &st) == 0)
337          && (p_access->info.i_size != st.st_size))
338         {
339             p_access->info.i_size = st.st_size;
340             p_access->info.i_update |= INPUT_UPDATE_SIZE;
341         }
342     }
343 #endif
344
345     if( i_ret > 0 )
346         p_access->info.i_pos += i_ret;
347     else if( i_ret == 0 )
348         p_access->info.b_eof = VLC_TRUE;
349
350     return i_ret;
351 }
352
353 #ifdef HAVE_MMAP
354 # define MMAP_SIZE (1 << 20)
355
356 static block_t *mmapBlock (access_t *p_access)
357 {
358     access_sys_t *p_sys = p_access->p_sys;
359
360     const int flags = MAP_SHARED;
361     off_t offset = p_access->info.i_pos & ~p_sys->pagemask;
362     size_t align = p_access->info.i_pos & p_sys->pagemask;
363     size_t length = (MMAP_SIZE > p_sys->pagemask) ? MMAP_SIZE : (p_sys->pagemask + 1);
364     void *addr;
365
366 #ifndef NDEBUG
367     int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
368     if (dbgpos != p_access->info.i_pos)
369         msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
370                  p_access->info.i_pos, dbgpos);
371 #endif
372
373     if (p_access->info.i_pos >= p_access->info.i_size)
374     {
375         /* End of file - check if file size changed... */
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 beyond 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     block_t *block = block_mmap_Alloc (addr, length);
416     if (block == NULL)
417         return NULL;
418
419     block->p_buffer += align;
420     block->i_buffer -= align;
421
422 #ifndef NDEBUG
423     msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
424              (unsigned long)length, addr, (unsigned long)offset);
425
426     /* Compare normal I/O with memory mapping */
427     char *buf = malloc (block->i_buffer);
428     ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
429
430     if (i_read != (ssize_t)block->i_buffer)
431         msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
432                  (unsigned)block->i_buffer);
433     if (memcmp (buf, block->p_buffer, block->i_buffer))
434         msg_Err (p_access, "inconsistent data buffer");
435     free (buf);
436 #endif
437
438     return block;
439 }
440 #endif
441
442 /*****************************************************************************
443  * Seek: seek to a specific location in a file
444  *****************************************************************************/
445 static int Seek (access_t *p_access, int64_t i_pos)
446 {
447     /* FIXME: i_size should really be unsigned */
448     if ((uint64_t)i_pos > (uint64_t)p_access->info.i_size)
449     {
450         /* This should only happen with corrupted files.
451          * But it also seems to happen with buggy demuxes (ASF) */
452         msg_Err (p_access, "seeking too far (0x"I64Fx" / 0x"I64Fx")",
453                  i_pos, p_access->info.i_size);
454         i_pos = p_access->info.i_size;
455     }
456
457     p_access->info.i_pos = i_pos;
458     p_access->info.b_eof = VLC_FALSE;
459
460 #if defined (HAVE_MMAP) && defined (NDEBUG)
461     if (p_access->pf_block == NULL)
462 #endif
463         lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
464     return VLC_SUCCESS;
465 }
466
467 /*****************************************************************************
468  * Control:
469  *****************************************************************************/
470 static int Control( access_t *p_access, int i_query, va_list args )
471 {
472     access_sys_t *p_sys = p_access->p_sys;
473     vlc_bool_t   *pb_bool;
474     int          *pi_int;
475     int64_t      *pi_64;
476
477     switch( i_query )
478     {
479         /* */
480         case ACCESS_CAN_SEEK:
481         case ACCESS_CAN_FASTSEEK:
482             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
483             *pb_bool = p_sys->b_seekable;
484             break;
485
486         case ACCESS_CAN_PAUSE:
487         case ACCESS_CAN_CONTROL_PACE:
488             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
489             *pb_bool = p_sys->b_pace_control;
490             break;
491
492         /* */
493         case ACCESS_GET_MTU:
494             pi_int = (int*)va_arg( args, int * );
495             *pi_int = 0;
496             break;
497
498         case ACCESS_GET_PTS_DELAY:
499             pi_64 = (int64_t*)va_arg( args, int64_t * );
500             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
501             break;
502
503         /* */
504         case ACCESS_SET_PAUSE_STATE:
505             /* Nothing to do */
506             break;
507
508         case ACCESS_GET_TITLE_INFO:
509         case ACCESS_SET_TITLE:
510         case ACCESS_SET_SEEKPOINT:
511         case ACCESS_SET_PRIVATE_ID_STATE:
512         case ACCESS_GET_META:
513         case ACCESS_GET_CONTENT_TYPE:
514             return VLC_EGENERIC;
515
516         default:
517             msg_Warn( p_access, "unimplemented query in control" );
518             return VLC_EGENERIC;
519
520     }
521     return VLC_SUCCESS;
522 }
523
524
525 static char *expand_path (const access_t *p_access, const char *path)
526 {
527     if (strncmp (path, "~/", 2) == 0)
528     {
529         char *res;
530
531          // TODO: we should also support the ~cmassiot/ syntax
532          if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
533              return NULL;
534          return res;
535     }
536
537 #if defined(WIN32)
538     if (!strcasecmp (p_access->psz_access, "file")
539       && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
540         // Explorer can open path such as file:/C:/ or file:///C:/
541         // hence remove leading / if found
542         return strdup (path + 1);
543 #endif
544
545     return strdup (path);
546 }
547
548
549 /*****************************************************************************
550  * open_file: Opens a specific file
551  *****************************************************************************/
552 static int open_file (access_t *p_access, const char *psz_name)
553 {
554     char *path = expand_path (p_access, psz_name);
555
556 #ifdef UNDER_CE
557     p_sys->fd = utf8_fopen( path, "rb" );
558     if ( !p_sys->fd )
559     {
560         msg_Err( p_access, "cannot open file %s", psz_name );
561         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
562                         _("VLC could not open the file \"%s\"."), psz_name );
563         free (path);
564         return VLC_EGENERIC;
565     }
566
567     fseek( p_sys->fd, 0, SEEK_END );
568     p_access->info.i_size = ftell( p_sys->fd );
569     p_access->info.i_update |= INPUT_UPDATE_SIZE;
570     fseek( p_sys->fd, 0, SEEK_SET );
571 #else
572     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
573     free (path);
574     if (fd == -1)
575     {
576         msg_Err (p_access, "cannot open file %s (%m)", psz_name);
577         intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
578                         _("VLC could not open the file \"%s\"."), psz_name);
579         return -1;
580     }
581
582 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
583     /* We'd rather use any available memory for reading ahead
584      * than for caching what we've already seen/heard */
585     fcntl (fd, F_RDAHEAD, 1);
586     fcntl (fd, F_NOCACHE, 1);
587 # endif
588 #endif
589
590     return fd;
591 }