]> git.sesse.net Git - vlc/blobdiff - modules/access/directory.c
Encode non-ASCII characters outside comments
[vlc] / modules / access / directory.c
index 28596614e139449a0dbfd7d47896a31b6ebb4709..f600040d67e26fcdf0ad2eecadae03e4f506aa54 100644 (file)
@@ -18,7 +18,7 @@
  *
  * 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.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -94,10 +94,8 @@ vlc_module_begin();
     add_string( "recursive", "expand" , NULL, RECURSIVE_TEXT,
                 RECURSIVE_LONGTEXT, VLC_FALSE );
       change_string_list( psz_recursive_list, psz_recursive_list_text, 0 );
-#ifdef HAVE_STRSEP
     add_string( "ignore-filetypes", "m3u,db,nfo,jpg,gif,sfv,txt,sub,idx,srt,cue",
                 NULL, IGNORE_TEXT, IGNORE_LONGTEXT, VLC_FALSE );
-#endif
     set_callbacks( Open, Close );
 
     add_submodule();
@@ -124,7 +122,7 @@ static int Demux( demux_t *p_demux );
 static int DemuxControl( demux_t *p_demux, int i_query, va_list args );
 
 
-static int ReadDir( playlist_t *, char *psz_name, int i_mode, int *pi_pos,
+static int ReadDir( playlist_t *, const char *psz_name, int i_mode,
                     playlist_item_t * );
 
 /*****************************************************************************
@@ -282,8 +280,7 @@ static int Read( access_t *p_access, uint8_t *p_buffer, int i_len)
     }
 
     p_item->input.i_type = ITEM_TYPE_DIRECTORY;
-    if( ReadDir( p_playlist, psz_name , i_mode, &i_pos,
-                 p_item ) != VLC_SUCCESS )
+    if( ReadDir( p_playlist, psz_name , i_mode, p_item ) != VLC_SUCCESS )
     {
     }
 end:
@@ -371,6 +368,7 @@ static int Demux( demux_t *p_demux )
 {
     return 0;
 }
+
 /*****************************************************************************
  * DemuxControl:
  *****************************************************************************/
@@ -383,38 +381,50 @@ static int Filter( const struct dirent *foo )
 {
     return VLC_TRUE;
 }
+
 /*****************************************************************************
  * ReadDir: read a directory and add its content to the list
  *****************************************************************************/
-static int ReadDir( playlist_t *p_playlist,
-                    char *psz_name, int i_mode, int *pi_position,
-                    playlist_item_t *p_parent )
+static int ReadDir( playlist_t *p_playlist, const char *psz_name,
+                    int i_mode, playlist_item_t *p_parent )
 {
     struct dirent   **pp_dir_content;
     int             i_dir_content, i;
     playlist_item_t *p_node;
 
     /* Build array with ignores */
-#ifdef HAVE_STRSEP
     char **ppsz_extensions = 0;
     int i_extensions = 0;
     char *psz_ignore = var_CreateGetString( p_playlist, "ignore-filetypes" );
     if( psz_ignore && *psz_ignore )
     {
-        char *psz_backup;
-        char *psz_parser = psz_backup = strdup( psz_ignore );
-        int a = 0;
+        char *psz_parser = psz_ignore;
+        int a;
 
-        while( strsep( &psz_parser, "," ) ) i_extensions++;
-        free( psz_backup );
+        for( a = 0; psz_parser[a] != '\0'; a++ )
+        {
+            if( psz_parser[a] == ',' ) i_extensions++;
+        }
 
         ppsz_extensions = (char **)malloc( sizeof( char * ) * i_extensions );
 
-        psz_parser = psz_ignore;
-        while( a < i_extensions &&
-               ( ppsz_extensions[a++] = strsep( &psz_parser, "," ) ) );
+        for( a = 0; a < i_extensions; a++ )
+        {
+            int b;
+            char *tmp;
+
+            while( psz_parser[0] != '\0' && psz_parser[0] == ' ' ) psz_parser++;
+            for( b = 0; psz_parser[b] != '\0'; b++ )
+            {
+                if( psz_parser[b] == ',' ) break;
+            }
+            tmp = malloc( b + 1 );
+            strncpy( tmp, psz_parser, b );
+            tmp[b] = 0;
+            ppsz_extensions[a] = tmp;
+            psz_parser += b+1;
+        }
     }
-#endif /* HAVE_STRSEP */
 
     /* Change the item to a node */
     if( p_parent->i_children == -1 )
@@ -459,6 +469,21 @@ static int ReadDir( playlist_t *p_playlist,
             if( 0 )
 #endif
             {
+#if defined( S_ISLNK )
+/*
+ * FIXME: there is a ToCToU race condition here; but it is rather tricky
+ * impossible to fix while keeping some kind of portable code, and maybe even
+ * in a non-portable way.
+ */
+                if( lstat( psz_uri, &stat_data )
+                 || S_ISLNK(stat_data.st_mode) )
+                {
+                    msg_Dbg( p_playlist, "Skipping directory symlink %s",
+                             psz_uri );
+                    free( psz_uri );
+                    continue;
+                }
+#endif
                 if( i_mode == MODE_NONE )
                 {
                     msg_Dbg( p_playlist, "Skipping subdirectory %s", psz_uri );
@@ -484,7 +509,7 @@ static int ReadDir( playlist_t *p_playlist,
                     p_node->input.i_type = ITEM_TYPE_DIRECTORY;
 
                     if( ReadDir( p_playlist, psz_uri , MODE_EXPAND,
-                                 pi_position, p_node ) != VLC_SUCCESS )
+                                 p_node ) != VLC_SUCCESS )
                     {
                         return VLC_EGENERIC;
                     }
@@ -498,7 +523,6 @@ static int ReadDir( playlist_t *p_playlist,
                 playlist_item_t *p_item;
                 char *psz_tmp1, *psz_tmp2, *psz_loc;
 
-#ifdef HAVE_STRSEP
                 if( i_extensions > 0 )
                 {
                     char *psz_dot = strrchr( p_dir_content->d_name, '.' );
@@ -518,7 +542,6 @@ static int ReadDir( playlist_t *p_playlist,
                         }
                     }
                 }
-#endif /* HAVE_STRSEP */
 
                 psz_loc = FromLocale( psz_uri );
                 psz_tmp1 = vlc_fix_readdir_charset( VLC_OBJECT(p_playlist),
@@ -528,7 +551,6 @@ static int ReadDir( playlist_t *p_playlist,
                 psz_loc = FromLocale( p_dir_content->d_name );
                 psz_tmp2 = vlc_fix_readdir_charset( VLC_OBJECT(p_playlist),
                                                     psz_loc );
-                msg_Err( p_playlist, "adding file %s from %s", psz_loc, p_dir_content->d_name );
                 LocaleFree( psz_loc );
 
                 p_item = playlist_ItemNewWithType( VLC_OBJECT(p_playlist),
@@ -536,7 +558,8 @@ static int ReadDir( playlist_t *p_playlist,
                 playlist_NodeAddItem( p_playlist,p_item,
                                       p_parent->pp_parents[0]->i_view,
                                       p_parent,
-                                      PLAYLIST_APPEND, PLAYLIST_END );
+                                      PLAYLIST_APPEND | PLAYLIST_PREPARSE,
+                                      PLAYLIST_END );
 
                 playlist_CopyParents( p_parent, p_item );
             }
@@ -544,11 +567,16 @@ static int ReadDir( playlist_t *p_playlist,
         free( psz_uri );
     }
 
-#ifdef HAVE_STRSEP
+    for( i = 0; i < i_extensions; i++ )
+    {
+        if( ppsz_extensions[i] )
+            free( ppsz_extensions[i] );
+    }
     if( ppsz_extensions ) free( ppsz_extensions );
     if( psz_ignore ) free( psz_ignore );
-#endif /* HAVE_STRSEP */
 
-    free( pp_dir_content );
+    for( i = 0; i < i_dir_content; i++ )
+        if( pp_dir_content[i] ) free( pp_dir_content[i] );
+    if( pp_dir_content ) free( pp_dir_content );
     return VLC_SUCCESS;
 }