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