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