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