1 /*****************************************************************************
2 * directory.c: expands a directory (directory: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: directory.c,v 1.4 2003/03/30 18:14:35 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 50000
61 typedef struct input_directory_s
63 char p_dir_buffer[MAX_DIR_SIZE];
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Open ( vlc_object_t * );
73 static void Close ( vlc_object_t * );
75 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
77 /*****************************************************************************
79 *****************************************************************************/
82 set_description( _("Standard filesystem directory input") );
83 set_capability( "access", 55 );
84 add_shortcut( "directory" );
85 add_shortcut( "dir" );
86 set_callbacks( Open, Close );
90 /*****************************************************************************
91 * Open: open the directory
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 input_thread_t * p_input = (input_thread_t *)p_this;
97 input_directory_t * p_access_data;
98 #ifdef HAVE_SYS_STAT_H
99 struct stat stat_info;
102 struct dirent * p_dir_content;
105 /* Initialize access plug-in structures. */
106 if( p_input->i_mtu == 0 )
109 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
112 p_input->pf_read = Read;
113 p_input->pf_set_program = NULL;
114 p_input->pf_set_area = NULL;
115 p_input->pf_seek = NULL;
117 /* Remove the ending '/' char */
118 psz_name = strdup( p_input->psz_name );
119 if( psz_name == NULL )
122 if( (psz_name[strlen(psz_name)-1] == '/') ||
123 (psz_name[strlen(psz_name)-1] == '\\') )
125 psz_name[strlen(psz_name)-1] = '\0';
129 #ifdef HAVE_SYS_STAT_H
130 if( ( stat( psz_name, &stat_info ) == -1 ) ||
131 !S_ISDIR( stat_info.st_mode ) )
133 if( !p_input->psz_access || strcmp(p_input->psz_access, "dir") )
140 msg_Dbg( p_input, "opening directory `%s'", psz_name );
141 p_access_data = malloc( sizeof(input_directory_t) );
142 p_input->p_access_data = (void *)p_access_data;
143 if( p_access_data == NULL )
145 msg_Err( p_input, "out of memory" );
150 /* have to cd into this dir */
151 p_current_dir = opendir( psz_name );
153 if( p_current_dir == NULL )
155 /* something went bad, get out of here ! */
157 msg_Warn( p_input, "cannot open directory `%s' (%s)",
158 psz_name, strerror(errno));
160 msg_Warn( p_input, "cannot open directory `%s'", psz_name );
162 free( p_access_data );
167 p_dir_content = readdir( p_current_dir );
169 /* while we still have entries in the directory */
170 while( p_dir_content != NULL && i_pos < MAX_DIR_SIZE )
172 int i_size_entry = strlen( psz_name ) +
173 strlen( p_dir_content->d_name ) + 2;
174 /* if it is "." or "..", forget it */
175 if( strcmp( p_dir_content->d_name, "." ) &&
176 strcmp( p_dir_content->d_name, ".." ) &&
177 i_pos + i_size_entry < MAX_DIR_SIZE )
179 msg_Dbg( p_input, "%s", p_dir_content->d_name );
180 sprintf( &p_access_data->p_dir_buffer[i_pos], "%s/%s",
181 psz_name, p_dir_content->d_name );
182 msg_Dbg( p_input, "%s", &p_access_data->p_dir_buffer[i_pos] );
183 i_pos += i_size_entry - 1;
184 p_access_data->p_dir_buffer[i_pos] = '\n';
187 p_dir_content = readdir( p_current_dir );
189 p_access_data->p_dir_buffer[i_pos] = '\0';
191 p_access_data->i_buf_length = i_pos;
192 p_access_data->i_buf_pos = 0;
194 msg_Dbg( p_input, "%s", p_access_data->p_dir_buffer );
196 closedir( p_current_dir );
199 /* Force m3u demuxer */
200 p_input->psz_demux = "m3u";
205 /*****************************************************************************
206 * Close: close the target
207 *****************************************************************************/
208 static void Close( vlc_object_t * p_this )
210 input_thread_t * p_input = (input_thread_t *)p_this;
211 input_directory_t * p_access_data =
212 (input_directory_t *)p_input->p_access_data;
214 msg_Info( p_input, "closing `%s/%s://%s'",
215 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
217 free( p_access_data );
220 /*****************************************************************************
221 * Read: read directory and output to demux.
222 *****************************************************************************/
223 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
225 input_directory_t * p_access_data =
226 (input_directory_t *)p_input->p_access_data;
227 unsigned int i_remaining = p_access_data->i_buf_length -
228 p_access_data->i_buf_pos;
230 if( i_remaining > 0 )
234 i_ret = __MIN( i_len, i_remaining );
236 &p_access_data->p_dir_buffer[p_access_data->i_buf_pos],
238 p_access_data->i_buf_pos += i_ret;
239 return (ssize_t) i_ret;