]> git.sesse.net Git - vlc/blob - modules/access/file.c
file access: don't use PathIsNetworkPath in store app
[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     int fd;
70
71     /* */
72     bool b_pace_control;
73 };
74
75 #if !defined (WIN32) && !defined (__OS2__)
76 static bool IsRemote (int fd)
77 {
78 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
79     struct statvfs stf;
80
81     if (fstatvfs (fd, &stf))
82         return false;
83     /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
84     return !(stf.f_flag & MNT_LOCAL);
85
86 #elif defined (HAVE_LINUX_MAGIC_H)
87     struct statfs stf;
88
89     if (fstatfs (fd, &stf))
90         return false;
91
92     switch ((unsigned long)stf.f_type)
93     {
94         case AFS_SUPER_MAGIC:
95         case CODA_SUPER_MAGIC:
96         case NCP_SUPER_MAGIC:
97         case NFS_SUPER_MAGIC:
98         case SMB_SUPER_MAGIC:
99         case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
100             return true;
101     }
102     return false;
103
104 #else
105     (void)fd;
106     return false;
107
108 #endif
109 }
110 # define IsRemote(fd,path) IsRemote(fd)
111
112 #else /* WIN32 || __OS2__ */
113 static bool IsRemote (const char *path)
114 {
115 # if !defined(__OS2__) && !defined(WINAPI_FAMILY_APP)
116     wchar_t *wpath = ToWide (path);
117     bool is_remote = (wpath != NULL && PathIsNetworkPathW (wpath));
118     free (wpath);
119     return is_remote;
120 # else
121     return (! strncmp(path, "\\\\", 2));
122 # endif
123 }
124 # define IsRemote(fd,path) IsRemote(path)
125 #endif
126
127 #ifndef HAVE_POSIX_FADVISE
128 # define posix_fadvise(fd, off, len, adv)
129 #endif
130
131 static ssize_t FileRead (access_t *, uint8_t *, size_t);
132 static int FileSeek (access_t *, uint64_t);
133 static ssize_t StreamRead (access_t *, uint8_t *, size_t);
134 static int NoSeek (access_t *, uint64_t);
135 static int FileControl (access_t *, int, va_list);
136
137 /*****************************************************************************
138  * FileOpen: open the file
139  *****************************************************************************/
140 int FileOpen( vlc_object_t *p_this )
141 {
142     access_t     *p_access = (access_t*)p_this;
143
144     /* Open file */
145     int fd = -1;
146
147     if (!strcasecmp (p_access->psz_access, "fd"))
148     {
149         char *end;
150         int oldfd = strtol (p_access->psz_location, &end, 10);
151
152         if (*end == '\0')
153             fd = vlc_dup (oldfd);
154         else if (*end == '/' && end > p_access->psz_location)
155         {
156             char *name = decode_URI_duplicate (end - 1);
157             if (name != NULL)
158             {
159                 name[0] = '.';
160                 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
161                 free (name);
162             }
163         }
164     }
165     else
166     {
167         const char *path = p_access->psz_filepath;
168
169         if (unlikely(path == NULL))
170             return VLC_EGENERIC;
171         msg_Dbg (p_access, "opening file `%s'", path);
172         fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
173         if (fd == -1)
174         {
175             msg_Err (p_access, "cannot open file %s (%m)", path);
176             dialog_Fatal (p_access, _("File reading failed"),
177                           _("VLC could not open the file \"%s\" (%m)."), path);
178         }
179     }
180     if (fd == -1)
181         return VLC_EGENERIC;
182
183     struct stat st;
184     if (fstat (fd, &st))
185     {
186         msg_Err (p_access, "failed to read (%m)");
187         goto error;
188     }
189
190 #if O_NONBLOCK
191     int flags = fcntl (fd, F_GETFL);
192     if (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode))
193         /* Force non-blocking mode where applicable (fd://) */
194         flags |= O_NONBLOCK;
195     else
196         /* Force blocking mode when not useful or not specified */
197         flags &= ~O_NONBLOCK;
198     fcntl (fd, F_SETFL, flags);
199 #endif
200
201     /* Directories can be opened and read from, but only readdir() knows
202      * how to parse the data. The directory plugin will do it. */
203     if (S_ISDIR (st.st_mode))
204     {
205 #ifdef HAVE_FDOPENDIR
206         DIR *handle = fdopendir (fd);
207         if (handle == NULL)
208             goto error; /* Uh? */
209         return DirInit (p_access, handle);
210 #else
211         msg_Dbg (p_access, "ignoring directory");
212         goto error;
213 #endif
214     }
215
216     access_sys_t *p_sys = malloc (sizeof (*p_sys));
217     if (unlikely(p_sys == NULL))
218         goto error;
219     access_InitFields (p_access);
220     p_access->pf_block = NULL;
221     p_access->pf_control = FileControl;
222     p_access->p_sys = p_sys;
223     p_sys->fd = fd;
224
225     if (S_ISREG (st.st_mode) || S_ISBLK (st.st_mode))
226     {
227         p_access->pf_read = FileRead;
228         p_access->pf_seek = FileSeek;
229         p_access->info.i_size = st.st_size;
230         p_sys->b_pace_control = true;
231
232         /* Demuxers will need the beginning of the file for probing. */
233         posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
234         /* In most cases, we only read the file once. */
235         posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
236 #ifdef F_RDAHEAD
237         fcntl (fd, F_RDAHEAD, 1);
238 #endif
239 #ifdef F_NOCACHE
240         fcntl (fd, F_NOCACHE, 0);
241 #endif
242     }
243     else
244     {
245         p_access->pf_read = StreamRead;
246         p_access->pf_seek = NoSeek;
247         p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
248     }
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  * Reads from a regular file.
281  */
282 static 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 val = read (fd, p_buffer, i_len);
287
288     if (val < 0)
289     {
290         switch (errno)
291         {
292             case EINTR:
293             case EAGAIN:
294                 return -1;
295         }
296
297         msg_Err (p_access, "read error: %m");
298         dialog_Fatal (p_access, _("File reading failed"),
299                       _("VLC could not read the file (%m)."));
300         val = 0;
301     }
302
303     p_access->info.i_pos += val;
304     p_access->info.b_eof = !val;
305     if (p_access->info.i_pos >= p_access->info.i_size)
306     {
307         struct stat st;
308
309         if (fstat (fd, &st) == 0)
310             p_access->info.i_size = st.st_size;
311     }
312     return val;
313 }
314
315
316 /*****************************************************************************
317  * Seek: seek to a specific location in a file
318  *****************************************************************************/
319 static int FileSeek (access_t *p_access, uint64_t i_pos)
320 {
321     p_access->info.i_pos = i_pos;
322     p_access->info.b_eof = false;
323
324     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
325     return VLC_SUCCESS;
326 }
327
328 /**
329  * Reads from a non-seekable file.
330  */
331 static ssize_t StreamRead (access_t *p_access, uint8_t *p_buffer, size_t i_len)
332 {
333     access_sys_t *p_sys = p_access->p_sys;
334     int fd = p_sys->fd;
335
336 #if !defined (WIN32) && !defined (__OS2__)
337     ssize_t val = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
338 #else
339     ssize_t val = read (fd, p_buffer, i_len);
340 #endif
341
342     if (val < 0)
343     {
344         switch (errno)
345         {
346             case EINTR:
347             case EAGAIN:
348                 return -1;
349         }
350         msg_Err (p_access, "read error: %m");
351         val = 0;
352     }
353
354     p_access->info.i_pos += val;
355     p_access->info.b_eof = !val;
356     return val;
357 }
358
359 static int NoSeek (access_t *p_access, uint64_t i_pos)
360 {
361     /* assert(0); ?? */
362     (void) p_access; (void) i_pos;
363     return VLC_EGENERIC;
364 }
365
366 /*****************************************************************************
367  * Control:
368  *****************************************************************************/
369 static int FileControl( access_t *p_access, int i_query, va_list args )
370 {
371     access_sys_t *p_sys = p_access->p_sys;
372     bool    *pb_bool;
373     int64_t *pi_64;
374
375     switch( i_query )
376     {
377         /* */
378         case ACCESS_CAN_SEEK:
379         case ACCESS_CAN_FASTSEEK:
380             pb_bool = (bool*)va_arg( args, bool* );
381             *pb_bool = (p_access->pf_seek != NoSeek);
382             break;
383
384         case ACCESS_CAN_PAUSE:
385         case ACCESS_CAN_CONTROL_PACE:
386             pb_bool = (bool*)va_arg( args, bool* );
387             *pb_bool = p_sys->b_pace_control;
388             break;
389
390         /* */
391         case ACCESS_GET_PTS_DELAY:
392             pi_64 = (int64_t*)va_arg( args, int64_t * );
393             if (IsRemote (p_sys->fd, p_access->psz_filepath))
394                 *pi_64 = var_InheritInteger (p_access, "network-caching");
395             else
396                 *pi_64 = var_InheritInteger (p_access, "file-caching");
397             *pi_64 *= 1000;
398             break;
399
400         /* */
401         case ACCESS_SET_PAUSE_STATE:
402             /* Nothing to do */
403             break;
404
405         case ACCESS_GET_TITLE_INFO:
406         case ACCESS_SET_TITLE:
407         case ACCESS_SET_SEEKPOINT:
408         case ACCESS_SET_PRIVATE_ID_STATE:
409         case ACCESS_GET_META:
410         case ACCESS_GET_PRIVATE_ID_STATE:
411         case ACCESS_GET_CONTENT_TYPE:
412             return VLC_EGENERIC;
413
414         default:
415             msg_Warn( p_access, "unimplemented query %d in control", i_query );
416             return VLC_EGENERIC;
417
418     }
419     return VLC_SUCCESS;
420 }