]> git.sesse.net Git - vlc/blob - modules/access/directory.c
Directory: ignore .ini files.
[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,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",
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;
145     if (strcmp (p_access->psz_path, "-"))
146         handle = utf8_opendir (p_access->psz_path);
147     else
148     {
149 #if 0   /* This won't work yet, it generates paths like "-/music.ogg".
150          * We'd need to use openat() here and in the file access... */
151         int fd = dup (0);
152         handle = fdopendir (fd);
153         if (handle == NULL)
154             close (fd);
155 #else
156         return VLC_EGENERIC;
157 #endif
158     }
159
160     if (handle == NULL)
161         return VLC_EGENERIC;
162
163     p_sys = malloc (sizeof (*p_sys));
164     if (!p_sys)
165     {
166         closedir( handle );
167         return VLC_ENOMEM;
168     }
169
170     p_access->p_sys = p_sys;
171     p_sys->current = NULL;
172     p_sys->handle = handle;
173     p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
174
175     /* Handle mode */
176     char *psz = var_CreateGetString( p_access, "recursive" );
177     if( *psz == '\0' || !strcasecmp( psz, "none" )  )
178         p_sys->mode = MODE_NONE;
179     else if( !strcasecmp( psz, "collapse" )  )
180         p_sys->mode = MODE_COLLAPSE;
181     else
182         p_sys->mode = MODE_EXPAND;
183     free( psz );
184
185     p_access->pf_read  = NULL;
186     p_access->pf_block = Block;
187     p_access->pf_seek  = NULL;
188     p_access->pf_control= Control;
189     free (p_access->psz_demux);
190     p_access->psz_demux = strdup ("xspf-open");
191
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * Close: close the target
197  *****************************************************************************/
198 static void Close( vlc_object_t * p_this )
199 {
200     access_t *p_access = (access_t*)p_this;
201     access_sys_t *p_sys = p_access->p_sys;
202
203     while (p_sys->current)
204     {
205         directory_t *current = p_sys->current;
206
207         p_sys->current = current->parent;
208         closedir (current->handle);
209         free (current->uri);
210         free (current);
211     }
212     if (p_sys->handle != NULL)
213         closedir (p_sys->handle); /* corner case,:Block() not called ever */
214     free (p_sys->ignored_exts);
215     free (p_sys);
216 }
217
218 /**
219  * URI-encodes a file path. The only reserved characters is slash.
220  */
221 static char *encode_path (const char *path)
222 {
223     static const char sep[]= "%2F";
224     char *enc = encode_URI_component (path), *ptr = enc;
225
226     if (enc == NULL)
227         return NULL;
228
229     /* Replace '%2F' with '/'. TODO: extend encode_URI*() */
230     /* (On Windows, both ':' and '\\' will be encoded) */
231     while ((ptr = strstr (ptr, sep)) != NULL)
232     {
233         *ptr++ = '/';
234         memmove (ptr, ptr + 2, strlen (ptr) - 1);
235     }
236     return enc;
237 }
238
239 /* Detect directories that recurse into themselves. */
240 static bool has_inode_loop (const directory_t *dir)
241 {
242 #ifndef WIN32
243     dev_t dev = dir->st.st_dev;
244     ino_t inode = dir->st.st_ino;
245
246     while ((dir = dir->parent) != NULL)
247         if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
248             return true;
249 #else
250 # define fstat( fd, st ) (0)
251 #endif
252     return false;
253 }
254
255 static block_t *Block (access_t *p_access)
256 {
257     access_sys_t *p_sys = p_access->p_sys;
258     directory_t *current = p_sys->current;
259
260     if (p_access->info.b_eof)
261         return NULL;
262
263     if (current == NULL)
264     {   /* Startup: send the XSPF header */
265         static const char header[] =
266             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
267             "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
268             " <trackList>\n";
269         block_t *block = block_Alloc (sizeof (header) - 1);
270         if (!block)
271             goto fatal;
272         memcpy (block->p_buffer, header, sizeof (header) - 1);
273
274         /* "Open" the base directory */
275         current = malloc (sizeof (*current) + strlen (p_access->psz_path));
276         if (current == NULL)
277         {
278             block_Release (block);
279             goto fatal;
280         }
281         current->parent = NULL;
282         current->handle = p_sys->handle;
283         strcpy (current->path, p_access->psz_path);
284         current->uri = encode_path (current->path);
285         if ((current->uri == NULL)
286          || fstat (dirfd (current->handle), &current->st))
287         {
288             free (current->uri);
289             free (current);
290             block_Release (block);
291             goto fatal;
292         }
293
294         p_sys->handle = NULL;
295         p_sys->current = current;
296         return block;
297     }
298
299     char *entry = utf8_readdir (current->handle);
300     if (entry == NULL)
301     {   /* End of directory, go back to parent */
302         closedir (current->handle);
303         p_sys->current = current->parent;
304         free (current);
305
306         if (p_sys->current == NULL)
307         {   /* End of XSPF playlist */
308             static const char footer[] =
309                 " </trackList>\n"
310                 "</playlist>\n";
311
312             block_t *block = block_Alloc (sizeof (footer) - 1);
313             if (!block)
314                 goto fatal;
315             memcpy (block->p_buffer, footer, sizeof (footer) - 1);
316             p_access->info.b_eof = true;
317             return block;
318         }
319         return NULL;
320     }
321
322     /* Skip current, parent and hidden directories */
323     if (entry[0] == '.')
324         return NULL;
325     /* Handle recursion */
326     if (p_sys->mode != MODE_COLLAPSE)
327     {
328         directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
329                                                  + strlen (entry));
330         if (sub == NULL)
331             return NULL;
332         sprintf (sub->path, "%s/%s", current->path, entry);
333
334         DIR *handle = utf8_opendir (sub->path);
335         if (handle != NULL)
336         {
337             sub->parent = current;
338             sub->handle = handle;
339
340             char *encoded = encode_URI_component (entry);
341             if ((encoded == NULL)
342              || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
343                  sub->uri = NULL;
344             free (encoded);
345
346             if ((p_sys->mode == MODE_NONE)
347              || fstat (dirfd (handle), &sub->st)
348              || has_inode_loop (sub)
349              || (sub->uri == NULL))
350             {
351                 closedir (handle);
352                 free (sub);
353                 return NULL;
354             }
355             p_sys->current = sub;
356             return NULL;
357         }
358         else
359             free (sub);
360     }
361
362     /* Skip files with ignored extensions */
363     if (p_sys->ignored_exts != NULL)
364     {
365         const char *ext = strrchr (entry, '.');
366         if (ext != NULL)
367         {
368             size_t extlen = strlen (++ext);
369             for (const char *type = p_sys->ignored_exts, *end;
370                  type[0]; type = end + 1)
371             {
372                 end = strchr (type, ',');
373                 if (end == NULL)
374                     end = type + strlen (type);
375
376                 if (type + extlen == end
377                  && !strncasecmp (ext, type, extlen))
378                     return NULL;
379
380                 if (*end == '\0')
381                     break;
382             }
383         }
384     }
385
386     char *encoded = encode_URI_component (entry);
387     free (entry);
388     if (encoded == NULL)
389         goto fatal;
390     int len = asprintf (&entry,
391                         "  <track><location>file://%s/%s</location></track>\n",
392                         current->uri, encoded);
393     free (encoded);
394     if (len == -1)
395         goto fatal;
396
397     /* TODO: new block allocator for malloc()ated data */
398     block_t *block = block_Alloc (len);
399     if (!block)
400     {
401         free (entry);
402         goto fatal;
403     }
404     memcpy (block->p_buffer, entry, len);
405     free (entry);
406     return block;
407
408 fatal:
409     p_access->info.b_eof = true;
410     return NULL;
411 }
412
413 /*****************************************************************************
414  * Control:
415  *****************************************************************************/
416 static int Control( access_t *p_access, int i_query, va_list args )
417 {
418     bool    *pb_bool;
419     int64_t *pi_64;
420
421     switch( i_query )
422     {
423         /* */
424         case ACCESS_CAN_SEEK:
425         case ACCESS_CAN_FASTSEEK:
426             pb_bool = (bool*)va_arg( args, bool* );
427             *pb_bool = false;
428             break;
429
430         case ACCESS_CAN_PAUSE:
431         case ACCESS_CAN_CONTROL_PACE:
432             pb_bool = (bool*)va_arg( args, bool* );
433             *pb_bool = true;
434             break;
435
436         /* */
437         case ACCESS_GET_PTS_DELAY:
438             pi_64 = (int64_t*)va_arg( args, int64_t * );
439             *pi_64 = DEFAULT_PTS_DELAY * 1000;
440             break;
441
442         /* */
443         case ACCESS_SET_PAUSE_STATE:
444         case ACCESS_GET_TITLE_INFO:
445         case ACCESS_SET_TITLE:
446         case ACCESS_SET_SEEKPOINT:
447         case ACCESS_SET_PRIVATE_ID_STATE:
448         case ACCESS_GET_CONTENT_TYPE:
449         case ACCESS_GET_META:
450             return VLC_EGENERIC;
451
452         default:
453             msg_Warn( p_access, "unimplemented query in control" );
454             return VLC_EGENERIC;
455     }
456     return VLC_SUCCESS;
457 }