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