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