]> git.sesse.net Git - vlc/blob - modules/access/directory.c
Remove unneeded casts and intermediaries in va_arg()-using boilerplate.
[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
54 #include <vlc_charset.h>
55 #include <vlc_url.h>
56 #include <vlc_strings.h>
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
64 #define RECURSIVE_TEXT N_("Subdirectory behavior")
65 #define RECURSIVE_LONGTEXT N_( \
66         "Select whether subdirectories must be expanded.\n" \
67         "none: subdirectories do not appear in the playlist.\n" \
68         "collapse: subdirectories appear but are expanded on first play.\n" \
69         "expand: all subdirectories are expanded.\n" )
70
71 static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
72 static const char *const psz_recursive_list_text[] = {
73     N_("none"), N_("collapse"), N_("expand") };
74
75 #define IGNORE_TEXT N_("Ignored extensions")
76 #define IGNORE_LONGTEXT N_( \
77         "Files with these extensions will not be added to playlist when " \
78         "opening a directory.\n" \
79         "This is useful if you add directories that contain playlist files " \
80         "for instance. Use a comma-separated list of extensions." )
81
82 vlc_module_begin ()
83     set_category( CAT_INPUT )
84     set_shortname( N_("Directory" ) )
85     set_subcategory( SUBCAT_INPUT_ACCESS )
86     set_description( N_("Standard filesystem directory input") )
87     set_capability( "access", 55 )
88     add_shortcut( "directory" )
89     add_shortcut( "dir" )
90     add_shortcut( "file" )
91     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
92                 RECURSIVE_LONGTEXT, false )
93       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 )
94     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",
95                 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false )
96     set_callbacks( Open, Close )
97 vlc_module_end ()
98
99
100 /*****************************************************************************
101  * Local prototypes, constants, structures
102  *****************************************************************************/
103
104 enum
105 {
106     MODE_EXPAND,
107     MODE_COLLAPSE,
108     MODE_NONE
109 };
110
111 typedef struct directory_t directory_t;
112 struct directory_t
113 {
114     directory_t *parent;
115     DIR         *handle;
116     char        *uri;
117 #ifndef WIN32
118     struct stat  st;
119 #endif
120     char         path[1];
121 };
122
123 struct access_sys_t
124 {
125     directory_t *current;
126     DIR *handle;
127     char *ignored_exts;
128     int mode;
129     int i_item_count;
130     char *psz_xspf_extension;
131 };
132
133 static block_t *Block( access_t * );
134 static int Control( access_t *, int, va_list );
135
136 /*****************************************************************************
137  * Open: open the directory
138  *****************************************************************************/
139 static int Open( vlc_object_t *p_this )
140 {
141     access_t *p_access = (access_t*)p_this;
142     access_sys_t *p_sys;
143
144     if( !p_access->psz_path )
145         return VLC_EGENERIC;
146
147     DIR *handle;
148     if (strcmp (p_access->psz_path, "-"))
149         handle = utf8_opendir (p_access->psz_path);
150     else
151     {
152 #if 0   /* This won't work yet, it generates paths like "-/music.ogg".
153          * We'd need to use openat() here and in the file access... */
154         int fd = dup (0);
155         handle = fdopendir (fd);
156         if (handle == NULL)
157             close (fd);
158 #else
159         return VLC_EGENERIC;
160 #endif
161     }
162
163     if (handle == NULL)
164         return VLC_EGENERIC;
165
166     p_sys = malloc (sizeof (*p_sys));
167     if (!p_sys)
168     {
169         closedir( handle );
170         return VLC_ENOMEM;
171     }
172
173     p_access->p_sys = p_sys;
174     p_sys->current = NULL;
175     p_sys->handle = handle;
176     p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
177     p_sys->i_item_count = 0;
178     p_sys->psz_xspf_extension = strdup( "" );
179
180     /* Handle mode */
181     char *psz = var_CreateGetString( p_access, "recursive" );
182     if( *psz == '\0' || !strcasecmp( psz, "none" )  )
183         p_sys->mode = MODE_NONE;
184     else if( !strcasecmp( psz, "collapse" )  )
185         p_sys->mode = MODE_COLLAPSE;
186     else
187         p_sys->mode = MODE_EXPAND;
188     free( psz );
189
190     p_access->pf_read  = NULL;
191     p_access->pf_block = Block;
192     p_access->pf_seek  = NULL;
193     p_access->pf_control= Control;
194     free (p_access->psz_demux);
195     p_access->psz_demux = strdup ("xspf-open");
196
197     return VLC_SUCCESS;
198 }
199
200 /*****************************************************************************
201  * Close: close the target
202  *****************************************************************************/
203 static void Close( vlc_object_t * p_this )
204 {
205     access_t *p_access = (access_t*)p_this;
206     access_sys_t *p_sys = p_access->p_sys;
207
208     while (p_sys->current)
209     {
210         directory_t *current = p_sys->current;
211
212         p_sys->current = current->parent;
213         closedir (current->handle);
214         free (current->uri);
215         free (current);
216     }
217     if (p_sys->handle != NULL)
218         closedir (p_sys->handle); /* corner case,:Block() not called ever */
219     free (p_sys->psz_xspf_extension);
220     free (p_sys->ignored_exts);
221     free (p_sys);
222 }
223
224 /* Detect directories that recurse into themselves. */
225 static bool has_inode_loop (const directory_t *dir)
226 {
227 #ifndef WIN32
228     dev_t dev = dir->st.st_dev;
229     ino_t inode = dir->st.st_ino;
230
231     while ((dir = dir->parent) != NULL)
232         if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
233             return true;
234 #else
235 # define fstat( fd, st ) (0)
236 #endif
237     return false;
238 }
239
240 static block_t *Block (access_t *p_access)
241 {
242     access_sys_t *p_sys = p_access->p_sys;
243     directory_t *current = p_sys->current;
244
245     if (p_access->info.b_eof)
246         return NULL;
247
248     if (current == NULL)
249     {   /* Startup: send the XSPF header */
250         static const char header[] =
251             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
252             "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
253             " <trackList>\n";
254         block_t *block = block_Alloc (sizeof (header) - 1);
255         if (!block)
256             goto fatal;
257         memcpy (block->p_buffer, header, sizeof (header) - 1);
258
259         /* "Open" the base directory */
260         current = malloc (sizeof (*current) + strlen (p_access->psz_path));
261         if (current == NULL)
262         {
263             block_Release (block);
264             goto fatal;
265         }
266         current->parent = NULL;
267         current->handle = p_sys->handle;
268         strcpy (current->path, p_access->psz_path);
269         current->uri = make_URI (current->path);
270         if ((current->uri == NULL)
271          || fstat (dirfd (current->handle), &current->st))
272         {
273             free (current->uri);
274             free (current);
275             block_Release (block);
276             goto fatal;
277         }
278
279         p_sys->handle = NULL;
280         p_sys->current = current;
281         return block;
282     }
283
284     char *entry = utf8_readdir (current->handle);
285     if (entry == NULL)
286     {   /* End of directory, go back to parent */
287         closedir (current->handle);
288         p_sys->current = current->parent;
289         free (current->uri);
290         free (current);
291
292         if (p_sys->current == NULL)
293         {   /* End of XSPF playlist */
294             char *footer;
295             int len = asprintf( &footer, " </trackList>\n" \
296                 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
297                 "%s" \
298                 " </extension>\n" \
299                 "</playlist>\n", p_sys->psz_xspf_extension );
300             if( len < 0 )
301                 goto fatal;
302
303             block_t *block = block_Alloc ( len );
304             if (!block)
305                 goto fatal;
306             memcpy (block->p_buffer, footer, len);
307             free( footer );
308             p_access->info.b_eof = true;
309             return block;
310         }
311         else
312         {
313             /* This was the end of a "subnode" */
314             /* Write the ID to the extension */
315             char *old_xspf_extension = p_sys->psz_xspf_extension;
316             if (old_xspf_extension == NULL)
317                 goto fatal;
318
319             int len2 = asprintf( &p_sys->psz_xspf_extension, "%s  </vlc:node>\n", old_xspf_extension );
320             if (len2 == -1)
321                 goto fatal;
322             free( old_xspf_extension );
323         }
324         return NULL;
325     }
326
327     /* Skip current, parent and hidden directories */
328     if (entry[0] == '.')
329     {
330         free (entry);
331         return NULL;
332     }
333     /* Handle recursion */
334     if (p_sys->mode != MODE_COLLAPSE)
335     {
336         directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
337                                                  + strlen (entry));
338         if (sub == NULL)
339         {
340             free (entry);
341             return NULL;
342         }
343         sprintf (sub->path, "%s/%s", current->path, entry);
344
345         DIR *handle = utf8_opendir (sub->path);
346         if (handle != NULL)
347         {
348             sub->parent = current;
349             sub->handle = handle;
350
351             char *encoded = encode_URI_component (entry);
352             if ((encoded == NULL)
353              || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
354                  sub->uri = NULL;
355             free (encoded);
356
357             if ((p_sys->mode == MODE_NONE)
358              || fstat (dirfd (handle), &sub->st)
359              || has_inode_loop (sub)
360              || (sub->uri == NULL))
361             {
362                 free (entry);
363                 closedir (handle);
364                 free (sub->uri);
365                 free (sub);
366                 return NULL;
367             }
368             p_sys->current = sub;
369
370             /* Add node to xspf extension */
371             char *old_xspf_extension = p_sys->psz_xspf_extension;
372             if (old_xspf_extension == NULL)
373             {
374                 free (entry);
375                 goto fatal;
376             }
377
378             char *title = convert_xml_special_chars (entry);
379             free (entry);
380             if (title == NULL
381              || asprintf (&p_sys->psz_xspf_extension, "%s"
382                           "  <vlc:node title=\"%s\">\n", old_xspf_extension,
383                           title) == -1)
384             {
385                 free (title);
386                 goto fatal;
387             }
388             free (title);
389             free (old_xspf_extension);
390             return NULL;
391         }
392         else
393             free (sub);
394     }
395
396     /* Skip files with ignored extensions */
397     if (p_sys->ignored_exts != NULL)
398     {
399         const char *ext = strrchr (entry, '.');
400         if (ext != NULL)
401         {
402             size_t extlen = strlen (++ext);
403             for (const char *type = p_sys->ignored_exts, *end;
404                  type[0]; type = end + 1)
405             {
406                 end = strchr (type, ',');
407                 if (end == NULL)
408                     end = type + strlen (type);
409
410                 if (type + extlen == end
411                  && !strncasecmp (ext, type, extlen))
412                 {
413                     free (entry);
414                     return NULL;
415                 }
416
417                 if (*end == '\0')
418                     break;
419             }
420         }
421     }
422
423     char *encoded = encode_URI_component (entry);
424     free (entry);
425     if (encoded == NULL)
426         goto fatal;
427     int len = asprintf (&entry,
428                         "  <track><location>%s/%s</location>\n" \
429                         "   <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
430                         "    <vlc:id>%d</vlc:id>\n" \
431                         "   </extension>\n" \
432                         "  </track>\n",
433                         current->uri, encoded, p_sys->i_item_count++);
434     free (encoded);
435     if (len == -1)
436         goto fatal;
437
438     /* Write the ID to the extension */
439     char *old_xspf_extension = p_sys->psz_xspf_extension;
440     if (old_xspf_extension == NULL)
441         goto fatal;
442
443     int len2 = asprintf( &p_sys->psz_xspf_extension, "%s   <vlc:item tid=\"%i\" />\n",
444                             old_xspf_extension, p_sys->i_item_count-1 );
445     if (len2 == -1)
446         goto fatal;
447     free( old_xspf_extension );
448
449     /* TODO: new block allocator for malloc()ated data */
450     block_t *block = block_Alloc (len);
451     if (!block)
452     {
453         free (entry);
454         goto fatal;
455     }
456     memcpy (block->p_buffer, entry, len);
457     free (entry);
458     return block;
459
460 fatal:
461     p_access->info.b_eof = true;
462     return NULL;
463 }
464
465 /*****************************************************************************
466  * Control:
467  *****************************************************************************/
468 static int Control( access_t *p_access, int i_query, va_list args )
469 {
470     switch( i_query )
471     {
472         /* */
473         case ACCESS_CAN_SEEK:
474         case ACCESS_CAN_FASTSEEK:
475             *va_arg( args, bool* ) = false;
476             break;
477
478         case ACCESS_CAN_PAUSE:
479         case ACCESS_CAN_CONTROL_PACE:
480             *va_arg( args, bool* ) = true;
481             break;
482
483         /* */
484         case ACCESS_GET_PTS_DELAY:
485             *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
486             break;
487
488         /* */
489         case ACCESS_SET_PAUSE_STATE:
490         case ACCESS_GET_TITLE_INFO:
491         case ACCESS_SET_TITLE:
492         case ACCESS_SET_SEEKPOINT:
493         case ACCESS_SET_PRIVATE_ID_STATE:
494         case ACCESS_GET_CONTENT_TYPE:
495         case ACCESS_GET_META:
496             return VLC_EGENERIC;
497
498         default:
499             msg_Warn( p_access, "unimplemented query in control" );
500             return VLC_EGENERIC;
501     }
502     return VLC_SUCCESS;
503 }