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