]> git.sesse.net Git - vlc/blob - modules/access/directory.c
Change default alsa device lookup
[vlc] / modules / access / directory.c
1 /*****************************************************************************
2  * directory.c: expands a directory (directory: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2002-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          RĂ©mi Denis-Courmont
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_access.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 #   include <sys/types.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 #   include <sys/stat.h>
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #   include <unistd.h>
46 #elif defined( WIN32 ) && !defined( UNDER_CE )
47 #   include <io.h>
48 #endif
49
50 #ifdef HAVE_DIRENT_H
51 #   include <dirent.h>
52 #endif
53 #ifdef __sun__
54 static inline int dirfd (DIR *dir)
55 {
56     return dir->dd_fd;
57 }
58 #endif
59
60 #include <vlc_charset.h>
61 #include <vlc_url.h>
62 #include <vlc_strings.h>
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 static int  Open ( vlc_object_t * );
68 static void Close( vlc_object_t * );
69
70 #define RECURSIVE_TEXT N_("Subdirectory behavior")
71 #define RECURSIVE_LONGTEXT N_( \
72         "Select whether subdirectories must be expanded.\n" \
73         "none: subdirectories do not appear in the playlist.\n" \
74         "collapse: subdirectories appear but are expanded on first play.\n" \
75         "expand: all subdirectories are expanded.\n" )
76
77 static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
78 static const char *const psz_recursive_list_text[] = {
79     N_("none"), N_("collapse"), N_("expand") };
80
81 #define IGNORE_TEXT N_("Ignored extensions")
82 #define IGNORE_LONGTEXT N_( \
83         "Files with these extensions will not be added to playlist when " \
84         "opening a directory.\n" \
85         "This is useful if you add directories that contain playlist files " \
86         "for instance. Use a comma-separated list of extensions." )
87
88 vlc_module_begin ()
89     set_category( CAT_INPUT )
90     set_shortname( N_("Directory" ) )
91     set_subcategory( SUBCAT_INPUT_ACCESS )
92     set_description( N_("Standard filesystem directory input") )
93     set_capability( "access", 55 )
94     add_shortcut( "directory" )
95     add_shortcut( "dir" )
96     add_shortcut( "file" )
97     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
98                 RECURSIVE_LONGTEXT, false )
99       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 )
100     add_string( "ignore-filetypes", "m3u,db,nfo,ini,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv,txt,sub,idx,srt,cue,ssa",
101                 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false )
102     set_callbacks( Open, Close )
103 vlc_module_end ()
104
105
106 /*****************************************************************************
107  * Local prototypes, constants, structures
108  *****************************************************************************/
109
110 enum
111 {
112     MODE_EXPAND,
113     MODE_COLLAPSE,
114     MODE_NONE
115 };
116
117 typedef struct directory_t directory_t;
118 struct directory_t
119 {
120     directory_t *parent;
121     DIR         *handle;
122     char        *uri;
123 #ifndef WIN32
124     struct stat  st;
125 #endif
126     char         path[1];
127 };
128
129 struct access_sys_t
130 {
131     directory_t *current;
132     DIR *handle;
133     char *ignored_exts;
134     int mode;
135     int i_item_count;
136     char *psz_xspf_extension;
137 };
138
139 static block_t *Block( access_t * );
140 static int Control( access_t *, int, va_list );
141
142 /*****************************************************************************
143  * Open: open the directory
144  *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
146 {
147     access_t *p_access = (access_t*)p_this;
148     access_sys_t *p_sys;
149
150     if( !p_access->psz_path )
151         return VLC_EGENERIC;
152
153     DIR *handle;
154     if (strcmp (p_access->psz_path, "-"))
155         handle = utf8_opendir (p_access->psz_path);
156     else
157     {
158 #if 0   /* This won't work yet, it generates paths like "-/music.ogg".
159          * We'd need to use openat() here and in the file access... */
160         int fd = dup (0);
161         handle = fdopendir (fd);
162         if (handle == NULL)
163             close (fd);
164 #else
165         return VLC_EGENERIC;
166 #endif
167     }
168
169     if (handle == NULL)
170         return VLC_EGENERIC;
171
172     p_sys = malloc (sizeof (*p_sys));
173     if (!p_sys)
174     {
175         closedir( handle );
176         return VLC_ENOMEM;
177     }
178
179     p_access->p_sys = p_sys;
180     p_sys->current = NULL;
181     p_sys->handle = handle;
182     p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
183     p_sys->i_item_count = 0;
184     p_sys->psz_xspf_extension = strdup( "" );
185
186     /* Handle mode */
187     char *psz = var_CreateGetString( p_access, "recursive" );
188     if( *psz == '\0' || !strcasecmp( psz, "none" )  )
189         p_sys->mode = MODE_NONE;
190     else if( !strcasecmp( psz, "collapse" )  )
191         p_sys->mode = MODE_COLLAPSE;
192     else
193         p_sys->mode = MODE_EXPAND;
194     free( psz );
195
196     p_access->pf_read  = NULL;
197     p_access->pf_block = Block;
198     p_access->pf_seek  = NULL;
199     p_access->pf_control= Control;
200     free (p_access->psz_demux);
201     p_access->psz_demux = strdup ("xspf-open");
202
203     return VLC_SUCCESS;
204 }
205
206 /*****************************************************************************
207  * Close: close the target
208  *****************************************************************************/
209 static void Close( vlc_object_t * p_this )
210 {
211     access_t *p_access = (access_t*)p_this;
212     access_sys_t *p_sys = p_access->p_sys;
213
214     while (p_sys->current)
215     {
216         directory_t *current = p_sys->current;
217
218         p_sys->current = current->parent;
219         closedir (current->handle);
220         free (current->uri);
221         free (current);
222     }
223     if (p_sys->handle != NULL)
224         closedir (p_sys->handle); /* corner case,:Block() not called ever */
225     free (p_sys->psz_xspf_extension);
226     free (p_sys->ignored_exts);
227     free (p_sys);
228 }
229
230 /* Detect directories that recurse into themselves. */
231 static bool has_inode_loop (const directory_t *dir)
232 {
233 #ifndef WIN32
234     dev_t dev = dir->st.st_dev;
235     ino_t inode = dir->st.st_ino;
236
237     while ((dir = dir->parent) != NULL)
238         if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
239             return true;
240 #else
241 # define fstat( fd, st ) (0)
242     VLC_UNUSED( dir );
243 #endif
244     return false;
245 }
246
247 static block_t *Block (access_t *p_access)
248 {
249     access_sys_t *p_sys = p_access->p_sys;
250     directory_t *current = p_sys->current;
251
252     if (p_access->info.b_eof)
253         return NULL;
254
255     if (current == NULL)
256     {   /* Startup: send the XSPF header */
257         static const char header[] =
258             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
259             "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
260             " <trackList>\n";
261         block_t *block = block_Alloc (sizeof (header) - 1);
262         if (!block)
263             goto fatal;
264         memcpy (block->p_buffer, header, sizeof (header) - 1);
265
266         /* "Open" the base directory */
267         current = malloc (sizeof (*current) + strlen (p_access->psz_path));
268         if (current == NULL)
269         {
270             block_Release (block);
271             goto fatal;
272         }
273         current->parent = NULL;
274         current->handle = p_sys->handle;
275         strcpy (current->path, p_access->psz_path);
276         current->uri = make_URI (current->path);
277         if ((current->uri == NULL)
278          || fstat (dirfd (current->handle), &current->st))
279         {
280             free (current->uri);
281             free (current);
282             block_Release (block);
283             goto fatal;
284         }
285
286         p_sys->handle = NULL;
287         p_sys->current = current;
288         return block;
289     }
290
291     char *entry = utf8_readdir (current->handle);
292     if (entry == NULL)
293     {   /* End of directory, go back to parent */
294         closedir (current->handle);
295         p_sys->current = current->parent;
296         free (current->uri);
297         free (current);
298
299         if (p_sys->current == NULL)
300         {   /* End of XSPF playlist */
301             char *footer;
302             int len = asprintf( &footer, " </trackList>\n" \
303                 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
304                 "%s" \
305                 " </extension>\n" \
306                 "</playlist>\n", p_sys->psz_xspf_extension );
307             if( len < 0 )
308                 goto fatal;
309
310             block_t *block = block_Alloc ( len );
311             if (!block)
312                 goto fatal;
313             memcpy (block->p_buffer, footer, len);
314             free( footer );
315             p_access->info.b_eof = true;
316             return block;
317         }
318         else
319         {
320             /* This was the end of a "subnode" */
321             /* Write the ID to the extension */
322             char *old_xspf_extension = p_sys->psz_xspf_extension;
323             if (old_xspf_extension == NULL)
324                 goto fatal;
325
326             int len2 = asprintf( &p_sys->psz_xspf_extension, "%s  </vlc:node>\n", old_xspf_extension );
327             if (len2 == -1)
328                 goto fatal;
329             free( old_xspf_extension );
330         }
331         return NULL;
332     }
333
334     /* Skip current, parent and hidden directories */
335     if (entry[0] == '.')
336     {
337         free (entry);
338         return NULL;
339     }
340     /* Handle recursion */
341     if (p_sys->mode != MODE_COLLAPSE)
342     {
343         directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
344                                                  + strlen (entry));
345         if (sub == NULL)
346         {
347             free (entry);
348             return NULL;
349         }
350         sprintf (sub->path, "%s/%s", current->path, entry);
351
352         DIR *handle = utf8_opendir (sub->path);
353         if (handle != NULL)
354         {
355             sub->parent = current;
356             sub->handle = handle;
357
358             char *encoded = encode_URI_component (entry);
359             if ((encoded == NULL)
360              || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
361                  sub->uri = NULL;
362             free (encoded);
363
364             if ((p_sys->mode == MODE_NONE)
365              || fstat (dirfd (handle), &sub->st)
366              || has_inode_loop (sub)
367              || (sub->uri == NULL))
368             {
369                 free (entry);
370                 closedir (handle);
371                 free (sub->uri);
372                 free (sub);
373                 return NULL;
374             }
375             p_sys->current = sub;
376
377             /* Add node to xspf extension */
378             char *old_xspf_extension = p_sys->psz_xspf_extension;
379             if (old_xspf_extension == NULL)
380             {
381                 free (entry);
382                 goto fatal;
383             }
384
385             char *title = convert_xml_special_chars (entry);
386             free (entry);
387             if (title == NULL
388              || asprintf (&p_sys->psz_xspf_extension, "%s"
389                           "  <vlc:node title=\"%s\">\n", old_xspf_extension,
390                           title) == -1)
391             {
392                 free (title);
393                 goto fatal;
394             }
395             free (title);
396             free (old_xspf_extension);
397             return NULL;
398         }
399         else
400             free (sub);
401     }
402
403     /* Skip files with ignored extensions */
404     if (p_sys->ignored_exts != NULL)
405     {
406         const char *ext = strrchr (entry, '.');
407         if (ext != NULL)
408         {
409             size_t extlen = strlen (++ext);
410             for (const char *type = p_sys->ignored_exts, *end;
411                  type[0]; type = end + 1)
412             {
413                 end = strchr (type, ',');
414                 if (end == NULL)
415                     end = type + strlen (type);
416
417                 if (type + extlen == end
418                  && !strncasecmp (ext, type, extlen))
419                 {
420                     free (entry);
421                     return NULL;
422                 }
423
424                 if (*end == '\0')
425                     break;
426             }
427         }
428     }
429
430     char *encoded = encode_URI_component (entry);
431     free (entry);
432     if (encoded == NULL)
433         goto fatal;
434     int len = asprintf (&entry,
435                         "  <track><location>%s/%s</location>\n" \
436                         "   <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
437                         "    <vlc:id>%d</vlc:id>\n" \
438                         "   </extension>\n" \
439                         "  </track>\n",
440                         current->uri, encoded, p_sys->i_item_count++);
441     free (encoded);
442     if (len == -1)
443         goto fatal;
444
445     /* Write the ID to the extension */
446     char *old_xspf_extension = p_sys->psz_xspf_extension;
447     if (old_xspf_extension == NULL)
448         goto fatal;
449
450     int len2 = asprintf( &p_sys->psz_xspf_extension, "%s   <vlc:item tid=\"%i\" />\n",
451                             old_xspf_extension, p_sys->i_item_count-1 );
452     if (len2 == -1)
453         goto fatal;
454     free( old_xspf_extension );
455
456     /* TODO: new block allocator for malloc()ated data */
457     block_t *block = block_Alloc (len);
458     if (!block)
459     {
460         free (entry);
461         goto fatal;
462     }
463     memcpy (block->p_buffer, entry, len);
464     free (entry);
465     return block;
466
467 fatal:
468     p_access->info.b_eof = true;
469     return NULL;
470 }
471
472 /*****************************************************************************
473  * Control:
474  *****************************************************************************/
475 static int Control( access_t *p_access, int i_query, va_list args )
476 {
477     switch( i_query )
478     {
479         /* */
480         case ACCESS_CAN_SEEK:
481         case ACCESS_CAN_FASTSEEK:
482             *va_arg( args, bool* ) = false;
483             break;
484
485         case ACCESS_CAN_PAUSE:
486         case ACCESS_CAN_CONTROL_PACE:
487             *va_arg( args, bool* ) = true;
488             break;
489
490         /* */
491         case ACCESS_GET_PTS_DELAY:
492             *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
493             break;
494
495         /* */
496         case ACCESS_SET_PAUSE_STATE:
497         case ACCESS_GET_TITLE_INFO:
498         case ACCESS_SET_TITLE:
499         case ACCESS_SET_SEEKPOINT:
500         case ACCESS_SET_PRIVATE_ID_STATE:
501         case ACCESS_GET_CONTENT_TYPE:
502         case ACCESS_GET_META:
503             return VLC_EGENERIC;
504
505         default:
506             msg_Warn( p_access, "unimplemented query in control" );
507             return VLC_EGENERIC;
508     }
509     return VLC_SUCCESS;
510 }