From 424681e5dc7cf19bde9ed5512e4e3a1e51799610 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <rem@videolan.org>
Date: Sat, 25 Nov 2006 14:10:59 +0000
Subject: [PATCH] Skins2 theme loader:  - use utf8_(read|open)dir  - do not
 assume that stat() succeeds (tiny race condition)

---
 modules/gui/skins2/src/theme_loader.cpp | 32 ++++++++++++-------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/modules/gui/skins2/src/theme_loader.cpp b/modules/gui/skins2/src/theme_loader.cpp
index 73655004e66b..6aef3021e78e 100644
--- a/modules/gui/skins2/src/theme_loader.cpp
+++ b/modules/gui/skins2/src/theme_loader.cpp
@@ -278,7 +278,7 @@ bool ThemeLoader::extract( const string &fileName )
 {
     bool result = true;
     char *tmpdir = tempnam( NULL, "vlt" );
-    string tempPath = tmpdir;
+    string tempPath = sFromLocale( tmpdir );
     free( tmpdir );
 
     // Extract the file in a temporary directory
@@ -311,7 +311,7 @@ bool ThemeLoader::extract( const string &fileName )
             list<string>::const_iterator it;
             for( it = resPath.begin(); it != resPath.end(); it++ )
             {
-                if( findFile( sToLocale( *it ), WINAMP2_XML_FILE, xmlFile ) )
+                if( findFile( *it, WINAMP2_XML_FILE, xmlFile ) )
                     break;
             }
         }
@@ -410,10 +410,10 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
     const string &sep = OSFactory::instance( getIntf() )->getDirSeparator();
 
     DIR *pCurrDir;
-    struct dirent *pDirContent;
+    char *pszDirContent;
 
     // Open the dir
-    pCurrDir = opendir( rootDir.c_str() );
+    pCurrDir = utf8_opendir( rootDir.c_str() );
 
     if( pCurrDir == NULL )
     {
@@ -422,22 +422,20 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
         return false;
     }
 
-    // Get the first directory entry
-    pDirContent = (dirent*)readdir( pCurrDir );
-
     // While we still have entries in the directory
-    while( pDirContent != NULL )
+    while( ( pszDirContent = utf8_readdir( pCurrDir ) ) != NULL )
     {
-        string newURI = rootDir + sep + pDirContent->d_name;
+        string newURI = rootDir + sep + pszDirContent;
 
         // Skip . and ..
-        if( string( pDirContent->d_name ) != "." &&
-            string( pDirContent->d_name ) != ".." )
+        if( string( pszDirContent ) != "." &&
+            string( pszDirContent ) != ".." )
         {
 #if defined( S_ISDIR )
             struct stat stat_data;
-            stat( newURI.c_str(), &stat_data );
-            if( S_ISDIR(stat_data.st_mode) )
+
+            if( ( utf8_stat( newURI.c_str(), &stat_data ) == 0 )
+             && S_ISDIR(stat_data.st_mode) )
 #elif defined( DT_DIR )
             if( pDirContent->d_type & DT_DIR )
 #else
@@ -447,6 +445,7 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
                 // Can we find the file in this subdirectory?
                 if( findFile( newURI, rFileName, themeFilePath ) )
                 {
+                    free( pszDirContent );
                     closedir( pCurrDir );
                     return true;
                 }
@@ -454,16 +453,17 @@ bool ThemeLoader::findFile( const string &rootDir, const string &rFileName,
             else
             {
                 // Found the theme file?
-                if( rFileName == string( pDirContent->d_name ) )
+                if( rFileName == string( pszDirContent ) )
                 {
-                    themeFilePath = sFromLocale( newURI );
+                    themeFilePath = newURI;
+                    free( pszDirContent );
                     closedir( pCurrDir );
                     return true;
                 }
             }
         }
 
-        pDirContent = (dirent*)readdir( pCurrDir );
+        free( pszDirContent );
     }
 
     closedir( pCurrDir );
-- 
GitLab