]> git.sesse.net Git - vlc/blob - modules/access/directory.c
Merge back branch 0.8.6-playlist-vlm to trunk.
[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     int  i_mode, i_activity;
202
203     playlist_item_t *p_item, *p_root_category;
204     vlc_bool_t b_play = VLC_FALSE;
205
206     playlist_t *p_playlist =
207         (playlist_t *) vlc_object_find( p_access,
208                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
209
210     if( !p_playlist )
211     {
212         msg_Err( p_access, "can't find playlist" );
213         goto end;
214     }
215
216     char *ptr;
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;
391     int             i_dir_content, i;
392     playlist_item_t *p_node;
393
394     /* Build array with ignores */
395     char **ppsz_extensions = 0;
396     int i_extensions = 0;
397     char *psz_ignore = var_CreateGetString( p_playlist, "ignore-filetypes" );
398     if( psz_ignore && *psz_ignore )
399     {
400         char *psz_parser = psz_ignore;
401         int a;
402
403         for( a = 0; psz_parser[a] != '\0'; a++ )
404         {
405             if( psz_parser[a] == ',' ) i_extensions++;
406         }
407
408         ppsz_extensions = (char **)malloc( sizeof( char * ) * i_extensions );
409
410         for( a = 0; a < i_extensions; a++ )
411         {
412             char *tmp, *ptr;
413
414             while( psz_parser[0] != '\0' && psz_parser[0] == ' ' ) psz_parser++;
415             ptr = strchr( psz_parser, ',');
416             tmp = ( ptr == NULL )
417                  ? strdup( psz_parser )
418                  : strndup( psz_parser, ptr - psz_parser );
419
420             ppsz_extensions[a] = tmp;
421             psz_parser = ptr + 1;
422         }
423     }
424
425     /* get the first directory entry */
426     i_dir_content = scandir( psz_name, &pp_dir_content, Filter, alphasort );
427     if( i_dir_content == -1 )
428     {
429         msg_Warn( p_playlist, "failed to read directory" );
430         return VLC_EGENERIC;
431     }
432     else if( i_dir_content <= 0 )
433     {
434         /* directory is empty */
435         return VLC_SUCCESS;
436     }
437
438     /* While we still have entries in the directory */
439     for( i = 0; i < i_dir_content; i++ )
440     {
441         struct dirent *p_dir_content = pp_dir_content[i];
442         int i_size_entry = strlen( psz_name ) +
443                            strlen( p_dir_content->d_name ) + 2;
444         char *psz_uri = (char *)malloc( sizeof(char) * i_size_entry );
445
446         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
447
448         /* if it starts with '.' then forget it */
449         if( p_dir_content->d_name[0] != '.' )
450         {
451 #if defined( S_ISDIR )
452             struct stat stat_data;
453
454             if( !stat( psz_uri, &stat_data )
455              && S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
456 #elif defined( DT_DIR )
457             if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
458 #else
459             if( 0 )
460 #endif
461             {
462 #if defined( S_ISLNK )
463 /*
464  * FIXME: there is a ToCToU race condition here; but it is rather tricky
465  * impossible to fix while keeping some kind of portable code, and maybe even
466  * in a non-portable way.
467  */
468                 if( lstat( psz_uri, &stat_data )
469                  || S_ISLNK(stat_data.st_mode) )
470                 {
471                     msg_Dbg( p_playlist, "skipping directory symlink %s",
472                              psz_uri );
473                     free( psz_uri );
474                     continue;
475                 }
476 #endif
477                 if( i_mode == MODE_NONE )
478                 {
479                     msg_Dbg( p_playlist, "skipping subdirectory %s", psz_uri );
480                     free( psz_uri );
481                     continue;
482                 }
483                 else if( i_mode == MODE_EXPAND )
484                 {
485                     char *psz_newname, *psz_tmp;
486                     msg_Dbg(p_playlist, "reading subdirectory %s", psz_uri );
487
488                     psz_tmp = FromLocale( p_dir_content->d_name );
489                     psz_newname = vlc_fix_readdir_charset(
490                                                 p_playlist, psz_tmp );
491                     LocaleFree( psz_tmp );
492
493                     if( p_parent_category )
494                     {
495                         p_node = playlist_NodeCreate( p_playlist, psz_newname,
496                                                       p_parent_category );
497                     }
498                     else
499                     {
500                         p_node = playlist_NodeCreate( p_playlist, psz_newname,
501                                                       p_parent_category );
502                     }
503
504                     /* If we had the parent in category, the it is now node.
505                      * Else, we still don't have  */
506                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
507                                  p_node, p_parent_category ? p_node : NULL )
508                           != VLC_SUCCESS )
509                     {
510                         return VLC_EGENERIC;
511                     }
512
513                     /* an strdup() just because of Mac OS X */
514                     free( psz_newname );
515                 }
516             }
517             else
518             {
519                 input_item_t *p_input;
520                 char *psz_tmp1, *psz_tmp2, *psz_loc;
521
522                 if( i_extensions > 0 )
523                 {
524                     char *psz_dot = strrchr( p_dir_content->d_name, '.' );
525                     if( psz_dot++ && *psz_dot )
526                     {
527                         int a;
528                         for( a = 0; a < i_extensions; a++ )
529                         {
530                             if( !strcmp( psz_dot, ppsz_extensions[a] ) )
531                                 break;
532                         }
533                         if( a < i_extensions )
534                         {
535                             msg_Dbg( p_playlist, "ignoring file %s", psz_uri );
536                             free( psz_uri );
537                             continue;
538                         }
539                     }
540                 }
541
542                 psz_loc = FromLocale( psz_uri );
543                 psz_tmp1 = vlc_fix_readdir_charset( VLC_OBJECT(p_playlist),
544                                                     psz_loc );
545                 LocaleFree( psz_loc );
546
547                 psz_loc = FromLocale( p_dir_content->d_name );
548                 psz_tmp2 = vlc_fix_readdir_charset( VLC_OBJECT(p_playlist),
549                                                     psz_loc );
550                 LocaleFree( psz_loc );
551
552                 p_input = input_ItemNewWithType( VLC_OBJECT(p_playlist),
553                                                  psz_tmp1, psz_tmp2, 0, NULL,
554                                                  -1, ITEM_TYPE_VFILE );
555
556                 playlist_AddWhereverNeeded( p_playlist, p_input, p_parent,
557                                             p_parent_category, VLC_FALSE,
558                                             PLAYLIST_APPEND|PLAYLIST_PREPARSE);
559             }
560         }
561         free( psz_uri );
562     }
563
564     for( i = 0; i < i_extensions; i++ )
565     {
566         if( ppsz_extensions[i] )
567             free( ppsz_extensions[i] );
568     }
569     if( ppsz_extensions ) free( ppsz_extensions );
570     if( psz_ignore ) free( psz_ignore );
571
572     for( i = 0; i < i_dir_content; i++ )
573         if( pp_dir_content[i] ) free( pp_dir_content[i] );
574     if( pp_dir_content ) free( pp_dir_content );
575     return VLC_SUCCESS;
576 }