]> git.sesse.net Git - vlc/blob - modules/access/file.c
System knows better whether we can seek.
[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_interface.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
51 #if defined( WIN32 )
52 #   include <io.h>
53 #   include <ctype.h>
54 #else
55 #   include <unistd.h>
56 #   include <poll.h>
57 #endif
58
59 #if defined( WIN32 ) && !defined( UNDER_CE )
60 #   ifdef lseek
61 #      undef lseek
62 #   endif
63 #   define lseek _lseeki64
64 #elif defined( UNDER_CE )
65 /* FIXME the commandline on wince is a mess */
66 # define dup(a) -1
67 #endif
68
69 #include <vlc_charset.h>
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 static int  Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
76
77 #define CACHING_TEXT N_("Caching value in ms")
78 #define CACHING_LONGTEXT N_( \
79     "Caching value for files. This " \
80     "value should be set in milliseconds." )
81
82 vlc_module_begin ()
83     set_description( N_("File input") )
84     set_shortname( N_("File") )
85     set_category( CAT_INPUT )
86     set_subcategory( SUBCAT_INPUT_ACCESS )
87     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
88     add_obsolete_string( "file-cat" )
89     set_capability( "access", 50 )
90     add_shortcut( "file" )
91     add_shortcut( "stream" )
92     set_callbacks( Open, Close )
93 vlc_module_end ()
94
95
96 /*****************************************************************************
97  * Exported prototypes
98  *****************************************************************************/
99 static int  Seek( access_t *, int64_t );
100 static ssize_t Read( access_t *, uint8_t *, size_t );
101 static int  Control( access_t *, int, va_list );
102
103 static int  open_file( access_t *, const char * );
104
105 struct access_sys_t
106 {
107     unsigned int i_nb_reads;
108
109     int fd;
110
111     /* */
112     bool b_seekable;
113     bool b_pace_control;
114 };
115
116 /*****************************************************************************
117  * Open: open the file
118  *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
120 {
121     access_t     *p_access = (access_t*)p_this;
122     access_sys_t *p_sys;
123
124     bool    b_stdin = !strcmp (p_access->psz_path, "-");
125
126     /* Update default_pts to a suitable value for file access */
127     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
128
129     STANDARD_READ_ACCESS_INIT;
130     p_sys->i_nb_reads = 0;
131
132     if (!strcasecmp (p_access->psz_access, "stream"))
133         p_sys->b_pace_control = false;
134     else
135         p_sys->b_pace_control = true;
136
137     /* Open file */
138     msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
139
140     int fd = -1;
141     if (b_stdin)
142         fd = dup (0);
143     else
144         fd = open_file (p_access, p_access->psz_path);
145     if (fd == -1)
146         goto error;
147
148 #ifdef HAVE_SYS_STAT_H
149     struct stat st;
150
151     if (fstat (fd, &st))
152     {
153         msg_Err (p_access, "failed to read (%m)");
154         goto error;
155     }
156     /* Directories can be opened and read from, but only readdir() knows
157      * how to parse the data. The directory plugin will do it. */
158     if (S_ISDIR (st.st_mode))
159     {
160         msg_Dbg (p_access, "ignoring directory");
161         goto error;
162     }
163     if (S_ISREG (st.st_mode))
164         p_access->info.i_size = st.st_size;
165     else if (!S_ISBLK (st.st_mode))
166         p_sys->b_seekable = false;
167 #else
168     p_sys->b_seekable = !b_stdin;
169 # warning File size not known!
170 #endif
171
172     p_sys->fd = fd;
173     return VLC_SUCCESS;
174
175 error:
176     if (fd != -1)
177         close (fd);
178     free (p_sys);
179     return VLC_EGENERIC;
180 }
181
182 /*****************************************************************************
183  * Close: close the target
184  *****************************************************************************/
185 static void Close (vlc_object_t * p_this)
186 {
187     access_t     *p_access = (access_t*)p_this;
188     access_sys_t *p_sys = p_access->p_sys;
189
190     close (p_sys->fd);
191     free (p_sys);
192 }
193
194
195 #include <vlc_network.h>
196
197 /*****************************************************************************
198  * Read: standard read on a file descriptor.
199  *****************************************************************************/
200 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
201 {
202     access_sys_t *p_sys = p_access->p_sys;
203     int fd = p_sys->fd;
204     ssize_t i_ret;
205
206 #ifndef WIN32
207     if (!p_sys->b_seekable)
208         i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
209     else
210 #endif
211         i_ret = read (fd, p_buffer, i_len);
212
213     if( i_ret < 0 )
214     {
215         switch (errno)
216         {
217             case EINTR:
218             case EAGAIN:
219                 break;
220
221             default:
222                 msg_Err (p_access, "failed to read (%m)");
223                 intf_UserFatal (p_access, false, _("File reading failed"),
224                                 _("VLC could not read the file."));
225                 p_access->info.b_eof = true;
226                 return 0;
227         }
228     }
229     else if( i_ret > 0 )
230         p_access->info.i_pos += i_ret;
231     else
232         p_access->info.b_eof = true;
233
234     p_sys->i_nb_reads++;
235
236 #ifdef HAVE_SYS_STAT_H
237     if( p_access->info.i_size != 0 &&
238         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
239     {
240         struct stat st;
241
242         if ((fstat (fd, &st) == 0)
243          && (p_access->info.i_size != st.st_size))
244         {
245             p_access->info.i_size = st.st_size;
246             p_access->info.i_update |= INPUT_UPDATE_SIZE;
247         }
248     }
249 #endif
250     return i_ret;
251 }
252
253
254 /*****************************************************************************
255  * Seek: seek to a specific location in a file
256  *****************************************************************************/
257 static int Seek (access_t *p_access, int64_t i_pos)
258 {
259     p_access->info.i_pos = i_pos;
260     p_access->info.b_eof = false;
261
262     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
263     return VLC_SUCCESS;
264 }
265
266 /*****************************************************************************
267  * Control:
268  *****************************************************************************/
269 static int Control( access_t *p_access, int i_query, va_list args )
270 {
271     access_sys_t *p_sys = p_access->p_sys;
272     bool    *pb_bool;
273     int64_t *pi_64;
274
275     switch( i_query )
276     {
277         /* */
278         case ACCESS_CAN_SEEK:
279         case ACCESS_CAN_FASTSEEK:
280             pb_bool = (bool*)va_arg( args, bool* );
281             *pb_bool = p_sys->b_seekable;
282             break;
283
284         case ACCESS_CAN_PAUSE:
285         case ACCESS_CAN_CONTROL_PACE:
286             pb_bool = (bool*)va_arg( args, bool* );
287             *pb_bool = p_sys->b_pace_control;
288             break;
289
290         /* */
291         case ACCESS_GET_PTS_DELAY:
292             pi_64 = (int64_t*)va_arg( args, int64_t * );
293             *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
294             break;
295
296         /* */
297         case ACCESS_SET_PAUSE_STATE:
298             /* Nothing to do */
299             break;
300
301         case ACCESS_GET_TITLE_INFO:
302         case ACCESS_SET_TITLE:
303         case ACCESS_SET_SEEKPOINT:
304         case ACCESS_SET_PRIVATE_ID_STATE:
305         case ACCESS_GET_META:
306         case ACCESS_GET_PRIVATE_ID_STATE:
307         case ACCESS_GET_CONTENT_TYPE:
308             return VLC_EGENERIC;
309
310         default:
311             msg_Warn( p_access, "unimplemented query %d in control", i_query );
312             return VLC_EGENERIC;
313
314     }
315     return VLC_SUCCESS;
316 }
317
318 /*****************************************************************************
319  * open_file: Opens a specific file
320  *****************************************************************************/
321 static int open_file (access_t *p_access, const char *path)
322 {
323 #if defined(WIN32)
324     if (!strcasecmp (p_access->psz_access, "file")
325       && ('/' == path[0]) && isalpha (path[1])
326       && (':' == path[2]) && ('/' == path[3]))
327         /* Explorer can open path such as file:/C:/ or file:///C:/
328          * hence remove leading / if found */
329         path++;
330 #endif
331
332     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
333     if (fd == -1)
334     {
335         msg_Err (p_access, "cannot open file %s (%m)", path);
336         intf_UserFatal (p_access, false, _("File reading failed"),
337                         _("VLC could not open the file \"%s\"."), path);
338         return -1;
339     }
340
341 #if defined(HAVE_FCNTL)
342     /* We'd rather use any available memory for reading ahead
343      * than for caching what we've already seen/heard */
344 # if defined(F_RDAHEAD)
345     fcntl (fd, F_RDAHEAD, 1);
346 # endif
347 # if defined(F_NOCACHE)
348     fcntl (fd, F_NOCACHE, 1);
349 # endif
350 #endif
351
352     return fd;
353 }