/*****************************************************************************
* directory.c: expands a directory (directory: access plug-in)
*****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
+ * Copyright (C) 2002-2008 the VideoLAN team
* $Id$
*
- * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
+ * Authors: Derk-Jan Hartman <hartman at videolan dot org>
+ * RĂ©mi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <vlc/vlc.h>
-#include <vlc/input.h>
-#include <stdlib.h>
-#include <string.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_access.h>
+
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
-#ifdef HAVE_ERRNO_H
-# include <errno.h>
-#endif
-#ifdef HAVE_FCNTL_H
-# include <fcntl.h>
-#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
# include <io.h>
#endif
-#if (!defined( WIN32 ) || defined(__MINGW32__))
-/* Mingw has its own version of dirent */
+#ifdef HAVE_DIRENT_H
# include <dirent.h>
#endif
-/*****************************************************************************
- * Constants and structures
- *****************************************************************************/
-#define MAX_DIR_SIZE 100000
-
-#define MODE_EXPAND 0
-#define MODE_COLLAPSE 1
-#define MODE_NONE 2
-
-typedef struct input_directory_s
-{
- char p_dir_buffer[MAX_DIR_SIZE];
- int i_buf_pos;
- int i_buf_length;
- int i_pos;
-} input_directory_t;
-
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int Open ( vlc_object_t * );
-static void Close ( vlc_object_t * );
-
-static ssize_t Read ( input_thread_t *, byte_t *, size_t );
-int ReadDir( input_thread_t *p_input, char *psz_name , int i_mode );
+#include <vlc_charset.h>
+#include <vlc_url.h>
+#include <vlc_strings.h>
/*****************************************************************************
* Module descriptor
*****************************************************************************/
-#define RECURSIVE_TEXT N_("Includes subdirectories?")
+static int Open ( vlc_object_t * );
+static void Close( vlc_object_t * );
+
+#define RECURSIVE_TEXT N_("Subdirectory behavior")
#define RECURSIVE_LONGTEXT N_( \
"Select whether subdirectories must be expanded.\n" \
"none: subdirectories do not appear in the playlist.\n" \
"collapse: subdirectories appear but are expanded on first play.\n" \
"expand: all subdirectories are expanded.\n" )
-static char *psz_recursive_list[] = { "none", "collapse", "expand" };
-static char *psz_recursive_list_text[] = { N_("none"), N_("collapse"),
- N_("expand") };
-
-vlc_module_begin();
- set_description( _("Standard filesystem directory input") );
- set_capability( "access", 55 );
- add_shortcut( "directory" );
- add_shortcut( "dir" );
+static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
+static const char *const psz_recursive_list_text[] = {
+ N_("none"), N_("collapse"), N_("expand") };
+
+#define IGNORE_TEXT N_("Ignored extensions")
+#define IGNORE_LONGTEXT N_( \
+ "Files with these extensions will not be added to playlist when " \
+ "opening a directory.\n" \
+ "This is useful if you add directories that contain playlist files " \
+ "for instance. Use a comma-separated list of extensions." )
+
+vlc_module_begin ()
+ set_category( CAT_INPUT )
+ set_shortname( N_("Directory" ) )
+ set_subcategory( SUBCAT_INPUT_ACCESS )
+ set_description( N_("Standard filesystem directory input") )
+ set_capability( "access", 55 )
+ add_shortcut( "directory" )
+ add_shortcut( "dir" )
+ add_shortcut( "file" )
add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
- RECURSIVE_LONGTEXT, VLC_FALSE );
- change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
- set_callbacks( Open, Close );
-vlc_module_end();
+ RECURSIVE_LONGTEXT, false )
+ change_string_list( psz_recursive_list, psz_recursive_list_text, 0 )
+ 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",
+ NULL, IGNORE_TEXT, IGNORE_LONGTEXT, false )
+ set_callbacks( Open, Close )
+vlc_module_end ()
/*****************************************************************************
- * Open: open the directory
+ * Local prototypes, constants, structures
*****************************************************************************/
-static int Open( vlc_object_t *p_this )
+
+enum
{
- input_thread_t * p_input = (input_thread_t *)p_this;
- char * psz_name;
- input_directory_t * p_access_data;
- char * psz_mode;
- int i_mode;
-#ifdef HAVE_SYS_STAT_H
- struct stat stat_info;
+ MODE_EXPAND,
+ MODE_COLLAPSE,
+ MODE_NONE
+};
+
+typedef struct directory_t directory_t;
+struct directory_t
+{
+ directory_t *parent;
+ DIR *handle;
+ char *uri;
+#ifndef WIN32
+ struct stat st;
#endif
+ char path[1];
+};
- /* Initialize access plug-in structures. */
- if( p_input->i_mtu == 0 )
- {
- /* Improve speed. */
- p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
- }
+struct access_sys_t
+{
+ directory_t *current;
+ DIR *handle;
+ char *ignored_exts;
+ int mode;
+ int i_item_count;
+ char *psz_xspf_extension;
+};
- p_input->pf_read = Read;
- p_input->pf_set_program = NULL;
- p_input->pf_set_area = NULL;
- p_input->pf_seek = NULL;
+static block_t *Block( access_t * );
+static int Control( access_t *, int, va_list );
- /* Remove the ending '/' char */
- psz_name = strdup( p_input->psz_name );
- if( psz_name == NULL )
+/*****************************************************************************
+ * Open: open the directory
+ *****************************************************************************/
+static int Open( vlc_object_t *p_this )
+{
+ access_t *p_access = (access_t*)p_this;
+ access_sys_t *p_sys;
+
+ if( !p_access->psz_path )
return VLC_EGENERIC;
- if( (psz_name[strlen(psz_name)-1] == '/') ||
- (psz_name[strlen(psz_name)-1] == '\\') )
+ DIR *handle;
+ if (strcmp (p_access->psz_path, "-"))
+ handle = utf8_opendir (p_access->psz_path);
+ else
{
- psz_name[strlen(psz_name)-1] = '\0';
- }
-
-#ifdef HAVE_SYS_STAT_H
- if( ( stat( psz_name, &stat_info ) == -1 ) ||
- !S_ISDIR( stat_info.st_mode ) )
+#if 0 /* This won't work yet, it generates paths like "-/music.ogg".
+ * We'd need to use openat() here and in the file access... */
+ int fd = dup (0);
+ handle = fdopendir (fd);
+ if (handle == NULL)
+ close (fd);
#else
- if( !p_input->psz_access || strcmp(p_input->psz_access, "dir") )
-#endif
- {
- free( psz_name );
return VLC_EGENERIC;
+#endif
}
- /* Initialize structure */
- msg_Dbg( p_input, "opening directory `%s'", psz_name );
- p_access_data = malloc( sizeof(input_directory_t) );
- p_input->p_access_data = (void *)p_access_data;
- if( p_access_data == NULL )
- {
- msg_Err( p_input, "out of memory" );
- free( psz_name );
- return VLC_ENOMEM;
- }
- p_access_data->i_pos = 0;
-
- psz_mode = config_GetPsz( p_input , "recursive" );
- if( !psz_mode )
- {
- msg_Err( p_input, "Unable to get configuration" );
+ if (handle == NULL)
return VLC_EGENERIC;
- }
- if( !strncmp( psz_mode, "none" , 4 ) )
- {
- i_mode = MODE_NONE;
- }
- else if( !strncmp( psz_mode, "collapse", 8 ) )
+ p_sys = malloc (sizeof (*p_sys));
+ if (!p_sys)
{
- i_mode = MODE_COLLAPSE;
- }
- else
- {
- i_mode = MODE_EXPAND;
- }
- if( ReadDir( p_input, psz_name , i_mode ) != VLC_SUCCESS )
- {
- free( p_access_data );
- free( psz_name );
- return VLC_EGENERIC;
+ closedir( handle );
+ return VLC_ENOMEM;
}
- msg_Dbg(p_input,"Directory read complete. Read %i bytes",
- p_access_data->i_pos);
-
- p_access_data->p_dir_buffer[p_access_data->i_pos] = '\0';
- p_access_data->i_pos++;
- p_access_data->i_buf_length = p_access_data->i_pos;
- p_access_data->i_buf_pos = 0;
+ p_access->p_sys = p_sys;
+ p_sys->current = NULL;
+ p_sys->handle = handle;
+ p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
+ p_sys->i_item_count = 0;
+ p_sys->psz_xspf_extension = strdup( "" );
+
+ /* Handle mode */
+ char *psz = var_CreateGetString( p_access, "recursive" );
+ if( *psz == '\0' || !strcasecmp( psz, "none" ) )
+ p_sys->mode = MODE_NONE;
+ else if( !strcasecmp( psz, "collapse" ) )
+ p_sys->mode = MODE_COLLAPSE;
+ else
+ p_sys->mode = MODE_EXPAND;
+ free( psz );
- /* Force m3u demuxer */
- p_input->psz_demux = "m3u";
+ p_access->pf_read = NULL;
+ p_access->pf_block = Block;
+ p_access->pf_seek = NULL;
+ p_access->pf_control= Control;
+ free (p_access->psz_demux);
+ p_access->psz_demux = strdup ("xspf-open");
return VLC_SUCCESS;
}
*****************************************************************************/
static void Close( vlc_object_t * p_this )
{
- input_thread_t * p_input = (input_thread_t *)p_this;
- input_directory_t * p_access_data =
- (input_directory_t *)p_input->p_access_data;
+ access_t *p_access = (access_t*)p_this;
+ access_sys_t *p_sys = p_access->p_sys;
- msg_Info( p_input, "closing `%s/%s://%s'",
- p_input->psz_access, p_input->psz_demux, p_input->psz_name );
+ while (p_sys->current)
+ {
+ directory_t *current = p_sys->current;
- free( p_access_data );
+ p_sys->current = current->parent;
+ closedir (current->handle);
+ free (current->uri);
+ free (current);
+ }
+ if (p_sys->handle != NULL)
+ closedir (p_sys->handle); /* corner case,:Block() not called ever */
+ free (p_sys->psz_xspf_extension);
+ free (p_sys->ignored_exts);
+ free (p_sys);
}
-/*****************************************************************************
- * Read: read directory and output to demux.
- *****************************************************************************/
-static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
+/**
+ * URI-encodes a file path. The only reserved characters is slash.
+ */
+static char *encode_path (const char *path)
{
- input_directory_t * p_access_data =
- (input_directory_t *)p_input->p_access_data;
- unsigned int i_remaining = p_access_data->i_buf_length -
- p_access_data->i_buf_pos;
+ static const char sep[]= "%2F";
+ char *enc = encode_URI_component (path), *ptr = enc;
- if( i_remaining > 0 )
+ if (enc == NULL)
+ return NULL;
+
+ /* Replace '%2F' with '/'. TODO: extend encode_URI*() */
+ /* (On Windows, both ':' and '\\' will be encoded) */
+ while ((ptr = strstr (ptr, sep)) != NULL)
{
- int i_ret;
-
- i_ret = __MIN( i_len, i_remaining );
- memcpy( p_buffer,
- &p_access_data->p_dir_buffer[p_access_data->i_buf_pos],
- i_ret );
- p_access_data->i_buf_pos += i_ret;
- return (ssize_t) i_ret;
+ *ptr++ = '/';
+ memmove (ptr, ptr + 2, strlen (ptr) - 1);
}
- return 0;
+ return enc;
}
-/* Local functions */
-
-/*****************************************************************************
- * ReadDir: read a directory and add its content to the list
- *****************************************************************************/
-int ReadDir( input_thread_t *p_input, char *psz_name , int i_mode )
+/* Detect directories that recurse into themselves. */
+static bool has_inode_loop (const directory_t *dir)
{
- DIR * p_current_dir;
- struct dirent * p_dir_content;
+#ifndef WIN32
+ dev_t dev = dir->st.st_dev;
+ ino_t inode = dir->st.st_ino;
- input_directory_t * p_access_data =
- (input_directory_t *)p_input->p_access_data;
+ while ((dir = dir->parent) != NULL)
+ if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
+ return true;
+#else
+# define fstat( fd, st ) (0)
+#endif
+ return false;
+}
- /* have to cd into this dir */
- p_current_dir = opendir( psz_name );
+static block_t *Block (access_t *p_access)
+{
+ access_sys_t *p_sys = p_access->p_sys;
+ directory_t *current = p_sys->current;
+
+ if (p_access->info.b_eof)
+ return NULL;
+
+ if (current == NULL)
+ { /* Startup: send the XSPF header */
+ static const char header[] =
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
+ " <trackList>\n";
+ block_t *block = block_Alloc (sizeof (header) - 1);
+ if (!block)
+ goto fatal;
+ memcpy (block->p_buffer, header, sizeof (header) - 1);
+
+ /* "Open" the base directory */
+ current = malloc (sizeof (*current) + strlen (p_access->psz_path));
+ if (current == NULL)
+ {
+ block_Release (block);
+ goto fatal;
+ }
+ current->parent = NULL;
+ current->handle = p_sys->handle;
+ strcpy (current->path, p_access->psz_path);
+ current->uri = encode_path (current->path);
+ if ((current->uri == NULL)
+ || fstat (dirfd (current->handle), ¤t->st))
+ {
+ free (current->uri);
+ free (current);
+ block_Release (block);
+ goto fatal;
+ }
- if( p_current_dir == NULL )
- {
- /* something went bad, get out of here ! */
-# ifdef HAVE_ERRNO_H
- msg_Warn( p_input, "cannot open directory `%s' (%s)",
- psz_name, strerror(errno));
-# else
- msg_Warn( p_input, "cannot open directory `%s'", psz_name );
-# endif
- return VLC_EGENERIC;
+ p_sys->handle = NULL;
+ p_sys->current = current;
+ return block;
}
- p_dir_content = readdir( p_current_dir );
+ char *entry = utf8_readdir (current->handle);
+ if (entry == NULL)
+ { /* End of directory, go back to parent */
+ closedir (current->handle);
+ p_sys->current = current->parent;
+ free (current);
+
+ if (p_sys->current == NULL)
+ { /* End of XSPF playlist */
+ char *footer;
+ int len = asprintf( &footer, " </trackList>\n" \
+ " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
+ "%s" \
+ " </extension>\n" \
+ "</playlist>\n", p_sys->psz_xspf_extension );
+ if( len < 0 )
+ goto fatal;
+
+ block_t *block = block_Alloc ( len );
+ if (!block)
+ goto fatal;
+ memcpy (block->p_buffer, footer, len);
+ free( footer );
+ p_access->info.b_eof = true;
+ return block;
+ }
+ else
+ {
+ /* This was the end of a "subnode" */
+ /* Write the ID to the extension */
+ char *old_xspf_extension = p_sys->psz_xspf_extension;
+ if (old_xspf_extension == NULL)
+ goto fatal;
+
+ int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
+ if (len2 == -1)
+ goto fatal;
+ free( old_xspf_extension );
+ }
+ return NULL;
+ }
- /* while we still have entries in the directory */
- while( p_dir_content != NULL && p_access_data->i_pos < MAX_DIR_SIZE )
+ /* Skip current, parent and hidden directories */
+ if (entry[0] == '.')
+ return NULL;
+ /* Handle recursion */
+ if (p_sys->mode != MODE_COLLAPSE)
{
- int i_size_entry = strlen( psz_name ) +
- strlen( p_dir_content->d_name ) + 2;
- char *psz_entry = (char *)malloc( sizeof(char)*i_size_entry);
-
- sprintf( psz_entry, "%s/%s",psz_name,p_dir_content->d_name);
-
-#if 0 /* Disable this message, it makes too much output */
- msg_Dbg( p_input, "Entry %s",psz_entry );
-#endif
-
- /* if it is "." or "..", forget it */
- if( strcmp( p_dir_content->d_name, "." ) &&
- strcmp( p_dir_content->d_name, ".." ) &&
- p_access_data->i_pos + i_size_entry < MAX_DIR_SIZE )
+ directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
+ + strlen (entry));
+ if (sub == NULL)
+ return NULL;
+ sprintf (sub->path, "%s/%s", current->path, entry);
+
+ DIR *handle = utf8_opendir (sub->path);
+ if (handle != NULL)
{
-#if defined( DT_DIR )
- if( p_dir_content->d_type == DT_DIR )
-#elif defined( S_ISDIR )
- struct stat stat_data;
- stat( psz_entry, &stat_data );
- if( S_ISDIR(stat_data.st_mode) )
-#else
- if( 0 )
-#endif
+ sub->parent = current;
+ sub->handle = handle;
+
+ char *encoded = encode_URI_component (entry);
+ if ((encoded == NULL)
+ || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
+ sub->uri = NULL;
+ free (encoded);
+
+ if ((p_sys->mode == MODE_NONE)
+ || fstat (dirfd (handle), &sub->st)
+ || has_inode_loop (sub)
+ || (sub->uri == NULL))
{
- if( i_mode == MODE_NONE )
- {
- msg_Dbg( p_input, "Skipping subdirectory %s",psz_entry );
- p_dir_content = readdir( p_current_dir );
- continue;
- }
- else if(i_mode == MODE_EXPAND )
- {
- msg_Dbg(p_input, "Reading subdirectory %s",psz_entry );
- if( ReadDir( p_input, psz_entry , MODE_EXPAND )
- != VLC_SUCCESS )
- {
- return VLC_EGENERIC;
- }
- }
- else
- {
- sprintf( &p_access_data->p_dir_buffer[p_access_data->i_pos],
- "%s", psz_entry );
- p_access_data->i_pos += i_size_entry -1 ;
- p_access_data->p_dir_buffer[p_access_data->i_pos] = '\n';
- p_access_data->i_pos++;
- }
+ closedir (handle);
+ free (sub);
+ return NULL;
}
- else
+ p_sys->current = sub;
+
+ /* Add node to xspf extension */
+ char *old_xspf_extension = p_sys->psz_xspf_extension;
+ if (old_xspf_extension == NULL)
+ goto fatal;
+
+ char *title = convert_xml_special_chars (entry);
+ if (title == NULL
+ || asprintf (&p_sys->psz_xspf_extension, "%s"
+ " <vlc:node title=\"%s\">\n", old_xspf_extension,
+ title) == -1)
{
- sprintf( &p_access_data->p_dir_buffer[p_access_data->i_pos],
- "%s", psz_entry );
- p_access_data->i_pos += i_size_entry - 1;
- p_access_data->p_dir_buffer[p_access_data->i_pos] = '\n';
- p_access_data->i_pos++;
+ free (title);
+ goto fatal;
+ }
+ free (title);
+ free (old_xspf_extension);
+ return NULL;
+ }
+ else
+ free (sub);
+ }
+
+ /* Skip files with ignored extensions */
+ if (p_sys->ignored_exts != NULL)
+ {
+ const char *ext = strrchr (entry, '.');
+ if (ext != NULL)
+ {
+ size_t extlen = strlen (++ext);
+ for (const char *type = p_sys->ignored_exts, *end;
+ type[0]; type = end + 1)
+ {
+ end = strchr (type, ',');
+ if (end == NULL)
+ end = type + strlen (type);
+
+ if (type + extlen == end
+ && !strncasecmp (ext, type, extlen))
+ return NULL;
+
+ if (*end == '\0')
+ break;
}
}
- free( psz_entry );
- p_dir_content = readdir( p_current_dir );
}
- closedir( p_current_dir );
+
+ char *encoded = encode_URI_component (entry);
+ free (entry);
+ if (encoded == NULL)
+ goto fatal;
+ int len = asprintf (&entry,
+ " <track><location>file://%s/%s</location>\n" \
+ " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
+ " <vlc:id>%d</vlc:id>\n" \
+ " </extension>\n" \
+ " </track>\n",
+ current->uri, encoded, p_sys->i_item_count++);
+ free (encoded);
+ if (len == -1)
+ goto fatal;
+
+ /* Write the ID to the extension */
+ char *old_xspf_extension = p_sys->psz_xspf_extension;
+ if (old_xspf_extension == NULL)
+ goto fatal;
+
+ int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
+ old_xspf_extension, p_sys->i_item_count-1 );
+ if (len2 == -1)
+ goto fatal;
+ free( old_xspf_extension );
+
+ /* TODO: new block allocator for malloc()ated data */
+ block_t *block = block_Alloc (len);
+ if (!block)
+ {
+ free (entry);
+ goto fatal;
+ }
+ memcpy (block->p_buffer, entry, len);
+ free (entry);
+ return block;
+
+fatal:
+ p_access->info.b_eof = true;
+ return NULL;
+}
+
+/*****************************************************************************
+ * Control:
+ *****************************************************************************/
+static int Control( access_t *p_access, int i_query, va_list args )
+{
+ bool *pb_bool;
+ int64_t *pi_64;
+
+ switch( i_query )
+ {
+ /* */
+ case ACCESS_CAN_SEEK:
+ case ACCESS_CAN_FASTSEEK:
+ pb_bool = (bool*)va_arg( args, bool* );
+ *pb_bool = false;
+ break;
+
+ case ACCESS_CAN_PAUSE:
+ case ACCESS_CAN_CONTROL_PACE:
+ pb_bool = (bool*)va_arg( args, bool* );
+ *pb_bool = true;
+ break;
+
+ /* */
+ case ACCESS_GET_PTS_DELAY:
+ pi_64 = (int64_t*)va_arg( args, int64_t * );
+ *pi_64 = DEFAULT_PTS_DELAY * 1000;
+ break;
+
+ /* */
+ case ACCESS_SET_PAUSE_STATE:
+ case ACCESS_GET_TITLE_INFO:
+ case ACCESS_SET_TITLE:
+ case ACCESS_SET_SEEKPOINT:
+ case ACCESS_SET_PRIVATE_ID_STATE:
+ case ACCESS_GET_CONTENT_TYPE:
+ case ACCESS_GET_META:
+ return VLC_EGENERIC;
+
+ default:
+ msg_Warn( p_access, "unimplemented query in control" );
+ return VLC_EGENERIC;
+ }
return VLC_SUCCESS;
}