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