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