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