]> git.sesse.net Git - vlc/blob - modules/access/file.c
Read-ahead only makes sense for seekable file descriptors
[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_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
38
39 #include <assert.h>
40 #include <errno.h>
41 #ifdef HAVE_SYS_TYPES_H
42 #   include <sys/types.h>
43 #endif
44 #ifdef HAVE_SYS_STAT_H
45 #   include <sys/stat.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #   include <fcntl.h>
49 #endif
50 #if defined (__linux__)
51 #   include <sys/vfs.h>
52 #ifdef HAVE_LINUX_MAGIC_H
53 #   include <linux/magic.h>
54 #endif
55 #elif defined (HAVE_SYS_MOUNT_H)
56 #   include <sys/param.h>
57 #   include <sys/mount.h>
58 #endif
59
60 #if defined( WIN32 )
61 #   include <io.h>
62 #   include <ctype.h>
63 #   include <shlwapi.h>
64 #else
65 #   include <unistd.h>
66 #   include <poll.h>
67 #endif
68
69 #if defined( WIN32 ) && !defined( UNDER_CE )
70 #   ifdef lseek
71 #      undef lseek
72 #   endif
73 #   define lseek _lseeki64
74 #elif defined( UNDER_CE )
75 /* FIXME the commandline on wince is a mess */
76 # define dup(a) -1
77 # define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
78 #endif
79
80 #include <vlc_charset.h>
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 static int  Open ( vlc_object_t * );
86 static void Close( vlc_object_t * );
87
88 #define CACHING_TEXT N_("Caching value (ms)")
89 #define CACHING_LONGTEXT N_( \
90     "Caching value for files, in milliseconds." )
91
92 #define NETWORK_CACHING_TEXT N_("Extra network caching value (ms)")
93 #define NETWORK_CACHING_LONGTEXT N_( \
94     "Supplementary caching value for remote files, in milliseconds." )
95
96 vlc_module_begin ()
97     set_description( N_("File input") )
98     set_shortname( N_("File") )
99     set_category( CAT_INPUT )
100     set_subcategory( SUBCAT_INPUT_ACCESS )
101     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL,
102                  CACHING_TEXT, CACHING_LONGTEXT, true )
103         change_safe()
104     add_integer( "network-caching", 3 * DEFAULT_PTS_DELAY / 1000, NULL,
105                  NETWORK_CACHING_TEXT, NETWORK_CACHING_LONGTEXT, true )
106         change_safe()
107     add_obsolete_string( "file-cat" )
108     set_capability( "access", 50 )
109     add_shortcut( "file" )
110     add_shortcut( "fd" )
111     add_shortcut( "stream" )
112     set_callbacks( Open, Close )
113 vlc_module_end ()
114
115
116 /*****************************************************************************
117  * Exported prototypes
118  *****************************************************************************/
119 static int  Seek( access_t *, int64_t );
120 static int  NoSeek( access_t *, int64_t );
121 static ssize_t Read( access_t *, uint8_t *, size_t );
122 static int  Control( access_t *, int, va_list );
123
124 static int  open_file( access_t *, const char * );
125
126 struct access_sys_t
127 {
128     unsigned int i_nb_reads;
129
130     int fd;
131
132     /* */
133     unsigned caching;
134     bool b_pace_control;
135 };
136
137 #ifndef WIN32
138 static bool IsRemote (int fd)
139 {
140 #ifdef HAVE_FSTATFS
141     struct statfs stf;
142
143     if (fstatfs (fd, &stf))
144         return false;
145
146 #if defined(MNT_LOCAL)
147     return !(stf.f_flags & MNT_LOCAL);
148
149 #else
150 #   ifdef HAVE_LINUX_MAGIC_H
151     switch (stf.f_type)
152     {
153         case AFS_SUPER_MAGIC:
154         case CODA_SUPER_MAGIC:
155         case NCP_SUPER_MAGIC:
156         case NFS_SUPER_MAGIC:
157         case SMB_SUPER_MAGIC:
158         case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
159             return true;
160     }
161     return false;
162 #   endif
163 #endif
164 #else /* !HAVE_FSTATFS */
165     (void)fd;
166     return false;
167
168 #endif
169 }
170 #endif
171
172 #ifndef HAVE_POSIX_FADVISE
173 # define posix_fadvise(fd, off, len, adv)
174 #endif
175
176 /*****************************************************************************
177  * Open: open the file
178  *****************************************************************************/
179 static int Open( vlc_object_t *p_this )
180 {
181     access_t     *p_access = (access_t*)p_this;
182     access_sys_t *p_sys;
183 #ifdef WIN32
184     wchar_t wpath[MAX_PATH+1];
185     bool is_remote = false;
186 #endif
187
188     STANDARD_READ_ACCESS_INIT;
189     p_sys->i_nb_reads = 0;
190     p_sys->b_pace_control = true;
191
192     /* Open file */
193     int fd = -1;
194
195     if (!strcasecmp (p_access->psz_access, "fd"))
196         fd = dup (atoi (p_access->psz_path));
197     else if (!strcmp (p_access->psz_path, "-"))
198         fd = dup (0);
199     else
200     {
201         msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
202         fd = open_file (p_access, p_access->psz_path);
203 #ifdef WIN32
204         if (MultiByteToWideChar (CP_UTF8, 0, p_access->psz_path, -1,
205                                  wpath, MAX_PATH)
206          && PathIsNetworkPathW (wpath))
207             is_remote = true;
208 # define IsRemote( fd ) ((void)fd, is_remote)
209 #endif
210     }
211     if (fd == -1)
212         goto error;
213
214 #ifdef HAVE_SYS_STAT_H
215     struct stat st;
216
217     if (fstat (fd, &st))
218     {
219         msg_Err (p_access, "failed to read (%m)");
220         goto error;
221     }
222     /* Directories can be opened and read from, but only readdir() knows
223      * how to parse the data. The directory plugin will do it. */
224     if (S_ISDIR (st.st_mode))
225     {
226         msg_Dbg (p_access, "ignoring directory");
227         goto error;
228     }
229     if (S_ISREG (st.st_mode))
230         p_access->info.i_size = st.st_size;
231     else if (!S_ISBLK (st.st_mode))
232     {
233         p_access->pf_seek = NoSeek;
234         p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
235     }
236 #else
237 # warning File size not known!
238 #endif
239
240     p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
241     if (IsRemote(fd))
242         p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
243
244     p_sys->fd = fd;
245
246     if (p_access->pf_seek != NoSeek)
247     {
248         /* Demuxers will need the beginning of the file for probing. */
249         posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
250         /* In most cases, we only read the file once. */
251         posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
252 #if defined(HAVE_FCNTL)
253         /* We'd rather use any available memory for reading ahead
254          * than for caching what we've already seen/heard */
255 # if defined(F_RDAHEAD)
256         fcntl (fd, F_RDAHEAD, 1);
257 # endif
258 # if defined(F_NOCACHE)
259         fcntl (fd, F_NOCACHE, 1);
260 # endif
261 #endif
262     }
263     return VLC_SUCCESS;
264
265 error:
266     if (fd != -1)
267         close (fd);
268     free (p_sys);
269     return VLC_EGENERIC;
270 }
271
272 /*****************************************************************************
273  * Close: close the target
274  *****************************************************************************/
275 static void Close (vlc_object_t * p_this)
276 {
277     access_t     *p_access = (access_t*)p_this;
278     access_sys_t *p_sys = p_access->p_sys;
279
280     close (p_sys->fd);
281     free (p_sys);
282 }
283
284
285 #include <vlc_network.h>
286
287 /*****************************************************************************
288  * Read: standard read on a file descriptor.
289  *****************************************************************************/
290 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
291 {
292     access_sys_t *p_sys = p_access->p_sys;
293     int fd = p_sys->fd;
294     ssize_t i_ret;
295
296 #ifndef WIN32
297     if (p_access->pf_seek == NoSeek)
298         i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
299     else
300 #endif
301         i_ret = read (fd, p_buffer, i_len);
302
303     if( i_ret < 0 )
304     {
305         switch (errno)
306         {
307             case EINTR:
308             case EAGAIN:
309                 break;
310
311             default:
312                 msg_Err (p_access, "failed to read (%m)");
313                 dialog_Fatal (p_access, _("File reading failed"), "%s",
314                               _("VLC could not read the file."));
315                 p_access->info.b_eof = true;
316                 return 0;
317         }
318     }
319     else if( i_ret > 0 )
320         p_access->info.i_pos += i_ret;
321     else
322         p_access->info.b_eof = true;
323
324     p_sys->i_nb_reads++;
325
326     if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
327      || (p_access->info.i_pos > p_access->info.i_size))
328     {
329 #ifdef HAVE_SYS_STAT_H
330         struct stat st;
331
332         if ((fstat (fd, &st) == 0)
333          && (p_access->info.i_size != st.st_size))
334         {
335             p_access->info.i_size = st.st_size;
336             p_access->info.i_update |= INPUT_UPDATE_SIZE;
337         }
338 #endif
339     }
340     return i_ret;
341 }
342
343
344 /*****************************************************************************
345  * Seek: seek to a specific location in a file
346  *****************************************************************************/
347 static int Seek (access_t *p_access, int64_t i_pos)
348 {
349     p_access->info.i_pos = i_pos;
350     p_access->info.b_eof = false;
351
352     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
353     return VLC_SUCCESS;
354 }
355
356 static int NoSeek (access_t *p_access, int64_t i_pos)
357 {
358     /* assert(0); ?? */
359     (void) p_access; (void) i_pos;
360     return VLC_EGENERIC;
361 }
362
363 /*****************************************************************************
364  * Control:
365  *****************************************************************************/
366 static int Control( access_t *p_access, int i_query, va_list args )
367 {
368     access_sys_t *p_sys = p_access->p_sys;
369     bool    *pb_bool;
370     int64_t *pi_64;
371
372     switch( i_query )
373     {
374         /* */
375         case ACCESS_CAN_SEEK:
376         case ACCESS_CAN_FASTSEEK:
377             pb_bool = (bool*)va_arg( args, bool* );
378             *pb_bool = (p_access->pf_seek != NoSeek);
379             break;
380
381         case ACCESS_CAN_PAUSE:
382         case ACCESS_CAN_CONTROL_PACE:
383             pb_bool = (bool*)va_arg( args, bool* );
384             *pb_bool = p_sys->b_pace_control;
385             break;
386
387         /* */
388         case ACCESS_GET_PTS_DELAY:
389             pi_64 = (int64_t*)va_arg( args, int64_t * );
390             *pi_64 = p_sys->caching * INT64_C(1000);
391             break;
392
393         /* */
394         case ACCESS_SET_PAUSE_STATE:
395             /* Nothing to do */
396             break;
397
398         case ACCESS_GET_TITLE_INFO:
399         case ACCESS_SET_TITLE:
400         case ACCESS_SET_SEEKPOINT:
401         case ACCESS_SET_PRIVATE_ID_STATE:
402         case ACCESS_GET_META:
403         case ACCESS_GET_PRIVATE_ID_STATE:
404         case ACCESS_GET_CONTENT_TYPE:
405             return VLC_EGENERIC;
406
407         default:
408             msg_Warn( p_access, "unimplemented query %d in control", i_query );
409             return VLC_EGENERIC;
410
411     }
412     return VLC_SUCCESS;
413 }
414
415 /*****************************************************************************
416  * open_file: Opens a specific file
417  *****************************************************************************/
418 static int open_file (access_t *p_access, const char *path)
419 {
420     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK);
421     if (fd == -1)
422     {
423         msg_Err (p_access, "cannot open file %s (%m)", path);
424         dialog_Fatal (p_access, _("File reading failed"),
425                       _("VLC could not open the file \"%s\"."), path);
426         return -1;
427     }
428
429     return fd;
430 }