]> git.sesse.net Git - vlc/blobdiff - modules/access/directory.c
* modules/access/dvb/linux_dvb.c: Disabled FE_CAN_CLEAN_SETUP check because
[vlc] / modules / access / directory.c
index 091ad551c29726d03e099a7ec845f2a77d90b616..642b38b451e86c0cbaa8b63f7c866613e91f0a53 100644 (file)
@@ -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),
@@ -543,11 +566,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;
 }