1 /*****************************************************************************
2 * directory.c: expands a directory (directory: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2002-2008 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
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 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_access.h>
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
46 #elif defined( WIN32 ) && !defined( UNDER_CE )
54 #include <vlc_charset.h>
56 #include <vlc_strings.h>
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
64 #define RECURSIVE_TEXT N_("Subdirectory behavior")
65 #define RECURSIVE_LONGTEXT N_( \
66 "Select whether subdirectories must be expanded.\n" \
67 "none: subdirectories do not appear in the playlist.\n" \
68 "collapse: subdirectories appear but are expanded on first play.\n" \
69 "expand: all subdirectories are expanded.\n" )
71 static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
72 static const char *const psz_recursive_list_text[] = {
73 N_("none"), N_("collapse"), N_("expand") };
75 #define IGNORE_TEXT N_("Ignored extensions")
76 #define IGNORE_LONGTEXT N_( \
77 "Files with these extensions will not be added to playlist when " \
78 "opening a directory.\n" \
79 "This is useful if you add directories that contain playlist files " \
80 "for instance. Use a comma-separated list of extensions." )
83 set_category( CAT_INPUT )
84 set_shortname( N_("Directory" ) )
85 set_subcategory( SUBCAT_INPUT_ACCESS )
86 set_description( N_("Standard filesystem directory input") )
87 set_capability( "access", 55 )
88 add_shortcut( "directory" )
90 add_shortcut( "file" )
91 add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
92 RECURSIVE_LONGTEXT, false )
93 change_string_list( psz_recursive_list, psz_recursive_list_text, 0 )
94 add_string( "ignore-filetypes", "m3u,db,nfo,ini,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv,txt,sub,idx,srt,cue,ssa",
95 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false )
96 set_callbacks( Open, Close )
100 /*****************************************************************************
101 * Local prototypes, constants, structures
102 *****************************************************************************/
111 typedef struct directory_t directory_t;
125 directory_t *current;
130 char *psz_xspf_extension;
133 static block_t *Block( access_t * );
134 static int Control( access_t *, int, va_list );
136 /*****************************************************************************
137 * Open: open the directory
138 *****************************************************************************/
139 static int Open( vlc_object_t *p_this )
141 access_t *p_access = (access_t*)p_this;
144 if( !p_access->psz_path )
148 if (strcmp (p_access->psz_path, "-"))
149 handle = utf8_opendir (p_access->psz_path);
152 #if 0 /* This won't work yet, it generates paths like "-/music.ogg".
153 * We'd need to use openat() here and in the file access... */
155 handle = fdopendir (fd);
166 p_sys = malloc (sizeof (*p_sys));
173 p_access->p_sys = p_sys;
174 p_sys->current = NULL;
175 p_sys->handle = handle;
176 p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
177 p_sys->i_item_count = 0;
178 p_sys->psz_xspf_extension = strdup( "" );
181 char *psz = var_CreateGetString( p_access, "recursive" );
182 if( *psz == '\0' || !strcasecmp( psz, "none" ) )
183 p_sys->mode = MODE_NONE;
184 else if( !strcasecmp( psz, "collapse" ) )
185 p_sys->mode = MODE_COLLAPSE;
187 p_sys->mode = MODE_EXPAND;
190 p_access->pf_read = NULL;
191 p_access->pf_block = Block;
192 p_access->pf_seek = NULL;
193 p_access->pf_control= Control;
194 free (p_access->psz_demux);
195 p_access->psz_demux = strdup ("xspf-open");
200 /*****************************************************************************
201 * Close: close the target
202 *****************************************************************************/
203 static void Close( vlc_object_t * p_this )
205 access_t *p_access = (access_t*)p_this;
206 access_sys_t *p_sys = p_access->p_sys;
208 while (p_sys->current)
210 directory_t *current = p_sys->current;
212 p_sys->current = current->parent;
213 closedir (current->handle);
217 if (p_sys->handle != NULL)
218 closedir (p_sys->handle); /* corner case,:Block() not called ever */
219 free (p_sys->psz_xspf_extension);
220 free (p_sys->ignored_exts);
225 * URI-encodes a file path. The only reserved characters is slash.
227 static char *encode_path (const char *path)
229 static const char sep[]= "%2F";
230 char *enc = encode_URI_component (path), *ptr = enc;
235 /* Replace '%2F' with '/'. TODO: extend encode_URI*() */
236 /* (On Windows, both ':' and '\\' will be encoded) */
237 while ((ptr = strstr (ptr, sep)) != NULL)
240 memmove (ptr, ptr + 2, strlen (ptr) - 1);
245 /* Detect directories that recurse into themselves. */
246 static bool has_inode_loop (const directory_t *dir)
249 dev_t dev = dir->st.st_dev;
250 ino_t inode = dir->st.st_ino;
252 while ((dir = dir->parent) != NULL)
253 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
256 # define fstat( fd, st ) (0)
261 static block_t *Block (access_t *p_access)
263 access_sys_t *p_sys = p_access->p_sys;
264 directory_t *current = p_sys->current;
266 if (p_access->info.b_eof)
270 { /* Startup: send the XSPF header */
271 static const char header[] =
272 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
273 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
275 block_t *block = block_Alloc (sizeof (header) - 1);
278 memcpy (block->p_buffer, header, sizeof (header) - 1);
280 /* "Open" the base directory */
281 current = malloc (sizeof (*current) + strlen (p_access->psz_path));
284 block_Release (block);
287 current->parent = NULL;
288 current->handle = p_sys->handle;
289 strcpy (current->path, p_access->psz_path);
290 current->uri = encode_path (current->path);
291 if ((current->uri == NULL)
292 || fstat (dirfd (current->handle), ¤t->st))
296 block_Release (block);
300 p_sys->handle = NULL;
301 p_sys->current = current;
305 char *entry = utf8_readdir (current->handle);
307 { /* End of directory, go back to parent */
308 closedir (current->handle);
309 p_sys->current = current->parent;
312 if (p_sys->current == NULL)
313 { /* End of XSPF playlist */
315 int len = asprintf( &footer, " </trackList>\n" \
316 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
319 "</playlist>\n", p_sys->psz_xspf_extension );
323 block_t *block = block_Alloc ( len );
326 memcpy (block->p_buffer, footer, len);
328 p_access->info.b_eof = true;
333 /* This was the end of a "subnode" */
334 /* Write the ID to the extension */
335 char *old_xspf_extension = p_sys->psz_xspf_extension;
336 if (old_xspf_extension == NULL)
339 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
342 free( old_xspf_extension );
347 /* Skip current, parent and hidden directories */
350 /* Handle recursion */
351 if (p_sys->mode != MODE_COLLAPSE)
353 directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
357 sprintf (sub->path, "%s/%s", current->path, entry);
359 DIR *handle = utf8_opendir (sub->path);
362 sub->parent = current;
363 sub->handle = handle;
365 char *encoded = encode_URI_component (entry);
366 if ((encoded == NULL)
367 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
371 if ((p_sys->mode == MODE_NONE)
372 || fstat (dirfd (handle), &sub->st)
373 || has_inode_loop (sub)
374 || (sub->uri == NULL))
380 p_sys->current = sub;
382 /* Add node to xspf extension */
383 char *old_xspf_extension = p_sys->psz_xspf_extension;
384 if (old_xspf_extension == NULL)
387 char *title = convert_xml_special_chars (entry);
389 || asprintf (&p_sys->psz_xspf_extension, "%s"
390 " <vlc:node title=\"%s\">\n", old_xspf_extension,
397 free (old_xspf_extension);
404 /* Skip files with ignored extensions */
405 if (p_sys->ignored_exts != NULL)
407 const char *ext = strrchr (entry, '.');
410 size_t extlen = strlen (++ext);
411 for (const char *type = p_sys->ignored_exts, *end;
412 type[0]; type = end + 1)
414 end = strchr (type, ',');
416 end = type + strlen (type);
418 if (type + extlen == end
419 && !strncasecmp (ext, type, extlen))
428 char *encoded = encode_URI_component (entry);
432 int len = asprintf (&entry,
433 " <track><location>file://%s/%s</location>\n" \
434 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
435 " <vlc:id>%d</vlc:id>\n" \
438 current->uri, encoded, p_sys->i_item_count++);
443 /* Write the ID to the extension */
444 char *old_xspf_extension = p_sys->psz_xspf_extension;
445 if (old_xspf_extension == NULL)
448 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
449 old_xspf_extension, p_sys->i_item_count-1 );
452 free( old_xspf_extension );
454 /* TODO: new block allocator for malloc()ated data */
455 block_t *block = block_Alloc (len);
461 memcpy (block->p_buffer, entry, len);
466 p_access->info.b_eof = true;
470 /*****************************************************************************
472 *****************************************************************************/
473 static int Control( access_t *p_access, int i_query, va_list args )
481 case ACCESS_CAN_SEEK:
482 case ACCESS_CAN_FASTSEEK:
483 pb_bool = (bool*)va_arg( args, bool* );
487 case ACCESS_CAN_PAUSE:
488 case ACCESS_CAN_CONTROL_PACE:
489 pb_bool = (bool*)va_arg( args, bool* );
494 case ACCESS_GET_PTS_DELAY:
495 pi_64 = (int64_t*)va_arg( args, int64_t * );
496 *pi_64 = DEFAULT_PTS_DELAY * 1000;
500 case ACCESS_SET_PAUSE_STATE:
501 case ACCESS_GET_TITLE_INFO:
502 case ACCESS_SET_TITLE:
503 case ACCESS_SET_SEEKPOINT:
504 case ACCESS_SET_PRIVATE_ID_STATE:
505 case ACCESS_GET_CONTENT_TYPE:
506 case ACCESS_GET_META:
510 msg_Warn( p_access, "unimplemented query in control" );