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