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