From aa63199977a21fcb78c371b025166ff7affc9e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Fri, 16 Jun 2017 12:31:26 +0200 Subject: [PATCH 1/2] FileUtil: Don't manually strip trailing slashes POSIX allows one or more trailing slashes for directories. From POSIX.1-2008, section 3.271 (Base Definitions / Pathname): > A pathname can optionally contain one or more trailing > characters. Multiple successive characters are considered to > be the same as one , except for the case of exactly two > leading characters. On Windows, the extra trailing slashes are ignored for directories too. --- Source/Core/Common/FileUtil.cpp | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 0a1a425830..3c6c6449d3 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -51,29 +51,15 @@ // REMEMBER: strdup considered harmful! namespace File { -// Remove any ending forward slashes from directory paths -// Modifies argument. -static void StripTailDirSlashes(std::string& fname) -{ - if (fname.length() > 1) - { - while (fname.back() == DIR_SEP_CHR) - fname.pop_back(); - } -} - // Returns true if file filename exists bool Exists(const std::string& filename) { struct stat file_info; - std::string copy(filename); - StripTailDirSlashes(copy); - #ifdef _WIN32 - int result = _tstat64(UTF8ToTStr(copy).c_str(), &file_info); + int result = _tstat64(UTF8ToTStr(filename).c_str(), &file_info); #else - int result = stat(copy.c_str(), &file_info); + int result = stat(filename.c_str(), &file_info); #endif return (result == 0); @@ -84,13 +70,10 @@ bool IsDirectory(const std::string& filename) { struct stat file_info; - std::string copy(filename); - StripTailDirSlashes(copy); - #ifdef _WIN32 - int result = _tstat64(UTF8ToTStr(copy).c_str(), &file_info); + int result = _tstat64(UTF8ToTStr(filename).c_str(), &file_info); #else - int result = stat(copy.c_str(), &file_info); + int result = stat(filename.c_str(), &file_info); #endif if (result < 0) From 01faa5c852e218befdeed3b041ddf03688201e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Fri, 16 Jun 2017 12:43:41 +0200 Subject: [PATCH 2/2] FileUtil: Use errno for printing errors in IsDirectory stat() returns an error code in errno on both POSIX compliant platforms and Windows. This means we should always use errno instead of GetLastErrorMsg which uses GetLastError() (Win32) on Windows. --- Source/Core/Common/FileUtil.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 3c6c6449d3..597908f740 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -78,8 +78,7 @@ bool IsDirectory(const std::string& filename) if (result < 0) { - WARN_LOG(COMMON, "IsDirectory: stat failed on %s: %s", filename.c_str(), - GetLastErrorMsg().c_str()); + WARN_LOG(COMMON, "IsDirectory: stat failed on %s: %s", filename.c_str(), strerror(errno)); return false; }