]> git.sesse.net Git - vlc/blob - modules/access/file.c
v4l2 access: setup the picture format (like the v4l2 demux)
[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 #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 /*****************************************************************************
134  * FileOpen: open the file
135  *****************************************************************************/
136 int FileOpen( vlc_object_t *p_this )
137 {
138     access_t     *p_access = (access_t*)p_this;
139
140     /* Open file */
141     int fd = -1;
142
143     if (!strcasecmp (p_access->psz_access, "fd"))
144     {
145         char *end;
146         int oldfd = strtol (p_access->psz_location, &end, 10);
147
148         if (*end == '\0')
149             fd = vlc_dup (oldfd);
150         else if (*end == '/' && end > p_access->psz_location)
151         {
152             char *name = decode_URI_duplicate (end - 1);
153             if (name != NULL)
154             {
155                 name[0] = '.';
156                 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
157                 free (name);
158             }
159         }
160     }
161     else
162     {
163         const char *path = p_access->psz_filepath;
164
165         if (unlikely(path == NULL))
166             return VLC_EGENERIC;
167         msg_Dbg (p_access, "opening file `%s'", path);
168         fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
169         if (fd == -1)
170         {
171             msg_Err (p_access, "cannot open file %s (%m)", path);
172             dialog_Fatal (p_access, _("File reading failed"),
173                           _("VLC could not open the file \"%s\". (%m)"), path);
174         }
175     }
176     if (fd == -1)
177         return VLC_EGENERIC;
178
179     struct stat st;
180     if (fstat (fd, &st))
181     {
182         msg_Err (p_access, "failed to read (%m)");
183         goto error;
184     }
185
186 #if O_NONBLOCK
187     int flags = fcntl (fd, F_GETFL);
188     if (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode))
189         /* Force non-blocking mode where applicable (fd://) */
190         flags |= O_NONBLOCK;
191     else
192         /* Force blocking mode when not useful or not specified */
193         flags &= ~O_NONBLOCK;
194     fcntl (fd, F_SETFL, flags);
195 #endif
196
197     /* Directories can be opened and read from, but only readdir() knows
198      * how to parse the data. The directory plugin will do it. */
199     if (S_ISDIR (st.st_mode))
200     {
201 #ifdef HAVE_FDOPENDIR
202         DIR *handle = fdopendir (fd);
203         if (handle == NULL)
204             goto error; /* Uh? */
205         return DirInit (p_access, handle);
206 #else
207         msg_Dbg (p_access, "ignoring directory");
208         goto error;
209 #endif
210     }
211
212     access_sys_t *p_sys = malloc (sizeof (*p_sys));
213     if (unlikely(p_sys == NULL))
214         goto error;
215     access_InitFields (p_access);
216     p_access->pf_read = FileRead;
217     p_access->pf_block = NULL;
218     p_access->pf_control = FileControl;
219     p_access->pf_seek = FileSeek;
220     p_access->p_sys = p_sys;
221     p_sys->i_nb_reads = 0;
222     p_sys->fd = fd;
223     p_sys->b_pace_control = true;
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
233     if (p_access->pf_seek != NoSeek)
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     return VLC_SUCCESS;
251
252 error:
253     close (fd);
254     return VLC_EGENERIC;
255 }
256
257 /*****************************************************************************
258  * FileClose: close the target
259  *****************************************************************************/
260 void FileClose (vlc_object_t * p_this)
261 {
262     access_t     *p_access = (access_t*)p_this;
263
264     if (p_access->pf_read == NULL)
265     {
266         DirClose (p_this);
267         return;
268     }
269
270     access_sys_t *p_sys = p_access->p_sys;
271
272     close (p_sys->fd);
273     free (p_sys);
274 }
275
276
277 #include <vlc_network.h>
278
279 /*****************************************************************************
280  * Read: standard read on a file descriptor.
281  *****************************************************************************/
282 ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
283 {
284     access_sys_t *p_sys = p_access->p_sys;
285     int fd = p_sys->fd;
286     ssize_t i_ret;
287
288 #if !defined (WIN32) && !defined (__OS2__)
289     if (p_access->pf_seek == NoSeek)
290         i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
291     else
292 #endif
293         i_ret = read (fd, p_buffer, i_len);
294
295     if( i_ret < 0 )
296     {
297         switch (errno)
298         {
299             case EINTR:
300             case EAGAIN:
301                 break;
302
303             default:
304                 msg_Err (p_access, "failed to read (%m)");
305                 dialog_Fatal (p_access, _("File reading failed"),
306                               _("VLC could not read the file (%m)."));
307                 p_access->info.b_eof = true;
308                 return 0;
309         }
310     }
311     else if( i_ret > 0 )
312         p_access->info.i_pos += i_ret;
313     else
314         p_access->info.b_eof = true;
315
316     p_sys->i_nb_reads++;
317
318     if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
319      || (p_access->info.i_pos > p_access->info.i_size))
320     {
321         struct stat st;
322
323         if ((fstat (fd, &st) == 0)
324          && (p_access->info.i_size != (uint64_t)st.st_size))
325         {
326             p_access->info.i_size = st.st_size;
327             p_access->info.i_update |= INPUT_UPDATE_SIZE;
328         }
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 }