1 /*****************************************************************************
2 * directory.c: expands a directory (directory: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: directory.c,v 1.6 2004/02/17 13:13:31 gbazin Exp $
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 *****************************************************************************/
28 #include <vlc/input.h>
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
47 #elif defined( WIN32 ) && !defined( UNDER_CE )
51 #if (!defined( WIN32 ) || defined(__MINGW32__))
52 /* Mingw has its own version of dirent */
56 /*****************************************************************************
57 * Constants and structures
58 *****************************************************************************/
59 #define MAX_DIR_SIZE 100000
62 #define MODE_COLLAPSE 1
65 typedef struct input_directory_s
67 char p_dir_buffer[MAX_DIR_SIZE];
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open ( vlc_object_t * );
78 static void Close ( vlc_object_t * );
80 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
81 int ReadDir( input_thread_t *p_input, char *psz_name , int i_mode );
83 /*****************************************************************************
85 *****************************************************************************/
86 #define RECURSIVE_TEXT N_("Includes subdirectories ?")
87 #define RECURSIVE_LONGTEXT N_( \
88 "Select whether subdirectories must be expanded.\n" \
89 "none : subdirectories do not appear in the playlist.\n" \
90 "collapse : subdirectories appear but are expanded on first play.\n" \
91 "expand : all subdirectories are expanded.\n" )
93 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
94 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
98 set_description( _("Standard filesystem directory input") );
99 set_capability( "access", 55 );
100 add_shortcut( "directory" );
101 add_shortcut( "dir" );
102 add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
103 RECURSIVE_LONGTEXT, VLC_FALSE );
104 change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
105 set_callbacks( Open, Close );
109 /*****************************************************************************
110 * Open: open the directory
111 *****************************************************************************/
112 static int Open( vlc_object_t *p_this )
114 input_thread_t * p_input = (input_thread_t *)p_this;
116 input_directory_t * p_access_data;
119 #ifdef HAVE_SYS_STAT_H
120 struct stat stat_info;
123 /* Initialize access plug-in structures. */
124 if( p_input->i_mtu == 0 )
127 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
130 p_input->pf_read = Read;
131 p_input->pf_set_program = NULL;
132 p_input->pf_set_area = NULL;
133 p_input->pf_seek = NULL;
135 /* Remove the ending '/' char */
136 psz_name = strdup( p_input->psz_name );
137 if( psz_name == NULL )
140 if( (psz_name[strlen(psz_name)-1] == '/') ||
141 (psz_name[strlen(psz_name)-1] == '\\') )
143 psz_name[strlen(psz_name)-1] = '\0';
146 #ifdef HAVE_SYS_STAT_H
147 if( ( stat( psz_name, &stat_info ) == -1 ) ||
148 !S_ISDIR( stat_info.st_mode ) )
150 if( !p_input->psz_access || strcmp(p_input->psz_access, "dir") )
157 /* Initialize structure */
158 msg_Dbg( p_input, "opening directory `%s'", psz_name );
159 p_access_data = malloc( sizeof(input_directory_t) );
160 p_input->p_access_data = (void *)p_access_data;
161 if( p_access_data == NULL )
163 msg_Err( p_input, "out of memory" );
167 p_access_data->i_pos = 0;
169 psz_mode = config_GetPsz( p_input , "recursive" );
172 msg_Err( p_input, "Unable to get configuration" );
176 if( !strncmp( psz_mode, "none" , 4 ) )
180 else if( !strncmp( psz_mode, "collapse", 8 ) )
182 i_mode = MODE_COLLAPSE;
186 i_mode = MODE_EXPAND;
188 if( ReadDir( p_input, psz_name , i_mode ) != VLC_SUCCESS )
190 free( p_access_data );
195 msg_Dbg(p_input,"Directory read complete. Read %i bytes",
196 p_access_data->i_pos);
198 p_access_data->p_dir_buffer[p_access_data->i_pos] = '\0';
199 p_access_data->i_pos++;
200 p_access_data->i_buf_length = p_access_data->i_pos;
201 p_access_data->i_buf_pos = 0;
203 /* Force m3u demuxer */
204 p_input->psz_demux = "m3u";
209 /*****************************************************************************
210 * Close: close the target
211 *****************************************************************************/
212 static void Close( vlc_object_t * p_this )
214 input_thread_t * p_input = (input_thread_t *)p_this;
215 input_directory_t * p_access_data =
216 (input_directory_t *)p_input->p_access_data;
218 msg_Info( p_input, "closing `%s/%s://%s'",
219 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
221 free( p_access_data );
224 /*****************************************************************************
225 * Read: read directory and output to demux.
226 *****************************************************************************/
227 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
229 input_directory_t * p_access_data =
230 (input_directory_t *)p_input->p_access_data;
231 unsigned int i_remaining = p_access_data->i_buf_length -
232 p_access_data->i_buf_pos;
234 if( i_remaining > 0 )
238 i_ret = __MIN( i_len, i_remaining );
240 &p_access_data->p_dir_buffer[p_access_data->i_buf_pos],
242 p_access_data->i_buf_pos += i_ret;
243 return (ssize_t) i_ret;
248 /* Local functions */
250 /*****************************************************************************
251 * ReadDir: read a directory and add its content to the list
252 *****************************************************************************/
253 int ReadDir( input_thread_t *p_input, char *psz_name , int i_mode )
256 struct dirent * p_dir_content;
258 input_directory_t * p_access_data =
259 (input_directory_t *)p_input->p_access_data;
261 /* have to cd into this dir */
262 p_current_dir = opendir( psz_name );
264 if( p_current_dir == NULL )
266 /* something went bad, get out of here ! */
268 msg_Warn( p_input, "cannot open directory `%s' (%s)",
269 psz_name, strerror(errno));
271 msg_Warn( p_input, "cannot open directory `%s'", psz_name );
276 p_dir_content = readdir( p_current_dir );
278 /* while we still have entries in the directory */
279 while( p_dir_content != NULL && p_access_data->i_pos < MAX_DIR_SIZE )
281 int i_size_entry = strlen( psz_name ) +
282 strlen( p_dir_content->d_name ) + 2;
283 char *psz_entry = (char *)malloc( sizeof(char)*i_size_entry);
285 sprintf( psz_entry, "%s/%s",psz_name,p_dir_content->d_name);
287 #if 0 /* Disable this message, it makes too much output */
288 msg_Dbg( p_input, "Entry %s",psz_entry );
291 /* if it is "." or "..", forget it */
292 if( strcmp( p_dir_content->d_name, "." ) &&
293 strcmp( p_dir_content->d_name, ".." ) &&
294 p_access_data->i_pos + i_size_entry < MAX_DIR_SIZE )
296 #if defined( DT_DIR )
297 if( p_dir_content->d_type == DT_DIR )
298 #elif defined( S_ISDIR )
299 struct stat stat_data;
300 stat( psz_entry, &stat_data );
301 if( S_ISDIR(stat_data.st_mode) )
306 if( i_mode == MODE_NONE )
308 msg_Dbg( p_input, "Skipping subdirectory %s",psz_entry );
309 p_dir_content = readdir( p_current_dir );
312 else if(i_mode == MODE_EXPAND )
314 msg_Dbg(p_input, "Reading subdirectory %s",psz_entry );
315 if( ReadDir( p_input, psz_entry , MODE_EXPAND )
323 sprintf( &p_access_data->p_dir_buffer[p_access_data->i_pos],
325 p_access_data->i_pos += i_size_entry -1 ;
326 p_access_data->p_dir_buffer[p_access_data->i_pos] = '\n';
327 p_access_data->i_pos++;
332 sprintf( &p_access_data->p_dir_buffer[p_access_data->i_pos],
334 p_access_data->i_pos += i_size_entry - 1;
335 p_access_data->p_dir_buffer[p_access_data->i_pos] = '\n';
336 p_access_data->i_pos++;
340 p_dir_content = readdir( p_current_dir );
342 closedir( p_current_dir );