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_description( _("Standard filesystem directory input") );
79 set_capability( "access2", 55 );
80 add_shortcut( "directory" );
81 add_shortcut( "dir" );
82 add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
83 RECURSIVE_LONGTEXT, VLC_FALSE );
84 change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
85 set_callbacks( Open, Close );
88 set_description( "Directory EOF");
89 set_capability( "demux2", 0 );
90 add_shortcut( "directory" );
91 set_callbacks( DemuxOpen, NULL );
95 /*****************************************************************************
96 * Local prototypes, constants, structures
97 *****************************************************************************/
100 #define MODE_COLLAPSE 1
103 static int Read( access_t *, uint8_t *, int );
104 static int ReadNull( access_t *, uint8_t *, int );
105 static int Control( access_t *, int, va_list );
107 static int Demux( demux_t *p_demux );
108 static int DemuxControl( demux_t *p_demux, int i_query, va_list args );
111 static int ReadDir( playlist_t *, char *psz_name, int i_mode, int *pi_pos,
114 /*****************************************************************************
115 * Open: open the directory
116 *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
119 access_t *p_access = (access_t*)p_this;
121 #ifdef HAVE_SYS_STAT_H
122 struct stat stat_info;
124 if( ( stat( p_access->psz_path, &stat_info ) == -1 ) ||
125 !S_ISDIR( stat_info.st_mode ) )
127 if( strcmp( p_access->psz_access, "dir") &&
128 strcmp( p_access->psz_access, "directory") )
134 p_access->pf_read = Read;
135 p_access->pf_block = NULL;
136 p_access->pf_seek = NULL;
137 p_access->pf_control= Control;
140 p_access->psz_demux = strdup( "directory" );
145 /*****************************************************************************
146 * Close: close the target
147 *****************************************************************************/
148 static void Close( vlc_object_t * p_this )
153 /*****************************************************************************
154 * ReadNull: read the directory
155 *****************************************************************************/
156 static int ReadNull( access_t *p_access, uint8_t *p_buffer, int i_len)
158 /* Return fake data */
159 memset( p_buffer, 0, i_len );
163 /*****************************************************************************
164 * Read: read the directory
165 *****************************************************************************/
166 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len)
168 char *psz_name = NULL;
172 playlist_t *p_playlist =
173 (playlist_t *) vlc_object_find( p_access,
174 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
178 msg_Err( p_access, "can't find playlist" );
182 /* Remove the ending '/' char */
183 psz_name = strdup( p_access->psz_path );
184 if( psz_name == NULL )
187 if( (psz_name[strlen(psz_name)-1] == '/') ||
188 (psz_name[strlen(psz_name)-1] == '\\') )
190 psz_name[strlen(psz_name)-1] = '\0';
193 /* Initialize structure */
194 psz = var_CreateGetString( p_access, "recursive" );
195 if( *psz == '\0' || !strncmp( psz, "none" , 4 ) )
199 else if( !strncmp( psz, "collapse", 8 ) )
201 i_mode = MODE_COLLAPSE;
205 i_mode = MODE_EXPAND;
209 /* Make sure we are deleted when we are done */
210 // p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
211 /* The playlist position we will use for the add */
212 i_pos = p_playlist->i_index + 1;
214 msg_Dbg( p_access, "opening directory `%s'", psz_name );
216 p_playlist->status.p_item->input.i_type = ITEM_TYPE_DIRECTORY;
217 if( ReadDir( p_playlist, psz_name , i_mode, &i_pos,
218 p_playlist->status.p_item
225 /* Begin to read the directory */
226 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,p_playlist->status.i_view,
227 p_playlist->status.p_item, NULL );
228 if( psz_name ) free( psz_name );
229 vlc_object_release( p_playlist );
231 /* Return fake data forever */
232 p_access->pf_read = ReadNull;
233 return ReadNull( p_access, p_buffer, i_len );
236 /*****************************************************************************
238 *****************************************************************************/
239 static int Control( access_t *p_access, int i_query, va_list args )
248 case ACCESS_CAN_SEEK:
249 case ACCESS_CAN_FASTSEEK:
250 case ACCESS_CAN_PAUSE:
251 case ACCESS_CAN_CONTROL_PACE:
252 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
253 *pb_bool = VLC_FALSE; /* FIXME */
258 pi_int = (int*)va_arg( args, int * );
262 case ACCESS_GET_PTS_DELAY:
263 pi_64 = (int64_t*)va_arg( args, int64_t * );
264 *pi_64 = DEFAULT_PTS_DELAY * 1000;
268 case ACCESS_SET_PAUSE_STATE:
269 case ACCESS_GET_TITLE_INFO:
270 case ACCESS_SET_TITLE:
271 case ACCESS_SET_SEEKPOINT:
272 case ACCESS_SET_PRIVATE_ID_STATE:
276 msg_Warn( p_access, "unimplemented query in control" );
282 /*****************************************************************************
284 *****************************************************************************/
285 static int DemuxOpen ( vlc_object_t *p_this )
287 demux_t *p_demux = (demux_t*)p_this;
289 if( strcmp( p_demux->psz_demux, "directory" ) )
292 p_demux->pf_demux = Demux;
293 p_demux->pf_control = DemuxControl;
297 /*****************************************************************************
299 *****************************************************************************/
300 static int Demux( demux_t *p_demux )
304 /*****************************************************************************
306 *****************************************************************************/
307 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
309 return demux2_vaControlHelper( p_demux->s,
314 /*****************************************************************************
315 * ReadDir: read a directory and add its content to the list
316 *****************************************************************************/
317 static int ReadDir( playlist_t *p_playlist,
318 char *psz_name , int i_mode, int *pi_position,
319 playlist_item_t *p_parent )
322 struct dirent * p_dir_content;
323 playlist_item_t *p_node;
326 /* Change the item to a node */
327 if( p_parent->i_children == -1)
329 playlist_ItemToNode( p_playlist,p_parent );
332 p_current_dir = opendir( psz_name );
334 if( p_current_dir == NULL )
336 /* something went bad, get out of here ! */
338 msg_Warn( p_playlist, "cannot open directory `%s' (%s)",
339 psz_name, strerror(errno));
341 msg_Warn( p_playlist, "cannot open directory `%s'", psz_name );
346 /* get the first directory entry */
347 p_dir_content = readdir( p_current_dir );
349 /* while we still have entries in the directory */
350 while( p_dir_content != NULL )
352 int i_size_entry = strlen( psz_name ) +
353 strlen( p_dir_content->d_name ) + 2;
354 char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
356 sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
358 /* if it starts with '.' then forget it */
359 if( p_dir_content->d_name[0] != '.' )
361 #if defined( S_ISDIR )
362 struct stat stat_data;
363 stat( psz_uri, &stat_data );
364 if( S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
365 #elif defined( DT_DIR )
366 if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
371 if( i_mode == MODE_NONE )
373 msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
374 p_dir_content = readdir( p_current_dir );
377 else if(i_mode == MODE_EXPAND )
379 msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
380 p_node = playlist_NodeCreate( p_playlist,
381 p_parent->pp_parents[0]->i_view,
384 p_node->input.i_type = ITEM_TYPE_DIRECTORY;
385 /* We need to declare the parents of the node as the
386 * same of the parent's ones */
387 for( i= 1 ; i< p_parent->i_parents; i ++ )
389 playlist_ItemAddParent( p_node,
390 p_parent->pp_parents[i]->i_view,
393 if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
394 pi_position, p_node ) != VLC_SUCCESS )
402 playlist_item_t *p_item = playlist_ItemNew( p_playlist,
403 psz_uri, p_dir_content->d_name );
404 fprintf(stderr,"STARTTTTt\n");
405 playlist_NodeAddItem( p_playlist,p_item,
406 p_parent->pp_parents[0]->i_view,
408 PLAYLIST_APPEND, PLAYLIST_END );
409 fprintf(stderr,"DONE\n");
410 /* We need to declare the parents of the node as the
411 * same of the parent's ones */
412 for( i= 1 ; i< p_parent->i_parents; i ++ )
414 playlist_ItemAddParent( p_item,
415 p_parent->pp_parents[i]->i_view,
421 p_dir_content = readdir( p_current_dir );
423 closedir( p_current_dir );