]> git.sesse.net Git - vlc/blob - modules/access/directory.c
* Massive spelling corrections.
[vlc] / modules / access / directory.c
1 /*****************************************************************************
2  * directory.c: expands a directory (directory: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
5  * $Id$
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
28 #include <vlc/vlc.h>
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
31
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_SYS_TYPES_H
35 #   include <sys/types.h>
36 #endif
37 #ifdef HAVE_SYS_STAT_H
38 #   include <sys/stat.h>
39 #endif
40 #ifdef HAVE_ERRNO_H
41 #   include <errno.h>
42 #endif
43 #ifdef HAVE_FCNTL_H
44 #   include <fcntl.h>
45 #endif
46
47 #ifdef HAVE_UNISTD_H
48 #   include <unistd.h>
49 #elif defined( WIN32 ) && !defined( UNDER_CE )
50 #   include <io.h>
51 #endif
52
53 #if (!defined( WIN32 ) || defined(__MINGW32__))
54 /* Mingw has its own version of dirent */
55 #   include <dirent.h>
56 #endif
57
58 /*****************************************************************************
59  * Constants and structures
60  *****************************************************************************/
61 #define MODE_EXPAND 0
62 #define MODE_COLLAPSE 1
63 #define MODE_NONE 2
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int     Open   ( vlc_object_t * );
69 static void    Close  ( vlc_object_t * );
70
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 );
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 #define RECURSIVE_TEXT N_("Subdirectory behavior")
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" )
83
84 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
85 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
86                                            N_("expand") };
87
88 vlc_module_begin();
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 );
97 vlc_module_end();
98
99
100 /*****************************************************************************
101  * Open: open the directory
102  *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
104 {
105     input_thread_t *            p_input = (input_thread_t *)p_this;
106 #ifdef HAVE_SYS_STAT_H
107     struct stat                 stat_info;
108 #endif
109
110     /* Initialize access plug-in structures. */
111     if( p_input->i_mtu == 0 )
112     {
113         /* Improve speed. */
114         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
115     }
116
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;
121
122 #ifdef HAVE_SYS_STAT_H
123     if( ( stat( p_input->psz_name, &stat_info ) == -1 ) ||
124         !S_ISDIR( stat_info.st_mode ) )
125 #else
126     if( !p_input->psz_access || strcmp(p_input->psz_access, "dir") )
127 #endif
128     {
129         return VLC_EGENERIC;
130     }
131
132     /* Force a demux */
133     p_input->psz_demux = "dummy";
134
135     return VLC_SUCCESS;
136 }
137
138 /*****************************************************************************
139  * Close: close the target
140  *****************************************************************************/
141 static void Close( vlc_object_t * p_this )
142 {
143     input_thread_t * p_input = (input_thread_t *)p_this;
144
145     msg_Info( p_input, "closing `%s/%s://%s'",
146               p_input->psz_access, p_input->psz_demux, p_input->psz_name );
147 }
148
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 )
153 {
154     char *                      psz_name;
155     char *                      psz_mode;
156     int                         i_mode, i_pos;
157
158     playlist_t * p_playlist = (playlist_t *) vlc_object_find(
159                         p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
160
161     if( !p_playlist )
162     {
163         msg_Err( p_input, "can't find playlist" );
164         goto end;
165     }
166     
167     /* Remove the ending '/' char */
168     psz_name = strdup( p_input->psz_name );
169     if( psz_name == NULL )
170         goto end;
171
172     if( (psz_name[strlen(psz_name)-1] == '/') ||
173         (psz_name[strlen(psz_name)-1] == '\\') )
174     {
175         psz_name[strlen(psz_name)-1] = '\0';
176     }
177     
178     /* Initialize structure */
179     psz_mode = config_GetPsz( p_input , "recursive" );
180     if( !psz_mode || !strncmp( psz_mode, "none" , 4 )  )
181     {
182         i_mode = MODE_NONE;
183     }
184     else if( !strncmp( psz_mode, "collapse", 8 )  )
185     {
186         i_mode = MODE_COLLAPSE;
187     }
188     else
189     {
190         i_mode = MODE_EXPAND;
191     }
192     
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;
197
198     msg_Dbg( p_input, "opening directory `%s'", psz_name );
199     if( ReadDir( p_playlist, psz_name , i_mode, &i_pos ) != VLC_SUCCESS )
200     {
201         free( psz_name );
202         goto end;
203     }
204
205 end:
206     vlc_object_release( p_playlist );
207     p_input->b_eof = 1;
208     return 0;
209 }
210
211 /* Local functions */
212
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 )
217 {
218     DIR *                       p_current_dir;
219     struct dirent *             p_dir_content;
220
221     /* Open the dir */
222     p_current_dir = opendir( psz_name );
223
224     if( p_current_dir == NULL )
225     {
226         /* something went bad, get out of here ! */
227 #   ifdef HAVE_ERRNO_H
228         msg_Warn( p_playlist, "cannot open directory `%s' (%s)",
229                   psz_name, strerror(errno));
230 #   else
231         msg_Warn( p_playlist, "cannot open directory `%s'", psz_name );
232 #   endif
233         return VLC_EGENERIC;
234     }
235
236     /* get the first directory entry */
237     p_dir_content = readdir( p_current_dir );
238
239     /* while we still have entries in the directory */
240     while( p_dir_content != NULL )
241     {
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);
245
246         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
247
248         /* if it starts with '.' then forget it */
249         if( p_dir_content->d_name[0] != '.' )
250         {
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 )
257 #else
258             if( 0 )
259 #endif
260             {
261                 if( i_mode == MODE_NONE )
262                 {
263                     msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
264                     p_dir_content = readdir( p_current_dir );
265                     continue;
266                 }
267                 else if(i_mode == MODE_EXPAND )
268                 {
269                     msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
270                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND, pi_position )
271                                  != VLC_SUCCESS )
272                     {
273                         return VLC_EGENERIC;
274                     }
275                 }
276             }
277             else
278             {
279                 playlist_Add( p_playlist, psz_uri, p_dir_content->d_name,
280                           PLAYLIST_INSERT, *pi_position );
281                 (*pi_position)++;
282             }
283         }
284         free( psz_uri );
285         p_dir_content = readdir( p_current_dir );
286     }
287     closedir( p_current_dir );
288     return VLC_SUCCESS;
289 }