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>
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 #define RECURSIVE_TEXT N_("Subdirectory behavior")
64 #define RECURSIVE_LONGTEXT N_( \
65 "Select whether subdirectories must be expanded.\n" \
66 "none: subdirectories do not appear in the playlist.\n" \
67 "collapse: subdirectories appear but are expanded on first play.\n" \
68 "expand: all subdirectories are expanded.\n" )
70 static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
71 static const char *const psz_recursive_list_text[] = {
72 N_("none"), N_("collapse"), N_("expand") };
74 #define IGNORE_TEXT N_("Ignored extensions")
75 #define IGNORE_LONGTEXT N_( \
76 "Files with these extensions will not be added to playlist when " \
77 "opening a directory.\n" \
78 "This is useful if you add directories that contain playlist files " \
79 "for instance. Use a comma-separated list of extensions." )
82 set_category( CAT_INPUT )
83 set_shortname( N_("Directory" ) )
84 set_subcategory( SUBCAT_INPUT_ACCESS )
85 set_description( N_("Standard filesystem directory input") )
86 set_capability( "access", 55 )
87 add_shortcut( "directory" )
89 add_shortcut( "file" )
90 add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
91 RECURSIVE_LONGTEXT, false );
92 change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
93 add_string( "ignore-filetypes", "m3u,db,nfo,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",
94 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false );
95 set_callbacks( Open, Close )
99 /*****************************************************************************
100 * Local prototypes, constants, structures
101 *****************************************************************************/
110 typedef struct directory_t directory_t;
124 directory_t *current;
129 char *psz_xspf_extension;
132 static block_t *Block( access_t * );
133 static int Control( access_t *, int, va_list );
135 /*****************************************************************************
136 * Open: open the directory
137 *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
140 access_t *p_access = (access_t*)p_this;
143 if( !p_access->psz_path )
147 if (strcmp (p_access->psz_path, "-"))
148 handle = utf8_opendir (p_access->psz_path);
151 #if 0 /* This won't work yet, it generates paths like "-/music.ogg".
152 * We'd need to use openat() here and in the file access... */
154 handle = fdopendir (fd);
165 p_sys = malloc (sizeof (*p_sys));
172 p_access->p_sys = p_sys;
173 p_sys->current = NULL;
174 p_sys->handle = handle;
175 p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
176 p_sys->i_item_count = 0;
177 p_sys->psz_xspf_extension = strdup( "" );
180 char *psz = var_CreateGetString( p_access, "recursive" );
181 if( *psz == '\0' || !strcasecmp( psz, "none" ) )
182 p_sys->mode = MODE_NONE;
183 else if( !strcasecmp( psz, "collapse" ) )
184 p_sys->mode = MODE_COLLAPSE;
186 p_sys->mode = MODE_EXPAND;
189 p_access->pf_read = NULL;
190 p_access->pf_block = Block;
191 p_access->pf_seek = NULL;
192 p_access->pf_control= Control;
193 free (p_access->psz_demux);
194 p_access->psz_demux = strdup ("xspf-open");
199 /*****************************************************************************
200 * Close: close the target
201 *****************************************************************************/
202 static void Close( vlc_object_t * p_this )
204 access_t *p_access = (access_t*)p_this;
205 access_sys_t *p_sys = p_access->p_sys;
207 while (p_sys->current)
209 directory_t *current = p_sys->current;
211 p_sys->current = current->parent;
212 closedir (current->handle);
216 if (p_sys->handle != NULL)
217 closedir (p_sys->handle); /* corner case,:Block() not called ever */
218 free (p_sys->psz_xspf_extension);
219 free (p_sys->ignored_exts);
224 * URI-encodes a file path. The only reserved characters is slash.
226 static char *encode_path (const char *path)
228 static const char sep[]= "%2F";
229 char *enc = encode_URI_component (path), *ptr = enc;
234 /* Replace '%2F' with '/'. TODO: extend encode_URI*() */
235 /* (On Windows, both ':' and '\\' will be encoded) */
236 while ((ptr = strstr (ptr, sep)) != NULL)
239 memmove (ptr, ptr + 2, strlen (ptr) - 1);
244 /* Detect directories that recurse into themselves. */
245 static bool has_inode_loop (const directory_t *dir)
248 dev_t dev = dir->st.st_dev;
249 ino_t inode = dir->st.st_ino;
251 while ((dir = dir->parent) != NULL)
252 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
255 # define fstat( fd, st ) (0)
260 static block_t *Block (access_t *p_access)
262 access_sys_t *p_sys = p_access->p_sys;
263 directory_t *current = p_sys->current;
265 if (p_access->info.b_eof)
269 { /* Startup: send the XSPF header */
270 static const char header[] =
271 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
272 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
274 block_t *block = block_Alloc (sizeof (header) - 1);
277 memcpy (block->p_buffer, header, sizeof (header) - 1);
279 /* "Open" the base directory */
280 current = malloc (sizeof (*current) + strlen (p_access->psz_path));
283 block_Release (block);
286 current->parent = NULL;
287 current->handle = p_sys->handle;
288 strcpy (current->path, p_access->psz_path);
289 current->uri = encode_path (current->path);
290 if ((current->uri == NULL)
291 || fstat (dirfd (current->handle), ¤t->st))
295 block_Release (block);
299 p_sys->handle = NULL;
300 p_sys->current = current;
304 char *entry = utf8_readdir (current->handle);
306 { /* End of directory, go back to parent */
307 closedir (current->handle);
308 p_sys->current = current->parent;
311 if (p_sys->current == NULL)
312 { /* End of XSPF playlist */
314 int len = asprintf( &footer, " </trackList>\n" \
315 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
318 "</playlist>\n", p_sys->psz_xspf_extension );
322 block_t *block = block_Alloc ( len );
325 memcpy (block->p_buffer, footer, len);
327 p_access->info.b_eof = true;
332 /* This was the end of a "subnode" */
333 /* Write the ID to the extension */
334 char *old_xspf_extension = p_sys->psz_xspf_extension;
335 if (old_xspf_extension == NULL)
338 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
341 free( old_xspf_extension );
346 /* Skip current, parent and hidden directories */
349 /* Handle recursion */
350 if (p_sys->mode != MODE_COLLAPSE)
352 directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
356 sprintf (sub->path, "%s/%s", current->path, entry);
358 DIR *handle = utf8_opendir (sub->path);
361 sub->parent = current;
362 sub->handle = handle;
364 char *encoded = encode_URI_component (entry);
365 if ((encoded == NULL)
366 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
370 if ((p_sys->mode == MODE_NONE)
371 || fstat (dirfd (handle), &sub->st)
372 || has_inode_loop (sub)
373 || (sub->uri == NULL))
379 p_sys->current = sub;
381 /* Add node to xspf extension */
382 char *old_xspf_extension = p_sys->psz_xspf_extension;
383 if (old_xspf_extension == NULL)
386 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:node title=\"%s\">\n", old_xspf_extension, entry );
389 free( old_xspf_extension );
397 /* Skip files with ignored extensions */
398 if (p_sys->ignored_exts != NULL)
400 const char *ext = strrchr (entry, '.');
403 size_t extlen = strlen (++ext);
404 for (const char *type = p_sys->ignored_exts, *end;
405 type[0]; type = end + 1)
407 end = strchr (type, ',');
409 end = type + strlen (type);
411 if (type + extlen == end
412 && !strncasecmp (ext, type, extlen))
421 char *encoded = encode_URI_component (entry);
425 int len = asprintf (&entry,
426 " <track><location>file://%s/%s</location>\n" \
427 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
428 " <vlc:id>%d</vlc:id>\n" \
431 current->uri, encoded, p_sys->i_item_count++);
436 /* Write the ID to the extension */
437 char *old_xspf_extension = p_sys->psz_xspf_extension;
438 if (old_xspf_extension == NULL)
441 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
442 old_xspf_extension, p_sys->i_item_count-1 );
445 free( old_xspf_extension );
447 /* TODO: new block allocator for malloc()ated data */
448 block_t *block = block_Alloc (len);
454 memcpy (block->p_buffer, entry, len);
459 p_access->info.b_eof = true;
463 /*****************************************************************************
465 *****************************************************************************/
466 static int Control( access_t *p_access, int i_query, va_list args )
474 case ACCESS_CAN_SEEK:
475 case ACCESS_CAN_FASTSEEK:
476 pb_bool = (bool*)va_arg( args, bool* );
480 case ACCESS_CAN_PAUSE:
481 case ACCESS_CAN_CONTROL_PACE:
482 pb_bool = (bool*)va_arg( args, bool* );
487 case ACCESS_GET_PTS_DELAY:
488 pi_64 = (int64_t*)va_arg( args, int64_t * );
489 *pi_64 = DEFAULT_PTS_DELAY * 1000;
493 case ACCESS_SET_PAUSE_STATE:
494 case ACCESS_GET_TITLE_INFO:
495 case ACCESS_SET_TITLE:
496 case ACCESS_SET_SEEKPOINT:
497 case ACCESS_SET_PRIVATE_ID_STATE:
498 case ACCESS_GET_CONTENT_TYPE:
499 case ACCESS_GET_META:
503 msg_Warn( p_access, "unimplemented query in control" );