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 /*****************************************************************************
59 * Constants and structures
60 *****************************************************************************/
62 #define MODE_COLLAPSE 1
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close ( vlc_object_t * );
71 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
72 int ReadDir( playlist_t *p_playlist, char *psz_name , int i_mode, int *pi_pos );
74 /*****************************************************************************
76 *****************************************************************************/
77 #define RECURSIVE_TEXT N_("Subdirectory behaviour")
78 #define RECURSIVE_LONGTEXT N_( \
79 "Select whether subdirectories must be expanded.\n" \
80 "none: subdirectories do not appear in the playlist.\n" \
81 "collapse: subdirectories appear but are expanded on first play.\n" \
82 "expand: all subdirectories are expanded.\n" )
84 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
85 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
89 set_description( _("Standard filesystem directory input") );
90 set_capability( "access", 55 );
91 add_shortcut( "directory" );
92 add_shortcut( "dir" );
93 add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
94 RECURSIVE_LONGTEXT, VLC_FALSE );
95 change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
96 set_callbacks( Open, Close );
100 /*****************************************************************************
101 * Open: open the directory
102 *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
105 input_thread_t * p_input = (input_thread_t *)p_this;
106 #ifdef HAVE_SYS_STAT_H
107 struct stat stat_info;
110 /* Initialize access plug-in structures. */
111 if( p_input->i_mtu == 0 )
114 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
117 p_input->pf_read = Read;
118 p_input->pf_set_program = NULL;
119 p_input->pf_set_area = NULL;
120 p_input->pf_seek = NULL;
122 #ifdef HAVE_SYS_STAT_H
123 if( ( stat( p_input->psz_name, &stat_info ) == -1 ) ||
124 !S_ISDIR( stat_info.st_mode ) )
126 if( !p_input->psz_access || strcmp(p_input->psz_access, "dir") )
133 p_input->psz_demux = "dummy";
138 /*****************************************************************************
139 * Close: close the target
140 *****************************************************************************/
141 static void Close( vlc_object_t * p_this )
143 input_thread_t * p_input = (input_thread_t *)p_this;
145 msg_Info( p_input, "closing `%s/%s://%s'",
146 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
149 /*****************************************************************************
150 * Read: read the directory
151 *****************************************************************************/
152 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
158 playlist_t * p_playlist = (playlist_t *) vlc_object_find(
159 p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
163 msg_Err( p_input, "can't find playlist" );
167 /* Remove the ending '/' char */
168 psz_name = strdup( p_input->psz_name );
169 if( psz_name == NULL )
172 if( (psz_name[strlen(psz_name)-1] == '/') ||
173 (psz_name[strlen(psz_name)-1] == '\\') )
175 psz_name[strlen(psz_name)-1] = '\0';
178 /* Initialize structure */
179 psz_mode = config_GetPsz( p_input , "recursive" );
180 if( !psz_mode || !strncmp( psz_mode, "none" , 4 ) )
184 else if( !strncmp( psz_mode, "collapse", 8 ) )
186 i_mode = MODE_COLLAPSE;
190 i_mode = MODE_EXPAND;
193 /* Make sure we are deleted when we are done */
194 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
195 /* The playlist position we will use for the add */
196 i_pos = p_playlist->i_index + 1;
198 msg_Dbg( p_input, "opening directory `%s'", psz_name );
199 if( ReadDir( p_playlist, psz_name , i_mode, &i_pos ) != VLC_SUCCESS )
206 vlc_object_release( p_playlist );
211 /* Local functions */
213 /*****************************************************************************
214 * ReadDir: read a directory and add its content to the list
215 *****************************************************************************/
216 int ReadDir( playlist_t *p_playlist, char *psz_name , int i_mode, int *pi_position )
219 struct dirent * p_dir_content;
222 p_current_dir = opendir( psz_name );
224 if( p_current_dir == NULL )
226 /* something went bad, get out of here ! */
228 msg_Warn( p_playlist, "cannot open directory `%s' (%s)",
229 psz_name, strerror(errno));
231 msg_Warn( p_playlist, "cannot open directory `%s'", psz_name );
236 /* get the first directory entry */
237 p_dir_content = readdir( p_current_dir );
239 /* while we still have entries in the directory */
240 while( p_dir_content != NULL )
242 int i_size_entry = strlen( psz_name ) +
243 strlen( p_dir_content->d_name ) + 2;
244 char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
246 sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
248 /* if it starts with '.' then forget it */
249 if( p_dir_content->d_name[0] != '.' )
251 #if defined( S_ISDIR )
252 struct stat stat_data;
253 stat( psz_uri, &stat_data );
254 if( S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
255 #elif defined( DT_DIR )
256 if( p_dir_content->d_type == DT_DIR && i_mode != MODE_COLLAPSE )
261 if( i_mode == MODE_NONE )
263 msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
264 p_dir_content = readdir( p_current_dir );
267 else if(i_mode == MODE_EXPAND )
269 msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
270 if( ReadDir( p_playlist, psz_uri , MODE_EXPAND, pi_position )
279 playlist_Add( p_playlist, psz_uri, p_dir_content->d_name,
280 PLAYLIST_INSERT, *pi_position );
285 p_dir_content = readdir( p_current_dir );
287 closedir( p_current_dir );