]> git.sesse.net Git - vlc/blob - modules/access/directory.c
Remove useless includes
[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 #elif defined( UNDER_CE )
49 #   define strcoll strcmp
50 #endif
51
52 #ifdef HAVE_DIRENT_H
53 #   include <dirent.h>
54 #endif
55
56 #include <vlc_charset.h>
57 #include <vlc_url.h>
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 static int  Open ( vlc_object_t * );
63 static void Close( vlc_object_t * );
64
65 #define RECURSIVE_TEXT N_("Subdirectory behavior")
66 #define RECURSIVE_LONGTEXT N_( \
67         "Select whether subdirectories must be expanded.\n" \
68         "none: subdirectories do not appear in the playlist.\n" \
69         "collapse: subdirectories appear but are expanded on first play.\n" \
70         "expand: all subdirectories are expanded.\n" )
71
72 static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
73 static const char *const psz_recursive_list_text[] = {
74     N_("none"), N_("collapse"), N_("expand") };
75
76 #define IGNORE_TEXT N_("Ignored extensions")
77 #define IGNORE_LONGTEXT N_( \
78         "Files with these extensions will not be added to playlist when " \
79         "opening a directory.\n" \
80         "This is useful if you add directories that contain playlist files " \
81         "for instance. Use a comma-separated list of extensions." )
82
83 vlc_module_begin();
84     set_category( CAT_INPUT );
85     set_shortname( N_("Directory" ) );
86     set_subcategory( SUBCAT_INPUT_ACCESS );
87     set_description( N_("Standard filesystem directory input") );
88     set_capability( "access", 55 );
89     add_shortcut( "directory" );
90     add_shortcut( "dir" );
91     add_shortcut( "file" );
92     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
93                 RECURSIVE_LONGTEXT, false );
94       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
95     add_string( "ignore-filetypes", "m3u,db,nfo,jpg,gif,sfv,txt,sub,idx,srt,cue",
96                 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false );
97     set_callbacks( Open, Close );
98 vlc_module_end();
99
100
101 /*****************************************************************************
102  * Local prototypes, constants, structures
103  *****************************************************************************/
104
105 enum
106 {
107     MODE_EXPAND,
108     MODE_COLLAPSE,
109     MODE_NONE
110 };
111
112 typedef struct directory_t directory_t;
113 struct directory_t
114 {
115     directory_t *parent;
116     DIR         *handle;
117     char        *uri;
118     struct stat  st;
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         return VLC_ENOMEM;
151
152     p_access->p_sys = p_sys;
153     p_sys->current = NULL;
154     p_sys->handle = handle;
155     p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
156
157     /* Handle mode */
158     char *psz = var_CreateGetString( p_access, "recursive" );
159     if( *psz == '\0' || !strcasecmp( psz, "none" )  )
160         p_sys->mode = MODE_NONE;
161     else if( !strcasecmp( psz, "collapse" )  )
162         p_sys->mode = MODE_COLLAPSE;
163     else
164         p_sys->mode = MODE_EXPAND;
165     free( psz );
166
167     p_access->pf_read  = NULL;
168     p_access->pf_block = Block;
169     p_access->pf_seek  = NULL;
170     p_access->pf_control= Control;
171     free (p_access->psz_demux);
172     p_access->psz_demux = strdup ("xspf-open");
173
174     return VLC_SUCCESS;
175 }
176
177 /*****************************************************************************
178  * Close: close the target
179  *****************************************************************************/
180 static void Close( vlc_object_t * p_this )
181 {
182     access_t *p_access = (access_t*)p_this;
183     access_sys_t *p_sys = p_access->p_sys;
184
185     while (p_sys->current)
186     {
187         directory_t *current = p_sys->current;
188
189         p_sys->current = current->parent;
190         closedir (current->handle);
191         free (current->uri);
192         free (current);
193     }
194     if (p_sys->handle != NULL)
195         closedir (p_sys->handle); /* corner case,:Block() not called ever */
196     free (p_sys->ignored_exts);
197     free (p_sys);
198 }
199
200 /**
201  * URI-encodes a file path. The only reserved characters is slash.
202  */
203 static char *encode_path (const char *path)
204 {
205     static const char sep[]= "%2F";
206     char *enc = encode_URI_component (path), *ptr = enc;
207
208     if (enc == NULL)
209         return NULL;
210
211     /* Replace '%2F' with '/'. TODO: extend encode_URI*() */
212     /* (On Windows, both ':' and '\\' will be encoded) */
213     while ((ptr = strstr (ptr, sep)) != NULL)
214     {
215         *ptr++ = '/';
216         memmove (ptr, ptr + 2, strlen (ptr) - 1);
217     }
218     return enc;
219 }
220
221 /* Detect directories that recurse into themselves. */
222 static bool has_inode_loop (const directory_t *dir)
223 {
224     dev_t dev = dir->st.st_dev;
225     ino_t inode = dir->st.st_ino;
226
227     while ((dir = dir->parent) != NULL)
228         if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
229             return true;
230     return false;
231 }
232
233 static block_t *Block (access_t *p_access)
234 {
235     access_sys_t *p_sys = p_access->p_sys;
236     directory_t *current = p_sys->current;
237
238     if (p_access->info.b_eof)
239         return NULL;
240
241     if (current == NULL)
242     {   /* Startup: send the XSPF header */
243         static const char header[] =
244             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
245             "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n"
246             " <trackList>\n";
247         block_t *block = block_Alloc (sizeof (header) - 1);
248         if (!block)
249             goto fatal;
250         memcpy (block->p_buffer, header, sizeof (header) - 1);
251
252         /* "Open" the base directory */
253         current = malloc (sizeof (*current) + strlen (p_access->psz_path));
254         if (current == NULL)
255         {
256             block_Release (block);
257             goto fatal;
258         }
259         current->parent = NULL;
260         current->handle = p_sys->handle;
261         strcpy (current->path, p_access->psz_path);
262         current->uri = encode_path (current->path);
263         if ((current->uri == NULL)
264          || fstat (dirfd (current->handle), &current->st))
265         {
266             free (current->uri);
267             free (current);
268             block_Release (block);
269             goto fatal;
270         }
271
272         p_sys->handle = NULL;
273         p_sys->current = current;
274         return block;
275     }
276
277     char *entry = utf8_readdir (current->handle);
278     if (entry == NULL)
279     {   /* End of directory, go back to parent */
280         closedir (current->handle);
281         p_sys->current = current->parent;
282         free (current);
283
284         if (p_sys->current == NULL)
285         {   /* End of XSPF playlist */
286             static const char footer[] =
287                 " </trackList>\n"
288                 "</playlist>\n";
289
290             block_t *block = block_Alloc (sizeof (footer) - 1);
291             if (!block)
292                 goto fatal;
293             memcpy (block->p_buffer, footer, sizeof (footer) - 1);
294             p_access->info.b_eof = true;
295             return block;
296         }
297         return NULL;
298     }
299
300     /* Skip current and parent directories */
301     if (!strcmp (entry, ".") || !strcmp (entry, ".."))
302         return NULL;
303     /* Handle recursion */
304     if (p_sys->mode != MODE_COLLAPSE)
305     {
306         directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
307                                                  + strlen (entry));
308         if (sub == NULL)
309             return NULL;
310         sprintf (sub->path, "%s/%s", current->path, entry);
311
312         DIR *handle = utf8_opendir (sub->path);
313         if (handle != NULL)
314         {
315             sub->parent = current;
316             sub->handle = handle;
317             sub->uri = encode_path (sub->path);
318
319             if ((p_sys->mode == MODE_NONE)
320              || fstat (dirfd (handle), &sub->st)
321              || has_inode_loop (sub)
322              || (sub->uri == NULL))
323             {
324                 closedir (handle);
325                 free (sub);
326                 return NULL;
327             }
328             p_sys->current = sub;
329         }
330         else
331             free (sub);
332     }
333
334     /* Skip files with ignored extensions */
335     if (p_sys->ignored_exts != NULL)
336     {
337         const char *ext = strrchr (entry, '.');
338         if (ext != NULL)
339         {
340             size_t extlen = strlen (++ext);
341             for (const char *type = p_sys->ignored_exts, *end;
342                  type[0]; type = end + 1)
343             {
344                 end = strchr (type, ',');
345                 if (end == NULL)
346                     end = type + strlen (type);
347
348                 if (type + extlen == end
349                  && !strncasecmp (ext, type, extlen))
350                     return NULL;
351             }
352         }
353     }
354
355     char *encoded = encode_URI_component (entry);
356     free (entry);
357     if (encoded == NULL)
358         goto fatal;
359     int len = asprintf (&entry,
360                         "  <track><location>file://%s/%s</location></track>\n",
361                         current->uri, encoded);
362     free (encoded);
363     if (len == -1)
364         goto fatal;
365
366     /* TODO: new block allocator for malloc()ated data */
367     block_t *block = block_Alloc (len);
368     if (!block)
369     {
370         free (entry);
371         goto fatal;
372     }
373     memcpy (block->p_buffer, entry, len);
374     free (entry);
375     return block;
376
377 fatal:
378     p_access->info.b_eof = true;
379     return NULL;
380 }
381
382 /*****************************************************************************
383  * Control:
384  *****************************************************************************/
385 static int Control( access_t *p_access, int i_query, va_list args )
386 {
387     bool   *pb_bool;
388     int          *pi_int;
389     int64_t      *pi_64;
390
391     switch( i_query )
392     {
393         /* */
394         case ACCESS_CAN_SEEK:
395         case ACCESS_CAN_FASTSEEK:
396             pb_bool = (bool*)va_arg( args, bool* );
397             *pb_bool = false;
398             break;
399
400         case ACCESS_CAN_PAUSE:
401         case ACCESS_CAN_CONTROL_PACE:
402             pb_bool = (bool*)va_arg( args, bool* );
403             *pb_bool = true;
404             break;
405
406         /* */
407         case ACCESS_GET_MTU:
408             pi_int = (int*)va_arg( args, int * );
409             *pi_int = 0;
410             break;
411
412         case ACCESS_GET_PTS_DELAY:
413             pi_64 = (int64_t*)va_arg( args, int64_t * );
414             *pi_64 = DEFAULT_PTS_DELAY * 1000;
415             break;
416
417         /* */
418         case ACCESS_SET_PAUSE_STATE:
419         case ACCESS_GET_TITLE_INFO:
420         case ACCESS_SET_TITLE:
421         case ACCESS_SET_SEEKPOINT:
422         case ACCESS_SET_PRIVATE_ID_STATE:
423         case ACCESS_GET_CONTENT_TYPE:
424         case ACCESS_GET_META:
425             return VLC_EGENERIC;
426
427         default:
428             msg_Warn( p_access, "unimplemented query in control" );
429             return VLC_EGENERIC;
430     }
431     return VLC_SUCCESS;
432 }