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);
224 /* Detect directories that recurse into themselves. */
225 static bool has_inode_loop (const directory_t *dir)
228 dev_t dev = dir->st.st_dev;
229 ino_t inode = dir->st.st_ino;
231 while ((dir = dir->parent) != NULL)
232 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
235 # define fstat( fd, st ) (0)
240 static block_t *Block (access_t *p_access)
242 access_sys_t *p_sys = p_access->p_sys;
243 directory_t *current = p_sys->current;
245 if (p_access->info.b_eof)
249 { /* Startup: send the XSPF header */
250 static const char header[] =
251 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
252 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
254 block_t *block = block_Alloc (sizeof (header) - 1);
257 memcpy (block->p_buffer, header, sizeof (header) - 1);
259 /* "Open" the base directory */
260 current = malloc (sizeof (*current) + strlen (p_access->psz_path));
263 block_Release (block);
266 current->parent = NULL;
267 current->handle = p_sys->handle;
268 strcpy (current->path, p_access->psz_path);
269 current->uri = make_URI (current->path);
270 if ((current->uri == NULL)
271 || fstat (dirfd (current->handle), ¤t->st))
275 block_Release (block);
279 p_sys->handle = NULL;
280 p_sys->current = current;
284 char *entry = utf8_readdir (current->handle);
286 { /* End of directory, go back to parent */
287 closedir (current->handle);
288 p_sys->current = current->parent;
292 if (p_sys->current == NULL)
293 { /* End of XSPF playlist */
295 int len = asprintf( &footer, " </trackList>\n" \
296 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
299 "</playlist>\n", p_sys->psz_xspf_extension );
303 block_t *block = block_Alloc ( len );
306 memcpy (block->p_buffer, footer, len);
308 p_access->info.b_eof = true;
313 /* This was the end of a "subnode" */
314 /* Write the ID to the extension */
315 char *old_xspf_extension = p_sys->psz_xspf_extension;
316 if (old_xspf_extension == NULL)
319 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
322 free( old_xspf_extension );
327 /* Skip current, parent and hidden directories */
333 /* Handle recursion */
334 if (p_sys->mode != MODE_COLLAPSE)
336 directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
343 sprintf (sub->path, "%s/%s", current->path, entry);
345 DIR *handle = utf8_opendir (sub->path);
348 sub->parent = current;
349 sub->handle = handle;
351 char *encoded = encode_URI_component (entry);
352 if ((encoded == NULL)
353 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
357 if ((p_sys->mode == MODE_NONE)
358 || fstat (dirfd (handle), &sub->st)
359 || has_inode_loop (sub)
360 || (sub->uri == NULL))
368 p_sys->current = sub;
370 /* Add node to xspf extension */
371 char *old_xspf_extension = p_sys->psz_xspf_extension;
372 if (old_xspf_extension == NULL)
378 char *title = convert_xml_special_chars (entry);
381 || asprintf (&p_sys->psz_xspf_extension, "%s"
382 " <vlc:node title=\"%s\">\n", old_xspf_extension,
389 free (old_xspf_extension);
396 /* Skip files with ignored extensions */
397 if (p_sys->ignored_exts != NULL)
399 const char *ext = strrchr (entry, '.');
402 size_t extlen = strlen (++ext);
403 for (const char *type = p_sys->ignored_exts, *end;
404 type[0]; type = end + 1)
406 end = strchr (type, ',');
408 end = type + strlen (type);
410 if (type + extlen == end
411 && !strncasecmp (ext, type, extlen))
423 char *encoded = encode_URI_component (entry);
427 int len = asprintf (&entry,
428 " <track><location>%s/%s</location>\n" \
429 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
430 " <vlc:id>%d</vlc:id>\n" \
433 current->uri, encoded, p_sys->i_item_count++);
438 /* Write the ID to the extension */
439 char *old_xspf_extension = p_sys->psz_xspf_extension;
440 if (old_xspf_extension == NULL)
443 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
444 old_xspf_extension, p_sys->i_item_count-1 );
447 free( old_xspf_extension );
449 /* TODO: new block allocator for malloc()ated data */
450 block_t *block = block_Alloc (len);
456 memcpy (block->p_buffer, entry, len);
461 p_access->info.b_eof = true;
465 /*****************************************************************************
467 *****************************************************************************/
468 static int Control( access_t *p_access, int i_query, va_list args )
473 case ACCESS_CAN_SEEK:
474 case ACCESS_CAN_FASTSEEK:
475 *va_arg( args, bool* ) = false;
478 case ACCESS_CAN_PAUSE:
479 case ACCESS_CAN_CONTROL_PACE:
480 *va_arg( args, bool* ) = true;
484 case ACCESS_GET_PTS_DELAY:
485 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
489 case ACCESS_SET_PAUSE_STATE:
490 case ACCESS_GET_TITLE_INFO:
491 case ACCESS_SET_TITLE:
492 case ACCESS_SET_SEEKPOINT:
493 case ACCESS_SET_PRIVATE_ID_STATE:
494 case ACCESS_GET_CONTENT_TYPE:
495 case ACCESS_GET_META:
499 msg_Warn( p_access, "unimplemented query in control" );