]> git.sesse.net Git - vlc/blob - modules/access/directory.c
* modules/access/file.c, directory.c: bug and memory leak fixes
[vlc] / modules / access / directory.c
1 /*****************************************************************************
2  * directory.c: expands a directory (directory: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
31
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_SYS_TYPES_H
35 #   include <sys/types.h>
36 #endif
37 #ifdef HAVE_SYS_STAT_H
38 #   include <sys/stat.h>
39 #endif
40 #ifdef HAVE_ERRNO_H
41 #   include <errno.h>
42 #endif
43 #ifdef HAVE_FCNTL_H
44 #   include <fcntl.h>
45 #endif
46
47 #ifdef HAVE_UNISTD_H
48 #   include <unistd.h>
49 #elif defined( WIN32 ) && !defined( UNDER_CE )
50 #   include <io.h>
51 #elif defined( UNDER_CE )
52 #   define strcoll strcmp
53 #endif
54
55 #ifdef HAVE_DIRENT_H
56 #   include <dirent.h>
57 #endif
58
59 #include "charset.h"
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 static int  Open ( vlc_object_t * );
65 static void Close( vlc_object_t * );
66
67 static int  DemuxOpen ( vlc_object_t * );
68
69 #define RECURSIVE_TEXT N_("Subdirectory behavior")
70 #define RECURSIVE_LONGTEXT N_( \
71         "Select whether subdirectories must be expanded.\n" \
72         "none: subdirectories do not appear in the playlist.\n" \
73         "collapse: subdirectories appear but are expanded on first play.\n" \
74         "expand: all subdirectories are expanded.\n" )
75
76 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
77 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
78                                            N_("expand") };
79
80 #define IGNORE_TEXT N_("Ignored extensions")
81 #define IGNORE_LONGTEXT N_( \
82         "Files with these extensions will not be added to playlist when " \
83         "opening a directory.\n" \
84         "This is useful if you add directories that contain playlist files " \
85         "for instance. Use a comma-separated list of extensions." )
86
87 vlc_module_begin();
88     set_category( CAT_INPUT );
89     set_shortname( _("Directory" ) );
90     set_subcategory( SUBCAT_INPUT_ACCESS );
91     set_description( _("Standard filesystem directory input") );
92     set_capability( "access2", 55 );
93     add_shortcut( "directory" );
94     add_shortcut( "dir" );
95     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
96                 RECURSIVE_LONGTEXT, VLC_FALSE );
97       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
98     add_string( "ignore-filetypes", "m3u,db,nfo,jpg,gif,sfv,txt,sub,idx,srt,cue",
99                 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, VLC_FALSE );
100     set_callbacks( Open, Close );
101
102     add_submodule();
103         set_description( "Directory EOF");
104         set_capability( "demux2", 0 );
105         add_shortcut( "directory" );
106         set_callbacks( DemuxOpen, NULL );
107 vlc_module_end();
108
109
110 /*****************************************************************************
111  * Local prototypes, constants, structures
112  *****************************************************************************/
113
114 #define MODE_EXPAND 0
115 #define MODE_COLLAPSE 1
116 #define MODE_NONE 2
117
118 static int Read( access_t *, uint8_t *, int );
119 static int ReadNull( access_t *, uint8_t *, int );
120 static int Control( access_t *, int, va_list );
121
122 static int Demux( demux_t *p_demux );
123 static int DemuxControl( demux_t *p_demux, int i_query, va_list args );
124
125
126 static int ReadDir( playlist_t *, const char *psz_name, int i_mode,
127                     playlist_item_t *, playlist_item_t * );
128
129 /*****************************************************************************
130  * Open: open the directory
131  *****************************************************************************/
132 static int Open( vlc_object_t *p_this )
133 {
134     access_t *p_access = (access_t*)p_this;
135
136 #ifdef HAVE_SYS_STAT_H
137     struct stat stat_info;
138     char *psz_path = ToLocale( p_access->psz_path );
139
140     if( ( stat( psz_path, &stat_info ) == -1 ) ||
141         !S_ISDIR( stat_info.st_mode ) )
142 #elif defined(WIN32)
143     int i_ret;
144
145 #   ifdef UNICODE
146     wchar_t psz_path[MAX_PATH];
147     mbstowcs( psz_path, p_access->psz_path, MAX_PATH );
148     psz_path[MAX_PATH-1] = 0;
149 #   else
150     char *psz_path = p_access->psz_path;
151 #   endif /* UNICODE */
152
153     i_ret = GetFileAttributes( psz_path );
154     if( i_ret == -1 || !(i_ret & FILE_ATTRIBUTE_DIRECTORY) )
155
156 #else
157     if( strcmp( p_access->psz_access, "dir") &&
158         strcmp( p_access->psz_access, "directory") )
159 #endif
160     {
161         LocaleFree( psz_path );
162         return VLC_EGENERIC;
163     }
164
165     LocaleFree( psz_path );
166     p_access->pf_read  = Read;
167     p_access->pf_block = NULL;
168     p_access->pf_seek  = NULL;
169     p_access->pf_control= Control;
170
171     /* Force a demux */
172     p_access->psz_demux = strdup( "directory" );
173
174     return VLC_SUCCESS;
175 }
176
177 /*****************************************************************************
178  * Close: close the target
179  *****************************************************************************/
180 static void Close( vlc_object_t * p_this )
181 {
182 }
183
184 /*****************************************************************************
185  * ReadNull: read the directory
186  *****************************************************************************/
187 static int ReadNull( access_t *p_access, uint8_t *p_buffer, int i_len)
188 {
189     /* Return fake data */
190     memset( p_buffer, 0, i_len );
191     return i_len;
192 }
193
194 /*****************************************************************************
195  * Read: read the directory
196  *****************************************************************************/
197 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len)
198 {
199     char *psz_name = NULL;
200     char *psz;
201     char *ptr;
202     int  i_mode, i_activity;
203
204     playlist_item_t *p_item, *p_root_category;
205     vlc_bool_t b_play = VLC_FALSE;
206
207     playlist_t *p_playlist =
208         (playlist_t *) vlc_object_find( p_access,
209                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
210
211     if( !p_playlist )
212     {
213         msg_Err( p_access, "can't find playlist" );
214         goto end;
215     }
216
217     psz_name = ToLocale( p_access->psz_path );
218     ptr = strdup( psz_name );
219     LocaleFree( psz_name );
220     if( ptr == NULL )
221         goto end;
222
223     psz_name = ptr;
224
225     /* Remove the ending '/' char */
226     ptr += strlen( ptr );
227     if( ( ptr > psz_name ) )
228     {
229         switch( *--ptr )
230         {
231             case '/':
232             case '\\':
233                 *ptr = '\0';
234         }
235     }
236
237     /* Handle mode */
238     psz = var_CreateGetString( p_access, "recursive" );
239     if( *psz == '\0' || !strncmp( psz, "none" , 4 )  )
240         i_mode = MODE_NONE;
241     else if( !strncmp( psz, "collapse", 8 )  )
242         i_mode = MODE_COLLAPSE;
243     else
244         i_mode = MODE_EXPAND;
245     free( psz );
246
247     msg_Dbg( p_access, "opening directory `%s'", p_access->psz_path );
248
249     if( p_playlist->status.p_item->p_input ==
250         ((input_thread_t *)p_access->p_parent)->input.p_item )
251     {
252         p_item = p_playlist->status.p_item;
253         b_play = VLC_TRUE;
254         msg_Dbg( p_access, "starting directory playback");
255     }
256     else
257     {
258         input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
259                                                         input.p_item;
260         p_item = playlist_LockItemGetByInput( p_playlist, p_current );
261         msg_Dbg( p_access, "not starting directory playback");
262         if( !p_item )
263         {
264             msg_Dbg( p_playlist, "unable to find item in playlist");
265             return -1;
266         }
267         b_play = VLC_FALSE;
268     }
269     p_item->p_input->i_type = ITEM_TYPE_DIRECTORY;
270
271     p_root_category = playlist_LockItemToNode( p_playlist, p_item );
272
273     i_activity = var_GetInteger( p_playlist, "activity" );
274     var_SetInteger( p_playlist, "activity", i_activity +
275                     DIRECTORY_ACTIVITY );
276
277     ReadDir( p_playlist, psz_name , i_mode, p_item, p_root_category );
278
279     i_activity = var_GetInteger( p_playlist, "activity" );
280     var_SetInteger( p_playlist, "activity", i_activity -
281                     DIRECTORY_ACTIVITY );
282 end:
283
284     /* Begin to read the directory */
285     if( b_play )
286     {
287 #if 0
288        /// \bug we can start playing an already deleted item. Fix ?*/
289        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, 1242,
290                           p_playlist->status.p_item, NULL );
291 #endif
292     }
293     if( psz_name ) free( psz_name );
294     vlc_object_release( p_playlist );
295
296     /* Return fake data forever */
297     p_access->pf_read = ReadNull;
298     return ReadNull( p_access, p_buffer, i_len );
299 }
300
301 /*****************************************************************************
302  * DemuxOpen:
303  *****************************************************************************/
304 static int Control( access_t *p_access, int i_query, va_list args )
305 {
306     vlc_bool_t   *pb_bool;
307     int          *pi_int;
308     int64_t      *pi_64;
309
310     switch( i_query )
311     {
312         /* */
313         case ACCESS_CAN_SEEK:
314         case ACCESS_CAN_FASTSEEK:
315         case ACCESS_CAN_PAUSE:
316         case ACCESS_CAN_CONTROL_PACE:
317             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
318             *pb_bool = VLC_FALSE;    /* FIXME */
319             break;
320
321         /* */
322         case ACCESS_GET_MTU:
323             pi_int = (int*)va_arg( args, int * );
324             *pi_int = 0;
325             break;
326
327         case ACCESS_GET_PTS_DELAY:
328             pi_64 = (int64_t*)va_arg( args, int64_t * );
329             *pi_64 = DEFAULT_PTS_DELAY * 1000;
330             break;
331
332         /* */
333         case ACCESS_SET_PAUSE_STATE:
334         case ACCESS_GET_TITLE_INFO:
335         case ACCESS_SET_TITLE:
336         case ACCESS_SET_SEEKPOINT:
337         case ACCESS_SET_PRIVATE_ID_STATE:
338             return VLC_EGENERIC;
339
340         default:
341             msg_Warn( p_access, "unimplemented query in control" );
342             return VLC_EGENERIC;
343     }
344     return VLC_SUCCESS;
345 }
346
347 /*****************************************************************************
348  * DemuxOpen:
349  *****************************************************************************/
350 static int DemuxOpen ( vlc_object_t *p_this )
351 {
352     demux_t *p_demux = (demux_t*)p_this;
353
354     if( strcmp( p_demux->psz_demux, "directory" ) )
355         return VLC_EGENERIC;
356
357     p_demux->pf_demux   = Demux;
358     p_demux->pf_control = DemuxControl;
359     return VLC_SUCCESS;
360 }
361
362 /*****************************************************************************
363  * Demux: EOF
364  *****************************************************************************/
365 static int Demux( demux_t *p_demux )
366 {
367     return 0;
368 }
369
370 /*****************************************************************************
371  * DemuxControl:
372  *****************************************************************************/
373 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
374 {
375     return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
376 }
377
378 static int Filter( const struct dirent *foo )
379 {
380     return VLC_TRUE;
381 }
382
383 /*****************************************************************************
384  * ReadDir: read a directory and add its content to the list
385  *****************************************************************************/
386 static int ReadDir( playlist_t *p_playlist, const char *psz_name,
387                     int i_mode, playlist_item_t *p_parent,
388                     playlist_item_t *p_parent_category )
389 {
390     struct dirent   **pp_dir_content = 0;
391     int             i_dir_content, i, i_return = VLC_SUCCESS;
392     playlist_item_t *p_node;
393
394     char **ppsz_extensions = 0;
395     int i_extensions = 0;
396     char *psz_ignore;
397
398     /* Get the first directory entry */
399     i_dir_content = scandir( psz_name, &pp_dir_content, Filter, alphasort );
400     if( i_dir_content == -1 )
401     {
402         msg_Warn( p_playlist, "failed to read directory" );
403         return VLC_EGENERIC;
404     }
405     else if( i_dir_content <= 0 )
406     {
407         /* directory is empty */
408         if( pp_dir_content ) free( pp_dir_content );
409         return VLC_SUCCESS;
410     }
411
412     /* Build array with ignores */
413     psz_ignore = var_CreateGetString( p_playlist, "ignore-filetypes" );
414     if( psz_ignore && *psz_ignore )
415     {
416         char *psz_parser = psz_ignore;
417         int a;
418
419         for( a = 0; psz_parser[a] != '\0'; a++ )
420         {
421             if( psz_parser[a] == ',' ) i_extensions++;
422         }
423
424         ppsz_extensions = (char **)malloc( sizeof( char * ) * i_extensions );
425
426         for( a = 0; a < i_extensions; a++ )
427         {
428             char *tmp, *ptr;
429
430             while( psz_parser[0] != '\0' && psz_parser[0] == ' ' ) psz_parser++;
431             ptr = strchr( psz_parser, ',');
432             tmp = ( ptr == NULL )
433                  ? strdup( psz_parser )
434                  : strndup( psz_parser, ptr - psz_parser );
435
436             ppsz_extensions[a] = tmp;
437             psz_parser = ptr + 1;
438         }
439     }
440
441     /* While we still have entries in the directory */
442     for( i = 0; i < i_dir_content; i++ )
443     {
444         struct dirent *p_dir_content = pp_dir_content[i];
445         int i_size_entry = strlen( psz_name ) +
446                            strlen( p_dir_content->d_name ) + 2;
447         char *psz_uri = (char *)malloc( sizeof(char) * i_size_entry );
448
449         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
450
451         /* if it starts with '.' then forget it */
452         if( p_dir_content->d_name[0] != '.' )
453         {
454 #if defined( S_ISDIR )
455             struct stat stat_data;
456
457             if( !stat( psz_uri, &stat_data )
458              && S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
459 #elif defined( DT_DIR )
460             if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
461 #else
462             if( 0 )
463 #endif
464             {
465 #if defined( S_ISLNK )
466 /*
467  * FIXME: there is a ToCToU race condition here; but it is rather tricky
468  * impossible to fix while keeping some kind of portable code, and maybe even
469  * in a non-portable way.
470  */
471                 if( lstat( psz_uri, &stat_data )
472                  || S_ISLNK(stat_data.st_mode) )
473                 {
474                     msg_Dbg( p_playlist, "skipping directory symlink %s",
475                              psz_uri );
476                     free( psz_uri );
477                     continue;
478                 }
479 #endif
480                 if( i_mode == MODE_NONE )
481                 {
482                     msg_Dbg( p_playlist, "skipping subdirectory %s", psz_uri );
483                     free( psz_uri );
484                     continue;
485                 }
486                 else if( i_mode == MODE_EXPAND )
487                 {
488                     char *psz_newname, *psz_tmp;
489                     msg_Dbg(p_playlist, "reading subdirectory %s", psz_uri );
490
491                     psz_tmp = FromLocale( p_dir_content->d_name );
492                     psz_newname = vlc_fix_readdir_charset(
493                                                 p_playlist, psz_tmp );
494                     LocaleFree( psz_tmp );
495
496                     if( p_parent_category )
497                     {
498                         p_node = playlist_NodeCreate( p_playlist, psz_newname,
499                                                       p_parent_category );
500                     }
501                     else
502                     {
503                         p_node = playlist_NodeCreate( p_playlist, psz_newname,
504                                                       p_parent_category );
505                     }
506
507                     /* an strdup() just because of Mac OS X */
508                     free( psz_newname );
509
510                     /* If we had the parent in category, the it is now node.
511                      * Else, we still don't have  */
512                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
513                                  p_node, p_parent_category ? p_node : NULL )
514                           != VLC_SUCCESS )
515                     {
516                         i_return = VLC_EGENERIC;
517                         break;
518                     }
519                 }
520             }
521             else
522             {
523                 input_item_t *p_input;
524                 char *psz_tmp1, *psz_tmp2, *psz_loc;
525
526                 if( i_extensions > 0 )
527                 {
528                     char *psz_dot = strrchr( p_dir_content->d_name, '.' );
529                     if( psz_dot++ && *psz_dot )
530                     {
531                         int a;
532                         for( a = 0; a < i_extensions; a++ )
533                         {
534                             if( !strcmp( psz_dot, ppsz_extensions[a] ) )
535                                 break;
536                         }
537                         if( a < i_extensions )
538                         {
539                             msg_Dbg( p_playlist, "ignoring file %s", psz_uri );
540                             free( psz_uri );
541                             continue;
542                         }
543                     }
544                 }
545
546                 psz_loc = FromLocale( psz_uri );
547                 psz_tmp1 = vlc_fix_readdir_charset( VLC_OBJECT(p_playlist),
548                                                     psz_loc );
549                 LocaleFree( psz_loc );
550
551                 psz_loc = FromLocale( p_dir_content->d_name );
552                 psz_tmp2 = vlc_fix_readdir_charset( VLC_OBJECT(p_playlist),
553                                                     psz_loc );
554                 LocaleFree( psz_loc );
555
556                 p_input = input_ItemNewWithType( VLC_OBJECT(p_playlist),
557                                                  psz_tmp1, psz_tmp2, 0, NULL,
558                                                  -1, ITEM_TYPE_VFILE );
559
560                 playlist_AddWhereverNeeded( p_playlist, p_input, p_parent,
561                                             p_parent_category, VLC_FALSE,
562                                             PLAYLIST_APPEND|PLAYLIST_PREPARSE);
563             }
564         }
565         free( psz_uri );
566     }
567
568     for( i = 0; i < i_extensions; i++ )
569         if( ppsz_extensions[i] ) free( ppsz_extensions[i] );
570     if( ppsz_extensions ) free( ppsz_extensions );
571
572     if( psz_ignore ) free( psz_ignore );
573
574     for( i = 0; i < i_dir_content; i++ )
575         if( pp_dir_content[i] ) free( pp_dir_content[i] );
576     if( pp_dir_content ) free( pp_dir_content );
577
578     return i_return;
579 }