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