]> git.sesse.net Git - vlc/blob - modules/access/directory.c
Change --directory-version-sort boolean config option to a --directory-sort multiple...
[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 "fs.h"
35 #include <vlc_access.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #   include <fcntl.h>
42 #elif defined( WIN32 ) && !defined( UNDER_CE )
43 #   include <io.h>
44 #endif
45
46 #include <vlc_fs.h>
47 #include <vlc_url.h>
48 #include <vlc_strings.h>
49 #include <vlc_charset.h>
50
51 enum
52 {
53     MODE_NONE,
54     MODE_COLLAPSE,
55     MODE_EXPAND,
56 };
57
58 typedef struct directory_t directory_t;
59 struct directory_t
60 {
61     directory_t *parent;
62     DIR         *handle;
63     char        *uri;
64     char       **filev;
65     int          filec, i;
66 #ifdef HAVE_OPENAT
67     dev_t        device;
68     ino_t        inode;
69 #else
70     char         *path;
71 #endif
72 };
73
74 struct access_sys_t
75 {
76     directory_t *current;
77     char *ignored_exts;
78     char mode;
79     bool header;
80     int i_item_count;
81     char *xspf_ext;
82     int (*compar)(const char **a, const char **b);
83 };
84
85 /* Select non-hidden files only */
86 static int visible (const char *name)
87 {
88     return name[0] != '.';
89 }
90
91 static int collate (const char **a, const char **b)
92 {
93 #ifdef HAVE_STRCOLL
94     return strcoll (*a, *b);
95 #else
96     return strcmp  (*a, *b);
97 #endif
98 }
99
100 static int version (const char **a, const char **b)
101 {
102     return strverscmp (*a, *b);
103 }
104
105 /*****************************************************************************
106  * Open: open the directory
107  *****************************************************************************/
108 int DirOpen( vlc_object_t *p_this )
109 {
110     access_t *p_access = (access_t*)p_this;
111
112     if( !p_access->psz_filepath )
113         return VLC_EGENERIC;
114
115     DIR *handle = vlc_opendir (p_access->psz_filepath);
116     if (handle == NULL)
117         return VLC_EGENERIC;
118
119     return DirInit (p_access, handle);
120 }
121
122 int DirInit (access_t *p_access, DIR *handle)
123 {
124     access_sys_t *p_sys = malloc (sizeof (*p_sys));
125     if (unlikely(p_sys == NULL))
126         goto error;
127
128     char *uri;
129     if (!strcmp (p_access->psz_access, "fd"))
130     {
131         if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
132             uri = NULL;
133     }
134     else
135         uri = make_URI (p_access->psz_filepath, "file");
136     if (unlikely(uri == NULL))
137         goto error;
138
139     /* "Open" the base directory */
140     directory_t *root = malloc (sizeof (*root));
141     if (unlikely(root == NULL))
142     {
143         free (uri);
144         goto error;
145     }
146
147     char *psz_sort = var_InheritString (p_access, "directory-sort");
148     if (!psz_sort)
149         p_sys->compar = collate;
150     else if (!strcasecmp (psz_sort, "version"))
151         p_sys->compar = version;
152     else if (!strcasecmp (psz_sort, "none"))
153         p_sys->compar = NULL;
154     else
155         p_sys->compar = collate;
156     free(psz_sort);
157
158     root->parent = NULL;
159     root->handle = handle;
160     root->uri = uri;
161     root->filec = vlc_loaddir (handle, &root->filev, visible, p_sys->compar);
162     if (root->filec < 0)
163         root->filev = NULL;
164     root->i = 0;
165 #ifdef HAVE_OPENAT
166     struct stat st;
167     if (fstat (dirfd (handle), &st))
168     {
169         free (root);
170         free (uri);
171         goto error;
172     }
173     root->device = st.st_dev;
174     root->inode = st.st_ino;
175 #else
176     root->path = strdup (p_access->psz_filepath);
177 #endif
178
179     p_access->p_sys = p_sys;
180     p_sys->current = root;
181     p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
182     p_sys->header = true;
183     p_sys->i_item_count = 0;
184     p_sys->xspf_ext = strdup ("");
185
186     /* Handle mode */
187     char *psz = var_InheritString (p_access, "recursive");
188     if (psz == NULL || !strcasecmp (psz, "none"))
189         p_sys->mode = MODE_NONE;
190     else if( !strcasecmp( psz, "collapse" )  )
191         p_sys->mode = MODE_COLLAPSE;
192     else
193         p_sys->mode = MODE_EXPAND;
194     free( psz );
195
196     access_InitFields(p_access);
197     p_access->pf_read  = NULL;
198     p_access->pf_block = DirBlock;
199     p_access->pf_seek  = NULL;
200     p_access->pf_control= DirControl;
201     free (p_access->psz_demux);
202     p_access->psz_demux = strdup ("xspf-open");
203
204     return VLC_SUCCESS;
205
206 error:
207     closedir (handle);
208     free (p_sys);
209     return VLC_EGENERIC;
210 }
211
212 /*****************************************************************************
213  * Close: close the target
214  *****************************************************************************/
215 void DirClose( vlc_object_t * p_this )
216 {
217     access_t *p_access = (access_t*)p_this;
218     access_sys_t *p_sys = p_access->p_sys;
219
220     while (p_sys->current)
221     {
222         directory_t *current = p_sys->current;
223
224         p_sys->current = current->parent;
225         closedir (current->handle);
226         free (current->uri);
227         while (current->i < current->filec)
228             free (current->filev[current->i++]);
229         free (current->filev);
230 #ifndef HAVE_OPENAT
231         free (current->path);
232 #endif
233         free (current);
234     }
235
236     free (p_sys->xspf_ext);
237     free (p_sys->ignored_exts);
238     free (p_sys);
239 }
240
241 #ifdef HAVE_OPENAT
242 /* Detect directories that recurse into themselves. */
243 static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
244 {
245     while (dir != NULL)
246     {
247         if ((dir->device == dev) && (dir->inode == inode))
248             return true;
249         dir = dir->parent;
250     }
251     return false;
252 }
253 #endif
254
255 block_t *DirBlock (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 (p_sys->header)
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/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/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         p_sys->header = false;
274         return block;
275     }
276
277     if (current->i >= current->filec)
278     {   /* End of directory, go back to parent */
279         closedir (current->handle);
280         p_sys->current = current->parent;
281         free (current->uri);
282         free (current->filev);
283 #ifndef HAVE_OPENAT
284         free (current->path);
285 #endif
286         free (current);
287
288         if (p_sys->current == NULL)
289         {   /* End of XSPF playlist */
290             char *footer;
291             int len = asprintf (&footer, " </trackList>\n"
292                 " <extension application=\"http://www.videolan.org/"
293                                              "vlc/playlist/0\">\n"
294                 "%s"
295                 " </extension>\n"
296                 "</playlist>\n", p_sys->xspf_ext ? p_sys->xspf_ext : "");
297             if (unlikely(len == -1))
298                 goto fatal;
299
300             block_t *block = block_heap_Alloc (footer, footer, len);
301             if (unlikely(block == NULL))
302                 free (footer);
303             p_access->info.b_eof = true;
304             return block;
305         }
306         else
307         {
308             /* This was the end of a "subnode" */
309             /* Write the ID to the extension */
310             char *old_xspf_ext = p_sys->xspf_ext;
311             if (old_xspf_ext != NULL
312              && asprintf (&p_sys->xspf_ext, "%s  </vlc:node>\n",
313                           old_xspf_ext ? old_xspf_ext : "") == -1)
314                 p_sys->xspf_ext = NULL;
315             free (old_xspf_ext);
316         }
317         return NULL;
318     }
319
320     char *entry = current->filev[current->i++];
321
322     /* Handle recursion */
323     if (p_sys->mode != MODE_COLLAPSE)
324     {
325         DIR *handle;
326 #ifdef HAVE_OPENAT
327         int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
328         if (fd == -1)
329             goto skip; /* File cannot be opened... forget it */
330
331         struct stat st;
332         if (fstat (fd, &st))
333         {
334             close (fd);
335             goto skip; /* cannot stat?! */
336         }
337         if (!S_ISDIR (st.st_mode))
338         {
339             close (fd);
340             goto notdir;
341         }
342         if (p_sys->mode == MODE_NONE
343          || has_inode_loop (current, st.st_dev, st.st_ino)
344          || (handle = fdopendir (fd)) == NULL)
345         {
346             close (fd);
347             goto skip;
348         }
349 #else
350         char *path;
351         if (asprintf (&path, "%s/%s", current->path, entry) == -1)
352             goto skip;
353         if ((handle = vlc_opendir (path)) == NULL)
354             goto notdir;
355         if (p_sys->mode == MODE_NONE)
356             goto skip;
357 #endif
358         directory_t *sub = malloc (sizeof (*sub));
359         if (unlikely(sub == NULL))
360         {
361             closedir (handle);
362 #ifndef HAVE_OPENAT
363             free (path);
364 #endif
365             goto skip;
366         }
367         sub->parent = current;
368         sub->handle = handle;
369         sub->filec = vlc_loaddir (handle, &sub->filev, visible, p_sys->compar);
370         if (sub->filec < 0)
371             sub->filev = NULL;
372         sub->i = 0;
373 #ifdef HAVE_OPENAT
374         sub->device = st.st_dev;
375         sub->inode = st.st_ino;
376 #else
377         sub->path = path;
378 #endif
379         p_sys->current = sub;
380
381         char *encoded = encode_URI_component (entry);
382         if (encoded == NULL
383          || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
384              sub->uri = NULL;
385         free (encoded);
386         if (unlikely(sub->uri == NULL))
387         {
388             free (entry);
389             goto fatal;
390         }
391
392         /* Add node to XSPF extension */
393         char *old_xspf_ext = p_sys->xspf_ext;
394         EnsureUTF8 (entry);
395         char *title = convert_xml_special_chars (entry);
396         if (old_xspf_ext != NULL
397          && asprintf (&p_sys->xspf_ext, "%s  <vlc:node title=\"%s\">\n",
398                       old_xspf_ext, title ? title : "?") == -1)
399             p_sys->xspf_ext = NULL;
400         free (old_xspf_ext);
401         free (title);
402         goto skip;
403     }
404
405 notdir:
406     /* Skip files with ignored extensions */
407     if (p_sys->ignored_exts != NULL)
408     {
409         const char *ext = strrchr (entry, '.');
410         if (ext != NULL)
411         {
412             size_t extlen = strlen (++ext);
413             for (const char *type = p_sys->ignored_exts, *end;
414                  type[0]; type = end + 1)
415             {
416                 end = strchr (type, ',');
417                 if (end == NULL)
418                     end = type + strlen (type);
419
420                 if (type + extlen == end
421                  && !strncasecmp (ext, type, extlen))
422                 {
423                     free (entry);
424                     return NULL;
425                 }
426
427                 if (*end == '\0')
428                     break;
429             }
430         }
431     }
432
433     char *encoded = encode_URI_component (entry);
434     free (entry);
435     if (encoded == NULL)
436         goto fatal;
437     int len = asprintf (&entry,
438                         "  <track><location>%s/%s</location>\n" \
439                         "   <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
440                         "    <vlc:id>%d</vlc:id>\n" \
441                         "   </extension>\n" \
442                         "  </track>\n",
443                         current->uri, encoded, p_sys->i_item_count++);
444     free (encoded);
445     if (len == -1)
446         goto fatal;
447
448     /* Write the ID to the extension */
449     char *old_xspf_ext = p_sys->xspf_ext;
450     if (old_xspf_ext != NULL
451      && asprintf (&p_sys->xspf_ext, "%s   <vlc:item tid=\"%i\" />\n",
452                   old_xspf_ext, p_sys->i_item_count - 1) == -1)
453         p_sys->xspf_ext = NULL;
454     free (old_xspf_ext);
455
456     block_t *block = block_heap_Alloc (entry, entry, len);
457     if (unlikely(block == NULL))
458     {
459         free (entry);
460         goto fatal;
461     }
462     return block;
463
464 fatal:
465     p_access->info.b_eof = true;
466     return NULL;
467
468 skip:
469     free (entry);
470     return NULL;
471 }
472
473 /*****************************************************************************
474  * Control:
475  *****************************************************************************/
476 int DirControl( access_t *p_access, int i_query, va_list args )
477 {
478     switch( i_query )
479     {
480         /* */
481         case ACCESS_CAN_SEEK:
482         case ACCESS_CAN_FASTSEEK:
483             *va_arg( args, bool* ) = false;
484             break;
485
486         case ACCESS_CAN_PAUSE:
487         case ACCESS_CAN_CONTROL_PACE:
488             *va_arg( args, bool* ) = true;
489             break;
490
491         /* */
492         case ACCESS_GET_PTS_DELAY:
493             *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
494             break;
495
496         /* */
497         case ACCESS_SET_PAUSE_STATE:
498         case ACCESS_GET_TITLE_INFO:
499         case ACCESS_SET_TITLE:
500         case ACCESS_SET_SEEKPOINT:
501         case ACCESS_SET_PRIVATE_ID_STATE:
502         case ACCESS_GET_CONTENT_TYPE:
503         case ACCESS_GET_META:
504             return VLC_EGENERIC;
505
506         default:
507             msg_Warn( p_access, "unimplemented query in control" );
508             return VLC_EGENERIC;
509     }
510     return VLC_SUCCESS;
511 }