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 static inline int dirfd (DIR *dir)
60 #include <vlc_charset.h>
62 #include <vlc_strings.h>
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Open ( vlc_object_t * );
68 static void Close( vlc_object_t * );
70 #define RECURSIVE_TEXT N_("Subdirectory behavior")
71 #define RECURSIVE_LONGTEXT N_( \
72 "Select whether subdirectories must be expanded.\n" \
73 "none: subdirectories do not appear in the playlist.\n" \
74 "collapse: subdirectories appear but are expanded on first play.\n" \
75 "expand: all subdirectories are expanded.\n" )
77 static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
78 static const char *const psz_recursive_list_text[] = {
79 N_("none"), N_("collapse"), N_("expand") };
81 #define IGNORE_TEXT N_("Ignored extensions")
82 #define IGNORE_LONGTEXT N_( \
83 "Files with these extensions will not be added to playlist when " \
84 "opening a directory.\n" \
85 "This is useful if you add directories that contain playlist files " \
86 "for instance. Use a comma-separated list of extensions." )
89 set_category( CAT_INPUT )
90 set_shortname( N_("Directory" ) )
91 set_subcategory( SUBCAT_INPUT_ACCESS )
92 set_description( N_("Standard filesystem directory input") )
93 set_capability( "access", 55 )
94 add_shortcut( "directory" )
96 add_shortcut( "file" )
97 add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
98 RECURSIVE_LONGTEXT, false )
99 change_string_list( psz_recursive_list, psz_recursive_list_text, 0 )
100 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",
101 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false )
102 set_callbacks( Open, Close )
106 /*****************************************************************************
107 * Local prototypes, constants, structures
108 *****************************************************************************/
117 typedef struct directory_t directory_t;
131 directory_t *current;
136 char *psz_xspf_extension;
139 static block_t *Block( access_t * );
140 static int Control( access_t *, int, va_list );
142 /*****************************************************************************
143 * Open: open the directory
144 *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
147 access_t *p_access = (access_t*)p_this;
150 if( !p_access->psz_path )
154 if (strcmp (p_access->psz_path, "-"))
155 handle = utf8_opendir (p_access->psz_path);
158 #if 0 /* This won't work yet, it generates paths like "-/music.ogg".
159 * We'd need to use openat() here and in the file access... */
161 handle = fdopendir (fd);
172 p_sys = malloc (sizeof (*p_sys));
179 p_access->p_sys = p_sys;
180 p_sys->current = NULL;
181 p_sys->handle = handle;
182 p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
183 p_sys->i_item_count = 0;
184 p_sys->psz_xspf_extension = strdup( "" );
187 char *psz = var_CreateGetString( p_access, "recursive" );
188 if( *psz == '\0' || !strcasecmp( psz, "none" ) )
189 p_sys->mode = MODE_NONE;
190 else if( !strcasecmp( psz, "collapse" ) )
191 p_sys->mode = MODE_COLLAPSE;
193 p_sys->mode = MODE_EXPAND;
196 p_access->pf_read = NULL;
197 p_access->pf_block = Block;
198 p_access->pf_seek = NULL;
199 p_access->pf_control= Control;
200 free (p_access->psz_demux);
201 p_access->psz_demux = strdup ("xspf-open");
206 /*****************************************************************************
207 * Close: close the target
208 *****************************************************************************/
209 static void Close( vlc_object_t * p_this )
211 access_t *p_access = (access_t*)p_this;
212 access_sys_t *p_sys = p_access->p_sys;
214 while (p_sys->current)
216 directory_t *current = p_sys->current;
218 p_sys->current = current->parent;
219 closedir (current->handle);
223 if (p_sys->handle != NULL)
224 closedir (p_sys->handle); /* corner case,:Block() not called ever */
225 free (p_sys->psz_xspf_extension);
226 free (p_sys->ignored_exts);
230 /* Detect directories that recurse into themselves. */
231 static bool has_inode_loop (const directory_t *dir)
234 dev_t dev = dir->st.st_dev;
235 ino_t inode = dir->st.st_ino;
237 while ((dir = dir->parent) != NULL)
238 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
242 # define fstat( fd, st ) (0)
248 static block_t *Block (access_t *p_access)
250 access_sys_t *p_sys = p_access->p_sys;
251 directory_t *current = p_sys->current;
253 if (p_access->info.b_eof)
257 { /* Startup: send the XSPF header */
258 static const char header[] =
259 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
260 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
262 block_t *block = block_Alloc (sizeof (header) - 1);
265 memcpy (block->p_buffer, header, sizeof (header) - 1);
267 /* "Open" the base directory */
268 current = malloc (sizeof (*current) + strlen (p_access->psz_path));
271 block_Release (block);
274 current->parent = NULL;
275 current->handle = p_sys->handle;
276 strcpy (current->path, p_access->psz_path);
277 current->uri = make_URI (current->path);
278 if ((current->uri == NULL)
279 || fstat (dirfd (current->handle), ¤t->st))
283 block_Release (block);
287 p_sys->handle = NULL;
288 p_sys->current = current;
292 char *entry = utf8_readdir (current->handle);
294 { /* End of directory, go back to parent */
295 closedir (current->handle);
296 p_sys->current = current->parent;
300 if (p_sys->current == NULL)
301 { /* End of XSPF playlist */
303 int len = asprintf( &footer, " </trackList>\n" \
304 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
307 "</playlist>\n", p_sys->psz_xspf_extension );
311 block_t *block = block_Alloc ( len );
314 memcpy (block->p_buffer, footer, len);
316 p_access->info.b_eof = true;
321 /* This was the end of a "subnode" */
322 /* Write the ID to the extension */
323 char *old_xspf_extension = p_sys->psz_xspf_extension;
324 if (old_xspf_extension == NULL)
327 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
330 free( old_xspf_extension );
335 /* Skip current, parent and hidden directories */
341 /* Handle recursion */
342 if (p_sys->mode != MODE_COLLAPSE)
344 directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
351 sprintf (sub->path, "%s/%s", current->path, entry);
353 DIR *handle = utf8_opendir (sub->path);
356 sub->parent = current;
357 sub->handle = handle;
359 char *encoded = encode_URI_component (entry);
360 if ((encoded == NULL)
361 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
365 if ((p_sys->mode == MODE_NONE)
366 || fstat (dirfd (handle), &sub->st)
367 || has_inode_loop (sub)
368 || (sub->uri == NULL))
376 p_sys->current = sub;
378 /* Add node to xspf extension */
379 char *old_xspf_extension = p_sys->psz_xspf_extension;
380 if (old_xspf_extension == NULL)
386 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))
431 char *encoded = encode_URI_component (entry);
435 int len = asprintf (&entry,
436 " <track><location>%s/%s</location>\n" \
437 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
438 " <vlc:id>%d</vlc:id>\n" \
441 current->uri, encoded, p_sys->i_item_count++);
446 /* Write the ID to the extension */
447 char *old_xspf_extension = p_sys->psz_xspf_extension;
448 if (old_xspf_extension == NULL)
451 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
452 old_xspf_extension, p_sys->i_item_count-1 );
455 free( old_xspf_extension );
457 /* TODO: new block allocator for malloc()ated data */
458 block_t *block = block_Alloc (len);
464 memcpy (block->p_buffer, entry, len);
469 p_access->info.b_eof = true;
473 /*****************************************************************************
475 *****************************************************************************/
476 static int Control( access_t *p_access, int i_query, va_list args )
481 case ACCESS_CAN_SEEK:
482 case ACCESS_CAN_FASTSEEK:
483 *va_arg( args, bool* ) = false;
486 case ACCESS_CAN_PAUSE:
487 case ACCESS_CAN_CONTROL_PACE:
488 *va_arg( args, bool* ) = true;
492 case ACCESS_GET_PTS_DELAY:
493 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
497 case ACCESS_SET_PAUSE_STATE:
498 case ACCESS_GET_TITLE_INFO:
499 case ACCESS_SET_TITLE:
500 case ACCESS_SET_SEEKPOINT:
501 case ACCESS_SET_PRIVATE_ID_STATE:
502 case ACCESS_GET_CONTENT_TYPE:
503 case ACCESS_GET_META:
507 msg_Warn( p_access, "unimplemented query in control" );