]> git.sesse.net Git - vlc/blob - modules/access/file.c
Remove old mmap support
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_interface.h>
37
38 #include <assert.h>
39 #include <errno.h>
40 #ifdef HAVE_SYS_TYPES_H
41 #   include <sys/types.h>
42 #endif
43 #ifdef HAVE_SYS_STAT_H
44 #   include <sys/stat.h>
45 #endif
46 #ifdef HAVE_FCNTL_H
47 #   include <fcntl.h>
48 #endif
49
50 #if defined( WIN32 ) && !defined( UNDER_CE )
51 #   include <io.h>
52 #else
53 #   include <unistd.h>
54 #   include <poll.h>
55 #endif
56
57 #if defined( WIN32 ) && !defined( UNDER_CE )
58 #   ifdef lseek
59 #      undef lseek
60 #   endif
61 #   define lseek _lseeki64
62 #elif defined( UNDER_CE )
63 #   ifdef read
64 #      undef read
65 #   endif
66 #   define read(a,b,c) fread(b,1,c,a)
67 #   define close(a) fclose(a)
68 #   ifdef lseek
69 #      undef lseek
70 #   endif
71 #   define lseek fseek
72 #endif
73
74 #include <vlc_charset.h>
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 static int  Open ( vlc_object_t * );
80 static void Close( vlc_object_t * );
81
82 #define CACHING_TEXT N_("Caching value in ms")
83 #define CACHING_LONGTEXT N_( \
84     "Caching value for files. This " \
85     "value should be set in milliseconds." )
86
87 vlc_module_begin();
88     set_description( _("File input") );
89     set_shortname( _("File") );
90     set_category( CAT_INPUT );
91     set_subcategory( SUBCAT_INPUT_ACCESS );
92     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
93     add_obsolete_string( "file-cat" );
94     set_capability( "access2", 50 );
95     add_shortcut( "file" );
96     add_shortcut( "stream" );
97     add_shortcut( "kfir" );
98     set_callbacks( Open, Close );
99 vlc_module_end();
100
101
102 /*****************************************************************************
103  * Exported prototypes
104  *****************************************************************************/
105 static int  Seek( access_t *, int64_t );
106 static ssize_t Read( access_t *, uint8_t *, size_t );
107 static int  Control( access_t *, int, va_list );
108
109 static int  open_file( access_t *, const char * );
110
111 struct access_sys_t
112 {
113     unsigned int i_nb_reads;
114     vlc_bool_t   b_kfir;
115
116     int fd;
117
118     /* */
119     vlc_bool_t b_seekable;
120     vlc_bool_t b_pace_control;
121 };
122
123 /*****************************************************************************
124  * Open: open the file
125  *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
127 {
128     access_t     *p_access = (access_t*)p_this;
129     access_sys_t *p_sys;
130
131     vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
132
133     /* Update default_pts to a suitable value for file access */
134     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
135
136     STANDARD_READ_ACCESS_INIT;
137     p_sys->i_nb_reads = 0;
138     p_sys->b_kfir = VLC_FALSE;
139     int fd = p_sys->fd = -1;
140
141     if (!strcasecmp (p_access->psz_access, "stream"))
142     {
143         p_sys->b_seekable = VLC_FALSE;
144         p_sys->b_pace_control = VLC_FALSE;
145     }
146     else if (!strcasecmp (p_access->psz_access, "kfir"))
147     {
148         p_sys->b_seekable = VLC_FALSE;
149         p_sys->b_pace_control = VLC_FALSE;
150         p_sys->b_kfir = VLC_TRUE;
151     }
152     else
153     {
154         p_sys->b_seekable = VLC_TRUE;
155         p_sys->b_pace_control = VLC_TRUE;
156     }
157
158     /* Open file */
159     msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
160
161     if (b_stdin)
162         fd = dup (0);
163     else
164         fd = open_file (p_access, p_access->psz_path);
165
166 #ifdef HAVE_SYS_STAT_H
167     struct stat st;
168
169     while (fd != -1)
170     {
171         if (fstat (fd, &st))
172             msg_Err (p_access, "fstat(%d): %m", fd);
173         else
174         if (S_ISDIR (st.st_mode))
175             /* The directory plugin takes care of that */
176             msg_Dbg (p_access, "file is a directory, aborting");
177         else
178             break; // success
179
180         close (fd);
181         fd = -1;
182     }
183 #endif
184
185     if (fd == -1)
186     {
187         free (p_sys);
188         return VLC_EGENERIC;
189     }
190     p_sys->fd = fd;
191
192 #ifdef HAVE_SYS_STAT_H
193     p_access->info.i_size = st.st_size;
194     if (!S_ISREG (st.st_mode)
195      && !S_ISBLK (st.st_mode)
196      && !S_ISCHR (st.st_mode))
197         p_sys->b_seekable = VLC_FALSE;
198 #else
199     p_sys->b_seekable = !b_stdin;
200 # warning File size not known!
201 #endif
202
203     return VLC_SUCCESS;
204 }
205
206 /*****************************************************************************
207  * Close: close the target
208  *****************************************************************************/
209 static void Close (vlc_object_t * p_this)
210 {
211     access_t     *p_access = (access_t*)p_this;
212     access_sys_t *p_sys = p_access->p_sys;
213
214     close (p_sys->fd);
215     free (p_sys);
216 }
217
218 /*****************************************************************************
219  * Read: standard read on a file descriptor.
220  *****************************************************************************/
221 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
222 {
223     access_sys_t *p_sys = p_access->p_sys;
224     ssize_t i_ret;
225     int fd = p_sys->fd;
226
227 #if !defined(WIN32) && !defined(UNDER_CE)
228     if( !p_sys->b_pace_control )
229     {
230         if( !p_sys->b_kfir )
231         {
232             /* Find if some data is available. This won't work under Windows. */
233             do
234             {
235                 struct pollfd ufd;
236
237                 if( p_access->b_die )
238                     return 0;
239
240                 memset (&ufd, 0, sizeof (ufd));
241                 ufd.fd = fd;
242                 ufd.events = POLLIN;
243
244                 i_ret = poll (&ufd, 1, 500);
245             }
246             while (i_ret <= 0);
247
248             i_ret = read (fd, p_buffer, i_len);
249         }
250         else
251         {
252             /* b_kfir ; work around a buggy poll() driver implementation */
253             while (((i_ret = read (fd, p_buffer, i_len)) == 0)
254                 && !p_access->b_die)
255             {
256                 msleep( INPUT_ERROR_SLEEP );
257             }
258         }
259     }
260     else
261 #endif /* WIN32 || UNDER_CE */
262         /* b_pace_control || WIN32 */
263         i_ret = read( fd, p_buffer, i_len );
264
265     if( i_ret < 0 )
266     {
267         switch (errno)
268         {
269             case EINTR:
270             case EAGAIN:
271                 break;
272
273             default:
274                 msg_Err (p_access, "read failed (%m)");
275                 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
276                                 _("VLC could not read the file."));
277         }
278
279         /* Delay a bit to avoid consuming all the CPU. This is particularly
280          * useful when reading from an unconnected FIFO. */
281         msleep( INPUT_ERROR_SLEEP );
282     }
283
284     p_sys->i_nb_reads++;
285
286 #ifdef HAVE_SYS_STAT_H
287     if( p_access->info.i_size != 0 &&
288         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
289     {
290         struct stat st;
291
292         if ((fstat (fd, &st) == 0)
293          && (p_access->info.i_size != st.st_size))
294         {
295             p_access->info.i_size = st.st_size;
296             p_access->info.i_update |= INPUT_UPDATE_SIZE;
297         }
298     }
299 #endif
300
301     if( i_ret > 0 )
302         p_access->info.i_pos += i_ret;
303     else if( i_ret == 0 )
304         p_access->info.b_eof = VLC_TRUE;
305
306     return i_ret;
307 }
308
309
310 /*****************************************************************************
311  * Seek: seek to a specific location in a file
312  *****************************************************************************/
313 static int Seek (access_t *p_access, int64_t i_pos)
314 {
315     p_access->info.i_pos = i_pos;
316     p_access->info.b_eof = VLC_FALSE;
317
318     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
319     return VLC_SUCCESS;
320 }
321
322 /*****************************************************************************
323  * Control:
324  *****************************************************************************/
325 static int Control( access_t *p_access, int i_query, va_list args )
326 {
327     access_sys_t *p_sys = p_access->p_sys;
328     vlc_bool_t   *pb_bool;
329     int          *pi_int;
330     int64_t      *pi_64;
331
332     switch( i_query )
333     {
334         /* */
335         case ACCESS_CAN_SEEK:
336         case ACCESS_CAN_FASTSEEK:
337             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
338             *pb_bool = p_sys->b_seekable;
339             break;
340
341         case ACCESS_CAN_PAUSE:
342         case ACCESS_CAN_CONTROL_PACE:
343             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
344             *pb_bool = p_sys->b_pace_control;
345             break;
346
347         /* */
348         case ACCESS_GET_MTU:
349             pi_int = (int*)va_arg( args, int * );
350             *pi_int = 0;
351             break;
352
353         case ACCESS_GET_PTS_DELAY:
354             pi_64 = (int64_t*)va_arg( args, int64_t * );
355             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
356             break;
357
358         /* */
359         case ACCESS_SET_PAUSE_STATE:
360             /* Nothing to do */
361             break;
362
363         case ACCESS_GET_TITLE_INFO:
364         case ACCESS_SET_TITLE:
365         case ACCESS_SET_SEEKPOINT:
366         case ACCESS_SET_PRIVATE_ID_STATE:
367         case ACCESS_GET_META:
368         case ACCESS_GET_PRIVATE_ID_STATE:
369         case ACCESS_GET_CONTENT_TYPE:
370             return VLC_EGENERIC;
371
372         default:
373             msg_Warn( p_access, "unimplemented query %d in control", i_query );
374             return VLC_EGENERIC;
375
376     }
377     return VLC_SUCCESS;
378 }
379
380
381 static char *expand_path (const access_t *p_access, const char *path)
382 {
383     if (strncmp (path, "~/", 2) == 0)
384     {
385         char *res;
386
387          // TODO: we should also support the ~cmassiot/ syntax
388          if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
389              return NULL;
390          return res;
391     }
392
393 #if defined(WIN32)
394     if (!strcasecmp (p_access->psz_access, "file")
395       && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
396         // Explorer can open path such as file:/C:/ or file:///C:/
397         // hence remove leading / if found
398         return strdup (path + 1);
399 #endif
400
401     return strdup (path);
402 }
403
404
405 /*****************************************************************************
406  * open_file: Opens a specific file
407  *****************************************************************************/
408 static int open_file (access_t *p_access, const char *psz_name)
409 {
410     char *path = expand_path (p_access, psz_name);
411
412 #ifdef UNDER_CE
413     p_sys->fd = utf8_fopen( path, "rb" );
414     if ( !p_sys->fd )
415     {
416         msg_Err( p_access, "cannot open file %s", psz_name );
417         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
418                         _("VLC could not open the file \"%s\"."), psz_name );
419         free (path);
420         return VLC_EGENERIC;
421     }
422
423     fseek( p_sys->fd, 0, SEEK_END );
424     p_access->info.i_size = ftell( p_sys->fd );
425     p_access->info.i_update |= INPUT_UPDATE_SIZE;
426     fseek( p_sys->fd, 0, SEEK_SET );
427 #else
428     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
429     free (path);
430     if (fd == -1)
431     {
432         msg_Err (p_access, "cannot open file %s (%m)", psz_name);
433         intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
434                         _("VLC could not open the file \"%s\"."), psz_name);
435         return -1;
436     }
437
438 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
439     /* We'd rather use any available memory for reading ahead
440      * than for caching what we've already seen/heard */
441     fcntl (fd, F_RDAHEAD, 1);
442     fcntl (fd, F_NOCACHE, 1);
443 # endif
444 #endif
445
446     return fd;
447 }