]> git.sesse.net Git - vlc/blob - modules/access/file.c
Mark srtp-key and srtp-salt safe
[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 #elif defined( __OS2__ )
62 #   include <ctype.h>
63 #else
64 #   include <unistd.h>
65 #endif
66 #include <dirent.h>
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 PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
76 #endif
77
78 #include <vlc_fs.h>
79 #include <vlc_url.h>
80
81 struct access_sys_t
82 {
83     unsigned int i_nb_reads;
84
85     int fd;
86
87     /* */
88     unsigned caching;
89     bool b_pace_control;
90 };
91
92 #if !defined (WIN32) && !defined (__OS2__)
93 static bool IsRemote (int fd)
94 {
95 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
96     struct statvfs stf;
97
98     if (fstatvfs (fd, &stf))
99         return false;
100     /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
101     return !(stf.f_flag & MNT_LOCAL);
102
103 #elif defined (HAVE_LINUX_MAGIC_H)
104     struct statfs stf;
105
106     if (fstatfs (fd, &stf))
107         return false;
108
109     switch ((unsigned long)stf.f_type)
110     {
111         case AFS_SUPER_MAGIC:
112         case CODA_SUPER_MAGIC:
113         case NCP_SUPER_MAGIC:
114         case NFS_SUPER_MAGIC:
115         case SMB_SUPER_MAGIC:
116         case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
117             return true;
118     }
119     return false;
120
121 #else
122     (void)fd;
123     return false;
124
125 #endif
126 }
127 #endif
128
129 #ifndef HAVE_POSIX_FADVISE
130 # define posix_fadvise(fd, off, len, adv)
131 #endif
132
133 /*****************************************************************************
134  * Open: open the file
135  *****************************************************************************/
136 int Open( vlc_object_t *p_this )
137 {
138     access_t     *p_access = (access_t*)p_this;
139 #ifdef WIN32
140     bool is_remote = false;
141 #endif
142
143     /* Open file */
144     int fd = -1;
145
146     if (!strcasecmp (p_access->psz_access, "fd"))
147     {
148         char *end;
149         int oldfd = strtol (p_access->psz_location, &end, 10);
150
151         if (*end == '\0')
152             fd = vlc_dup (oldfd);
153         else if (*end == '/' && end > p_access->psz_location)
154         {
155             char *name = decode_URI_duplicate (end - 1);
156             if (name != NULL)
157             {
158                 name[0] = '.';
159                 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
160                 free (name);
161             }
162         }
163     }
164     else
165     {
166         const char *path = p_access->psz_filepath;
167
168         msg_Dbg (p_access, "opening file `%s'", path);
169         fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
170         if (fd == -1)
171         {
172             msg_Err (p_access, "cannot open file %s (%m)", path);
173             dialog_Fatal (p_access, _("File reading failed"),
174                           _("VLC could not open the file \"%s\"."), path);
175         }
176
177 #ifdef WIN32
178         wchar_t wpath[MAX_PATH+1];
179         if (MultiByteToWideChar (CP_UTF8, 0, path, -1,
180                                  wpath, MAX_PATH)
181          && PathIsNetworkPathW (wpath))
182             is_remote = true;
183 # define IsRemote( fd ) ((void)fd, is_remote)
184 #endif
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->caching = var_InheritInteger (p_access, "file-caching");
222     if (IsRemote(fd))
223         p_sys->caching += var_InheritInteger (p_access, "network-caching");
224     p_sys->b_pace_control = true;
225
226     if (S_ISREG (st.st_mode))
227         p_access->info.i_size = st.st_size;
228     else if (!S_ISBLK (st.st_mode))
229     {
230         p_access->pf_seek = NoSeek;
231         p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
232     }
233
234     if (p_access->pf_seek != NoSeek)
235     {
236         /* Demuxers will need the beginning of the file for probing. */
237         posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
238         /* In most cases, we only read the file once. */
239         posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
240 #if defined(HAVE_FCNTL)
241         /* We'd rather use any available memory for reading ahead
242          * than for caching what we've already seen/heard */
243 # if defined(F_RDAHEAD)
244         fcntl (fd, F_RDAHEAD, 1);
245 # endif
246 # if defined(F_NOCACHE)
247         fcntl (fd, F_NOCACHE, 1);
248 # endif
249 #endif
250     }
251     return VLC_SUCCESS;
252
253 error:
254     close (fd);
255     return VLC_EGENERIC;
256 }
257
258 /*****************************************************************************
259  * Close: close the target
260  *****************************************************************************/
261 void Close (vlc_object_t * p_this)
262 {
263     access_t     *p_access = (access_t*)p_this;
264
265     if (p_access->pf_read == NULL)
266     {
267         DirClose (p_this);
268         return;
269     }
270
271     access_sys_t *p_sys = p_access->p_sys;
272
273     close (p_sys->fd);
274     free (p_sys);
275 }
276
277
278 #include <vlc_network.h>
279
280 /*****************************************************************************
281  * Read: standard read on a file descriptor.
282  *****************************************************************************/
283 ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
284 {
285     access_sys_t *p_sys = p_access->p_sys;
286     int fd = p_sys->fd;
287     ssize_t i_ret;
288
289 #if !defined (WIN32) && !defined (__OS2__)
290     if (p_access->pf_seek == NoSeek)
291         i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
292     else
293 #endif
294         i_ret = read (fd, p_buffer, i_len);
295
296     if( i_ret < 0 )
297     {
298         switch (errno)
299         {
300             case EINTR:
301             case EAGAIN:
302                 break;
303
304             default:
305                 msg_Err (p_access, "failed to read (%m)");
306                 dialog_Fatal (p_access, _("File reading failed"), "%s",
307                               _("VLC could not read the file."));
308                 p_access->info.b_eof = true;
309                 return 0;
310         }
311     }
312     else if( i_ret > 0 )
313         p_access->info.i_pos += i_ret;
314     else
315         p_access->info.b_eof = true;
316
317     p_sys->i_nb_reads++;
318
319     if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
320      || (p_access->info.i_pos > p_access->info.i_size))
321     {
322 #ifdef HAVE_SYS_STAT_H
323         struct stat st;
324
325         if ((fstat (fd, &st) == 0)
326          && (p_access->info.i_size != (uint64_t)st.st_size))
327         {
328             p_access->info.i_size = st.st_size;
329             p_access->info.i_update |= INPUT_UPDATE_SIZE;
330         }
331 #endif
332     }
333     return i_ret;
334 }
335
336
337 /*****************************************************************************
338  * Seek: seek to a specific location in a file
339  *****************************************************************************/
340 int FileSeek (access_t *p_access, uint64_t i_pos)
341 {
342     p_access->info.i_pos = i_pos;
343     p_access->info.b_eof = false;
344
345     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
346     return VLC_SUCCESS;
347 }
348
349 int NoSeek (access_t *p_access, uint64_t i_pos)
350 {
351     /* assert(0); ?? */
352     (void) p_access; (void) i_pos;
353     return VLC_EGENERIC;
354 }
355
356 /*****************************************************************************
357  * Control:
358  *****************************************************************************/
359 int FileControl( access_t *p_access, int i_query, va_list args )
360 {
361     access_sys_t *p_sys = p_access->p_sys;
362     bool    *pb_bool;
363     int64_t *pi_64;
364
365     switch( i_query )
366     {
367         /* */
368         case ACCESS_CAN_SEEK:
369         case ACCESS_CAN_FASTSEEK:
370             pb_bool = (bool*)va_arg( args, bool* );
371             *pb_bool = (p_access->pf_seek != NoSeek);
372             break;
373
374         case ACCESS_CAN_PAUSE:
375         case ACCESS_CAN_CONTROL_PACE:
376             pb_bool = (bool*)va_arg( args, bool* );
377             *pb_bool = p_sys->b_pace_control;
378             break;
379
380         /* */
381         case ACCESS_GET_PTS_DELAY:
382             pi_64 = (int64_t*)va_arg( args, int64_t * );
383             *pi_64 = p_sys->caching * INT64_C(1000);
384             break;
385
386         /* */
387         case ACCESS_SET_PAUSE_STATE:
388             /* Nothing to do */
389             break;
390
391         case ACCESS_GET_TITLE_INFO:
392         case ACCESS_SET_TITLE:
393         case ACCESS_SET_SEEKPOINT:
394         case ACCESS_SET_PRIVATE_ID_STATE:
395         case ACCESS_GET_META:
396         case ACCESS_GET_PRIVATE_ID_STATE:
397         case ACCESS_GET_CONTENT_TYPE:
398             return VLC_EGENERIC;
399
400         default:
401             msg_Warn( p_access, "unimplemented query %d in control", i_query );
402             return VLC_EGENERIC;
403
404     }
405     return VLC_SUCCESS;
406 }