]> git.sesse.net Git - vlc/blob - modules/access/directory.c
* src/extras/dirent.c, ALL: fixed win32/ce dirent replacement and made it accessible...
[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 #ifdef HAVE_DIRENT_H
54 #   include <dirent.h>
55 #endif
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 static int  Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
62
63 static int  DemuxOpen ( vlc_object_t * );
64
65 #define RECURSIVE_TEXT N_("Subdirectory behavior")
66 #define RECURSIVE_LONGTEXT N_( \
67         "Select whether subdirectories must be expanded.\n" \
68         "none: subdirectories do not appear in the playlist.\n" \
69         "collapse: subdirectories appear but are expanded on first play.\n" \
70         "expand: all subdirectories are expanded.\n" )
71
72 static char *psz_recursive_list[] = { "none", "collapse", "expand" };
73 static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
74                                            N_("expand") };
75
76 vlc_module_begin();
77     set_category( CAT_INPUT );
78     set_shortname( _("Directory" ) );
79     set_subcategory( SUBCAT_INPUT_ACCESS );
80     set_description( _("Standard filesystem directory input") );
81     set_capability( "access2", 55 );
82     add_shortcut( "directory" );
83     add_shortcut( "dir" );
84     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
85                 RECURSIVE_LONGTEXT, VLC_FALSE );
86       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
87     set_callbacks( Open, Close );
88
89     add_submodule();
90         set_description( "Directory EOF");
91         set_capability( "demux2", 0 );
92         add_shortcut( "directory" );
93         set_callbacks( DemuxOpen, NULL );
94 vlc_module_end();
95
96
97 /*****************************************************************************
98  * Local prototypes, constants, structures
99  *****************************************************************************/
100
101 #define MODE_EXPAND 0
102 #define MODE_COLLAPSE 1
103 #define MODE_NONE 2
104
105 static int Read( access_t *, uint8_t *, int );
106 static int ReadNull( access_t *, uint8_t *, int );
107 static int Control( access_t *, int, va_list );
108
109 static int Demux( demux_t *p_demux );
110 static int DemuxControl( demux_t *p_demux, int i_query, va_list args );
111
112
113 static int ReadDir( playlist_t *, char *psz_name, int i_mode, int *pi_pos,
114                     playlist_item_t * );
115
116 /*****************************************************************************
117  * Open: open the directory
118  *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
120 {
121     access_t *p_access = (access_t*)p_this;
122
123 #ifdef HAVE_SYS_STAT_H
124     struct stat stat_info;
125
126     if( ( stat( p_access->psz_path, &stat_info ) == -1 ) ||
127         !S_ISDIR( stat_info.st_mode ) )
128 #else
129     if( strcmp( p_access->psz_access, "dir") &&
130         strcmp( p_access->psz_access, "directory") )
131 #endif
132     {
133         return VLC_EGENERIC;
134     }
135
136     p_access->pf_read  = Read;
137     p_access->pf_block = NULL;
138     p_access->pf_seek  = NULL;
139     p_access->pf_control= Control;
140
141     /* Force a demux */
142     p_access->psz_demux = strdup( "directory" );
143
144     return VLC_SUCCESS;
145 }
146
147 /*****************************************************************************
148  * Close: close the target
149  *****************************************************************************/
150 static void Close( vlc_object_t * p_this )
151 {
152 }
153
154 /*****************************************************************************
155  * ReadNull: read the directory
156  *****************************************************************************/
157 static int ReadNull( access_t *p_access, uint8_t *p_buffer, int i_len)
158 {
159     /* Return fake data */
160     memset( p_buffer, 0, i_len );
161     return i_len;
162 }
163
164 /*****************************************************************************
165  * Read: read the directory
166  *****************************************************************************/
167 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len)
168 {
169     char *psz_name = NULL;
170     char *psz;
171     int  i_mode, i_pos;
172
173     playlist_item_t *p_item;
174     vlc_bool_t b_play = VLC_FALSE;
175
176     playlist_t *p_playlist =
177         (playlist_t *) vlc_object_find( p_access,
178                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
179
180     if( !p_playlist )
181     {
182         msg_Err( p_access, "can't find playlist" );
183         goto end;
184     }
185
186     /* Remove the ending '/' char */
187     psz_name = strdup( p_access->psz_path );
188     if( psz_name == NULL )
189         goto end;
190
191     if( (psz_name[strlen(psz_name)-1] == '/') ||
192         (psz_name[strlen(psz_name)-1] == '\\') )
193     {
194         psz_name[strlen(psz_name)-1] = '\0';
195     }
196
197     /* Initialize structure */
198     psz = var_CreateGetString( p_access, "recursive" );
199     if( *psz == '\0' || !strncmp( psz, "none" , 4 )  )
200     {
201         i_mode = MODE_NONE;
202     }
203     else if( !strncmp( psz, "collapse", 8 )  )
204     {
205         i_mode = MODE_COLLAPSE;
206     }
207     else
208     {
209         i_mode = MODE_EXPAND;
210     }
211     free( psz );
212
213     /* Make sure we are deleted when we are done */
214     /* The playlist position we will use for the add */
215     i_pos = p_playlist->i_index + 1;
216
217     msg_Dbg( p_access, "opening directory `%s'", psz_name );
218
219     if( &p_playlist->status.p_item->input ==
220         ((input_thread_t *)p_access->p_parent)->input.p_item )
221     {
222         p_item = p_playlist->status.p_item;
223         b_play = VLC_TRUE;
224         msg_Dbg( p_access, "starting directory playback");
225     }
226     else
227     {
228         input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
229                                                         input.p_item;
230         p_item = playlist_LockItemGetByInput( p_playlist, p_current );
231         msg_Dbg( p_access, "not starting directory playback");
232         if( !p_item )
233         {
234             msg_Dbg( p_playlist, "unable to find item in playlist");
235             return -1;
236         }
237         b_play = VLC_FALSE;
238     }
239     p_item->input.i_type = ITEM_TYPE_DIRECTORY;
240     if( ReadDir( p_playlist, psz_name , i_mode, &i_pos,
241                  p_item ) != VLC_SUCCESS )
242     {
243     }
244 end:
245
246     /* Begin to read the directory */
247     if( b_play )
248     {
249         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
250                           p_playlist->status.i_view,
251                           p_playlist->status.p_item, NULL );
252     }
253     if( psz_name ) free( psz_name );
254     vlc_object_release( p_playlist );
255
256     /* Return fake data forever */
257     p_access->pf_read = ReadNull;
258     return ReadNull( p_access, p_buffer, i_len );
259 }
260
261 /*****************************************************************************
262  * DemuxOpen:
263  *****************************************************************************/
264 static int Control( access_t *p_access, int i_query, va_list args )
265 {
266     vlc_bool_t   *pb_bool;
267     int          *pi_int;
268     int64_t      *pi_64;
269
270     switch( i_query )
271     {
272         /* */
273         case ACCESS_CAN_SEEK:
274         case ACCESS_CAN_FASTSEEK:
275         case ACCESS_CAN_PAUSE:
276         case ACCESS_CAN_CONTROL_PACE:
277             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
278             *pb_bool = VLC_FALSE;    /* FIXME */
279             break;
280
281         /* */
282         case ACCESS_GET_MTU:
283             pi_int = (int*)va_arg( args, int * );
284             *pi_int = 0;
285             break;
286
287         case ACCESS_GET_PTS_DELAY:
288             pi_64 = (int64_t*)va_arg( args, int64_t * );
289             *pi_64 = DEFAULT_PTS_DELAY * 1000;
290             break;
291
292         /* */
293         case ACCESS_SET_PAUSE_STATE:
294         case ACCESS_GET_TITLE_INFO:
295         case ACCESS_SET_TITLE:
296         case ACCESS_SET_SEEKPOINT:
297         case ACCESS_SET_PRIVATE_ID_STATE:
298             return VLC_EGENERIC;
299
300         default:
301             msg_Warn( p_access, "unimplemented query in control" );
302             return VLC_EGENERIC;
303     }
304     return VLC_SUCCESS;
305 }
306
307 /*****************************************************************************
308  * DemuxOpen:
309  *****************************************************************************/
310 static int DemuxOpen ( vlc_object_t *p_this )
311 {
312     demux_t *p_demux = (demux_t*)p_this;
313
314     if( strcmp( p_demux->psz_demux, "directory" ) )
315         return VLC_EGENERIC;
316
317     p_demux->pf_demux   = Demux;
318     p_demux->pf_control = DemuxControl;
319     return VLC_SUCCESS;
320 }
321
322 /*****************************************************************************
323  * Demux: EOF
324  *****************************************************************************/
325 static int Demux( demux_t *p_demux )
326 {
327     return 0;
328 }
329 /*****************************************************************************
330  * DemuxControl:
331  *****************************************************************************/
332 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
333 {
334     return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
335 }
336
337 #if defined(SYS_BEOS) || defined(WIN32)
338 /* BeOS doesn't have scandir/alphasort/versionsort */
339 static int alphasort( const struct dirent **a, const struct dirent **b )
340 {
341     return strcoll( (*a)->d_name, (*b)->d_name );
342 }
343
344 static int scandir( const char *name, struct dirent ***namelist,
345                     int (*filter) ( const struct dirent * ),
346                     int (*compar) ( const struct dirent **,
347                                     const struct dirent ** ) )
348 {
349     DIR            * p_dir;
350     struct dirent  * p_content;
351     struct dirent ** pp_list;
352     int              ret, size;
353
354     if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
355
356     ret     = 0;
357     pp_list = NULL;
358     while( ( p_content = readdir( p_dir ) ) )
359     {
360         if( filter && !filter( p_content ) )
361         {
362             continue;
363         }
364         pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
365         size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
366         pp_list[ret] = malloc( size );
367         memcpy( pp_list[ret], p_content, size );
368         ret++;
369     }
370
371     closedir( p_dir );
372
373     if( compar )
374     {
375         qsort( pp_list, ret, sizeof( struct dirent * ),
376                (int (*)(const void *, const void *)) compar );
377     }
378
379     *namelist = pp_list;
380     return ret;
381 }
382 #endif
383
384 static int Filter( const struct dirent *foo )
385 {
386     return VLC_TRUE;
387 }
388 /*****************************************************************************
389  * ReadDir: read a directory and add its content to the list
390  *****************************************************************************/
391 static int ReadDir( playlist_t *p_playlist,
392                     char *psz_name , int i_mode, int *pi_position,
393                     playlist_item_t *p_parent )
394 {
395     struct dirent   *p_dir_content;
396     struct dirent   **pp_dir_content;
397     int             i_dir_content, i = 0;
398     playlist_item_t *p_node;
399
400     /* Change the item to a node */
401     if( p_parent->i_children == -1 )
402     {
403         playlist_LockItemToNode( p_playlist,p_parent );
404     }
405
406     /* get the first directory entry */
407     i_dir_content = scandir( psz_name, &pp_dir_content, Filter, alphasort );
408     if( i_dir_content == -1 )
409     {
410         msg_Warn( p_playlist, "Failed to read directory" );
411         return VLC_EGENERIC;
412     }
413     else if( i_dir_content <= 0 )
414     {
415         /* directory is empty */
416         return VLC_SUCCESS;
417     }
418     p_dir_content = pp_dir_content[0];
419
420     /* while we still have entries in the directory */
421     while( i < i_dir_content )
422     {
423         int i_size_entry = strlen( psz_name ) +
424                            strlen( p_dir_content->d_name ) + 2;
425         char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
426
427         sprintf( psz_uri, "%s/%s", psz_name, p_dir_content->d_name );
428
429         /* if it starts with '.' then forget it */
430         if( p_dir_content->d_name[0] != '.' )
431         {
432 #if defined( S_ISDIR )
433             struct stat stat_data;
434             stat( psz_uri, &stat_data );
435             if( S_ISDIR(stat_data.st_mode) && i_mode != MODE_COLLAPSE )
436 #elif defined( DT_DIR )
437             if( ( p_dir_content->d_type & DT_DIR ) && i_mode != MODE_COLLAPSE )
438 #else
439             if( 0 )
440 #endif
441             {
442                 if( i_mode == MODE_NONE )
443                 {
444                     msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
445                     i++;
446                     p_dir_content = pp_dir_content[i];
447                     continue;
448                 }
449                 else if(i_mode == MODE_EXPAND )
450                 {
451                     char *psz_newname;
452                     msg_Dbg(p_playlist, "Reading subdirectory %s", psz_uri );
453
454                     if( !strncmp( psz_uri, psz_name, strlen( psz_name ) ) )
455                     {
456                         char *psz_subdir = psz_uri;
457                         /* Skip the parent path + the separator */
458                         psz_subdir += strlen( psz_name ) + 1;
459                         psz_newname = strdup( psz_subdir );
460                     }
461                     else
462                     {
463                         psz_newname = strdup( psz_uri );
464                     }
465                     p_node = playlist_NodeCreate( p_playlist,
466                                        p_parent->pp_parents[0]->i_view,
467                                        psz_newname, p_parent );
468
469                     playlist_CopyParents(  p_parent, p_node );
470
471                     p_node->input.i_type = ITEM_TYPE_DIRECTORY;
472
473                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
474                                  pi_position, p_node ) != VLC_SUCCESS )
475                     {
476                         return VLC_EGENERIC;
477                     }
478
479                     free( psz_newname );
480                 }
481             }
482             else
483             {
484                 playlist_item_t *p_item = playlist_ItemNewWithType(
485                                                 p_playlist,
486                                                 psz_uri,
487                                                 p_dir_content->d_name,
488                                                 ITEM_TYPE_VFILE );
489                 playlist_NodeAddItem( p_playlist,p_item,
490                                       p_parent->pp_parents[0]->i_view,
491                                       p_parent,
492                                       PLAYLIST_APPEND, PLAYLIST_END );
493
494                 playlist_CopyParents( p_parent, p_item );
495             }
496         }
497         free( psz_uri );
498         i++;
499         p_dir_content = pp_dir_content[i];
500     }
501     free( pp_dir_content );
502     return VLC_SUCCESS;
503 }