]> git.sesse.net Git - vlc/blob - modules/access/directory.c
Win32: kill a warning
[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 # undef fstat
242 # define fstat( fd, st ) (0)
243     VLC_UNUSED( dir );
244 #endif
245     return false;
246 }
247
248 static block_t *Block (access_t *p_access)
249 {
250     access_sys_t *p_sys = p_access->p_sys;
251     directory_t *current = p_sys->current;
252
253     if (p_access->info.b_eof)
254         return NULL;
255
256     if (current == NULL)
257     {   /* Startup: send the XSPF header */
258         static const char header[] =
259             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
260             "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
261             " <trackList>\n";
262         block_t *block = block_Alloc (sizeof (header) - 1);
263         if (!block)
264             goto fatal;
265         memcpy (block->p_buffer, header, sizeof (header) - 1);
266
267         /* "Open" the base directory */
268         current = malloc (sizeof (*current) + strlen (p_access->psz_path));
269         if (current == NULL)
270         {
271             block_Release (block);
272             goto fatal;
273         }
274         current->parent = NULL;
275         current->handle = p_sys->handle;
276         strcpy (current->path, p_access->psz_path);
277         current->uri = make_URI (current->path);
278         if ((current->uri == NULL)
279          || fstat (dirfd (current->handle), &current->st))
280         {
281             free (current->uri);
282             free (current);
283             block_Release (block);
284             goto fatal;
285         }
286
287         p_sys->handle = NULL;
288         p_sys->current = current;
289         return block;
290     }
291
292     char *entry = utf8_readdir (current->handle);
293     if (entry == NULL)
294     {   /* End of directory, go back to parent */
295         closedir (current->handle);
296         p_sys->current = current->parent;
297         free (current->uri);
298         free (current);
299
300         if (p_sys->current == NULL)
301         {   /* End of XSPF playlist */
302             char *footer;
303             int len = asprintf( &footer, " </trackList>\n" \
304                 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
305                 "%s" \
306                 " </extension>\n" \
307                 "</playlist>\n", p_sys->psz_xspf_extension );
308             if( len < 0 )
309                 goto fatal;
310
311             block_t *block = block_Alloc ( len );
312             if (!block)
313                 goto fatal;
314             memcpy (block->p_buffer, footer, len);
315             free( footer );
316             p_access->info.b_eof = true;
317             return block;
318         }
319         else
320         {
321             /* This was the end of a "subnode" */
322             /* Write the ID to the extension */
323             char *old_xspf_extension = p_sys->psz_xspf_extension;
324             if (old_xspf_extension == NULL)
325                 goto fatal;
326
327             int len2 = asprintf( &p_sys->psz_xspf_extension, "%s  </vlc:node>\n", old_xspf_extension );
328             if (len2 == -1)
329                 goto fatal;
330             free( old_xspf_extension );
331         }
332         return NULL;
333     }
334
335     /* Skip current, parent and hidden directories */
336     if (entry[0] == '.')
337     {
338         free (entry);
339         return NULL;
340     }
341     /* Handle recursion */
342     if (p_sys->mode != MODE_COLLAPSE)
343     {
344         directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
345                                                  + strlen (entry));
346         if (sub == NULL)
347         {
348             free (entry);
349             return NULL;
350         }
351         sprintf (sub->path, "%s/%s", current->path, entry);
352
353         DIR *handle = utf8_opendir (sub->path);
354         if (handle != NULL)
355         {
356             sub->parent = current;
357             sub->handle = handle;
358
359             char *encoded = encode_URI_component (entry);
360             if ((encoded == NULL)
361              || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
362                  sub->uri = NULL;
363             free (encoded);
364
365             if ((p_sys->mode == MODE_NONE)
366              || fstat (dirfd (handle), &sub->st)
367              || has_inode_loop (sub)
368              || (sub->uri == NULL))
369             {
370                 free (entry);
371                 closedir (handle);
372                 free (sub->uri);
373                 free (sub);
374                 return NULL;
375             }
376             p_sys->current = sub;
377
378             /* Add node to xspf extension */
379             char *old_xspf_extension = p_sys->psz_xspf_extension;
380             if (old_xspf_extension == NULL)
381             {
382                 free (entry);
383                 goto fatal;
384             }
385
386             char *title = convert_xml_special_chars (entry);
387             free (entry);
388             if (title == NULL
389              || asprintf (&p_sys->psz_xspf_extension, "%s"
390                           "  <vlc:node title=\"%s\">\n", old_xspf_extension,
391                           title) == -1)
392             {
393                 free (title);
394                 goto fatal;
395             }
396             free (title);
397             free (old_xspf_extension);
398             return NULL;
399         }
400         else
401             free (sub);
402     }
403
404     /* Skip files with ignored extensions */
405     if (p_sys->ignored_exts != NULL)
406     {
407         const char *ext = strrchr (entry, '.');
408         if (ext != NULL)
409         {
410             size_t extlen = strlen (++ext);
411             for (const char *type = p_sys->ignored_exts, *end;
412                  type[0]; type = end + 1)
413             {
414                 end = strchr (type, ',');
415                 if (end == NULL)
416                     end = type + strlen (type);
417
418                 if (type + extlen == end
419                  && !strncasecmp (ext, type, extlen))
420                 {
421                     free (entry);
422                     return NULL;
423                 }
424
425                 if (*end == '\0')
426                     break;
427             }
428         }
429     }
430
431     char *encoded = encode_URI_component (entry);
432     free (entry);
433     if (encoded == NULL)
434         goto fatal;
435     int len = asprintf (&entry,
436                         "  <track><location>%s/%s</location>\n" \
437                         "   <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
438                         "    <vlc:id>%d</vlc:id>\n" \
439                         "   </extension>\n" \
440                         "  </track>\n",
441                         current->uri, encoded, p_sys->i_item_count++);
442     free (encoded);
443     if (len == -1)
444         goto fatal;
445
446     /* Write the ID to the extension */
447     char *old_xspf_extension = p_sys->psz_xspf_extension;
448     if (old_xspf_extension == NULL)
449         goto fatal;
450
451     int len2 = asprintf( &p_sys->psz_xspf_extension, "%s   <vlc:item tid=\"%i\" />\n",
452                             old_xspf_extension, p_sys->i_item_count-1 );
453     if (len2 == -1)
454         goto fatal;
455     free( old_xspf_extension );
456
457     /* TODO: new block allocator for malloc()ated data */
458     block_t *block = block_Alloc (len);
459     if (!block)
460     {
461         free (entry);
462         goto fatal;
463     }
464     memcpy (block->p_buffer, entry, len);
465     free (entry);
466     return block;
467
468 fatal:
469     p_access->info.b_eof = true;
470     return NULL;
471 }
472
473 /*****************************************************************************
474  * Control:
475  *****************************************************************************/
476 static int Control( access_t *p_access, int i_query, va_list args )
477 {
478     switch( i_query )
479     {
480         /* */
481         case ACCESS_CAN_SEEK:
482         case ACCESS_CAN_FASTSEEK:
483             *va_arg( args, bool* ) = false;
484             break;
485
486         case ACCESS_CAN_PAUSE:
487         case ACCESS_CAN_CONTROL_PACE:
488             *va_arg( args, bool* ) = true;
489             break;
490
491         /* */
492         case ACCESS_GET_PTS_DELAY:
493             *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
494             break;
495
496         /* */
497         case ACCESS_SET_PAUSE_STATE:
498         case ACCESS_GET_TITLE_INFO:
499         case ACCESS_SET_TITLE:
500         case ACCESS_SET_SEEKPOINT:
501         case ACCESS_SET_PRIVATE_ID_STATE:
502         case ACCESS_GET_CONTENT_TYPE:
503         case ACCESS_GET_META:
504             return VLC_EGENERIC;
505
506         default:
507             msg_Warn( p_access, "unimplemented query in control" );
508             return VLC_EGENERIC;
509     }
510     return VLC_SUCCESS;
511 }