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