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