1 /*****************************************************************************
2 * directory.c: expands a directory (directory: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
7 * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
49 #elif defined( WIN32 ) && !defined( UNDER_CE )
53 #if (!defined( WIN32 ) || defined(__MINGW32__))
54 /* Mingw has its own version of dirent */
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
64 static int DemuxOpen ( vlc_object_t * );
66 #define RECURSIVE_TEXT N_("Subdirectory behavior")
67 #define RECURSIVE_LONGTEXT N_( \
68 "Select whether subdirectories must be expanded.\n" \
69 "none: subdirectories do not appear in the playlist.\n" \
70 "collapse: subdirectories appear but are expanded on first play.\n" \
71 "expand: all subdirectories are expanded.\n" )
73 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
74 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
78 set_category( CAT_INPUT );
79 set_name( _("Directory" ) );
80 set_subcategory( SUBCAT_INPUT_ACCESS );
81 set_description( _("Standard filesystem directory input") );
82 set_capability( "access2", 55 );
83 add_shortcut( "directory" );
84 add_shortcut( "dir" );
85 add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
86 RECURSIVE_LONGTEXT, VLC_FALSE );
87 change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
88 set_callbacks( Open, Close );
91 set_description( "Directory EOF");
92 set_capability( "demux2", 0 );
93 add_shortcut( "directory" );
94 set_callbacks( DemuxOpen, NULL );
98 /*****************************************************************************
99 * Local prototypes, constants, structures
100 *****************************************************************************/
102 #define MODE_EXPAND 0
103 #define MODE_COLLAPSE 1
106 static int Read( access_t *, uint8_t *, int );
107 static int ReadNull( access_t *, uint8_t *, int );
108 static int Control( access_t *, int, va_list );
110 static int Demux( demux_t *p_demux );
111 static int DemuxControl( demux_t *p_demux, int i_query, va_list args );
114 static int ReadDir( playlist_t *, char *psz_name, int i_mode, int *pi_pos,
117 /*****************************************************************************
118 * Open: open the directory
119 *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
122 access_t *p_access = (access_t*)p_this;
124 #ifdef HAVE_SYS_STAT_H
125 struct stat stat_info;
127 if( ( stat( p_access->psz_path, &stat_info ) == -1 ) ||
128 !S_ISDIR( stat_info.st_mode ) )
130 if( strcmp( p_access->psz_access, "dir") &&
131 strcmp( p_access->psz_access, "directory") )
137 p_access->pf_read = Read;
138 p_access->pf_block = NULL;
139 p_access->pf_seek = NULL;
140 p_access->pf_control= Control;
143 p_access->psz_demux = strdup( "directory" );
148 /*****************************************************************************
149 * Close: close the target
150 *****************************************************************************/
151 static void Close( vlc_object_t * p_this )
155 /*****************************************************************************
156 * ReadNull: read the directory
157 *****************************************************************************/
158 static int ReadNull( access_t *p_access, uint8_t *p_buffer, int i_len)
160 /* Return fake data */
161 memset( p_buffer, 0, i_len );
165 /*****************************************************************************
166 * Read: read the directory
167 *****************************************************************************/
168 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len)
170 char *psz_name = NULL;
174 playlist_item_t *p_item;
175 vlc_bool_t b_play = VLC_FALSE;
177 playlist_t *p_playlist =
178 (playlist_t *) vlc_object_find( p_access,
179 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
183 msg_Err( p_access, "can't find playlist" );
187 /* Remove the ending '/' char */
188 psz_name = strdup( p_access->psz_path );
189 if( psz_name == NULL )
192 if( (psz_name[strlen(psz_name)-1] == '/') ||
193 (psz_name[strlen(psz_name)-1] == '\\') )
195 psz_name[strlen(psz_name)-1] = '\0';
198 /* Initialize structure */
199 psz = var_CreateGetString( p_access, "recursive" );
200 if( *psz == '\0' || !strncmp( psz, "none" , 4 ) )
204 else if( !strncmp( psz, "collapse", 8 ) )
206 i_mode = MODE_COLLAPSE;
210 i_mode = MODE_EXPAND;
214 /* Make sure we are deleted when we are done */
215 /* The playlist position we will use for the add */
216 i_pos = p_playlist->i_index + 1;
218 msg_Dbg( p_access, "opening directory `%s'", psz_name );
220 if( &p_playlist->status.p_item->input ==
221 ((input_thread_t *)p_access->p_parent)->input.p_item )
223 p_item = p_playlist->status.p_item;
225 msg_Dbg( p_access, "starting directory playback");
229 input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
231 p_item = playlist_LockItemGetByInput( p_playlist, p_current );
232 msg_Dbg( p_access, "not starting directory playback");
235 msg_Dbg( p_playlist, "unable to find item in playlist");
240 p_item->input.i_type = ITEM_TYPE_DIRECTORY;
241 if( ReadDir( p_playlist, psz_name , i_mode, &i_pos,
242 p_item ) != VLC_SUCCESS )
247 /* Begin to read the directory */
250 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
251 p_playlist->status.i_view,
252 p_playlist->status.p_item, NULL );
254 if( psz_name ) free( psz_name );
255 vlc_object_release( p_playlist );
257 /* Return fake data forever */
258 p_access->pf_read = ReadNull;
259 return ReadNull( p_access, p_buffer, i_len );
262 /*****************************************************************************
264 *****************************************************************************/
265 static int Control( access_t *p_access, int i_query, va_list args )
274 case ACCESS_CAN_SEEK:
275 case ACCESS_CAN_FASTSEEK:
276 case ACCESS_CAN_PAUSE:
277 case ACCESS_CAN_CONTROL_PACE:
278 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
279 *pb_bool = VLC_FALSE; /* FIXME */
284 pi_int = (int*)va_arg( args, int * );
288 case ACCESS_GET_PTS_DELAY:
289 pi_64 = (int64_t*)va_arg( args, int64_t * );
290 *pi_64 = DEFAULT_PTS_DELAY * 1000;
294 case ACCESS_SET_PAUSE_STATE:
295 case ACCESS_GET_TITLE_INFO:
296 case ACCESS_SET_TITLE:
297 case ACCESS_SET_SEEKPOINT:
298 case ACCESS_SET_PRIVATE_ID_STATE:
302 msg_Warn( p_access, "unimplemented query in control" );
308 /*****************************************************************************
310 *****************************************************************************/
311 static int DemuxOpen ( vlc_object_t *p_this )
313 demux_t *p_demux = (demux_t*)p_this;
315 if( strcmp( p_demux->psz_demux, "directory" ) )
318 p_demux->pf_demux = Demux;
319 p_demux->pf_control = DemuxControl;
323 /*****************************************************************************
325 *****************************************************************************/
326 static int Demux( demux_t *p_demux )
330 /*****************************************************************************
332 *****************************************************************************/
333 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
335 return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
338 #if defined(SYS_BEOS) || defined(WIN32)
339 /* BeOS doesn't have scandir/alphasort/versionsort */
340 static int alphasort( const struct dirent **a, const struct dirent **b )
342 return strcoll( (*a)->d_name, (*b)->d_name );
345 static int scandir( const char *name, struct dirent ***namelist,
346 int (*filter) ( const struct dirent * ),
347 int (*compar) ( const struct dirent **,
348 const struct dirent ** ) )
351 struct dirent * p_content;
352 struct dirent ** pp_list;
355 if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
359 while( ( p_content = readdir( p_dir ) ) )
361 if( filter && !filter( p_content ) )
365 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
366 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
367 pp_list[ret] = malloc( size );
368 memcpy( pp_list[ret], p_content, size );
376 qsort( pp_list, ret, sizeof( struct dirent * ),
377 (int (*)(const void *, const void *)) compar );
385 static int Filter( const struct dirent *foo )
389 /*****************************************************************************
390 * ReadDir: read a directory and add its content to the list
391 *****************************************************************************/
392 static int ReadDir( playlist_t *p_playlist,
393 char *psz_name , int i_mode, int *pi_position,
394 playlist_item_t *p_parent )
396 struct dirent *p_dir_content;
397 struct dirent **pp_dir_content;
398 int i_dir_content, i = 0;
399 playlist_item_t *p_node;
401 /* Change the item to a node */
402 if( p_parent->i_children == -1 )
404 playlist_ItemToNode( p_playlist,p_parent );
407 /* get the first directory entry */
408 i_dir_content = scandir( psz_name, &pp_dir_content, Filter, alphasort );
409 if( i_dir_content == -1 )
411 msg_Warn( p_playlist, "Failed to read directory" );
414 p_dir_content = pp_dir_content[0];
416 /* while we still have entries in the directory */
417 while( i < i_dir_content )
419 int i_size_entry = strlen( psz_name ) +
420 strlen( p_dir_content->d_name ) + 2;
421 char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
423 sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
425 /* if it starts with '.' then forget it */
426 if( p_dir_content->d_name[0] != '.' )
428 #if defined( S_ISDIR )
429 struct stat stat_data;
430 stat( psz_uri, &stat_data );
431 if( S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
432 #elif defined( DT_DIR )
433 if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
438 if( i_mode == MODE_NONE )
440 msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
442 p_dir_content = pp_dir_content[i];
445 else if(i_mode == MODE_EXPAND )
448 msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
450 if( !strncmp( psz_uri, psz_name, strlen( psz_name ) ) )
452 char *psz_subdir = psz_uri;
453 /* Skip the parent path + the separator */
454 psz_subdir += strlen( psz_name ) + 1;
455 psz_newname = strdup( psz_subdir );
459 psz_newname = strdup( psz_uri );
461 p_node = playlist_NodeCreate( p_playlist,
462 p_parent->pp_parents[0]->i_view,
463 psz_newname, p_parent );
465 playlist_CopyParents( p_parent, p_node );
467 p_node->input.i_type = ITEM_TYPE_DIRECTORY;
469 if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
470 pi_position, p_node ) != VLC_SUCCESS )
480 playlist_item_t *p_item = playlist_ItemNew( p_playlist,
481 psz_uri, p_dir_content->d_name );
482 playlist_NodeAddItem( p_playlist,p_item,
483 p_parent->pp_parents[0]->i_view,
485 PLAYLIST_APPEND, PLAYLIST_END );
487 playlist_CopyParents( p_parent, p_item );
492 p_dir_content = pp_dir_content[i];
494 free( pp_dir_content );