add_savefile: remove callback parameter
[vlc/asuraparaju-public.git] / modules / access / directory.c
blobb778cf0e252ff37cac3c064a0a556c3092462a77
1 /*****************************************************************************
2 * directory.c: expands a directory (directory: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2002-2008 the VideoLAN team
5 * $Id$
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * RĂ©mi Denis-Courmont
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include "fs.h"
35 #include <vlc_access.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
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
49 #ifdef __sun__
50 static inline int dirfd (DIR *dir)
52 return dir->dd_fd;
54 #endif
56 #include <vlc_fs.h>
57 #include <vlc_url.h>
58 #include <vlc_strings.h>
60 enum
62 MODE_NONE,
63 MODE_COLLAPSE,
64 MODE_EXPAND,
67 typedef struct directory_t directory_t;
68 struct directory_t
70 directory_t *parent;
71 DIR *handle;
72 char *uri;
73 #ifndef WIN32
74 struct stat st;
75 #endif
76 #ifndef HAVE_OPENAT
77 char *path;
78 #endif
81 struct access_sys_t
83 directory_t *current;
84 DIR *handle;
85 char *uri;
86 char *ignored_exts;
87 int mode;
88 int i_item_count;
89 char *psz_xspf_extension;
92 /*****************************************************************************
93 * Open: open the directory
94 *****************************************************************************/
95 int DirOpen( vlc_object_t *p_this )
97 access_t *p_access = (access_t*)p_this;
99 if( !p_access->psz_filepath )
100 return VLC_EGENERIC;
102 DIR *handle = vlc_opendir (p_access->psz_filepath);
103 if (handle == NULL)
104 return VLC_EGENERIC;
106 return DirInit (p_access, handle);
109 int DirInit (access_t *p_access, DIR *handle)
111 access_sys_t *p_sys = malloc (sizeof (*p_sys));
112 if (unlikely(p_sys == NULL))
113 goto error;
115 char *uri;
116 if (!strcmp (p_access->psz_access, "fd"))
118 if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
119 uri = NULL;
121 else
122 uri = make_URI (p_access->psz_filepath, "file");
123 if (unlikely(uri == NULL))
124 goto error;
126 p_access->p_sys = p_sys;
127 p_sys->current = NULL;
128 p_sys->handle = handle;
129 p_sys->uri = uri;
130 p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
131 p_sys->i_item_count = 0;
132 p_sys->psz_xspf_extension = strdup( "" );
134 /* Handle mode */
135 char *psz = var_InheritString (p_access, "recursive");
136 if (psz == NULL || !strcasecmp (psz, "none"))
137 p_sys->mode = MODE_NONE;
138 else if( !strcasecmp( psz, "collapse" ) )
139 p_sys->mode = MODE_COLLAPSE;
140 else
141 p_sys->mode = MODE_EXPAND;
142 free( psz );
144 access_InitFields(p_access);
145 p_access->pf_read = NULL;
146 p_access->pf_block = DirBlock;
147 p_access->pf_seek = NULL;
148 p_access->pf_control= DirControl;
149 free (p_access->psz_demux);
150 p_access->psz_demux = strdup ("xspf-open");
152 return VLC_SUCCESS;
154 error:
155 closedir (handle);
156 free (p_sys);
157 return VLC_EGENERIC;
160 /*****************************************************************************
161 * Close: close the target
162 *****************************************************************************/
163 void DirClose( vlc_object_t * p_this )
165 access_t *p_access = (access_t*)p_this;
166 access_sys_t *p_sys = p_access->p_sys;
168 while (p_sys->current)
170 directory_t *current = p_sys->current;
172 p_sys->current = current->parent;
173 closedir (current->handle);
174 free (current->uri);
175 #ifndef HAVE_OPENAT
176 free (current->path);
177 #endif
178 free (current);
181 /* corner case: Block() not called ever */
182 if (p_sys->handle != NULL)
183 closedir (p_sys->handle);
184 free (p_sys->uri);
186 free (p_sys->psz_xspf_extension);
187 free (p_sys->ignored_exts);
188 free (p_sys);
191 /* Detect directories that recurse into themselves. */
192 static bool has_inode_loop (const directory_t *dir)
194 #ifndef WIN32
195 dev_t dev = dir->st.st_dev;
196 ino_t inode = dir->st.st_ino;
198 while ((dir = dir->parent) != NULL)
199 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
200 return true;
201 #else
202 # undef fstat
203 # define fstat( fd, st ) (0)
204 VLC_UNUSED( dir );
205 #endif
206 return false;
209 block_t *DirBlock (access_t *p_access)
211 access_sys_t *p_sys = p_access->p_sys;
212 directory_t *current = p_sys->current;
214 if (p_access->info.b_eof)
215 return NULL;
217 if (current == NULL)
218 { /* Startup: send the XSPF header */
219 static const char header[] =
220 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
221 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
222 " <trackList>\n";
223 block_t *block = block_Alloc (sizeof (header) - 1);
224 if (!block)
225 goto fatal;
226 memcpy (block->p_buffer, header, sizeof (header) - 1);
228 /* "Open" the base directory */
229 current = malloc (sizeof (*current));
230 if (current == NULL)
232 block_Release (block);
233 goto fatal;
235 current->parent = NULL;
236 current->handle = p_sys->handle;
237 #ifndef HAVE_OPENAT
238 current->path = strdup (p_access->psz_filepath);
239 #endif
240 current->uri = p_sys->uri;
241 if (fstat (dirfd (current->handle), &current->st))
243 free (current);
244 block_Release (block);
245 goto fatal;
248 p_sys->handle = NULL;
249 p_sys->uri = NULL;
250 p_sys->current = current;
251 return block;
254 char *entry = vlc_readdir (current->handle);
255 if (entry == NULL)
256 { /* End of directory, go back to parent */
257 closedir (current->handle);
258 p_sys->current = current->parent;
259 free (current->uri);
260 #ifndef HAVE_OPENAT
261 free (current->path);
262 #endif
263 free (current);
265 if (p_sys->current == NULL)
266 { /* End of XSPF playlist */
267 char *footer;
268 int len = asprintf( &footer, " </trackList>\n" \
269 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
270 "%s" \
271 " </extension>\n" \
272 "</playlist>\n", p_sys->psz_xspf_extension );
273 if (unlikely(len == -1))
274 goto fatal;
276 block_t *block = block_heap_Alloc (footer, footer, len);
277 if (unlikely(block == NULL))
278 free (footer);
279 p_access->info.b_eof = true;
280 return block;
282 else
284 /* This was the end of a "subnode" */
285 /* Write the ID to the extension */
286 char *old_xspf_extension = p_sys->psz_xspf_extension;
287 if (old_xspf_extension == NULL)
288 goto fatal;
290 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
291 if (len2 == -1)
292 goto fatal;
293 free( old_xspf_extension );
295 return NULL;
298 /* Skip current, parent and hidden directories */
299 if (entry[0] == '.')
301 free (entry);
302 return NULL;
304 /* Handle recursion */
305 if (p_sys->mode != MODE_COLLAPSE)
307 directory_t *sub = malloc (sizeof (*sub));
308 if (sub == NULL)
310 free (entry);
311 return NULL;
314 DIR *handle;
315 #ifdef HAVE_OPENAT
316 int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
317 if (fd != -1)
319 handle = fdopendir (fd);
320 if (handle == NULL)
321 close (fd);
323 else
324 handle = NULL;
325 #else
326 if (asprintf (&sub->path, "%s/%s", current->path, entry) != -1)
327 handle = vlc_opendir (sub->path);
328 else
329 handle = NULL;
330 #endif
331 if (handle != NULL)
333 sub->parent = current;
334 sub->handle = handle;
336 char *encoded = encode_URI_component (entry);
337 if ((encoded == NULL)
338 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
339 sub->uri = NULL;
340 free (encoded);
342 if ((p_sys->mode == MODE_NONE)
343 || fstat (dirfd (handle), &sub->st)
344 || has_inode_loop (sub)
345 || (sub->uri == NULL))
347 free (entry);
348 closedir (handle);
349 free (sub->uri);
350 free (sub);
351 return NULL;
353 p_sys->current = sub;
355 /* Add node to xspf extension */
356 char *old_xspf_extension = p_sys->psz_xspf_extension;
357 if (old_xspf_extension == NULL)
359 free (entry);
360 goto fatal;
363 char *title = convert_xml_special_chars (entry);
364 free (entry);
365 if (title == NULL
366 || asprintf (&p_sys->psz_xspf_extension, "%s"
367 " <vlc:node title=\"%s\">\n", old_xspf_extension,
368 title) == -1)
370 free (title);
371 goto fatal;
373 free (title);
374 free (old_xspf_extension);
375 return NULL;
377 else
378 free (sub);
381 /* Skip files with ignored extensions */
382 if (p_sys->ignored_exts != NULL)
384 const char *ext = strrchr (entry, '.');
385 if (ext != NULL)
387 size_t extlen = strlen (++ext);
388 for (const char *type = p_sys->ignored_exts, *end;
389 type[0]; type = end + 1)
391 end = strchr (type, ',');
392 if (end == NULL)
393 end = type + strlen (type);
395 if (type + extlen == end
396 && !strncasecmp (ext, type, extlen))
398 free (entry);
399 return NULL;
402 if (*end == '\0')
403 break;
408 char *encoded = encode_URI_component (entry);
409 free (entry);
410 if (encoded == NULL)
411 goto fatal;
412 int len = asprintf (&entry,
413 " <track><location>%s/%s</location>\n" \
414 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
415 " <vlc:id>%d</vlc:id>\n" \
416 " </extension>\n" \
417 " </track>\n",
418 current->uri, encoded, p_sys->i_item_count++);
419 free (encoded);
420 if (len == -1)
421 goto fatal;
423 /* Write the ID to the extension */
424 char *old_xspf_extension = p_sys->psz_xspf_extension;
425 if (old_xspf_extension == NULL)
426 goto fatal;
428 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
429 old_xspf_extension, p_sys->i_item_count-1 );
430 if (len2 == -1)
431 goto fatal;
432 free( old_xspf_extension );
434 block_t *block = block_heap_Alloc (entry, entry, len);
435 if (unlikely(block == NULL))
437 free (entry);
438 goto fatal;
440 return block;
442 fatal:
443 p_access->info.b_eof = true;
444 return NULL;
447 /*****************************************************************************
448 * Control:
449 *****************************************************************************/
450 int DirControl( access_t *p_access, int i_query, va_list args )
452 switch( i_query )
454 /* */
455 case ACCESS_CAN_SEEK:
456 case ACCESS_CAN_FASTSEEK:
457 *va_arg( args, bool* ) = false;
458 break;
460 case ACCESS_CAN_PAUSE:
461 case ACCESS_CAN_CONTROL_PACE:
462 *va_arg( args, bool* ) = true;
463 break;
465 /* */
466 case ACCESS_GET_PTS_DELAY:
467 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
468 break;
470 /* */
471 case ACCESS_SET_PAUSE_STATE:
472 case ACCESS_GET_TITLE_INFO:
473 case ACCESS_SET_TITLE:
474 case ACCESS_SET_SEEKPOINT:
475 case ACCESS_SET_PRIVATE_ID_STATE:
476 case ACCESS_GET_CONTENT_TYPE:
477 case ACCESS_GET_META:
478 return VLC_EGENERIC;
480 default:
481 msg_Warn( p_access, "unimplemented query in control" );
482 return VLC_EGENERIC;
484 return VLC_SUCCESS;