]> git.sesse.net Git - vlc/blob - modules/access/directory.c
* modules/*: sanitization of the modules description strings.
[vlc] / modules / access / directory.c
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 $
6  *
7  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32 #ifdef HAVE_SYS_TYPES_H
33 #   include <sys/types.h>
34 #endif
35 #ifdef HAVE_SYS_STAT_H
36 #   include <sys/stat.h>
37 #endif
38 #ifdef HAVE_ERRNO_H
39 #   include <errno.h>
40 #endif
41 #ifdef HAVE_FCNTL_H
42 #   include <fcntl.h>
43 #endif
44
45 #ifdef HAVE_UNISTD_H
46 #   include <unistd.h>
47 #elif defined( WIN32 ) && !defined( UNDER_CE )
48 #   include <io.h>
49 #endif
50
51 #if (!defined( WIN32 ) || defined(__MINGW32__))
52 /* Mingw has its own version of dirent */
53 #   include <dirent.h>
54 #endif
55
56 /*****************************************************************************
57  * Constants and structures
58  *****************************************************************************/
59 #define MAX_DIR_SIZE 50000
60
61 typedef struct input_directory_s
62 {
63     char   p_dir_buffer[MAX_DIR_SIZE];
64     int    i_buf_pos;
65     int    i_buf_length;
66 } input_directory_t;
67
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int     Open   ( vlc_object_t * );
73 static void    Close  ( vlc_object_t * );
74
75 static ssize_t Read   ( input_thread_t *, byte_t *, size_t );
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80
81 vlc_module_begin();
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 );
87 vlc_module_end();
88
89
90 /*****************************************************************************
91  * Open: open the directory
92  *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
94 {
95     input_thread_t *            p_input = (input_thread_t *)p_this;
96     char *                      psz_name;
97     input_directory_t *         p_access_data;
98 #ifdef HAVE_SYS_STAT_H
99     struct stat                 stat_info;
100 #endif
101     DIR *                       p_current_dir;
102     struct dirent *             p_dir_content;
103     int                         i_pos=0;
104     
105     /* Initialize access plug-in structures. */
106     if( p_input->i_mtu == 0 )
107     {
108         /* Improve speed. */
109         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
110     }
111
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;
116
117     /* Remove the ending '/' char */
118     psz_name = strdup( p_input->psz_name );
119     if( psz_name == NULL )
120         return VLC_EGENERIC;
121
122     if( (psz_name[strlen(psz_name)-1] == '/') ||
123         (psz_name[strlen(psz_name)-1] == '\\') )
124     {
125         psz_name[strlen(psz_name)-1] = '\0';
126     }
127
128
129 #ifdef HAVE_SYS_STAT_H
130     if( ( stat( psz_name, &stat_info ) == -1 ) ||
131         !S_ISDIR( stat_info.st_mode ) )
132 #else
133     if( !p_input->psz_access || strcmp(p_input->psz_access, "dir") )
134 #endif
135     {
136         free( psz_name );
137         return VLC_EGENERIC;
138     }
139
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 )
144     {
145         msg_Err( p_input, "out of memory" );
146         free( psz_name );
147         return VLC_ENOMEM;
148     }
149         
150     /* have to cd into this dir */
151     p_current_dir = opendir( psz_name );
152
153     if( p_current_dir == NULL )
154     {
155         /* something went bad, get out of here ! */
156 #   ifdef HAVE_ERRNO_H
157         msg_Warn( p_input, "cannot open directory `%s' (%s)",
158                   psz_name, strerror(errno));
159 #   else
160         msg_Warn( p_input, "cannot open directory `%s'", psz_name );
161 #   endif
162         free( p_access_data );
163         free( psz_name );
164         return VLC_EGENERIC;
165     }
166     
167     p_dir_content = readdir( p_current_dir );
168
169     /* while we still have entries in the directory */
170     while( p_dir_content != NULL && i_pos < MAX_DIR_SIZE )
171     {
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 )
178         {
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';
185             i_pos++;
186         }
187         p_dir_content = readdir( p_current_dir );
188     }
189     p_access_data->p_dir_buffer[i_pos] = '\0';
190     i_pos++;
191     p_access_data->i_buf_length = i_pos;
192     p_access_data->i_buf_pos = 0;
193     
194     msg_Dbg( p_input, "%s", p_access_data->p_dir_buffer );
195
196     closedir( p_current_dir );
197     free( psz_name );
198
199     /* Force m3u demuxer */
200     p_input->psz_demux = "m3u";
201
202     return VLC_SUCCESS;
203 }
204
205 /*****************************************************************************
206  * Close: close the target
207  *****************************************************************************/
208 static void Close( vlc_object_t * p_this )
209 {
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;
213
214     msg_Info( p_input, "closing `%s/%s://%s'", 
215               p_input->psz_access, p_input->psz_demux, p_input->psz_name );
216
217     free( p_access_data );
218 }
219
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 )
224 {
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;
229
230     if( i_remaining > 0 )
231     {
232         int i_ret;
233
234         i_ret = __MIN( i_len, i_remaining );
235         memcpy( p_buffer,
236                 &p_access_data->p_dir_buffer[p_access_data->i_buf_pos],
237                 i_ret );
238         p_access_data->i_buf_pos += i_ret;
239         return (ssize_t) i_ret;
240     }
241
242     return 0;
243 }