]> git.sesse.net Git - vlc/blob - modules/access/file.c
b2b2b23308068fefe33f9058c26946a455606bfd
[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                 return 0;
249         }
250     }
251     else if( i_ret > 0 )
252         p_access->info.i_pos += i_ret;
253     else if( i_ret == 0 )
254         p_access->info.b_eof = true;
255
256     p_sys->i_nb_reads++;
257
258 #ifdef HAVE_SYS_STAT_H
259     if( p_access->info.i_size != 0 &&
260         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
261     {
262         struct stat st;
263
264         if ((fstat (fd, &st) == 0)
265          && (p_access->info.i_size != st.st_size))
266         {
267             p_access->info.i_size = st.st_size;
268             p_access->info.i_update |= INPUT_UPDATE_SIZE;
269         }
270     }
271 #endif
272     return i_ret;
273 }
274
275
276 /*****************************************************************************
277  * Seek: seek to a specific location in a file
278  *****************************************************************************/
279 static int Seek (access_t *p_access, int64_t i_pos)
280 {
281     p_access->info.i_pos = i_pos;
282     p_access->info.b_eof = false;
283
284     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
285     return VLC_SUCCESS;
286 }
287
288 /*****************************************************************************
289  * Control:
290  *****************************************************************************/
291 static int Control( access_t *p_access, int i_query, va_list args )
292 {
293     access_sys_t *p_sys = p_access->p_sys;
294     bool   *pb_bool;
295     int          *pi_int;
296     int64_t      *pi_64;
297
298     switch( i_query )
299     {
300         /* */
301         case ACCESS_CAN_SEEK:
302         case ACCESS_CAN_FASTSEEK:
303             pb_bool = (bool*)va_arg( args, bool* );
304             *pb_bool = p_sys->b_seekable;
305             break;
306
307         case ACCESS_CAN_PAUSE:
308         case ACCESS_CAN_CONTROL_PACE:
309             pb_bool = (bool*)va_arg( args, bool* );
310             *pb_bool = p_sys->b_pace_control;
311             break;
312
313         /* */
314         case ACCESS_GET_MTU:
315             pi_int = (int*)va_arg( args, int * );
316             *pi_int = 0;
317             break;
318
319         case ACCESS_GET_PTS_DELAY:
320             pi_64 = (int64_t*)va_arg( args, int64_t * );
321             *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
322             break;
323
324         /* */
325         case ACCESS_SET_PAUSE_STATE:
326             /* Nothing to do */
327             break;
328
329         case ACCESS_GET_TITLE_INFO:
330         case ACCESS_SET_TITLE:
331         case ACCESS_SET_SEEKPOINT:
332         case ACCESS_SET_PRIVATE_ID_STATE:
333         case ACCESS_GET_META:
334         case ACCESS_GET_PRIVATE_ID_STATE:
335         case ACCESS_GET_CONTENT_TYPE:
336             return VLC_EGENERIC;
337
338         default:
339             msg_Warn( p_access, "unimplemented query %d in control", i_query );
340             return VLC_EGENERIC;
341
342     }
343     return VLC_SUCCESS;
344 }
345
346 /*****************************************************************************
347  * open_file: Opens a specific file
348  *****************************************************************************/
349 static int open_file (access_t *p_access, const char *path)
350 {
351 #if defined(WIN32)
352     if (!strcasecmp (p_access->psz_access, "file")
353       && ('/' == path[0]) && isalpha (path[1])
354       && (':' == path[2]) && ('/' == path[3]))
355         /* Explorer can open path such as file:/C:/ or file:///C:/
356          * hence remove leading / if found */
357         path++;
358 #endif
359
360 #ifdef UNDER_CE
361     p_sys->fd = utf8_fopen( path, "rb" );
362     if ( !p_sys->fd )
363     {
364         msg_Err( p_access, "cannot open file %s", path );
365         intf_UserFatal( p_access, false, _("File reading failed"),
366                         _("VLC could not open the file \"%s\"."), path );
367         return VLC_EGENERIC;
368     }
369
370     fseek( p_sys->fd, 0, SEEK_END );
371     p_access->info.i_size = ftell( p_sys->fd );
372     p_access->info.i_update |= INPUT_UPDATE_SIZE;
373     fseek( p_sys->fd, 0, SEEK_SET );
374 #else
375     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
376     if (fd == -1)
377     {
378         msg_Err (p_access, "cannot open file %s (%m)", path);
379         intf_UserFatal (p_access, false, _("File reading failed"),
380                         _("VLC could not open the file \"%s\"."), path);
381         return -1;
382     }
383
384 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
385     /* We'd rather use any available memory for reading ahead
386      * than for caching what we've already seen/heard */
387     fcntl (fd, F_RDAHEAD, 1);
388     fcntl (fd, F_NOCACHE, 1);
389 # endif
390 #endif
391
392     return fd;
393 }