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: 502b1f4aa483db41ac4a07ba6c238b1faf5c2f56 $
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Rémi Denis-Courmont
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 #ifdef _WIN32
61 # include <vlc_charset.h>
62 #endif
63 #include <vlc_fs.h>
64 #include <vlc_url.h>
65 #include <vlc_interrupt.h>
66 
67 struct access_sys_t
68 {
69     int fd;
70 
71     bool b_pace_control;
72 };
73 
74 #if !defined (_WIN32) && !defined (__OS2__)
IsRemote(int fd)75 static bool IsRemote (int fd)
76 {
77 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
78     struct statvfs stf;
79 
80     if (fstatvfs (fd, &stf))
81         return false;
82     /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
83     return !(stf.f_flag & MNT_LOCAL);
84 
85 #elif defined (HAVE_LINUX_MAGIC_H)
86     struct statfs stf;
87 
88     if (fstatfs (fd, &stf))
89         return false;
90 
91     switch ((unsigned long)stf.f_type)
92     {
93         case AFS_SUPER_MAGIC:
94         case CODA_SUPER_MAGIC:
95         case NCP_SUPER_MAGIC:
96         case NFS_SUPER_MAGIC:
97         case SMB_SUPER_MAGIC:
98         case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
99             return true;
100     }
101     return false;
102 
103 #else
104     (void)fd;
105     return false;
106 
107 #endif
108 }
109 # define IsRemote(fd,path) IsRemote(fd)
110 
111 #else /* _WIN32 || __OS2__ */
IsRemote(const char * path)112 static bool IsRemote (const char *path)
113 {
114 # if !defined(__OS2__) && !VLC_WINSTORE_APP
115     wchar_t *wpath = ToWide (path);
116     bool is_remote = (wpath != NULL && PathIsNetworkPathW (wpath));
117     free (wpath);
118     return is_remote;
119 # else
120     return (! strncmp(path, "\\\\", 2));
121 # endif
122 }
123 # define IsRemote(fd,path) IsRemote(path)
124 #endif
125 
126 #ifndef HAVE_POSIX_FADVISE
127 # define posix_fadvise(fd, off, len, adv)
128 #endif
129 
130 static ssize_t Read (stream_t *, void *, size_t);
131 static int FileSeek (stream_t *, uint64_t);
132 static int NoSeek (stream_t *, uint64_t);
133 static int FileControl (stream_t *, int, va_list);
134 
135 /*****************************************************************************
136  * FileOpen: open the file
137  *****************************************************************************/
FileOpen(vlc_object_t * p_this)138 int FileOpen( vlc_object_t *p_this )
139 {
140     stream_t *p_access = (stream_t*)p_this;
141 
142     /* Open file */
143     int fd = -1;
144 
145     if (!strcasecmp (p_access->psz_name, "fd"))
146     {
147         char *end;
148         int oldfd = strtol (p_access->psz_location, &end, 10);
149 
150         if (*end == '\0')
151             fd = vlc_dup (oldfd);
152         else if (*end == '/' && end > p_access->psz_location)
153         {
154             char *name = vlc_uri_decode_duplicate (end - 1);
155             if (name != NULL)
156             {
157                 name[0] = '.';
158                 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
159                 free (name);
160             }
161         }
162     }
163     else
164     {
165         if (unlikely(p_access->psz_filepath == NULL))
166             return VLC_EGENERIC;
167         fd = vlc_open (p_access->psz_filepath, O_RDONLY | O_NONBLOCK);
168     }
169 
170     if (fd == -1)
171     {
172         msg_Err (p_access, "cannot open file %s (%s)",
173                  p_access->psz_filepath ? p_access->psz_filepath
174                                         : p_access->psz_location,
175                  vlc_strerror_c(errno));
176         return VLC_EGENERIC;
177     }
178 
179     struct stat st;
180     if (fstat (fd, &st))
181     {
182         msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
183         goto error;
184     }
185 
186 #if O_NONBLOCK
187     /* Force blocking mode back */
188     fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) & ~O_NONBLOCK);
189 #endif
190 
191     /* Directories can be opened and read from, but only readdir() knows
192      * how to parse the data. The directory plugin will do it. */
193     if (S_ISDIR (st.st_mode))
194     {
195 #ifdef HAVE_FDOPENDIR
196         DIR *p_dir = fdopendir(fd);
197         if (!p_dir) {
198             msg_Err (p_access, "fdopendir error: %s", vlc_strerror_c(errno));
199             goto error;
200         }
201         return DirInit (p_access, p_dir);
202 #else
203         msg_Dbg (p_access, "ignoring directory");
204         goto error;
205 #endif
206     }
207 
208     access_sys_t *p_sys = vlc_obj_malloc(p_this, sizeof (*p_sys));
209     if (unlikely(p_sys == NULL))
210         goto error;
211     p_access->pf_read = Read;
212     p_access->pf_block = NULL;
213     p_access->pf_control = FileControl;
214     p_access->p_sys = p_sys;
215     p_sys->fd = fd;
216 
217     if (S_ISREG (st.st_mode) || S_ISBLK (st.st_mode))
218     {
219         p_access->pf_seek = FileSeek;
220         p_sys->b_pace_control = true;
221 
222         /* Demuxers will need the beginning of the file for probing. */
223         posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
224         /* In most cases, we only read the file once. */
225         posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
226 #ifdef F_NOCACHE
227         fcntl (fd, F_NOCACHE, 0);
228 #endif
229 #ifdef F_RDAHEAD
230         if (IsRemote(fd, p_access->psz_filepath))
231             fcntl (fd, F_RDAHEAD, 0);
232         else
233             fcntl (fd, F_RDAHEAD, 1);
234 #endif
235     }
236     else
237     {
238         p_access->pf_seek = NoSeek;
239         p_sys->b_pace_control = strcasecmp (p_access->psz_name, "stream");
240     }
241 
242     return VLC_SUCCESS;
243 
244 error:
245     vlc_close (fd);
246     return VLC_EGENERIC;
247 }
248 
249 /*****************************************************************************
250  * FileClose: close the target
251  *****************************************************************************/
FileClose(vlc_object_t * p_this)252 void FileClose (vlc_object_t * p_this)
253 {
254     stream_t     *p_access = (stream_t*)p_this;
255 
256     if (p_access->pf_read == NULL)
257     {
258         DirClose (p_this);
259         return;
260     }
261 
262     access_sys_t *p_sys = p_access->p_sys;
263 
264     vlc_close (p_sys->fd);
265 }
266 
267 
Read(stream_t * p_access,void * p_buffer,size_t i_len)268 static ssize_t Read (stream_t *p_access, void *p_buffer, size_t i_len)
269 {
270     access_sys_t *p_sys = p_access->p_sys;
271     int fd = p_sys->fd;
272 
273     ssize_t val = vlc_read_i11e (fd, p_buffer, i_len);
274     if (val < 0)
275     {
276         switch (errno)
277         {
278             case EINTR:
279             case EAGAIN:
280                 return -1;
281         }
282 
283         msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
284         val = 0;
285     }
286 
287     return val;
288 }
289 
290 /*****************************************************************************
291  * Seek: seek to a specific location in a file
292  *****************************************************************************/
FileSeek(stream_t * p_access,uint64_t i_pos)293 static int FileSeek (stream_t *p_access, uint64_t i_pos)
294 {
295     access_sys_t *sys = p_access->p_sys;
296 
297     if (lseek(sys->fd, i_pos, SEEK_SET) == (off_t)-1)
298         return VLC_EGENERIC;
299     return VLC_SUCCESS;
300 }
301 
NoSeek(stream_t * p_access,uint64_t i_pos)302 static int NoSeek (stream_t *p_access, uint64_t i_pos)
303 {
304     /* vlc_assert_unreachable(); ?? */
305     (void) p_access; (void) i_pos;
306     return VLC_EGENERIC;
307 }
308 
309 /*****************************************************************************
310  * Control:
311  *****************************************************************************/
FileControl(stream_t * p_access,int i_query,va_list args)312 static int FileControl( stream_t *p_access, int i_query, va_list args )
313 {
314     access_sys_t *p_sys = p_access->p_sys;
315     bool    *pb_bool;
316     int64_t *pi_64;
317 
318     switch( i_query )
319     {
320         case STREAM_CAN_SEEK:
321         case STREAM_CAN_FASTSEEK:
322             pb_bool = va_arg( args, bool * );
323             *pb_bool = (p_access->pf_seek != NoSeek);
324             break;
325 
326         case STREAM_CAN_PAUSE:
327         case STREAM_CAN_CONTROL_PACE:
328             pb_bool = va_arg( args, bool * );
329             *pb_bool = p_sys->b_pace_control;
330             break;
331 
332         case STREAM_GET_SIZE:
333         {
334             struct stat st;
335 
336             if (fstat (p_sys->fd, &st) || !S_ISREG(st.st_mode))
337                 return VLC_EGENERIC;
338             *va_arg( args, uint64_t * ) = st.st_size;
339             break;
340         }
341 
342         case STREAM_GET_PTS_DELAY:
343             pi_64 = va_arg( args, int64_t * );
344             if (IsRemote (p_sys->fd, p_access->psz_filepath))
345                 *pi_64 = var_InheritInteger (p_access, "network-caching");
346             else
347                 *pi_64 = var_InheritInteger (p_access, "file-caching");
348             *pi_64 *= 1000;
349             break;
350 
351         case STREAM_SET_PAUSE_STATE:
352             /* Nothing to do */
353             break;
354 
355         default:
356             return VLC_EGENERIC;
357 
358     }
359     return VLC_SUCCESS;
360 }
361