Commit ee0b9164 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

vlc_readdir: remove string duplication, simplify

parent 7625fc4b
...@@ -46,11 +46,25 @@ VLC_API int vlc_rename( const char *oldpath, const char *newpath ); ...@@ -46,11 +46,25 @@ VLC_API int vlc_rename( const char *oldpath, const char *newpath );
VLC_API char *vlc_getcwd( void ) VLC_USED; VLC_API char *vlc_getcwd( void ) VLC_USED;
#if defined( _WIN32 ) #if defined( _WIN32 )
typedef struct vlc_DIR
{
_WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
char *entry;
union
{
DWORD drives;
bool insert_dot_dot;
} u;
} vlc_DIR;
static inline int vlc_closedir( DIR *dir ) static inline int vlc_closedir( DIR *dir )
{ {
_WDIR *wdir = *(_WDIR **)dir; vlc_DIR *vdir = (vlc_DIR *)dir;
free( dir ); _WDIR *wdir = vdir->wdir;
return wdir ? _wclosedir( wdir ) : 0;
free( vdir->entry );
free( vdir );
return (wdir != NULL) ? _wclosedir( wdir ) : 0;
} }
# undef closedir # undef closedir
# define closedir vlc_closedir # define closedir vlc_closedir
......
...@@ -271,20 +271,17 @@ static int ScanDvbSNextFast( scan_t *p_scan, scan_configuration_t *p_cfg, double ...@@ -271,20 +271,17 @@ static int ScanDvbSNextFast( scan_t *p_scan, scan_configuration_t *p_cfg, double
/* find the requested file in the directory */ /* find the requested file in the directory */
for( ; ; ) { for( ; ; ) {
char *psz_filename; const char *psz_filename = vlc_readdir( p_dir );
if( ! (psz_filename = vlc_readdir( p_dir ) ) ) if( psz_filename == NULL )
break; break;
if( !strncmp( p_scan->parameter.sat_info.psz_name, psz_filename, 20 ) ) if( !strncmp( p_scan->parameter.sat_info.psz_name, psz_filename, 20 ) )
{ {
if( asprintf( &p_scan->parameter.sat_info.psz_path, "%s" DIR_SEP "%s", psz_dir, psz_filename ) == -1 ) if( asprintf( &p_scan->parameter.sat_info.psz_path, "%s" DIR_SEP "%s", psz_dir, psz_filename ) == -1 )
p_scan->parameter.sat_info.psz_path = NULL; p_scan->parameter.sat_info.psz_path = NULL;
free( psz_filename );
break; break;
} }
free( psz_filename );
} }
closedir( p_dir ); closedir( p_dir );
......
...@@ -166,7 +166,7 @@ static int Open( vlc_object_t * p_this ) ...@@ -166,7 +166,7 @@ static int Open( vlc_object_t * p_this )
if (p_src_dir != NULL) if (p_src_dir != NULL)
{ {
char *psz_file; const char *psz_file;
while ((psz_file = vlc_readdir(p_src_dir)) != NULL) while ((psz_file = vlc_readdir(p_src_dir)) != NULL)
{ {
if (strlen(psz_file) > 4) if (strlen(psz_file) > 4)
...@@ -179,7 +179,6 @@ static int Open( vlc_object_t * p_this ) ...@@ -179,7 +179,6 @@ static int Open( vlc_object_t * p_this )
if (!s_filename.compare(p_demux->psz_file)) if (!s_filename.compare(p_demux->psz_file))
#endif #endif
{ {
free (psz_file);
continue; // don't reuse the original opened file continue; // don't reuse the original opened file
} }
...@@ -229,7 +228,6 @@ static int Open( vlc_object_t * p_this ) ...@@ -229,7 +228,6 @@ static int Open( vlc_object_t * p_this )
} }
} }
} }
free (psz_file);
} }
closedir( p_src_dir ); closedir( p_src_dir );
} }
......
...@@ -287,23 +287,20 @@ static void ReadDir(intf_thread_t *intf) ...@@ -287,23 +287,20 @@ static void ReadDir(intf_thread_t *intf)
DirsDestroy(sys); DirsDestroy(sys);
char *entry; const char *entry;
while ((entry = vlc_readdir(current_dir))) { while ((entry = vlc_readdir(current_dir))) {
if (!sys->show_hidden_files && *entry == '.' && strcmp(entry, "..")) if (!sys->show_hidden_files && *entry == '.' && strcmp(entry, ".."))
goto next; continue;
struct dir_entry_t *dir_entry = malloc(sizeof *dir_entry); struct dir_entry_t *dir_entry = malloc(sizeof *dir_entry);
if (!dir_entry) if (unlikely(dir_entry == NULL))
goto next; continue;
dir_entry->file = IsFile(sys->current_dir, entry); dir_entry->file = IsFile(sys->current_dir, entry);
dir_entry->path = entry; dir_entry->path = xstrdup(entry);
INSERT_ELEM(sys->dir_entries, sys->n_dir_entries, INSERT_ELEM(sys->dir_entries, sys->n_dir_entries,
sys->n_dir_entries, dir_entry); sys->n_dir_entries, dir_entry);
continue; continue;
next:
free(entry);
} }
qsort(sys->dir_entries, sys->n_dir_entries, qsort(sys->dir_entries, sys->n_dir_entries,
......
...@@ -392,11 +392,10 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName, ...@@ -392,11 +392,10 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
// Path separator // Path separator
const string &sep = OSFactory::instance( getIntf() )->getDirSeparator(); const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
DIR *pCurrDir; const char *pszDirContent;
char *pszDirContent;
// Open the dir // Open the dir
pCurrDir = vlc_opendir( rootDir.c_str() ); DIR *pCurrDir = vlc_opendir( rootDir.c_str() );
if( pCurrDir == NULL ) if( pCurrDir == NULL )
{ {
...@@ -428,7 +427,6 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName, ...@@ -428,7 +427,6 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
// Can we find the file in this subdirectory? // Can we find the file in this subdirectory?
if( findFile( newURI, rFileName, themeFilePath ) ) if( findFile( newURI, rFileName, themeFilePath ) )
{ {
free( pszDirContent );
closedir( pCurrDir ); closedir( pCurrDir );
return true; return true;
} }
...@@ -439,14 +437,11 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName, ...@@ -439,14 +437,11 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
if( rFileName == string( pszDirContent ) ) if( rFileName == string( pszDirContent ) )
{ {
themeFilePath = newURI; themeFilePath = newURI;
free( pszDirContent );
closedir( pCurrDir ); closedir( pCurrDir );
return true; return true;
} }
} }
} }
free( pszDirContent );
} }
closedir( pCurrDir ); closedir( pCurrDir );
......
...@@ -136,15 +136,14 @@ ThemeRepository::~ThemeRepository() ...@@ -136,15 +136,14 @@ ThemeRepository::~ThemeRepository()
void ThemeRepository::parseDirectory( const string &rDir_locale ) void ThemeRepository::parseDirectory( const string &rDir_locale )
{ {
DIR *pDir; const char *pszDirContent;
char *pszDirContent;
// Path separator // Path separator
const string &sep = OSFactory::instance( getIntf() )->getDirSeparator(); const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
// Open the dir // Open the dir
// FIXME: parseDirectory should be invoked with UTF-8 input instead!! // FIXME: parseDirectory should be invoked with UTF-8 input instead!!
string rDir = sFromLocale( rDir_locale ); string rDir = sFromLocale( rDir_locale );
pDir = vlc_opendir( rDir.c_str() ); DIR *pDir = vlc_opendir( rDir.c_str() );
if( pDir == NULL ) if( pDir == NULL )
{ {
...@@ -174,8 +173,6 @@ void ThemeRepository::parseDirectory( const string &rDir_locale ) ...@@ -174,8 +173,6 @@ void ThemeRepository::parseDirectory( const string &rDir_locale )
msg_Dbg( getIntf(), "found skin %s", path.c_str() ); msg_Dbg( getIntf(), "found skin %s", path.c_str() );
} }
free( pszDirContent );
} }
closedir( pDir ); closedir( pDir );
......
...@@ -477,12 +477,11 @@ static int vlclua_opendir( lua_State *L ) ...@@ -477,12 +477,11 @@ static int vlclua_opendir( lua_State *L )
lua_newtable( L ); lua_newtable( L );
for( ;; ) for( ;; )
{ {
char *psz_filename = vlc_readdir( p_dir ); const char *psz_filename = vlc_readdir( p_dir );
if( !psz_filename ) break; if( !psz_filename ) break;
i++; i++;
lua_pushstring( L, psz_filename ); lua_pushstring( L, psz_filename );
lua_rawseti( L, -2, i ); lua_rawseti( L, -2, i );
free( psz_filename );
} }
closedir( p_dir ); closedir( p_dir );
return 1; return 1;
......
...@@ -312,14 +312,11 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path, ...@@ -312,14 +312,11 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
msg_Dbg( p_this, "looking for a subtitle file in %s", psz_dir ); msg_Dbg( p_this, "looking for a subtitle file in %s", psz_dir );
char *psz_name; const char *psz_name;
while( (psz_name = vlc_readdir( dir )) && i_sub_count < MAX_SUBTITLE_FILES ) while( (psz_name = vlc_readdir( dir )) && i_sub_count < MAX_SUBTITLE_FILES )
{ {
if( psz_name[0] == '.' || !subtitles_Filter( psz_name ) ) if( psz_name[0] == '.' || !subtitles_Filter( psz_name ) )
{
free( psz_name );
continue; continue;
}
char tmp_fname_noext[strlen( psz_name ) + 1]; char tmp_fname_noext[strlen( psz_name ) + 1];
char tmp_fname_trim[strlen( psz_name ) + 1]; char tmp_fname_trim[strlen( psz_name ) + 1];
...@@ -365,10 +362,7 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path, ...@@ -365,10 +362,7 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
sprintf( psz_path, "%s"DIR_SEP"%s", psz_dir, psz_name ); sprintf( psz_path, "%s"DIR_SEP"%s", psz_dir, psz_name );
if( !strcmp( psz_path, psz_fname ) ) if( !strcmp( psz_path, psz_fname ) )
{
free( psz_name );
continue; continue;
}
if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) && result ) if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) && result )
{ {
...@@ -386,7 +380,6 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path, ...@@ -386,7 +380,6 @@ char **subtitles_Detect( input_thread_t *p_this, char *psz_path,
psz_path, i_prio ); psz_path, i_prio );
} }
} }
free( psz_name );
} }
closedir( dir ); closedir( dir );
} }
......
...@@ -444,7 +444,7 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth, ...@@ -444,7 +444,7 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
/* Skip ".", ".." */ /* Skip ".", ".." */
if (!strcmp (file, ".") || !strcmp (file, "..")) if (!strcmp (file, ".") || !strcmp (file, ".."))
goto skip; continue;
/* Compute path relative to plug-in base directory */ /* Compute path relative to plug-in base directory */
if (reldir != NULL) if (reldir != NULL)
...@@ -455,7 +455,7 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth, ...@@ -455,7 +455,7 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
else else
relpath = strdup (file); relpath = strdup (file);
if (unlikely(relpath == NULL)) if (unlikely(relpath == NULL))
goto skip; continue;
/* Compute absolute path */ /* Compute absolute path */
if (asprintf (&abspath, "%s"DIR_SEP"%s", bank->base, relpath) == -1) if (asprintf (&abspath, "%s"DIR_SEP"%s", bank->base, relpath) == -1)
...@@ -493,7 +493,6 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth, ...@@ -493,7 +493,6 @@ static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
skip: skip:
free (relpath); free (relpath);
free (abspath); free (abspath);
free (file);
} }
closedir (dh); closedir (dh);
} }
......
...@@ -173,7 +173,7 @@ int playlist_FindArtInCache( input_item_t *p_item ) ...@@ -173,7 +173,7 @@ int playlist_FindArtInCache( input_item_t *p_item )
} }
bool b_found = false; bool b_found = false;
char *psz_filename; const char *psz_filename;
while( !b_found && (psz_filename = vlc_readdir( p_dir )) ) while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
{ {
if( !strncmp( psz_filename, "art", 3 ) ) if( !strncmp( psz_filename, "art", 3 ) )
...@@ -193,7 +193,6 @@ int playlist_FindArtInCache( input_item_t *p_item ) ...@@ -193,7 +193,6 @@ int playlist_FindArtInCache( input_item_t *p_item )
b_found = true; b_found = true;
} }
free( psz_filename );
} }
/* */ /* */
......
...@@ -145,14 +145,15 @@ DIR *vlc_opendir (const char *dirname) ...@@ -145,14 +145,15 @@ DIR *vlc_opendir (const char *dirname)
* @param dir directory handle as returned by vlc_opendir() * @param dir directory handle as returned by vlc_opendir()
* (must not be used by another thread concurrently) * (must not be used by another thread concurrently)
* *
* @return a UTF-8 string of the directory entry. Use free() to release it. * @return a UTF-8 string of the directory entry. The string is valid until
* the next call to vlc_readdir() or closedir() on the handle.
* If there are no more entries in the directory, NULL is returned. * If there are no more entries in the directory, NULL is returned.
* If an error occurs, errno is set and NULL is returned. * If an error occurs, errno is set and NULL is returned.
*/ */
char *vlc_readdir( DIR *dir ) char *vlc_readdir( DIR *dir )
{ {
struct dirent *ent = readdir (dir); struct dirent *ent = readdir (dir);
return (ent != NULL) ? strdup (ent->d_name) : NULL; return (ent != NULL) ? ent->d_name : NULL;
} }
/** /**
......
...@@ -127,7 +127,7 @@ int vlc_loaddir( DIR *dir, char ***namelist, ...@@ -127,7 +127,7 @@ int vlc_loaddir( DIR *dir, char ***namelist,
for (unsigned size = 0;;) for (unsigned size = 0;;)
{ {
errno = 0; errno = 0;
char *entry = vlc_readdir (dir); const char *entry = vlc_readdir (dir);
if (entry == NULL) if (entry == NULL)
{ {
if (errno) if (errno)
...@@ -136,10 +136,7 @@ int vlc_loaddir( DIR *dir, char ***namelist, ...@@ -136,10 +136,7 @@ int vlc_loaddir( DIR *dir, char ***namelist,
} }
if (!select (entry)) if (!select (entry))
{
free (entry);
continue; continue;
}
if (num >= size) if (num >= size)
{ {
...@@ -147,14 +144,13 @@ int vlc_loaddir( DIR *dir, char ***namelist, ...@@ -147,14 +144,13 @@ int vlc_loaddir( DIR *dir, char ***namelist,
char **newtab = realloc (tab, sizeof (*tab) * (size)); char **newtab = realloc (tab, sizeof (*tab) * (size));
if (unlikely(newtab == NULL)) if (unlikely(newtab == NULL))
{
free (entry);
goto error; goto error;
}
tab = newtab; tab = newtab;
} }
tab[num++] = entry; tab[num] = strdup(entry);
if (likely(tab[num] != NULL))
num++;
} }
if (compar != NULL) if (compar != NULL)
......
...@@ -129,17 +129,6 @@ char *vlc_getcwd (void) ...@@ -129,17 +129,6 @@ char *vlc_getcwd (void)
/* Under Windows, these wrappers return the list of drive letters /* Under Windows, these wrappers return the list of drive letters
* when called with an empty argument or just '\'. */ * when called with an empty argument or just '\'. */
typedef struct vlc_DIR
{
_WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
union
{
DWORD drives;
bool insert_dot_dot;
} u;
} vlc_DIR;
DIR *vlc_opendir (const char *dirname) DIR *vlc_opendir (const char *dirname)
{ {
wchar_t *wpath = widen_path (dirname); wchar_t *wpath = widen_path (dirname);
...@@ -175,6 +164,7 @@ DIR *vlc_opendir (const char *dirname) ...@@ -175,6 +164,7 @@ DIR *vlc_opendir (const char *dirname)
return NULL; return NULL;
} }
p_dir->wdir = wdir; p_dir->wdir = wdir;
p_dir->entry = NULL;
return (void *)p_dir; return (void *)p_dir;
} }
...@@ -182,6 +172,8 @@ char *vlc_readdir (DIR *dir) ...@@ -182,6 +172,8 @@ char *vlc_readdir (DIR *dir)
{ {
vlc_DIR *p_dir = (vlc_DIR *)dir; vlc_DIR *p_dir = (vlc_DIR *)dir;
free(p_dir->entry);
#if !VLC_WINSTORE_APP #if !VLC_WINSTORE_APP
/* Drive letters mode */ /* Drive letters mode */
if (p_dir->wdir == NULL) if (p_dir->wdir == NULL)
...@@ -196,24 +188,23 @@ char *vlc_readdir (DIR *dir) ...@@ -196,24 +188,23 @@ char *vlc_readdir (DIR *dir)
p_dir->u.drives &= ~(1UL << i); p_dir->u.drives &= ~(1UL << i);
assert (i < 26); assert (i < 26);
char *ret; if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
if (asprintf (&ret, "%c:\\", 'A' + i) == -1) p_dir->entry = NULL;
return NULL;
return ret;
} }
else
#endif #endif
if (p_dir->u.insert_dot_dot) if (p_dir->u.insert_dot_dot)
{ {
/* Adds "..", gruik! */ /* Adds "..", gruik! */
p_dir->u.insert_dot_dot = false; p_dir->u.insert_dot_dot = false;
return strdup (".."); p_dir->entry = strdup ("..");
} }
else
struct _wdirent *ent = _wreaddir (p_dir->wdir); {
if (ent == NULL) struct _wdirent *ent = _wreaddir (p_dir->wdir);
return NULL; p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
return FromWide (ent->d_name); }
return p_dir->entry;
} }
int vlc_stat (const char *filename, struct stat *buf) int vlc_stat (const char *filename, struct stat *buf)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment