]> git.sesse.net Git - vlc/blob - modules/access/directory.c
directory: close fd before return
[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
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 static int  Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
62
63 #define RECURSIVE_TEXT N_("Subdirectory behavior")
64 #define RECURSIVE_LONGTEXT N_( \
65         "Select whether subdirectories must be expanded.\n" \
66         "none: subdirectories do not appear in the playlist.\n" \
67         "collapse: subdirectories appear but are expanded on first play.\n" \
68         "expand: all subdirectories are expanded.\n" )
69
70 static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
71 static const char *const psz_recursive_list_text[] = {
72     N_("none"), N_("collapse"), N_("expand") };
73
74 #define IGNORE_TEXT N_("Ignored extensions")
75 #define IGNORE_LONGTEXT N_( \
76         "Files with these extensions will not be added to playlist when " \
77         "opening a directory.\n" \
78         "This is useful if you add directories that contain playlist files " \
79         "for instance. Use a comma-separated list of extensions." )
80
81 vlc_module_begin();
82     set_category( CAT_INPUT );
83     set_shortname( N_("Directory" ) );
84     set_subcategory( SUBCAT_INPUT_ACCESS );
85     set_description( N_("Standard filesystem directory input") );
86     set_capability( "access", 55 );
87     add_shortcut( "directory" );
88     add_shortcut( "dir" );
89     add_shortcut( "file" );
90     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
91                 RECURSIVE_LONGTEXT, false );
92       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
93     add_string( "ignore-filetypes", "m3u,db,nfo,jpg,gif,sfv,txt,sub,idx,srt,cue",
94                 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false );
95     set_callbacks( Open, Close );
96 vlc_module_end();
97
98
99 /*****************************************************************************
100  * Local prototypes, constants, structures
101  *****************************************************************************/
102
103 enum
104 {
105     MODE_EXPAND,
106     MODE_COLLAPSE,
107     MODE_NONE
108 };
109
110 typedef struct directory_t directory_t;
111 struct directory_t
112 {
113     directory_t *parent;
114     DIR         *handle;
115     char        *uri;
116 #ifndef WIN32
117     struct stat  st;
118 #endif
119     char         path[1];
120 };
121
122 struct access_sys_t
123 {
124     directory_t *current;
125     DIR *handle;
126     char *ignored_exts;
127     int mode;
128 };
129
130 static block_t *Block( access_t * );
131 static int Control( access_t *, int, va_list );
132
133 /*****************************************************************************
134  * Open: open the directory
135  *****************************************************************************/
136 static int Open( vlc_object_t *p_this )
137 {
138     access_t *p_access = (access_t*)p_this;
139     access_sys_t *p_sys;
140
141     if( !p_access->psz_path )
142         return VLC_EGENERIC;
143
144     DIR *handle = utf8_opendir (p_access->psz_path);
145     if (handle == NULL)
146         return VLC_EGENERIC;
147
148     p_sys = malloc (sizeof (*p_sys));
149     if (!p_sys)
150     {
151         closedir( handle );
152         return VLC_ENOMEM;
153     }
154
155     p_access->p_sys = p_sys;
156     p_sys->current = NULL;
157     p_sys->handle = handle;
158     p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
159
160     /* Handle mode */
161     char *psz = var_CreateGetString( p_access, "recursive" );
162     if( *psz == '\0' || !strcasecmp( psz, "none" )  )
163         p_sys->mode = MODE_NONE;
164     else if( !strcasecmp( psz, "collapse" )  )
165         p_sys->mode = MODE_COLLAPSE;
166     else
167         p_sys->mode = MODE_EXPAND;
168     free( psz );
169
170     p_access->pf_read  = NULL;
171     p_access->pf_block = Block;
172     p_access->pf_seek  = NULL;
173     p_access->pf_control= Control;
174     free (p_access->psz_demux);
175     p_access->psz_demux = strdup ("xspf-open");
176
177     return VLC_SUCCESS;
178 }
179
180 /*****************************************************************************
181  * Close: close the target
182  *****************************************************************************/
183 static void Close( vlc_object_t * p_this )
184 {
185     access_t *p_access = (access_t*)p_this;
186     access_sys_t *p_sys = p_access->p_sys;
187
188     while (p_sys->current)
189     {
190         directory_t *current = p_sys->current;
191
192         p_sys->current = current->parent;
193         closedir (current->handle);
194         free (current->uri);
195         free (current);
196     }
197     if (p_sys->handle != NULL)
198         closedir (p_sys->handle); /* corner case,:Block() not called ever */
199     free (p_sys->ignored_exts);
200     free (p_sys);
201 }
202
203 /**
204  * URI-encodes a file path. The only reserved characters is slash.
205  */
206 static char *encode_path (const char *path)
207 {
208     static const char sep[]= "%2F";
209     char *enc = encode_URI_component (path), *ptr = enc;
210
211     if (enc == NULL)
212         return NULL;
213
214     /* Replace '%2F' with '/'. TODO: extend encode_URI*() */
215     /* (On Windows, both ':' and '\\' will be encoded) */
216     while ((ptr = strstr (ptr, sep)) != NULL)
217     {
218         *ptr++ = '/';
219         memmove (ptr, ptr + 2, strlen (ptr) - 1);
220     }
221     return enc;
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/\">\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 = encode_path (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);
290
291         if (p_sys->current == NULL)
292         {   /* End of XSPF playlist */
293             static const char footer[] =
294                 " </trackList>\n"
295                 "</playlist>\n";
296
297             block_t *block = block_Alloc (sizeof (footer) - 1);
298             if (!block)
299                 goto fatal;
300             memcpy (block->p_buffer, footer, sizeof (footer) - 1);
301             p_access->info.b_eof = true;
302             return block;
303         }
304         return NULL;
305     }
306
307     /* Skip current and parent directories */
308     if (!strcmp (entry, ".") || !strcmp (entry, ".."))
309         return NULL;
310     /* Handle recursion */
311     if (p_sys->mode != MODE_COLLAPSE)
312     {
313         directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
314                                                  + strlen (entry));
315         if (sub == NULL)
316             return NULL;
317         sprintf (sub->path, "%s/%s", current->path, entry);
318
319         DIR *handle = utf8_opendir (sub->path);
320         if (handle != NULL)
321         {
322             sub->parent = current;
323             sub->handle = handle;
324             sub->uri = encode_path (sub->path);
325
326             if ((p_sys->mode == MODE_NONE)
327              || fstat (dirfd (handle), &sub->st)
328              || has_inode_loop (sub)
329              || (sub->uri == NULL))
330             {
331                 closedir (handle);
332                 free (sub);
333                 return NULL;
334             }
335             p_sys->current = sub;
336             return NULL;
337         }
338         else
339             free (sub);
340     }
341
342     /* Skip files with ignored extensions */
343     if (p_sys->ignored_exts != NULL)
344     {
345         const char *ext = strrchr (entry, '.');
346         if (ext != NULL)
347         {
348             size_t extlen = strlen (++ext);
349             for (const char *type = p_sys->ignored_exts, *end;
350                  type[0]; type = end + 1)
351             {
352                 end = strchr (type, ',');
353                 if (end == NULL)
354                     end = type + strlen (type);
355
356                 if (type + extlen == end
357                  && !strncasecmp (ext, type, extlen))
358                     return NULL;
359
360                 if (*end == '\0')
361                     break;
362             }
363         }
364     }
365
366     char *encoded = encode_URI_component (entry);
367     free (entry);
368     if (encoded == NULL)
369         goto fatal;
370     int len = asprintf (&entry,
371                         "  <track><location>file://%s/%s</location></track>\n",
372                         current->uri, encoded);
373     free (encoded);
374     if (len == -1)
375         goto fatal;
376
377     /* TODO: new block allocator for malloc()ated data */
378     block_t *block = block_Alloc (len);
379     if (!block)
380     {
381         free (entry);
382         goto fatal;
383     }
384     memcpy (block->p_buffer, entry, len);
385     free (entry);
386     return block;
387
388 fatal:
389     p_access->info.b_eof = true;
390     return NULL;
391 }
392
393 /*****************************************************************************
394  * Control:
395  *****************************************************************************/
396 static int Control( access_t *p_access, int i_query, va_list args )
397 {
398     bool   *pb_bool;
399     int          *pi_int;
400     int64_t      *pi_64;
401
402     switch( i_query )
403     {
404         /* */
405         case ACCESS_CAN_SEEK:
406         case ACCESS_CAN_FASTSEEK:
407             pb_bool = (bool*)va_arg( args, bool* );
408             *pb_bool = false;
409             break;
410
411         case ACCESS_CAN_PAUSE:
412         case ACCESS_CAN_CONTROL_PACE:
413             pb_bool = (bool*)va_arg( args, bool* );
414             *pb_bool = true;
415             break;
416
417         /* */
418         case ACCESS_GET_MTU:
419             pi_int = (int*)va_arg( args, int * );
420             *pi_int = 0;
421             break;
422
423         case ACCESS_GET_PTS_DELAY:
424             pi_64 = (int64_t*)va_arg( args, int64_t * );
425             *pi_64 = DEFAULT_PTS_DELAY * 1000;
426             break;
427
428         /* */
429         case ACCESS_SET_PAUSE_STATE:
430         case ACCESS_GET_TITLE_INFO:
431         case ACCESS_SET_TITLE:
432         case ACCESS_SET_SEEKPOINT:
433         case ACCESS_SET_PRIVATE_ID_STATE:
434         case ACCESS_GET_CONTENT_TYPE:
435         case ACCESS_GET_META:
436             return VLC_EGENERIC;
437
438         default:
439             msg_Warn( p_access, "unimplemented query in control" );
440             return VLC_EGENERIC;
441     }
442     return VLC_SUCCESS;
443 }