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