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