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