]> git.sesse.net Git - vlc/blob - modules/access/directory.c
* Don't bitch anymore about not found access_demux plugins.
[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  * Module descriptor
60  *****************************************************************************/
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
64 static int  DemuxOpen ( vlc_object_t * );
65
66 #define RECURSIVE_TEXT N_("Subdirectory behavior")
67 #define RECURSIVE_LONGTEXT N_( \
68         "Select whether subdirectories must be expanded.\n" \
69         "none: subdirectories do not appear in the playlist.\n" \
70         "collapse: subdirectories appear but are expanded on first play.\n" \
71         "expand: all subdirectories are expanded.\n" )
72
73 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
74 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
75                                            N_("expand") };
76
77 vlc_module_begin();
78     set_description( _("Standard filesystem directory input") );
79     set_capability( "access2", 55 );
80     add_shortcut( "directory" );
81     add_shortcut( "dir" );
82     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
83                 RECURSIVE_LONGTEXT, VLC_FALSE );
84       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
85     set_callbacks( Open, Close );
86
87     add_submodule();
88         set_description( _("Directory EOF") );
89         set_capability( "demux2", 0 );
90         add_shortcut( "directory" );
91         set_callbacks( DemuxOpen, NULL );
92 vlc_module_end();
93
94
95 /*****************************************************************************
96  * Local prototypes, constants, structures
97  *****************************************************************************/
98
99 #define MODE_EXPAND 0
100 #define MODE_COLLAPSE 1
101 #define MODE_NONE 2
102
103 static int Read( access_t *, uint8_t *, int );
104 static int ReadNull( access_t *, uint8_t *, int );
105 static int Control( access_t *, int, va_list );
106
107 static int Demux( demux_t *p_demux );
108 static int DemuxControl( demux_t *p_demux, int i_query, va_list args );
109
110
111 static int ReadDir( playlist_t *, char *psz_name, int i_mode, int *pi_pos );
112
113 /*****************************************************************************
114  * Open: open the directory
115  *****************************************************************************/
116 static int Open( vlc_object_t *p_this )
117 {
118     access_t *p_access = (access_t*)p_this;
119
120 #ifdef HAVE_SYS_STAT_H
121     struct stat stat_info;
122
123     if( ( stat( p_access->psz_path, &stat_info ) == -1 ) ||
124         !S_ISDIR( stat_info.st_mode ) )
125 #else
126     if( strcmp( p_access->psz_access, "dir") &&
127         strcmp( p_access->psz_access, "directory") )
128 #endif
129     {
130         return VLC_EGENERIC;
131     }
132
133     p_access->pf_read  = Read;
134     p_access->pf_block = NULL;
135     p_access->pf_seek  = NULL;
136     p_access->pf_control= Control;
137
138     /* Force a demux */
139     p_access->psz_demux = strdup( "directory" );
140
141     return VLC_SUCCESS;
142 }
143
144 /*****************************************************************************
145  * Close: close the target
146  *****************************************************************************/
147 static void Close( vlc_object_t * p_this )
148 {
149
150 }
151
152 /*****************************************************************************
153  * ReadNull: read the directory
154  *****************************************************************************/
155 static int ReadNull( access_t *p_access, uint8_t *p_buffer, int i_len)
156 {
157     /* Return fake data */
158     memset( p_buffer, 0, i_len );
159     return i_len;
160 }
161
162 /*****************************************************************************
163  * Read: read the directory
164  *****************************************************************************/
165 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len)
166 {
167     char *psz_name = NULL;
168     char *psz;
169     int  i_mode, i_pos;
170
171     playlist_t *p_playlist =
172         (playlist_t *) vlc_object_find( p_access,
173                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
174
175     if( !p_playlist )
176     {
177         msg_Err( p_access, "can't find playlist" );
178         goto end;
179     }
180
181     /* Remove the ending '/' char */
182     psz_name = strdup( p_access->psz_path );
183     if( psz_name == NULL )
184         goto end;
185
186     if( (psz_name[strlen(psz_name)-1] == '/') ||
187         (psz_name[strlen(psz_name)-1] == '\\') )
188     {
189         psz_name[strlen(psz_name)-1] = '\0';
190     }
191
192     /* Initialize structure */
193     psz = var_CreateGetString( p_access, "recursive" );
194     if( *psz == '\0' || !strncmp( psz, "none" , 4 )  )
195     {
196         i_mode = MODE_NONE;
197     }
198     else if( !strncmp( psz, "collapse", 8 )  )
199     {
200         i_mode = MODE_COLLAPSE;
201     }
202     else
203     {
204         i_mode = MODE_EXPAND;
205     }
206     free( psz );
207
208     /* Make sure we are deleted when we are done */
209     p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
210     /* The playlist position we will use for the add */
211     i_pos = p_playlist->i_index + 1;
212
213     msg_Dbg( p_access, "opening directory `%s'", psz_name );
214     if( ReadDir( p_playlist, psz_name , i_mode, &i_pos ) != VLC_SUCCESS )
215     {
216         goto end;
217     }
218
219 end:
220     if( psz_name ) free( psz_name );
221     vlc_object_release( p_playlist );
222
223     /* Return fake data forever */
224     p_access->pf_read = ReadNull;
225     return ReadNull( p_access, p_buffer, i_len );
226 }
227
228 /*****************************************************************************
229  * DemuxOpen:
230  *****************************************************************************/
231 static int Control( access_t *p_access, int i_query, va_list args )
232 {
233     vlc_bool_t   *pb_bool;
234     int          *pi_int;
235     int64_t      *pi_64;
236
237     switch( i_query )
238     {
239         /* */
240         case ACCESS_CAN_SEEK:
241         case ACCESS_CAN_FASTSEEK:
242         case ACCESS_CAN_PAUSE:
243         case ACCESS_CAN_CONTROL_PACE:
244             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
245             *pb_bool = VLC_FALSE;    /* FIXME */
246             break;
247
248         /* */
249         case ACCESS_GET_MTU:
250             pi_int = (int*)va_arg( args, int * );
251             *pi_int = 0;
252             break;
253
254         case ACCESS_GET_PTS_DELAY:
255             pi_64 = (int64_t*)va_arg( args, int64_t * );
256             *pi_64 = DEFAULT_PTS_DELAY * 1000;
257             break;
258
259         /* */
260         case ACCESS_SET_PAUSE_STATE:
261         case ACCESS_GET_TITLE_INFO:
262         case ACCESS_SET_TITLE:
263         case ACCESS_SET_SEEKPOINT:
264         case ACCESS_SET_PRIVATE_ID_STATE:
265             return VLC_EGENERIC;
266
267         default:
268             msg_Warn( p_access, "unimplemented query in control" );
269             return VLC_EGENERIC;
270     }
271     return VLC_SUCCESS;
272 }
273
274 /*****************************************************************************
275  * DemuxOpen:
276  *****************************************************************************/
277 static int DemuxOpen ( vlc_object_t *p_this )
278 {
279     demux_t *p_demux = (demux_t*)p_this;
280
281     if( strcmp( p_demux->psz_demux, "directory" ) )
282         return VLC_EGENERIC;
283
284     p_demux->pf_demux   = Demux;
285     p_demux->pf_control = DemuxControl;
286     return VLC_SUCCESS;
287 }
288
289 /*****************************************************************************
290  * Demux: EOF
291  *****************************************************************************/
292 static int Demux( demux_t *p_demux )
293 {
294     return 0;
295 }
296 /*****************************************************************************
297  * DemuxControl:
298  *****************************************************************************/
299 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
300 {
301     return demux2_vaControlHelper( p_demux->s,
302                                    0, 0, 0, 1,
303                                    i_query, args );
304 }
305
306 /*****************************************************************************
307  * ReadDir: read a directory and add its content to the list
308  *****************************************************************************/
309 static int ReadDir( playlist_t *p_playlist,
310                     char *psz_name , int i_mode, int *pi_position )
311 {
312     DIR *                       p_current_dir;
313     struct dirent *             p_dir_content;
314
315     /* Open the dir */
316     p_current_dir = opendir( psz_name );
317
318     if( p_current_dir == NULL )
319     {
320         /* something went bad, get out of here ! */
321 #   ifdef HAVE_ERRNO_H
322         msg_Warn( p_playlist, "cannot open directory `%s' (%s)",
323                   psz_name, strerror(errno));
324 #   else
325         msg_Warn( p_playlist, "cannot open directory `%s'", psz_name );
326 #   endif
327         return VLC_EGENERIC;
328     }
329
330     /* get the first directory entry */
331     p_dir_content = readdir( p_current_dir );
332
333     /* while we still have entries in the directory */
334     while( p_dir_content != NULL )
335     {
336         int i_size_entry = strlen( psz_name ) +
337                            strlen( p_dir_content->d_name ) + 2;
338         char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
339
340         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
341
342         /* if it starts with '.' then forget it */
343         if( p_dir_content->d_name[0] != '.' )
344         {
345 #if defined( S_ISDIR )
346             struct stat stat_data;
347             stat( psz_uri, &stat_data );
348             if( S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
349 #elif defined( DT_DIR )
350             if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
351 #else
352             if( 0 )
353 #endif
354             {
355                 if( i_mode == MODE_NONE )
356                 {
357                     msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
358                     p_dir_content = readdir( p_current_dir );
359                     continue;
360                 }
361                 else if(i_mode == MODE_EXPAND )
362                 {
363                     msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
364                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND, pi_position )
365                                  != VLC_SUCCESS )
366                     {
367                         return VLC_EGENERIC;
368                     }
369                 }
370             }
371             else
372             {
373                 playlist_Add( p_playlist, psz_uri, p_dir_content->d_name,
374                           PLAYLIST_INSERT, *pi_position );
375                 (*pi_position)++;
376             }
377         }
378         free( psz_uri );
379         p_dir_content = readdir( p_current_dir );
380     }
381     closedir( p_current_dir );
382     return VLC_SUCCESS;
383 }
384
385