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