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