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