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