]> git.sesse.net Git - vlc/blob - modules/access/directory.c
* ahum. == on a mask instead of &
[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 = 0;
155     char *psz_mode = 0;
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         goto end;
202     }
203
204 end:
205     if( psz_name ) free( psz_name );
206     if( psz_mode ) free( psz_mode );
207     vlc_object_release( p_playlist );
208     p_input->b_eof = 1;
209     return 0;
210 }
211
212 /* Local functions */
213
214 /*****************************************************************************
215  * ReadDir: read a directory and add its content to the list
216  *****************************************************************************/
217 int ReadDir( playlist_t *p_playlist, char *psz_name , int i_mode, int *pi_position )
218 {
219     DIR *                       p_current_dir;
220     struct dirent *             p_dir_content;
221
222     /* Open the dir */
223     p_current_dir = opendir( psz_name );
224
225     if( p_current_dir == NULL )
226     {
227         /* something went bad, get out of here ! */
228 #   ifdef HAVE_ERRNO_H
229         msg_Warn( p_playlist, "cannot open directory `%s' (%s)",
230                   psz_name, strerror(errno));
231 #   else
232         msg_Warn( p_playlist, "cannot open directory `%s'", psz_name );
233 #   endif
234         return VLC_EGENERIC;
235     }
236
237     /* get the first directory entry */
238     p_dir_content = readdir( p_current_dir );
239
240     /* while we still have entries in the directory */
241     while( p_dir_content != NULL )
242     {
243         int i_size_entry = strlen( psz_name ) +
244                            strlen( p_dir_content->d_name ) + 2;
245         char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
246
247         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
248
249         /* if it starts with '.' then forget it */
250         if( p_dir_content->d_name[0] != '.' )
251         {
252 #if defined( S_ISDIR )
253             struct stat stat_data;
254             stat( psz_uri, &stat_data );
255             if( S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
256 #elif defined( DT_DIR )
257             if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
258 #else
259             if( 0 )
260 #endif
261             {
262                 if( i_mode == MODE_NONE )
263                 {
264                     msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
265                     p_dir_content = readdir( p_current_dir );
266                     continue;
267                 }
268                 else if(i_mode == MODE_EXPAND )
269                 {
270                     msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
271                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND, pi_position )
272                                  != VLC_SUCCESS )
273                     {
274                         return VLC_EGENERIC;
275                     }
276                 }
277             }
278             else
279             {
280                 playlist_Add( p_playlist, psz_uri, p_dir_content->d_name,
281                           PLAYLIST_INSERT, *pi_position );
282                 (*pi_position)++;
283             }
284         }
285         free( psz_uri );
286         p_dir_content = readdir( p_current_dir );
287     }
288     closedir( p_current_dir );
289     return VLC_SUCCESS;
290 }