]> git.sesse.net Git - vlc/blob - modules/access/gnomevfs.c
Don't access unallocated memory
[vlc] / modules / access / gnomevfs.c
1 /*****************************************************************************
2  * gnomevfs.c: GnomeVFS input
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben -AT- videolan -DOT- org>
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 #include <libgnomevfs/gnome-vfs.h>
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "charset.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42
43 #define CACHING_TEXT N_("Caching value in ms")
44 #define CACHING_LONGTEXT N_( \
45     "Allows you to modify the default caching value for GnomeVFS streams."\
46     "This value should be set in millisecond units." )
47
48 vlc_module_begin();
49     set_description( _("GnomeVFS filesystem file input") );
50     set_shortname( _("GnomeVFS") );
51     set_category( CAT_INPUT );
52     set_subcategory( SUBCAT_INPUT_ACCESS );
53     add_integer( "gnomevfs-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
54     set_capability( "access2", 10 );
55     add_shortcut( "gnomevfs" );
56     set_callbacks( Open, Close );
57 vlc_module_end();
58
59
60 /*****************************************************************************
61  * Exported prototypes
62  *****************************************************************************/
63 static int  Seek( access_t *, int64_t );
64 static int  Read( access_t *, uint8_t *, int );
65 static int  Control( access_t *, int, va_list );
66
67 struct access_sys_t
68 {
69     unsigned int i_nb_reads;
70     char *psz_name;
71
72     GnomeVFSHandle *p_handle;
73     GnomeVFSFileInfo *p_file_info;
74
75     vlc_bool_t b_local;
76     vlc_bool_t b_seekable;
77     vlc_bool_t b_pace_control;
78 };
79
80 /*****************************************************************************
81  * Open: open the file
82  *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
84 {
85     access_t       *p_access = (access_t*)p_this;
86     access_sys_t   *p_sys = NULL;
87     char           *psz_name = NULL;
88     char           *psz = NULL;
89     char           *psz_uri = NULL;
90     GnomeVFSURI    *p_uri = NULL;
91     GnomeVFSResult  i_ret;
92     GnomeVFSHandle *p_handle = NULL;
93
94     if( !(gnome_vfs_init()) )
95     {
96         msg_Warn( p_access, "couldn't initilize GnomeVFS" );
97         return VLC_EGENERIC;
98     }
99
100     /* FIXME
101        Since GnomeVFS segfaults on exit if we initialize it without trying to
102        open a file with a valid protocol, try to open at least file:// */
103     gnome_vfs_open( &p_handle, "file://", 5 );
104
105     p_access->pf_read = Read;
106     p_access->pf_block = NULL;
107     p_access->pf_seek = Seek;
108     p_access->pf_control = Control;
109     p_access->info.i_update = 0;
110     p_access->info.i_size = 0;
111     p_access->info.i_pos = 0;
112     p_access->info.b_eof = VLC_FALSE;
113     p_access->info.i_title = 0;
114     p_access->info.i_seekpoint = 0;
115
116     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
117     if( !p_sys )
118         return VLC_ENOMEM;
119
120     p_sys->p_handle = p_handle;
121     p_sys->i_nb_reads = 0;
122     p_sys->b_pace_control = VLC_TRUE;
123
124     if( strcmp( "gnomevfs", p_access->psz_access ) &&
125                                             *(p_access->psz_access) != '\0')
126     {
127         psz_name = malloc( strlen( p_access->psz_access ) +
128                                             strlen( p_access->psz_path ) + 3 );
129         strcpy( psz_name, p_access->psz_access );
130         strcat( psz_name, "://" );
131         strcat( psz_name, p_access->psz_path );
132     }
133     else
134     {
135         psz_name = strdup( p_access->psz_path );
136     }
137
138     psz = ToLocale( psz_name );
139
140     /* Use gnome_vfs_make_uri_from_input_with_dirs for local paths, as it deals
141        relative directories, and gnome_vfs_make_uri_from_input_with_dirs
142        otherwise */
143     psz_uri = gnome_vfs_make_uri_from_input_with_dirs( psz,
144                                     GNOME_VFS_MAKE_URI_DIR_CURRENT);
145     if( *psz_uri != '/' )
146     {
147         g_free( psz_uri );
148         psz_uri = gnome_vfs_escape_host_and_path_string( psz );
149     }
150
151     p_uri = gnome_vfs_uri_new( psz_uri );
152     if( p_uri )
153     {
154         p_sys->p_file_info = gnome_vfs_file_info_new();
155         i_ret = gnome_vfs_get_file_info_uri( p_uri,
156                                                 p_sys->p_file_info, 8 ); 
157
158         if( i_ret )
159         {
160             msg_Err( p_access, "cannot get file info (%s)", 
161                                     gnome_vfs_result_to_string( i_ret ) );
162             gnome_vfs_file_info_unref( p_sys->p_file_info );
163             gnome_vfs_uri_unref( p_uri);
164             free( p_sys );
165             free( psz_uri );
166             free( psz_name );
167             return VLC_EGENERIC;
168         }
169     }
170     else
171     {
172         msg_Warn( p_access, "cannot parse MRL %s or unsupported protocol", psz_name );
173         LocaleFree( psz );
174         g_free( psz_uri );
175         free( p_sys );
176         free( psz_name );
177         return VLC_EGENERIC;
178     }
179     LocaleFree( psz );
180
181     msg_Dbg( p_access, "opening file `%s'", psz_uri );
182     i_ret = gnome_vfs_open( &(p_sys->p_handle), psz_uri, 5 );
183     if( i_ret )
184     {
185         msg_Warn( p_access, "cannot open file %s: %s", psz_uri,
186                                 gnome_vfs_result_to_string( i_ret ) );
187
188         LocaleFree( psz );
189         g_free( psz_uri );
190         gnome_vfs_uri_unref( p_uri);
191         free( p_sys ); 
192         free( psz_uri );
193         free( psz_name );
194         return VLC_EGENERIC;
195     }
196
197     if (GNOME_VFS_FILE_INFO_LOCAL( p_sys->p_file_info ))
198     {
199         p_sys->b_local = VLC_TRUE;
200     }
201
202     if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_REGULAR || 
203         p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_CHARACTER_DEVICE ||
204         p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_BLOCK_DEVICE )
205     {
206         p_sys->b_seekable = VLC_TRUE;
207         p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
208     }
209     else if( p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_FIFO
210               || p_sys->p_file_info->type == GNOME_VFS_FILE_TYPE_SOCKET )
211     {
212         p_sys->b_seekable = VLC_FALSE;
213     }
214     else
215     {
216         msg_Err( p_access, "unknown file type for `%s'", psz_name );
217         return VLC_EGENERIC;
218     }
219
220     if( p_sys->b_seekable && !p_access->info.i_size )
221     {
222         /* FIXME that's bad because all others access will be probed */
223         msg_Err( p_access, "file %s is empty, aborting", psz_name );
224         gnome_vfs_file_info_unref( p_sys->p_file_info );
225         free( p_sys );
226         free( psz_uri );
227         free( psz_name );
228         return VLC_EGENERIC;
229     }
230
231     /* Update default_pts to a suitable value for file access */
232     var_Create( p_access, "gnomevfs-caching",
233                                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
234
235     g_free( psz_uri );
236     p_sys->psz_name = psz_name;
237     gnome_vfs_uri_unref( p_uri);
238     return VLC_SUCCESS;
239 }
240
241 /*****************************************************************************
242  * Close: close the target
243  *****************************************************************************/
244 static void Close( vlc_object_t * p_this )
245 {
246     access_t     *p_access = (access_t*)p_this;
247     access_sys_t *p_sys = p_access->p_sys;
248     int i_result;
249
250     i_result = gnome_vfs_close( p_sys->p_handle );
251     if( i_result )
252     {
253          msg_Err( p_access, "cannot close %s: %s", p_sys->psz_name,
254                                 gnome_vfs_result_to_string( i_result ) );
255     }
256
257     gnome_vfs_file_info_unref( p_sys->p_file_info );
258
259     free( p_sys->psz_name );
260     free( p_sys );
261 }
262
263 /*****************************************************************************
264  * Read: standard read on a file descriptor.
265  *****************************************************************************/
266 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
267 {
268     access_sys_t *p_sys = p_access->p_sys;
269     GnomeVFSFileSize i_read_len;
270     int i_ret;
271
272     i_ret = gnome_vfs_read( p_sys->p_handle, p_buffer,
273                                   (GnomeVFSFileSize)i_len, &i_read_len );
274     if( i_ret )
275     {
276         p_access->info.b_eof = VLC_TRUE;
277         if( i_ret != GNOME_VFS_ERROR_EOF )
278         {
279             msg_Err( p_access, "read failed (%s)",
280                                     gnome_vfs_result_to_string( i_ret ) );
281         }
282     }
283     else
284     {
285         p_sys->i_nb_reads++;
286         if( p_access->info.i_size != 0 &&
287             (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 &&
288             p_sys->b_local )
289         {
290             gnome_vfs_file_info_clear( p_sys->p_file_info );
291             i_ret = gnome_vfs_get_file_info_from_handle( p_sys->p_handle,
292                                                 p_sys->p_file_info, 8 );
293             if( i_ret )
294             {
295                 msg_Warn( p_access, "couldn't get file properties again (%s)",
296                                         gnome_vfs_result_to_string( i_ret ) );
297             }
298             else
299             {
300                 p_access->info.i_size = (int64_t)(p_sys->p_file_info->size);
301             }
302         }
303     }
304
305     p_access->info.i_pos += (int64_t)i_read_len;
306
307     /* Some Acces (http) never return EOF and loop on the file */
308     if( p_access->info.i_pos > p_access->info.i_size )
309     {
310         p_access->info.b_eof = VLC_TRUE;
311         return 0;
312     }
313     return (int)i_read_len;
314 }
315
316 /*****************************************************************************
317  * Seek: seek to a specific location in a file
318  *****************************************************************************/
319 static int Seek( access_t *p_access, int64_t i_pos )
320 {
321     access_sys_t *p_sys = p_access->p_sys;
322     int i_ret;
323
324     i_ret = gnome_vfs_seek( p_sys->p_handle, GNOME_VFS_SEEK_START,
325                                             (GnomeVFSFileOffset)i_pos);
326     if ( !i_ret )
327     {
328         p_access->info.i_pos = i_pos;
329     }
330     else
331     {
332         GnomeVFSFileSize i_offset;
333         msg_Err( p_access, "cannot seek (%s)",
334                                         gnome_vfs_result_to_string( i_ret ) );
335         i_ret = gnome_vfs_tell( p_sys->p_handle, &i_offset );
336         if( !i_ret )
337         {
338             msg_Err( p_access, "cannot tell the current position (%s)",
339                                         gnome_vfs_result_to_string( i_ret ) );
340             return VLC_EGENERIC;
341         }
342     }
343     /* Reset eof */
344     p_access->info.b_eof = VLC_FALSE;
345
346     /* FIXME */
347     return VLC_SUCCESS;
348 }
349
350 /*****************************************************************************
351  * Control:
352  *****************************************************************************/
353 static int Control( access_t *p_access, int i_query, va_list args )
354 {
355     access_sys_t *p_sys = p_access->p_sys;
356     vlc_bool_t   *pb_bool;
357     int          *pi_int;
358     int64_t      *pi_64;
359
360     switch( i_query )
361     {
362         /* */
363         case ACCESS_CAN_SEEK:
364         case ACCESS_CAN_FASTSEEK:
365             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
366             *pb_bool = p_sys->b_seekable;
367             break;
368
369         case ACCESS_CAN_PAUSE:
370         case ACCESS_CAN_CONTROL_PACE:
371             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
372             *pb_bool = p_sys->b_pace_control;
373             break;
374
375         /* */
376         case ACCESS_GET_MTU:
377             pi_int = (int*)va_arg( args, int * );
378             *pi_int = 0;
379             break;
380
381         case ACCESS_GET_PTS_DELAY:
382             pi_64 = (int64_t*)va_arg( args, int64_t * );
383             *pi_64 = var_GetInteger( p_access,
384                                         "gnomevfs-caching" ) * I64C(1000);
385             break;
386
387         /* */
388         case ACCESS_SET_PAUSE_STATE:
389             /* Nothing to do */
390             break;
391
392         case ACCESS_GET_TITLE_INFO:
393         case ACCESS_SET_TITLE:
394         case ACCESS_SET_SEEKPOINT:
395         case ACCESS_SET_PRIVATE_ID_STATE:
396         case ACCESS_GET_META:
397             return VLC_EGENERIC;
398
399         default:
400             msg_Warn( p_access, "unimplemented query in control" );
401             return VLC_EGENERIC;
402
403     }
404     return VLC_SUCCESS;
405 }