]> git.sesse.net Git - vlc/blob - modules/access/directory.c
Access strings (Refs:#438)
[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 * );
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_pos;
202
203     playlist_item_t *p_item;
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     else
216     {
217         char *ptr;
218
219         psz_name = ToLocale( p_access->psz_path );
220         ptr = strdup( psz_name );
221         LocaleFree( psz_name );
222         if( ptr == NULL )
223             goto end;
224
225         psz_name = ptr;
226
227         /* Remove the ending '/' char */
228         ptr += strlen( ptr );
229         if( ( ptr > psz_name ) )
230         {
231             switch( *--ptr )
232             {
233                 case '/':
234                 case '\\':
235                     *ptr = '\0';
236             }
237         }
238     }
239
240     /* Initialize structure */
241     psz = var_CreateGetString( p_access, "recursive" );
242     if( *psz == '\0' || !strncmp( psz, "none" , 4 )  )
243     {
244         i_mode = MODE_NONE;
245     }
246     else if( !strncmp( psz, "collapse", 8 )  )
247     {
248         i_mode = MODE_COLLAPSE;
249     }
250     else
251     {
252         i_mode = MODE_EXPAND;
253     }
254     free( psz );
255
256     /* Make sure we are deleted when we are done */
257     /* The playlist position we will use for the add */
258     i_pos = p_playlist->i_index + 1;
259
260     msg_Dbg( p_access, "opening directory `%s'", p_access->psz_path );
261
262     if( &p_playlist->status.p_item->input ==
263         ((input_thread_t *)p_access->p_parent)->input.p_item )
264     {
265         p_item = p_playlist->status.p_item;
266         b_play = VLC_TRUE;
267         msg_Dbg( p_access, "starting directory playback");
268     }
269     else
270     {
271         input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
272                                                         input.p_item;
273         p_item = playlist_LockItemGetByInput( p_playlist, p_current );
274         msg_Dbg( p_access, "not starting directory playback");
275         if( !p_item )
276         {
277             msg_Dbg( p_playlist, "unable to find item in playlist");
278             return -1;
279         }
280         b_play = VLC_FALSE;
281     }
282
283     p_item->input.i_type = ITEM_TYPE_DIRECTORY;
284     if( ReadDir( p_playlist, psz_name , i_mode, p_item ) != VLC_SUCCESS )
285     {
286     }
287 end:
288
289     /* Begin to read the directory */
290     if( b_play )
291     {
292         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
293                           p_playlist->status.i_view,
294                           p_playlist->status.p_item, NULL );
295     }
296     if( psz_name ) free( psz_name );
297     vlc_object_release( p_playlist );
298
299     /* Return fake data forever */
300     p_access->pf_read = ReadNull;
301     return ReadNull( p_access, p_buffer, i_len );
302 }
303
304 /*****************************************************************************
305  * DemuxOpen:
306  *****************************************************************************/
307 static int Control( access_t *p_access, int i_query, va_list args )
308 {
309     vlc_bool_t   *pb_bool;
310     int          *pi_int;
311     int64_t      *pi_64;
312
313     switch( i_query )
314     {
315         /* */
316         case ACCESS_CAN_SEEK:
317         case ACCESS_CAN_FASTSEEK:
318         case ACCESS_CAN_PAUSE:
319         case ACCESS_CAN_CONTROL_PACE:
320             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
321             *pb_bool = VLC_FALSE;    /* FIXME */
322             break;
323
324         /* */
325         case ACCESS_GET_MTU:
326             pi_int = (int*)va_arg( args, int * );
327             *pi_int = 0;
328             break;
329
330         case ACCESS_GET_PTS_DELAY:
331             pi_64 = (int64_t*)va_arg( args, int64_t * );
332             *pi_64 = DEFAULT_PTS_DELAY * 1000;
333             break;
334
335         /* */
336         case ACCESS_SET_PAUSE_STATE:
337         case ACCESS_GET_TITLE_INFO:
338         case ACCESS_SET_TITLE:
339         case ACCESS_SET_SEEKPOINT:
340         case ACCESS_SET_PRIVATE_ID_STATE:
341             return VLC_EGENERIC;
342
343         default:
344             msg_Warn( p_access, "unimplemented query in control" );
345             return VLC_EGENERIC;
346     }
347     return VLC_SUCCESS;
348 }
349
350 /*****************************************************************************
351  * DemuxOpen:
352  *****************************************************************************/
353 static int DemuxOpen ( vlc_object_t *p_this )
354 {
355     demux_t *p_demux = (demux_t*)p_this;
356
357     if( strcmp( p_demux->psz_demux, "directory" ) )
358         return VLC_EGENERIC;
359
360     p_demux->pf_demux   = Demux;
361     p_demux->pf_control = DemuxControl;
362     return VLC_SUCCESS;
363 }
364
365 /*****************************************************************************
366  * Demux: EOF
367  *****************************************************************************/
368 static int Demux( demux_t *p_demux )
369 {
370     return 0;
371 }
372
373 /*****************************************************************************
374  * DemuxControl:
375  *****************************************************************************/
376 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
377 {
378     return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
379 }
380
381 static int Filter( const struct dirent *foo )
382 {
383     return VLC_TRUE;
384 }
385
386 /*****************************************************************************
387  * ReadDir: read a directory and add its content to the list
388  *****************************************************************************/
389 static int ReadDir( playlist_t *p_playlist, const char *psz_name,
390                     int i_mode, playlist_item_t *p_parent )
391 {
392     struct dirent   **pp_dir_content;
393     int             i_dir_content, i;
394     playlist_item_t *p_node;
395
396     /* Build array with ignores */
397     char **ppsz_extensions = 0;
398     int i_extensions = 0;
399     char *psz_ignore = var_CreateGetString( p_playlist, "ignore-filetypes" );
400     if( psz_ignore && *psz_ignore )
401     {
402         char *psz_parser = psz_ignore;
403         int a;
404
405         for( a = 0; psz_parser[a] != '\0'; a++ )
406         {
407             if( psz_parser[a] == ',' ) i_extensions++;
408         }
409
410         ppsz_extensions = (char **)malloc( sizeof( char * ) * i_extensions );
411
412         for( a = 0; a < i_extensions; a++ )
413         {
414             int b;
415             char *tmp;
416
417             while( psz_parser[0] != '\0' && psz_parser[0] == ' ' ) psz_parser++;
418             for( b = 0; psz_parser[b] != '\0'; b++ )
419             {
420                 if( psz_parser[b] == ',' ) break;
421             }
422             tmp = malloc( b + 1 );
423             strncpy( tmp, psz_parser, b );
424             tmp[b] = 0;
425             ppsz_extensions[a] = tmp;
426             psz_parser += b+1;
427         }
428     }
429
430     /* Change the item to a node */
431     if( p_parent->i_children == -1 )
432     {
433         playlist_LockItemToNode( p_playlist,p_parent );
434     }
435
436     /* get the first directory entry */
437     i_dir_content = scandir( psz_name, &pp_dir_content, Filter, alphasort );
438     if( i_dir_content == -1 )
439     {
440         msg_Warn( p_playlist, "Failed to read directory" );
441         return VLC_EGENERIC;
442     }
443     else if( i_dir_content <= 0 )
444     {
445         /* directory is empty */
446         return VLC_SUCCESS;
447     }
448
449     /* While we still have entries in the directory */
450     for( i = 0; i < i_dir_content; i++ )
451     {
452         struct dirent *p_dir_content = pp_dir_content[i];
453         int i_size_entry = strlen( psz_name ) +
454                            strlen( p_dir_content->d_name ) + 2;
455         char *psz_uri = (char *)malloc( sizeof(char) * i_size_entry );
456
457         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
458
459         /* if it starts with '.' then forget it */
460         if( p_dir_content->d_name[0] != '.' )
461         {
462 #if defined( S_ISDIR )
463             struct stat stat_data;
464
465             if( !stat( psz_uri, &stat_data )
466              && S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
467 #elif defined( DT_DIR )
468             if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
469 #else
470             if( 0 )
471 #endif
472             {
473 #if defined( S_ISLNK )
474 /*
475  * FIXME: there is a ToCToU race condition here; but it is rather tricky
476  * impossible to fix while keeping some kind of portable code, and maybe even
477  * in a non-portable way.
478  */
479                 if( lstat( psz_uri, &stat_data )
480                  || S_ISLNK(stat_data.st_mode) )
481                 {
482                     msg_Dbg( p_playlist, "Skipping directory symlink %s",
483                              psz_uri );
484                     free( psz_uri );
485                     continue;
486                 }
487 #endif
488                 if( i_mode == MODE_NONE )
489                 {
490                     msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
491                     free( psz_uri );
492                     continue;
493                 }
494                 else if( i_mode == MODE_EXPAND )
495                 {
496                     char *psz_newname, *psz_tmp;
497                     msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
498
499                     psz_tmp = FromLocale( p_dir_content->d_name );
500                     psz_newname = vlc_fix_readdir_charset(
501                                                 p_playlist, psz_tmp );
502                     LocaleFree( psz_tmp );
503
504                     p_node = playlist_NodeCreate( p_playlist,
505                                        p_parent->pp_parents[0]->i_view,
506                                        psz_newname, p_parent );
507
508                     playlist_CopyParents(  p_parent, p_node );
509
510                     p_node->input.i_type = ITEM_TYPE_DIRECTORY;
511
512                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
513                                  p_node ) != VLC_SUCCESS )
514                     {
515                         return VLC_EGENERIC;
516                     }
517
518                     /* an strdup() just because of Mac OS X */
519                     free( psz_newname );
520                 }
521             }
522             else
523             {
524                 playlist_item_t *p_item;
525                 char *psz_tmp1, *psz_tmp2, *psz_loc;
526
527                 if( i_extensions > 0 )
528                 {
529                     char *psz_dot = strrchr( p_dir_content->d_name, '.' );
530                     if( psz_dot++ && *psz_dot )
531                     {
532                         int a;
533                         for( a = 0; a < i_extensions; a++ )
534                         {
535                             if( !strcmp( psz_dot, ppsz_extensions[a] ) )
536                                 break;
537                         }
538                         if( a < i_extensions )
539                         {
540                             msg_Dbg( p_playlist, "Ignoring file %s", psz_uri );
541                             free( psz_uri );
542                             continue;
543                         }
544                     }
545                 }
546
547                 psz_loc = FromLocale( psz_uri );
548                 psz_tmp1 = vlc_fix_readdir_charset( VLC_OBJECT(p_playlist),
549                                                     psz_loc );
550                 LocaleFree( psz_loc );
551
552                 psz_loc = FromLocale( p_dir_content->d_name );
553                 psz_tmp2 = vlc_fix_readdir_charset( VLC_OBJECT(p_playlist),
554                                                     psz_loc );
555                 LocaleFree( psz_loc );
556
557                 p_item = playlist_ItemNewWithType( VLC_OBJECT(p_playlist),
558                         psz_tmp1, psz_tmp2, ITEM_TYPE_VFILE );
559                 playlist_NodeAddItem( p_playlist,p_item,
560                                       p_parent->pp_parents[0]->i_view,
561                                       p_parent,
562                                       PLAYLIST_APPEND | PLAYLIST_PREPARSE,
563                                       PLAYLIST_END );
564
565                 playlist_CopyParents( p_parent, p_item );
566             }
567         }
568         free( psz_uri );
569     }
570
571     for( i = 0; i < i_extensions; i++ )
572     {
573         if( ppsz_extensions[i] )
574             free( ppsz_extensions[i] );
575     }
576     if( ppsz_extensions ) free( ppsz_extensions );
577     if( psz_ignore ) free( psz_ignore );
578
579     for( i = 0; i < i_dir_content; i++ )
580         if( pp_dir_content[i] ) free( pp_dir_content[i] );
581     if( pp_dir_content ) free( pp_dir_content );
582     return VLC_SUCCESS;
583 }