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