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