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