]> git.sesse.net Git - vlc/blob - modules/access/file.c
* modules/access/file.c: we now regularly check the size of the file, in
[vlc] / modules / access / file.c
1 /*****************************************************************************
2  * file.c: file input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: file.c,v 1.8 2003/01/03 13:07:17 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32 #ifdef HAVE_SYS_TYPES_H
33 #   include <sys/types.h>
34 #endif
35 #ifdef HAVE_SYS_STAT_H
36 #   include <sys/stat.h>
37 #endif
38 #ifdef HAVE_ERRNO_H
39 #   include <errno.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #   include <fcntl.h>
43 #endif
44
45 #ifdef HAVE_UNISTD_H
46 #   include <unistd.h>
47 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
48 #   include <io.h>
49 #endif
50
51 /*****************************************************************************
52  * Exported prototypes
53  *****************************************************************************/
54 static int     Open   ( vlc_object_t * );
55 static void    Close  ( vlc_object_t * );
56
57 static void    Seek   ( input_thread_t *, off_t );
58 static ssize_t Read   ( input_thread_t *, byte_t *, size_t );
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 #define CACHING_TEXT N_("caching value in ms")
64 #define CACHING_LONGTEXT N_( \
65     "Allows you to modify the default caching value for file streams. This " \
66     "value should be set in miliseconds units." )
67
68 vlc_module_begin();
69     set_description( _("Standard filesystem file reading") );
70     add_category_hint( N_("file"), NULL );
71     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT );
72     set_capability( "access", 50 );
73     add_shortcut( "file" );
74     add_shortcut( "stream" );
75     set_callbacks( Open, Close );
76 vlc_module_end();
77  
78 /*****************************************************************************
79  * _input_socket_t: private access plug-in data, modified to add private
80  *                  fields
81  *****************************************************************************/
82 typedef struct _input_socket_s
83 {
84     input_socket_t      _socket;
85
86     unsigned int        i_nb_reads;
87 } _input_socket_t;
88
89 /*****************************************************************************
90  * Open: open the file
91  *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
93 {
94     input_thread_t *    p_input = (input_thread_t *)p_this;
95     char *              psz_name = p_input->psz_name;
96 #ifdef HAVE_SYS_STAT_H
97     int                 i_stat;
98     struct stat         stat_info;                                              
99 #endif
100     _input_socket_t *   p_access_data;
101     vlc_bool_t          b_stdin;
102
103     p_input->i_mtu = 0;
104
105     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
106
107 #ifdef HAVE_SYS_STAT_H
108     if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
109     {
110 #   ifdef HAVE_ERRNO_H
111         msg_Err( p_input, "cannot stat() file `%s' (%s)",
112                           psz_name, strerror(errno));
113 #   else
114         msg_Err( p_input, "cannot stat() file `%s'", psz_name );
115 #   endif
116         return VLC_EGENERIC;
117     }
118 #endif
119
120     p_input->pf_read = Read;
121     p_input->pf_set_program = input_SetProgram;
122     p_input->pf_set_area = NULL;
123     p_input->pf_seek = Seek;
124
125     vlc_mutex_lock( &p_input->stream.stream_lock );
126
127     p_input->stream.b_connected = 1;
128     if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
129     {
130         /* stream:%s */
131         p_input->stream.b_pace_control = 0;
132         p_input->stream.b_seekable = 0;
133         p_input->stream.p_selected_area->i_size = 0;
134     }
135     else
136     {
137         /* file:%s or %s */
138         p_input->stream.b_pace_control = 1;
139
140         if( b_stdin )
141         {
142             p_input->stream.b_seekable = 0;
143             p_input->stream.p_selected_area->i_size = 0;
144         }
145 #ifdef UNDER_CE
146         else if( VLC_TRUE )
147         {
148             /* We'll update i_size after it's been opened */
149             p_input->stream.b_seekable = 1;
150         }
151 #elif defined( HAVE_SYS_STAT_H )
152         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
153                   || S_ISBLK(stat_info.st_mode) )
154         {
155             p_input->stream.b_seekable = 1;
156             p_input->stream.p_selected_area->i_size = stat_info.st_size;
157         }
158         else if( S_ISFIFO(stat_info.st_mode)
159 #   if !defined( SYS_BEOS ) && !defined( WIN32 )
160                   || S_ISSOCK(stat_info.st_mode)
161 #   endif
162                )
163         {
164             p_input->stream.b_seekable = 0;
165             p_input->stream.p_selected_area->i_size = 0;
166         }
167 #endif
168         else
169         {
170             vlc_mutex_unlock( &p_input->stream.stream_lock );
171             msg_Err( p_input, "unknown file type for `%s'", psz_name );
172             return VLC_EGENERIC;
173         }
174     }
175  
176     p_input->stream.p_selected_area->i_tell = 0;
177     p_input->stream.i_method = INPUT_METHOD_FILE;
178     vlc_mutex_unlock( &p_input->stream.stream_lock );
179  
180     msg_Dbg( p_input, "opening file `%s'", psz_name );
181     p_access_data = malloc( sizeof(_input_socket_t) );
182     p_input->p_access_data = (void *)p_access_data;
183     if( p_access_data == NULL )
184     {
185         msg_Err( p_input, "out of memory" );
186         return VLC_ENOMEM;
187     }
188
189     p_access_data->i_nb_reads = 0;
190     if( b_stdin )
191     {
192         p_access_data->_socket.i_handle = 0;
193     }
194     else
195     {
196 #ifdef UNDER_CE
197         wchar_t psz_filename[MAX_PATH];
198         MultiByteToWideChar( CP_ACP, 0, psz_name, -1, psz_filename, MAX_PATH );
199
200         p_access_data->_socket.i_handle = (int)CreateFile( psz_filename,
201             GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 
202             FILE_ATTRIBUTE_NORMAL, NULL );
203
204         if( (HANDLE)p_access_data->_socket.i_handle == INVALID_HANDLE_VALUE )
205         {
206             msg_Err( p_input, "cannot open file %s", psz_name );
207             free( p_access_data );
208             return VLC_EGENERIC;
209         }
210         p_input->stream.p_selected_area->i_size =
211                 GetFileSize( (HANDLE)p_access_data->_socket.i_handle, NULL );
212 #else
213         p_access_data->_socket.i_handle = open( psz_name,
214                                                 O_NONBLOCK /*| O_LARGEFILE*/ );
215         if( p_access_data->_socket.i_handle == -1 )
216         {
217 #   ifdef HAVE_ERRNO_H
218             msg_Err( p_input, "cannot open file %s (%s)", psz_name,
219                               strerror(errno) );
220 #   else
221             msg_Err( p_input, "cannot open file %s", psz_name );
222 #   endif
223             free( p_access_data );
224             return VLC_EGENERIC;
225         }
226 #endif
227     }
228
229     if ( p_input->stream.b_seekable
230           && !p_input->stream.p_selected_area->i_size )
231     {
232         msg_Err( p_input, "file %s is empty, aborting", psz_name );
233         free( p_access_data );
234         return VLC_EGENERIC;
235     }
236
237     /* Update default_pts to a suitable value for file access */
238     p_input->i_pts_delay = config_GetInt( p_input, "file-caching" ) * 1000;
239
240     return VLC_SUCCESS;
241 }
242
243 /*****************************************************************************
244  * Close: close the target
245  *****************************************************************************/
246 static void Close( vlc_object_t * p_this )
247 {
248     input_thread_t * p_input = (input_thread_t *)p_this;
249     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
250
251     msg_Info( p_input, "closing `%s/%s://%s'", 
252               p_input->psz_access, p_input->psz_demux, p_input->psz_name );
253  
254 #ifdef UNDER_CE
255     CloseHandle( (HANDLE)p_access_data->i_handle );
256 #else
257     close( p_access_data->i_handle );
258 #endif
259
260     free( p_access_data );
261 }
262
263 /*****************************************************************************
264  * Read: standard read on a file descriptor.
265  *****************************************************************************/
266 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
267 {
268     _input_socket_t * p_access_data = (_input_socket_t *)p_input->p_access_data;
269     ssize_t i_ret;
270  
271 #ifdef UNDER_CE
272     if( !ReadFile( (HANDLE)p_access_data->_socket.i_handle, p_buffer, i_len,
273                    (LPDWORD)&i_ret, NULL ) )
274     {
275         i_ret = -1;
276     }
277 #else
278     i_ret = read( p_access_data->_socket.i_handle, p_buffer, i_len );
279 #endif
280
281     if( i_ret < 0 )
282     {
283 #ifdef HAVE_ERRNO_H
284         if ( errno != EINTR && errno != EAGAIN )
285             msg_Err( p_input, "read failed (%s)", strerror(errno) );
286 #else
287         msg_Err( p_input, "read failed" );
288 #endif
289
290         /* Delay a bit to avoid consuming all the CPU. This is particularly
291          * useful when reading from an unconnected FIFO. */
292         msleep( INPUT_ERROR_SLEEP );
293     }
294  
295     p_access_data->i_nb_reads++;
296 #ifdef HAVE_SYS_STAT_H
297     if ( p_input->stream.p_selected_area->i_size != 0
298             && (p_access_data->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
299     {
300         struct stat stat_info;
301         if ( fstat( p_access_data->_socket.i_handle, &stat_info ) == -1 )
302         {
303 #   ifdef HAVE_ERRNO_H
304             msg_Warn( p_input, "couldn't stat again the file (%s)",
305                       strerror(errno) );
306 #   else
307             msg_Warn( p_input, "couldn't stat again the file" );
308 #   endif
309         }
310         else
311         {
312             p_input->stream.p_selected_area->i_size = stat_info.st_size;
313             p_input->stream.b_changed = 1;
314         }
315     }
316 #endif
317
318     return i_ret;
319 }
320
321 /*****************************************************************************
322  * Seek: seek to a specific location in a file
323  *****************************************************************************/
324 static void Seek( input_thread_t * p_input, off_t i_pos )
325 {
326 #define S p_input->stream
327     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
328
329     lseek( p_access_data->i_handle, i_pos, SEEK_SET );
330
331     vlc_mutex_lock( &S.stream_lock );
332     S.p_selected_area->i_tell = i_pos;
333     if( S.p_selected_area->i_tell > S.p_selected_area->i_size )
334     {
335         msg_Err( p_input, "seeking too far" );
336         S.p_selected_area->i_tell = S.p_selected_area->i_size;
337     }
338     else if( S.p_selected_area->i_tell < 0 )
339     {
340         msg_Err( p_input, "seeking too early" );
341         S.p_selected_area->i_tell = 0;
342     }
343     vlc_mutex_unlock( &S.stream_lock );
344 #undef S
345 }
346