]> git.sesse.net Git - vlc/blob - modules/access/file.c
- Revert [17394] which reintroduced TOCTOU issues
[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 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_interaction.h>
32
33 #include <stdlib.h>
34 #include <string.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
53 #if defined( WIN32 ) && !defined( UNDER_CE )
54 /* fstat() support for large files on win32 */
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     "Caching value for files. This " \
83     "value should be set in milliseconds." )
84 #define CAT_TEXT N_("Concatenate with additional files")
85 #define CAT_LONGTEXT N_( \
86     "Play split files as if they were part of a unique file. " \
87     "You need to specify a comma-separated list of files." )
88
89 vlc_module_begin();
90     set_description( _("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  open_file( access_t *, const char * );
112
113 struct access_sys_t
114 {
115     unsigned int i_nb_reads;
116     vlc_bool_t   b_kfir;
117
118     /* Files list */
119     unsigned filec;
120     int     *filev;
121     int64_t *sizev;
122
123     /* Current file */
124     unsigned filep;
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     char         *catlist;
139
140     vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
141
142     /* Update default_pts to a suitable value for file access */
143     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
144
145     STANDARD_READ_ACCESS_INIT;
146     p_sys->i_nb_reads = 0;
147     p_sys->b_kfir = VLC_FALSE;
148     p_sys->filec = 1;
149     p_sys->filep = 0;
150
151     if (!strcasecmp (p_access->psz_access, "stream"))
152     {
153         p_sys->b_seekable = VLC_FALSE;
154         p_sys->b_pace_control = VLC_FALSE;
155     }
156     else if (!strcasecmp (p_access->psz_access, "kfir"))
157     {
158         p_sys->b_seekable = VLC_FALSE;
159         p_sys->b_pace_control = VLC_FALSE;
160         p_sys->b_kfir = VLC_TRUE;
161     }
162     else
163     {
164         p_sys->b_seekable = VLC_TRUE;
165         p_sys->b_pace_control = VLC_TRUE;
166     }
167
168     /* Count number of files */
169     catlist = var_CreateGetString (p_access, "file-cat");
170     if (catlist == NULL)
171     {
172         free (p_sys);
173         return VLC_ENOMEM;
174     }
175
176     if (*catlist)
177     {
178         for (const char *ptr = catlist; ptr != NULL; p_sys->filec++)
179         {
180             ptr = strchr (ptr, ',');
181             if (ptr != NULL)
182                 ptr++;
183         }
184     }
185
186     p_sys->filev = calloc (p_sys->filec, sizeof (p_sys->filev[0]));
187     if (p_sys->filev == NULL)
188     {
189         free (catlist);
190         free (p_sys);
191         return VLC_ENOMEM;
192     }
193
194     p_sys->sizev = calloc (p_sys->filec, sizeof (p_sys->sizev[0]));
195     if (p_sys->sizev == NULL)
196     {
197         free (catlist);
198         free (p_sys->filev);
199         free (p_sys);
200         return VLC_ENOMEM;
201     }
202
203     /* Open files */
204     char *filename = catlist;
205     for (unsigned i = 0; i < p_sys->filec; i++)
206     {
207         int fd = -1;
208
209         if (i == 0)
210         {
211             msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
212
213             if (b_stdin)
214                 fd = dup (0);
215             else
216                 fd = open_file (p_access, p_access->psz_path);
217         }
218         else
219         {
220             assert (filename != NULL);
221
222             char *ptr = strchr (filename, ',');
223             if (ptr != NULL)
224                 *ptr = 0;
225
226             msg_Dbg (p_access, "opening additionnal file `%s'", filename);
227             fd = open_file (p_access, filename);
228             filename = ptr + 1;
229         }
230
231 #ifdef HAVE_SYS_STAT_H
232         struct stat st;
233
234         while (fd != -1)
235         {
236             if (fstat (fd, &st))
237                 msg_Err (p_access, "fstat(%d): %s", fd, strerror (errno));
238             else
239             if (S_ISDIR (st.st_mode))
240                 /* The directory plugin takes care of that */
241                 msg_Dbg (p_access, "file is a directory, aborting");
242             else
243                 break; // success
244
245             close (fd);
246             fd = -1;
247         }
248 #endif
249
250         if (fd == -1)
251         {
252             free (catlist);
253             p_sys->filec = i;
254             Close (p_this);
255             return VLC_EGENERIC;
256         }
257         p_sys->filev[i] = fd;
258
259 #ifdef HAVE_SYS_STAT_H
260         p_sys->sizev[i] = st.st_size;
261         p_access->info.i_size += st.st_size;
262
263         if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
264          && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
265             // If one file is not seekable, the concatenation is not either
266             p_sys->b_seekable = VLC_FALSE;
267 #else
268         p_sys->b_seekable = !b_stdin;
269 #endif
270
271     }
272
273     free (catlist);
274
275     if (p_sys->b_seekable && !p_access->info.i_size)
276     {
277         /* FIXME that's bad because all others access will be probed */
278         msg_Err (p_access, "file is empty, aborting");
279         Close (p_this);
280         return VLC_EGENERIC;
281     }
282
283     return VLC_SUCCESS;
284 }
285
286 /*****************************************************************************
287  * Close: close the target
288  *****************************************************************************/
289 static void Close (vlc_object_t * p_this)
290 {
291     access_t     *p_access = (access_t*)p_this;
292     access_sys_t *p_sys = p_access->p_sys;
293
294     for (unsigned i = 0; i < p_sys->filec; i++)
295         close (p_sys->filev[i]);
296
297     free (p_sys->filev);
298     free (p_sys->sizev);
299     free (p_sys);
300 }
301
302 /*****************************************************************************
303  * Read: standard read on a file descriptor.
304  *****************************************************************************/
305 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
306 {
307     access_sys_t *p_sys = p_access->p_sys;
308     int i_ret;
309     int fd = p_sys->filev[p_sys->filep];
310
311 #if !defined(WIN32) && !defined(UNDER_CE)
312     if( !p_sys->b_pace_control )
313     {
314         if( !p_sys->b_kfir )
315         {
316             /* Find if some data is available. This won't work under Windows. */
317             do
318             {
319                 struct pollfd ufd;
320
321                 if( p_access->b_die )
322                     return 0;
323
324                 memset (&ufd, 0, sizeof (ufd));
325                 ufd.fd = fd;
326                 ufd.events = POLLIN;
327
328                 i_ret = poll (&ufd, 1, 500);
329             }
330             while (i_ret <= 0);
331
332             i_ret = read (fd, p_buffer, i_len);
333         }
334         else
335         {
336             /* b_kfir ; work around a buggy poll() driver implementation */
337             while (((i_ret = read (fd, p_buffer, i_len)) == 0)
338                 && !p_access->b_die)
339             {
340                 msleep( INPUT_ERROR_SLEEP );
341             }
342         }
343     }
344     else
345 #endif /* WIN32 || UNDER_CE */
346         /* b_pace_control || WIN32 */
347         i_ret = read( fd, p_buffer, i_len );
348
349     if( i_ret < 0 )
350     {
351         switch (errno)
352         {
353             case EINTR:
354             case EAGAIN:
355                 break;
356
357             default:
358                 msg_Err (p_access, "read failed (%s)", strerror (errno));
359                 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
360                                 _("VLC could not read file \"%s\"."),
361                                 strerror (errno));
362         }
363
364         /* Delay a bit to avoid consuming all the CPU. This is particularly
365          * useful when reading from an unconnected FIFO. */
366         msleep( INPUT_ERROR_SLEEP );
367     }
368
369     p_sys->i_nb_reads++;
370
371 #ifdef HAVE_SYS_STAT_H
372     if( p_access->info.i_size != 0 &&
373         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
374     {
375         struct stat st;
376         int i = p_sys->filep;
377
378         if ((fstat (fd, &st) == 0)
379          && (p_sys->sizev[i] != st.st_size))
380         {
381             p_access->info.i_size += st.st_size - p_sys->sizev[i];
382             p_sys->sizev[i] = st.st_size;
383             p_access->info.i_update |= INPUT_UPDATE_SIZE;
384         }
385     }
386 #endif
387
388     /* If we reached an EOF then switch to the next file in the list */
389     if (i_ret == 0)
390     {
391         if  (++p_sys->filep < p_sys->filec)
392             /* We have to read some data */
393             return Read (p_access, p_buffer, i_len);
394         else
395             p_sys->filep--;
396     }
397
398     if( i_ret > 0 )
399         p_access->info.i_pos += i_ret;
400     else if( i_ret == 0 )
401         p_access->info.b_eof = VLC_TRUE;
402
403     return i_ret;
404 }
405
406 /*****************************************************************************
407  * Seek: seek to a specific location in a file
408  *****************************************************************************/
409 static int Seek (access_t *p_access, int64_t i_pos)
410 {
411     access_sys_t *p_sys = p_access->p_sys;
412
413     if (p_access->info.i_size < p_access->info.i_pos)
414     {
415         msg_Err (p_access, "seeking too far");
416         i_pos = p_access->info.i_pos = p_access->info.i_size;
417     }
418     else if (p_access->info.i_pos < 0)
419     {
420         msg_Err (p_access, "seeking too early");
421         i_pos = p_access->info.i_pos = 0;
422     }
423
424     p_access->info.i_pos = i_pos;
425     p_access->info.b_eof = VLC_FALSE;
426
427     /* Determine which file we need to access */
428     unsigned i = 0;
429     assert (p_sys->filec > 0);
430
431     while (i_pos > p_sys->sizev[i])
432     {
433         i_pos -= p_sys->sizev[i++];
434         assert (i < p_sys->filec);
435     }
436     p_sys->filep = i;
437
438     lseek (p_sys->filev[i], i_pos, SEEK_SET);
439     return VLC_SUCCESS;
440 }
441
442 /*****************************************************************************
443  * Control:
444  *****************************************************************************/
445 static int Control( access_t *p_access, int i_query, va_list args )
446 {
447     access_sys_t *p_sys = p_access->p_sys;
448     vlc_bool_t   *pb_bool;
449     int          *pi_int;
450     int64_t      *pi_64;
451
452     switch( i_query )
453     {
454         /* */
455         case ACCESS_CAN_SEEK:
456         case ACCESS_CAN_FASTSEEK:
457             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
458             *pb_bool = p_sys->b_seekable;
459             break;
460
461         case ACCESS_CAN_PAUSE:
462         case ACCESS_CAN_CONTROL_PACE:
463             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
464             *pb_bool = p_sys->b_pace_control;
465             break;
466
467         /* */
468         case ACCESS_GET_MTU:
469             pi_int = (int*)va_arg( args, int * );
470             *pi_int = 0;
471             break;
472
473         case ACCESS_GET_PTS_DELAY:
474             pi_64 = (int64_t*)va_arg( args, int64_t * );
475             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
476             break;
477
478         /* */
479         case ACCESS_SET_PAUSE_STATE:
480             /* Nothing to do */
481             break;
482
483         case ACCESS_GET_TITLE_INFO:
484         case ACCESS_SET_TITLE:
485         case ACCESS_SET_SEEKPOINT:
486         case ACCESS_SET_PRIVATE_ID_STATE:
487         case ACCESS_GET_META:
488             return VLC_EGENERIC;
489
490         default:
491             msg_Warn( p_access, "unimplemented query in control" );
492             return VLC_EGENERIC;
493
494     }
495     return VLC_SUCCESS;
496 }
497
498
499 static char *expand_path (const access_t *p_access, const char *path)
500 {
501     if (strncmp (path, "~/", 2) == 0)
502     {
503         char *res;
504
505          // TODO: we should also support the ~cmassiot/ syntax
506          if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
507              return NULL;
508          return res;
509     }
510
511 #if defined(WIN32)
512     if (!strcasecmp (p_access->psz_access, "file")
513       && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
514         // Explorer can open path such as file:/C:/ or file:///C:/
515         // hence remove leading / if found
516         return strdup (path + 1);
517 #endif
518
519     return strdup (path);
520 }
521
522
523 /*****************************************************************************
524  * open_file: Opens a specific file
525  *****************************************************************************/
526 static int open_file (access_t *p_access, const char *psz_name)
527 {
528     char *path = expand_path (p_access, psz_name);
529
530 #ifdef UNDER_CE
531     p_sys->fd = utf8_fopen( path, "rb" );
532     if ( !p_sys->fd )
533     {
534         msg_Err( p_access, "cannot open file %s", psz_name );
535         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
536                         _("VLC could not open file \"%s\"."), psz_name );
537         free (path);
538         return VLC_EGENERIC;
539     }
540
541     fseek( p_sys->fd, 0, SEEK_END );
542     p_access->info.i_size = ftell( p_sys->fd );
543     p_access->info.i_update |= INPUT_UPDATE_SIZE;
544     fseek( p_sys->fd, 0, SEEK_SET );
545 #else
546     const char *psz_localname = ToLocale (path);
547     if (psz_localname == NULL)
548     {
549         msg_Err (p_access, "incorrect file name %s", psz_name);
550         free (path);
551         return -1;
552     }
553
554     // FIXME: support non-ANSI filenames on Win32
555     int fd = open (path, O_NONBLOCK /*| O_LARGEFILE*/);
556     LocaleFree (psz_localname);
557     free (path);
558
559     if (fd == -1)
560     {
561         msg_Err (p_access, "cannot open file %s (%s)", psz_name,
562                  strerror (errno));
563         intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"), 
564                         _("VLC could not open file \"%s\" (%s)."),
565                         psz_name, strerror (errno));
566         return -1;
567     }
568
569 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
570     /* We'd rather use any available memory for reading ahead
571      * than for caching what we've already seen/heard */
572     fcntl (fd, F_RDAHEAD, 1);
573     fcntl (fd, F_NOCACHE, 1);
574 # endif
575 #endif
576
577     return fd;
578 }