[vlc-devel] [PATCH 09/10] Rewrite access/directory.c as an access_browser. No more xspf generation ... Yeah!
Julien 'Lta' BALLET
elthariel at gmail.com
Mon May 26 11:41:49 CEST 2014
From: Julien 'Lta' BALLET <contact at lta.io>
---
modules/access/directory.c | 537 ++++++++++++++++++---------------------------
modules/access/file.c | 29 +--
modules/access/fs.c | 5 +-
modules/access/fs.h | 3 -
4 files changed, 228 insertions(+), 346 deletions(-)
diff --git a/modules/access/directory.c b/modules/access/directory.c
index 3d72dc0..53422de 100644
--- a/modules/access/directory.c
+++ b/modules/access/directory.c
@@ -1,11 +1,12 @@
/*****************************************************************************
- * directory.c: expands a directory (directory: access plug-in)
+ * directory.c: expands a directory (directory: access_browser plug-in)
*****************************************************************************
* Copyright (C) 2002-2008 VLC authors and VideoLAN
* $Id$
*
* Authors: Derk-Jan Hartman <hartman at videolan dot org>
* Rémi Denis-Courmont
+ * Julien 'Lta' BALLET <contact # lta.io>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
@@ -32,7 +33,7 @@
#include <vlc_common.h>
#include "fs.h"
-#include <vlc_access.h>
+#include <vlc_browsing.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -45,6 +46,15 @@
#include <vlc_strings.h>
#include <vlc_charset.h>
+static int DirBrowse( browse_t * );
+
+enum
+{
+ ENTRY_DIR = 0,
+ ENTRY_ENOTDIR = -1,
+ ENTRY_EACCESS = -2,
+};
+
enum
{
MODE_NONE,
@@ -68,14 +78,11 @@ struct directory_t
#endif
};
-struct access_sys_t
+struct browse_sys_t
{
directory_t *current;
char *ignored_exts;
char mode;
- bool header;
- int i_item_count;
- char *xspf_ext;
int (*compar)(const char **a, const char **b);
};
@@ -99,107 +106,181 @@ static int version (const char **a, const char **b)
return strverscmp (*a, *b);
}
-/*****************************************************************************
- * Open: open the directory
- *****************************************************************************/
-int DirOpen( vlc_object_t *p_this )
+#ifdef HAVE_OPENAT
+/* Detect directories that recurse into themselves. */
+static bool has_inode_loop( const directory_t *dir, dev_t dev, ino_t inode )
{
- access_t *p_access = (access_t*)p_this;
+ while (dir != NULL)
+ {
+ if ((dir->device == dev) && (dir->inode == inode))
+ return true;
+ dir = dir->parent;
+ }
+ return false;
+}
+#endif
- if( !p_access->psz_filepath )
- return VLC_EGENERIC;
+/* success -> returns ENTRY_DIR and the handle parameter is set to the handle,
+ * error -> return ENTRY_ENOTDIR or ENTRY_EACCESS */
+static int directory_open( directory_t *p_dir, char *psz_entry, DIR **handle )
+{
+ *handle = NULL;
- DIR *handle = vlc_opendir (p_access->psz_filepath);
- if (handle == NULL)
- return VLC_EGENERIC;
+#ifdef HAVE_OPENAT
+ int fd = vlc_openat( dirfd( p_dir->handle ), psz_entry,
+ O_RDONLY | O_DIRECTORY );
+
+ if (fd == -1)
+ {
+ if (errno == ENOTDIR)
+ return ENTRY_ENOTDIR;
+ else
+ return ENTRY_EACCESS;
+ }
+
+ struct stat st;
+ if( fstat( fd, &st )
+ || has_inode_loop( p_dir, st.st_dev, st.st_ino )
+ || ( *handle = fdopendir( fd ) ) == NULL )
+ {
+ close (fd);
+ return ENTRY_EACCESS;
+ }
+#else
+ char *path;
+ if( asprintf( &path, "%s/%s", current->path, entry ) == -1 )
+ goto ENTRY_EACCESS;
+ if( ( *handle = vlc_opendir( path ) ) == NULL )
+ goto ENTRY_ENOTDIR;
+#endif
- return DirInit (p_access, handle);
+ return ENTRY_DIR;
}
-int DirInit (access_t *p_access, DIR *handle)
+static bool directory_push( browse_sys_t *p_sys, DIR *handle, char *psz_uri )
{
- access_sys_t *p_sys = malloc (sizeof (*p_sys));
- if (unlikely(p_sys == NULL))
+ directory_t *p_dir;
+
+ p_dir = malloc( sizeof( *p_dir ) );
+ if( unlikely( p_dir == NULL ) )
+ return NULL;
+
+ psz_uri = strdup( psz_uri );
+ if( unlikely ( psz_uri == NULL ) )
goto error;
- char *uri;
- if (!strcmp (p_access->psz_access, "fd"))
- {
- if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
- uri = NULL;
- }
- else
- uri = vlc_path2uri (p_access->psz_filepath, "file");
- if (unlikely(uri == NULL))
+ p_dir->parent = p_sys->current;
+ p_dir->handle = handle;
+ p_dir->uri = psz_uri;
+ p_dir->filec = vlc_loaddir (handle, &p_dir->filev, visible, p_sys->compar);
+ if (p_dir->filec < 0)
+ p_dir->filev = NULL;
+ p_dir->i = 0;
+
+#ifdef HAVE_OPENAT
+ struct stat st;
+ if( fstat( dirfd( handle ), &st ) )
goto error;
+ p_dir->device = st.st_dev;
+ p_dir->inode = st.st_ino;
+#else
+ p_dir->path = make_path( psz_uri );
+ if( p_dir->path == NULL )
+ goto error;
+#endif
- /* "Open" the base directory */
- directory_t *root = malloc (sizeof (*root));
- if (unlikely(root == NULL))
- {
- free (uri);
+ p_sys->current = p_dir;
+ return true;
+
+ error:
+ closedir( handle );
+ free( p_dir );
+ if ( psz_uri != NULL )
+ free( psz_uri );
+
+ return false;
+}
+
+static bool directory_pop( browse_sys_t *p_sys )
+{
+ directory_t *p_old = p_sys->current;
+
+ if( p_old == NULL )
+ return false;
+
+ p_sys->current = p_old->parent;
+ closedir( p_old->handle );
+ free( p_old->uri );
+ free( p_old->filev );
+#ifndef HAVE_OPENAT
+ free (p_old->path);
+#endif
+ free( p_old );
+
+ return p_sys->current != NULL;
+}
+
+
+/*****************************************************************************
+ * Open: open the directory
+ *****************************************************************************/
+int DirOpen( vlc_object_t *p_this )
+{
+ browse_t *p_browse = (browse_t*)p_this;
+
+ DIR *handle = vlc_opendir( p_browse->psz_location );
+
+ if (handle == NULL)
+ return VLC_EGENERIC;
+
+ input_item_t *p_item = p_browse->p_node->p_item;
+ vlc_mutex_lock( &p_item->lock );
+ p_item->i_type = ITEM_TYPE_DIRECTORY;
+ vlc_mutex_unlock( &p_item->lock );
+
+ browse_sys_t *p_sys = malloc( sizeof( *p_sys ) );
+ if( unlikely( p_sys == NULL ) )
goto error;
- }
- char *psz_sort = var_InheritString (p_access, "directory-sort");
+ char *psz_sort = var_InheritString( p_browse, "directory-sort" );
if (!psz_sort)
p_sys->compar = collate;
- else if (!strcasecmp (psz_sort, "version"))
+ else if ( !strcasecmp ( psz_sort, "version" ) )
p_sys->compar = version;
- else if (!strcasecmp (psz_sort, "none"))
+ else if ( !strcasecmp ( psz_sort, "none" ) )
p_sys->compar = NULL;
else
p_sys->compar = collate;
free(psz_sort);
- root->parent = NULL;
- root->handle = handle;
- root->uri = uri;
- root->filec = vlc_loaddir (handle, &root->filev, visible, p_sys->compar);
- if (root->filec < 0)
- root->filev = NULL;
- root->i = 0;
-#ifdef HAVE_OPENAT
- struct stat st;
- if (fstat (dirfd (handle), &st))
- {
- free (root);
- free (uri);
+ /* "Open" the base directory */
+ p_sys->current = NULL;
+ if( !directory_push( p_sys, handle, p_item->psz_uri ) )
goto error;
- }
- root->device = st.st_dev;
- root->inode = st.st_ino;
-#else
- root->path = strdup (p_access->psz_filepath);
-#endif
- 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 ("");
+ p_browse->p_sys = p_sys;
+ p_sys->ignored_exts = var_InheritString (p_browse, "ignore-filetypes");
+
/* Handle mode */
- char *psz = var_InheritString (p_access, "recursive");
- if (psz == NULL || !strcasecmp (psz, "none"))
+ char *psz_rec = var_InheritString ( p_browse, "recursive" );
+ if( psz_rec == NULL || !strcasecmp ( psz_rec, "none" ) )
p_sys->mode = MODE_NONE;
- else if( !strcasecmp( psz, "collapse" ) )
+ else if( !strcasecmp( psz_rec, "collapse" ) )
p_sys->mode = MODE_COLLAPSE;
else
p_sys->mode = MODE_EXPAND;
- free( psz );
+ free( psz_rec );
+
+
+ p_browse->pf_browse = DirBrowse;
- access_InitFields(p_access);
- p_access->pf_read = NULL;
- p_access->pf_block = DirBlock;
- p_access->pf_seek = NULL;
- p_access->pf_control = DirControl;
return VLC_SUCCESS;
error:
- closedir (handle);
- free (p_sys);
+ closedir( handle );
+ if( p_sys != NULL )
+ free( p_sys );
return VLC_EGENERIC;
}
@@ -208,281 +289,91 @@ error:
*****************************************************************************/
void DirClose( vlc_object_t * p_this )
{
- access_t *p_access = (access_t*)p_this;
- access_sys_t *p_sys = p_access->p_sys;
+ browse_t *p_browse = (browse_t*)p_this;
+ browse_sys_t *p_sys = p_browse->p_sys;
- 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);
- }
+ while( directory_pop( p_sys ) )
+ ;
- free (p_sys->xspf_ext);
+ //free (p_sys->xspf_ext);
free (p_sys->ignored_exts);
free (p_sys);
}
-#ifdef HAVE_OPENAT
-/* Detect directories that recurse into themselves. */
-static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
+/* This function is a little bit too complex for what it seems to do, but the
+ * point is to de-recursify directory recusion to avoid overruning the stack
+ * in case there's a high directory depth */
+int DirBrowse( browse_t *p_browse )
{
- while (dir != NULL)
+ browse_sys_t *p_sys = p_browse->p_sys;
+ input_item_node_t *p_current_node = p_browse->p_node;
+
+ while( p_sys->current != NULL
+ && p_sys->current->i <= p_sys->current->filec )
{
- if ((dir->device == dev) && (dir->inode == inode))
- return true;
- dir = dir->parent;
- }
- return false;
-}
-#endif
+ directory_t *p_current = p_sys->current;
-block_t *DirBlock (access_t *p_access)
-{
- access_sys_t *p_sys = p_access->p_sys;
- directory_t *current = p_sys->current;
+ /* End of the current folder, let's pop directory and node */
+ if( p_current->i == p_current->filec )
+ {
+ directory_pop( p_sys );
+ p_current_node = p_current_node->p_parent;
+ continue;
+ }
- if (p_access->info.b_eof)
- return NULL;
+ char *psz_entry = p_current->filev[p_current->i++];
+ char *psz_full_uri, *psz_uri;
+ DIR *handle;
+ input_item_t *p_new = NULL;
+ int i_res;
- 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;
- }
+ /* Check if it is a directory or even readable */
+ i_res = directory_open( p_current, psz_entry, &handle );
- 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, len);
- 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;
- }
+ if( i_res == ENTRY_EACCESS
+ || ( i_res == ENTRY_DIR && p_sys->mode == MODE_NONE )
+ || ( i_res == ENTRY_ENOTDIR && has_ext( p_sys->ignored_exts, psz_entry ) ) )
+ continue;
- char *entry = current->filev[current->i++];
- /* Handle recursion */
- if (p_sys->mode != MODE_COLLAPSE)
- {
- DIR *handle;
-#ifdef HAVE_OPENAT
- int fd = vlc_openat (dirfd (current->handle), entry,
- O_RDONLY | O_DIRECTORY);
- if (fd == -1)
+ /* Create an input item for the current entry */
+ psz_uri = encode_URI_component( psz_entry );
+ if( psz_uri == NULL
+ || asprintf( &psz_full_uri, "%s/%s", p_current->uri, psz_uri ) == -1 )
{
- if (errno == ENOTDIR)
- goto notdir;
- goto skip; /* File cannot be opened... forget it */
+ closedir( handle );
+ continue;
}
+ free (psz_uri);
- struct stat st;
- if (fstat (fd, &st)
- || 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
- directory_t *sub = malloc (sizeof (*sub));
- if (unlikely(sub == NULL))
+ int i_type = i_res == ENTRY_DIR ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
+ p_new = input_item_NewWithType( psz_full_uri, psz_entry,
+ 0, NULL, 0, 0, i_type);
+ //msg_Dbg( p_browse, "|||||| psz_full_uri = %s ", psz_full_uri );
+ if( p_new == NULL )
{
- closedir (handle);
-#ifndef HAVE_OPENAT
- free (path);
-#endif
- goto skip;
- }
- sub->parent = current;
- sub->handle = handle;
- sub->filec = vlc_loaddir (handle, &sub->filev, visible, p_sys->compar);
- if (sub->filec < 0)
- sub->filev = NULL;
- sub->i = 0;
-#ifdef HAVE_OPENAT
- sub->device = st.st_dev;
- sub->inode = st.st_ino;
-#else
- 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;
+ free( psz_full_uri );
+ closedir( handle );
+ continue;
}
- /* Add node to XSPF extension */
- char *old_xspf_ext = p_sys->xspf_ext;
- EnsureUTF8 (entry);
- char *title = convert_xml_special_chars (entry);
- if (old_xspf_ext != NULL
- && asprintf (&p_sys->xspf_ext, "%s <vlc:node title=\"%s\">\n",
- old_xspf_ext, title ? title : "?") == -1)
- p_sys->xspf_ext = NULL;
- free (old_xspf_ext);
- free (title);
- goto skip;
- }
+ input_item_CopyOptions( p_current_node->p_item, p_new );
+ input_item_node_t *p_new_node = input_item_node_AppendItem( p_current_node, p_new );
-notdir:
- /* Skip files with ignored extensions */
- if (p_sys->ignored_exts != NULL)
- {
- const char *ext = strrchr (entry, '.');
- if (ext != NULL)
+ /* Handle directory flags and recursion if in EXPAND mode */
+ if( i_res == ENTRY_DIR )
{
- size_t extlen = strlen (++ext);
- for (const char *type = p_sys->ignored_exts, *end;
- type[0]; type = end + 1)
+ input_item_SetBrowsable( p_new, true );
+ if( p_sys->mode == MODE_EXPAND
+ && directory_push( p_sys, handle, psz_full_uri ) )
{
- end = strchr (type, ',');
- if (end == NULL)
- end = type + strlen (type);
-
- if (type + extlen == end
- && !strncasecmp (ext, type, extlen))
- {
- free (entry);
- return NULL;
- }
-
- if (*end == '\0')
- break;
+ p_current_node = p_new_node;
}
}
- }
-
- 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, len);
- if (unlikely(block == NULL))
- 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_GET_CONTENT_TYPE:
- *va_arg( args, char** ) = strdup("application/xspf+xml");
- return VLC_SUCCESS;
-
- default:
- return VLC_EGENERIC;
+ free( psz_full_uri );
+ input_item_Release( p_new );
}
- (void) p_access;
+
return VLC_SUCCESS;
}
diff --git a/modules/access/file.c b/modules/access/file.c
index 6a1bde1..4675706 100644
--- a/modules/access/file.c
+++ b/modules/access/file.c
@@ -200,19 +200,19 @@ int FileOpen( vlc_object_t *p_this )
fcntl (fd, F_SETFL, flags);
#endif
- /* Directories can be opened and read from, but only readdir() knows
- * how to parse the data. The directory plugin will do it. */
+ /* Directories need to be browsed, let's reports the item needs browsing
+ so the directory plugin of the browser can handle it */
if (S_ISDIR (st.st_mode))
{
-#ifdef HAVE_FDOPENDIR
- DIR *handle = fdopendir (fd);
- if (handle == NULL)
- goto error; /* Uh? */
- return DirInit (p_access, handle);
-#else
- msg_Dbg (p_access, "ignoring directory");
- goto error;
-#endif
+ input_item_t *p_item = input_GetItem(p_access->p_input);
+ if (p_item != NULL)
+ {
+ input_item_SetBrowsable(p_item, true);
+ vlc_mutex_lock( &p_item->lock );
+ p_item->i_type = ITEM_TYPE_DIRECTORY;
+ vlc_mutex_unlock( &p_item->lock );
+ }
+ return VLC_EGENERIC;
}
access_sys_t *p_sys = malloc (sizeof (*p_sys));
@@ -266,13 +266,6 @@ error:
void FileClose (vlc_object_t * p_this)
{
access_t *p_access = (access_t*)p_this;
-
- if (p_access->pf_read == NULL)
- {
- DirClose (p_this);
- return;
- }
-
access_sys_t *p_sys = p_access->p_sys;
close (p_sys->fd);
diff --git a/modules/access/fs.c b/modules/access/fs.c
index 175c549..3be83e0 100644
--- a/modules/access/fs.c
+++ b/modules/access/fs.c
@@ -70,7 +70,8 @@ vlc_module_begin ()
add_submodule()
set_section( N_("Directory" ), NULL )
- set_capability( "access", 55 )
+ set_subcategory( SUBCAT_INPUT_ACCESS )
+ set_capability( "access_browser", 50 )
add_string( "recursive", "expand" , RECURSIVE_TEXT,
RECURSIVE_LONGTEXT, false )
change_string_list( psz_recursive_list, psz_recursive_list_text )
@@ -81,7 +82,7 @@ vlc_module_begin ()
#ifndef HAVE_FDOPENDIR
add_shortcut( "file", "directory", "dir" )
#else
- add_shortcut( "directory", "dir" )
+ add_shortcut( "directory", "dir", "file" )
#endif
set_callbacks( DirOpen, DirClose )
vlc_module_end ()
diff --git a/modules/access/fs.h b/modules/access/fs.h
index 5124769..3ac3a70 100644
--- a/modules/access/fs.h
+++ b/modules/access/fs.h
@@ -24,7 +24,4 @@ int FileOpen (vlc_object_t *);
void FileClose (vlc_object_t *);
int DirOpen (vlc_object_t *);
-int DirInit (access_t *p_access, DIR *handle);
-block_t *DirBlock (access_t *);
-int DirControl (access_t *, int, va_list);
void DirClose (vlc_object_t *);
--
1.9.3
More information about the vlc-devel
mailing list