/*****************************************************************************
* 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_SYS_TYPES_H
-# include <sys/types.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
#endif
+
+#include <vlc_common.h>
+#include "fs.h"
+#include <vlc_access.h>
+
+#include <sys/types.h>
#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 <fcntl.h>
#elif defined( WIN32 ) && !defined( UNDER_CE )
# include <io.h>
#endif
-#if (!defined( WIN32 ) || defined(__MINGW32__))
-/* Mingw has its own version of dirent */
-# include <dirent.h>
-#endif
+#include <vlc_fs.h>
+#include <vlc_url.h>
+#include <vlc_strings.h>
-/*****************************************************************************
- * 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
+enum
{
- 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 * );
+ MODE_NONE,
+ MODE_COLLAPSE,
+ MODE_EXPAND,
+};
-static ssize_t Read ( input_thread_t *, byte_t *, size_t );
-int ReadDir( input_thread_t *p_input, char *psz_name , int i_mode );
+typedef struct directory_t directory_t;
+struct directory_t
+{
+ directory_t *parent;
+ DIR *handle;
+ char *uri;
+ char **filev;
+ int filec, i;
+#ifdef HAVE_OPENAT
+ dev_t device;
+ ino_t inode;
+#else
+ char *path;
+#endif
+};
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
-#define RECURSIVE_TEXT N_("Includes subdirectories?")
-#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" );
- 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();
+struct access_sys_t
+{
+ directory_t *current;
+ char *ignored_exts;
+ char mode;
+ bool header;
+ int i_item_count;
+ char *xspf_ext;
+};
+
+/* Select non-hidden files only */
+static int visible (const char *name)
+{
+ return name[0] != '.';
+}
+static int collate (const char **a, const char **b)
+{
+#ifdef HAVE_STRCOLL
+ return strcoll (*a, *b);
+#else
+ return strcmp (*a, *b);
+#endif
+}
/*****************************************************************************
* Open: open the directory
*****************************************************************************/
-static int Open( vlc_object_t *p_this )
+int DirOpen( vlc_object_t *p_this )
{
- 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;
-#endif
+ access_t *p_access = (access_t*)p_this;
- /* Initialize access plug-in structures. */
- if( p_input->i_mtu == 0 )
- {
- /* Improve speed. */
- p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
- }
-
- p_input->pf_read = Read;
- p_input->pf_set_program = NULL;
- p_input->pf_set_area = NULL;
- p_input->pf_seek = NULL;
-
- /* Remove the ending '/' char */
- psz_name = strdup( p_input->psz_name );
- if( psz_name == NULL )
+ if( !p_access->psz_filepath )
return VLC_EGENERIC;
- if( (psz_name[strlen(psz_name)-1] == '/') ||
- (psz_name[strlen(psz_name)-1] == '\\') )
- {
- 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 ) )
-#else
- if( !p_input->psz_access || strcmp(p_input->psz_access, "dir") )
-#endif
- {
- free( psz_name );
+ DIR *handle = vlc_opendir (p_access->psz_filepath);
+ if (handle == NULL)
return VLC_EGENERIC;
- }
- /* 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;
+ return DirInit (p_access, handle);
+}
- psz_mode = config_GetPsz( p_input , "recursive" );
- if( !psz_mode )
- {
- msg_Err( p_input, "Unable to get configuration" );
- return VLC_EGENERIC;
- }
+int DirInit (access_t *p_access, DIR *handle)
+{
+ access_sys_t *p_sys = malloc (sizeof (*p_sys));
+ if (unlikely(p_sys == NULL))
+ goto error;
- if( !strncmp( psz_mode, "none" , 4 ) )
+ char *uri;
+ if (!strcmp (p_access->psz_access, "fd"))
{
- i_mode = MODE_NONE;
- }
- else if( !strncmp( psz_mode, "collapse", 8 ) )
- {
- i_mode = MODE_COLLAPSE;
+ if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
+ uri = NULL;
}
else
+ uri = make_URI (p_access->psz_filepath, "file");
+ if (unlikely(uri == NULL))
+ goto error;
+
+ /* "Open" the base directory */
+ directory_t *root = malloc (sizeof (*root));
+ if (unlikely(root == NULL))
{
- i_mode = MODE_EXPAND;
+ free (uri);
+ goto error;
}
- if( ReadDir( p_input, psz_name , i_mode ) != VLC_SUCCESS )
+ root->parent = NULL;
+ root->handle = handle;
+ root->uri = uri;
+ root->filec = vlc_loaddir (handle, &root->filev, visible, collate);
+ if (root->filec < 0)
+ root->filev = NULL;
+ root->i = 0;
+#ifdef HAVE_OPENAT
+ struct stat st;
+ if (fstat (dirfd (handle), &st))
{
- free( p_access_data );
- free( psz_name );
- return VLC_EGENERIC;
+ free (root);
+ free (uri);
+ goto error;
}
+ root->device = st.st_dev;
+ root->inode = st.st_ino;
+#else
+ root->path = strdup (p_access->psz_filepath);
+#endif
- 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 = root;
+ p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
+ p_sys->header = true;
+ p_sys->i_item_count = 0;
+ p_sys->xspf_ext = strdup ("");
+
+ /* Handle mode */
+ char *psz = var_InheritString (p_access, "recursive");
+ if (psz == NULL || !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";
+ access_InitFields(p_access);
+ p_access->pf_read = NULL;
+ p_access->pf_block = DirBlock;
+ p_access->pf_seek = NULL;
+ p_access->pf_control= DirControl;
+ free (p_access->psz_demux);
+ p_access->psz_demux = strdup ("xspf-open");
return VLC_SUCCESS;
+
+error:
+ closedir (handle);
+ free (p_sys);
+ return VLC_EGENERIC;
}
/*****************************************************************************
* Close: close the target
*****************************************************************************/
-static void Close( vlc_object_t * p_this )
+void DirClose( 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;
+
+ p_sys->current = current->parent;
+ closedir (current->handle);
+ free (current->uri);
+ while (current->i < current->filec)
+ free (current->filev[current->i++]);
+ free (current->filev);
+#ifndef HAVE_OPENAT
+ free (current->path);
+#endif
+ free (current);
+ }
- free( p_access_data );
+ free (p_sys->xspf_ext);
+ 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 )
+#ifdef HAVE_OPENAT
+/* Detect directories that recurse into themselves. */
+static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
{
- 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;
-
- if( i_remaining > 0 )
+ while (dir != 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;
+ if ((dir->device == dev) && (dir->inode == inode))
+ return true;
+ dir = dir->parent;
}
- return 0;
+ return false;
}
+#endif
-/* 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 )
+block_t *DirBlock (access_t *p_access)
{
- DIR * p_current_dir;
- struct dirent * p_dir_content;
-
- input_directory_t * p_access_data =
- (input_directory_t *)p_input->p_access_data;
-
- /* have to cd into this dir */
- p_current_dir = opendir( psz_name );
+ access_sys_t *p_sys = p_access->p_sys;
+ directory_t *current = p_sys->current;
+
+ if (p_access->info.b_eof)
+ return NULL;
+
+ if (p_sys->header)
+ { /* 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);
+ p_sys->header = false;
+ return block;
+ }
- 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;
+ if (current->i >= current->filec)
+ { /* End of directory, go back to parent */
+ closedir (current->handle);
+ p_sys->current = current->parent;
+ free (current->uri);
+ free (current->filev);
+#ifndef HAVE_OPENAT
+ free (current->path);
+#endif
+ 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->xspf_ext ? p_sys->xspf_ext : "");
+ if (unlikely(len == -1))
+ goto fatal;
+
+ block_t *block = block_heap_Alloc (footer, footer, len);
+ if (unlikely(block == NULL))
+ 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_ext = p_sys->xspf_ext;
+ if (old_xspf_ext != NULL
+ && asprintf (&p_sys->xspf_ext, "%s </vlc:node>\n",
+ old_xspf_ext ? old_xspf_ext : "") == -1)
+ p_sys->xspf_ext = NULL;
+ free (old_xspf_ext);
+ }
+ return NULL;
}
- p_dir_content = readdir( p_current_dir );
+ char *entry = current->filev[current->i++];
- /* while we still have entries in the directory */
- while( p_dir_content != NULL && p_access_data->i_pos < MAX_DIR_SIZE )
+ /* 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 );
+ DIR *handle;
+#ifdef HAVE_OPENAT
+ int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
+ if (fd == -1)
+ goto skip; /* File cannot be opened... forget it */
+
+ struct stat st;
+ if (fstat (fd, &st))
+ {
+ close (fd);
+ goto skip; /* cannot stat?! */
+ }
+ if (!S_ISDIR (st.st_mode))
+ {
+ close (fd);
+ goto notdir;
+ }
+ if (p_sys->mode == MODE_NONE
+ || has_inode_loop (current, st.st_dev, st.st_ino)
+ || (handle = fdopendir (fd)) == NULL)
+ {
+ close (fd);
+ goto skip;
+ }
+#else
+ char *path;
+ if (asprintf (&path, "%s/%s", current->path, entry) == -1)
+ goto skip;
+ if ((handle = vlc_opendir (path)) == NULL)
+ goto notdir;
+ if (p_sys->mode == MODE_NONE)
+ goto skip;
#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));
+ if (unlikely(sub == 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) )
+ closedir (handle);
+#ifndef HAVE_OPENAT
+ free (path);
+#endif
+ goto skip;
+ }
+ sub->parent = current;
+ sub->handle = handle;
+ sub->filec = vlc_loaddir (handle, &sub->filev, visible, collate);
+ if (sub->filec < 0)
+ sub->filev = NULL;
+ sub->i = 0;
+#ifdef HAVE_OPENAT
+ sub->device = st.st_dev;
+ sub->inode = st.st_ino;
#else
- if( 0 )
+ sub->path = path;
#endif
+ p_sys->current = sub;
+
+ char *encoded = encode_URI_component (entry);
+ if (encoded == NULL
+ || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
+ sub->uri = NULL;
+ free (encoded);
+ if (unlikely(sub->uri == NULL))
+ {
+ free (entry);
+ goto fatal;
+ }
+
+ /* Add node to XSPF extension */
+ char *old_xspf_ext = p_sys->xspf_ext;
+ char *title = convert_xml_special_chars (entry);
+ if (old_xspf_ext != NULL && title != NULL
+ && asprintf (&p_sys->xspf_ext, "%s <vlc:node title=\"%s\">\n",
+ old_xspf_ext, title) == -1)
+ p_sys->xspf_ext = NULL;
+ free (old_xspf_ext);
+ free (title);
+ goto skip;
+ }
+
+notdir:
+ /* 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)
{
- 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
+ end = strchr (type, ',');
+ if (end == NULL)
+ end = type + strlen (type);
+
+ if (type + extlen == end
+ && !strncasecmp (ext, type, extlen))
{
- 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 (entry);
+ return NULL;
}
- }
- 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++;
+
+ 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>%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_ext = p_sys->xspf_ext;
+ if (old_xspf_ext != NULL
+ && asprintf (&p_sys->xspf_ext, "%s <vlc:item tid=\"%i\" />\n",
+ old_xspf_ext, p_sys->i_item_count - 1) == -1)
+ p_sys->xspf_ext = NULL;
+ free (old_xspf_ext);
+
+ block_t *block = block_heap_Alloc (entry, entry, len);
+ if (unlikely(block == NULL))
+ {
+ free (entry);
+ goto fatal;
+ }
+ return block;
+
+fatal:
+ p_access->info.b_eof = true;
+ return NULL;
+
+skip:
+ free (entry);
+ return NULL;
+}
+
+/*****************************************************************************
+ * Control:
+ *****************************************************************************/
+int DirControl( access_t *p_access, int i_query, va_list args )
+{
+ switch( i_query )
+ {
+ /* */
+ case ACCESS_CAN_SEEK:
+ case ACCESS_CAN_FASTSEEK:
+ *va_arg( args, bool* ) = false;
+ break;
+
+ case ACCESS_CAN_PAUSE:
+ case ACCESS_CAN_CONTROL_PACE:
+ *va_arg( args, bool* ) = true;
+ break;
+
+ /* */
+ case ACCESS_GET_PTS_DELAY:
+ *va_arg( args, int64_t * ) = 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;
}