]> git.sesse.net Git - vlc/blob - modules/access/file.c
Check for remote file - Linux support
[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 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
38
39 #include <assert.h>
40 #include <errno.h>
41 #ifdef HAVE_SYS_TYPES_H
42 #   include <sys/types.h>
43 #endif
44 #ifdef HAVE_SYS_STAT_H
45 #   include <sys/stat.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #   include <fcntl.h>
49 #endif
50 #if defined (__linux__)
51 #   include <sys/vfs.h>
52 #   include <linux/magic.h>
53 #   define HAVE_FSTATFS 1
54 #elif defined (HAVE_SYS_MOUNT_H)
55 #   include <sys/mount.h>
56 #   define HAVE_FSTATFS 1
57 #endif
58
59 #if defined( WIN32 )
60 #   include <io.h>
61 #   include <ctype.h>
62 #else
63 #   include <unistd.h>
64 #   include <poll.h>
65 #endif
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 dup(a) -1
75 #endif
76
77 #include <vlc_charset.h>
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 static int  Open ( vlc_object_t * );
83 static void Close( vlc_object_t * );
84
85 #define CACHING_TEXT N_("Caching value in ms")
86 #define CACHING_LONGTEXT N_( \
87     "Caching value for files. This " \
88     "value should be set in milliseconds." )
89
90 vlc_module_begin ()
91     set_description( N_("File input") )
92     set_shortname( N_("File") )
93     set_category( CAT_INPUT )
94     set_subcategory( SUBCAT_INPUT_ACCESS )
95     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
96         change_safe()
97     add_obsolete_string( "file-cat" )
98     set_capability( "access", 50 )
99     add_shortcut( "file" )
100     add_shortcut( "fd" )
101     add_shortcut( "stream" )
102     set_callbacks( Open, Close )
103 vlc_module_end ()
104
105
106 /*****************************************************************************
107  * Exported prototypes
108  *****************************************************************************/
109 static int  Seek( access_t *, int64_t );
110 static int  NoSeek( access_t *, int64_t );
111 static ssize_t Read( access_t *, uint8_t *, size_t );
112 static int  Control( access_t *, int, va_list );
113
114 static int  open_file( access_t *, const char * );
115
116 struct access_sys_t
117 {
118     unsigned int i_nb_reads;
119
120     int fd;
121
122     /* */
123     bool b_pace_control;
124 };
125
126 static bool IsRemote (int fd)
127 {
128 #ifdef HAVE_FSTATFS
129     struct statfs stf;
130
131     if (fstatfs (fd, &stf))
132         return false;
133
134 #if defined(MNT_LOCAL)
135     return !(stf.f_flags & MNT_LOCAL);
136
137 #elif defined (__linux__)
138     switch (stf.f_type)
139     {
140         case AFS_SUPER_MAGIC:
141         case CODA_SUPER_MAGIC:
142         case NCP_SUPER_MAGIC:
143         case NFS_SUPER_MAGIC:
144         case SMB_SUPER_MAGIC:
145         case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
146             return true;
147     }
148     return false;
149
150 #endif
151 #else /* !HAVE_FSTATFS */
152     return false;
153
154 #endif
155 }
156
157
158 /*****************************************************************************
159  * Open: open the file
160  *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
162 {
163     access_t     *p_access = (access_t*)p_this;
164     access_sys_t *p_sys;
165
166     /* Update default_pts to a suitable value for file access */
167     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
168
169     STANDARD_READ_ACCESS_INIT;
170     p_sys->i_nb_reads = 0;
171     p_sys->b_pace_control = true;
172
173     /* Open file */
174     int fd = -1;
175
176     if (!strcasecmp (p_access->psz_access, "fd"))
177         fd = dup (atoi (p_access->psz_path));
178     else if (!strcmp (p_access->psz_path, "-"))
179         fd = dup (0);
180     else
181     {
182         msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
183         fd = open_file (p_access, p_access->psz_path);
184     }
185     if (fd == -1)
186         goto error;
187
188 #ifdef HAVE_SYS_STAT_H
189     struct stat st;
190
191     if (fstat (fd, &st))
192     {
193         msg_Err (p_access, "failed to read (%m)");
194         goto error;
195     }
196     /* Directories can be opened and read from, but only readdir() knows
197      * how to parse the data. The directory plugin will do it. */
198     if (S_ISDIR (st.st_mode))
199     {
200         msg_Dbg (p_access, "ignoring directory");
201         goto error;
202     }
203     if (S_ISREG (st.st_mode))
204         p_access->info.i_size = st.st_size;
205     else if (!S_ISBLK (st.st_mode))
206     {
207         p_access->pf_seek = NoSeek;
208         p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
209     }
210 #else
211 # warning File size not known!
212 #endif
213
214     if (IsRemote(fd))
215     {
216         int i_cache = var_GetInteger (p_access, "file-caching") + 700;
217         var_SetInteger (p_access, "file-caching", i_cache);
218         msg_Warn (p_access, "Opening remote file, increasing cache: %d",
219                   i_cache);
220     }
221
222     p_sys->fd = fd;
223     return VLC_SUCCESS;
224
225 error:
226     if (fd != -1)
227         close (fd);
228     free (p_sys);
229     return VLC_EGENERIC;
230 }
231
232 /*****************************************************************************
233  * Close: close the target
234  *****************************************************************************/
235 static void Close (vlc_object_t * p_this)
236 {
237     access_t     *p_access = (access_t*)p_this;
238     access_sys_t *p_sys = p_access->p_sys;
239
240     close (p_sys->fd);
241     free (p_sys);
242 }
243
244
245 #include <vlc_network.h>
246
247 /*****************************************************************************
248  * Read: standard read on a file descriptor.
249  *****************************************************************************/
250 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
251 {
252     access_sys_t *p_sys = p_access->p_sys;
253     int fd = p_sys->fd;
254     ssize_t i_ret;
255
256 #ifndef WIN32
257     if (p_access->pf_seek == NoSeek)
258         i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
259     else
260 #endif
261         i_ret = read (fd, p_buffer, i_len);
262
263     if( i_ret < 0 )
264     {
265         switch (errno)
266         {
267             case EINTR:
268             case EAGAIN:
269                 break;
270
271             default:
272                 msg_Err (p_access, "failed to read (%m)");
273                 dialog_Fatal (p_access, _("File reading failed"), "%s",
274                               _("VLC could not read the file."));
275                 p_access->info.b_eof = true;
276                 return 0;
277         }
278     }
279     else if( i_ret > 0 )
280         p_access->info.i_pos += i_ret;
281     else
282         p_access->info.b_eof = true;
283
284     p_sys->i_nb_reads++;
285
286 #ifdef HAVE_SYS_STAT_H
287     if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
288      || (p_access->info.i_pos > p_access->info.i_size))
289     {
290         struct stat st;
291
292         if ((fstat (fd, &st) == 0)
293          && (p_access->info.i_size != st.st_size))
294         {
295             p_access->info.i_size = st.st_size;
296             p_access->info.i_update |= INPUT_UPDATE_SIZE;
297         }
298     }
299 #endif
300     return i_ret;
301 }
302
303
304 /*****************************************************************************
305  * Seek: seek to a specific location in a file
306  *****************************************************************************/
307 static int Seek (access_t *p_access, int64_t i_pos)
308 {
309     p_access->info.i_pos = i_pos;
310     p_access->info.b_eof = false;
311
312     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
313     return VLC_SUCCESS;
314 }
315
316 static int NoSeek (access_t *p_access, int64_t i_pos)
317 {
318     /* assert(0); ?? */
319     (void) p_access; (void) i_pos;
320     return VLC_EGENERIC;
321 }
322
323 /*****************************************************************************
324  * Control:
325  *****************************************************************************/
326 static int Control( access_t *p_access, int i_query, va_list args )
327 {
328     access_sys_t *p_sys = p_access->p_sys;
329     bool    *pb_bool;
330     int64_t *pi_64;
331
332     switch( i_query )
333     {
334         /* */
335         case ACCESS_CAN_SEEK:
336         case ACCESS_CAN_FASTSEEK:
337             pb_bool = (bool*)va_arg( args, bool* );
338             *pb_bool = (p_access->pf_seek != NoSeek);
339             break;
340
341         case ACCESS_CAN_PAUSE:
342         case ACCESS_CAN_CONTROL_PACE:
343             pb_bool = (bool*)va_arg( args, bool* );
344             *pb_bool = p_sys->b_pace_control;
345             break;
346
347         /* */
348         case ACCESS_GET_PTS_DELAY:
349             pi_64 = (int64_t*)va_arg( args, int64_t * );
350             *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
351             break;
352
353         /* */
354         case ACCESS_SET_PAUSE_STATE:
355             /* Nothing to do */
356             break;
357
358         case ACCESS_GET_TITLE_INFO:
359         case ACCESS_SET_TITLE:
360         case ACCESS_SET_SEEKPOINT:
361         case ACCESS_SET_PRIVATE_ID_STATE:
362         case ACCESS_GET_META:
363         case ACCESS_GET_PRIVATE_ID_STATE:
364         case ACCESS_GET_CONTENT_TYPE:
365             return VLC_EGENERIC;
366
367         default:
368             msg_Warn( p_access, "unimplemented query %d in control", i_query );
369             return VLC_EGENERIC;
370
371     }
372     return VLC_SUCCESS;
373 }
374
375 /*****************************************************************************
376  * open_file: Opens a specific file
377  *****************************************************************************/
378 static int open_file (access_t *p_access, const char *path)
379 {
380 #if defined(WIN32)
381     if (!strcasecmp (p_access->psz_access, "file")
382       && ('/' == path[0]) && isalpha (path[1])
383       && (':' == path[2]) && ('/' == path[3]))
384         /* Explorer can open path such as file:/C:/ or file:///C:/
385          * hence remove leading / if found */
386         path++;
387 #endif
388
389     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
390     if (fd == -1)
391     {
392         msg_Err (p_access, "cannot open file %s (%m)", path);
393         dialog_Fatal (p_access, _("File reading failed"),
394                       _("VLC could not open the file \"%s\"."), path);
395         return -1;
396     }
397
398 #if defined(HAVE_FCNTL)
399     /* We'd rather use any available memory for reading ahead
400      * than for caching what we've already seen/heard */
401 # if defined(F_RDAHEAD)
402     fcntl (fd, F_RDAHEAD, 1);
403 # endif
404 # if defined(F_NOCACHE)
405     fcntl (fd, F_NOCACHE, 1);
406 # endif
407 #endif
408
409     return fd;
410 }