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