From a9e7a9ce3d303966060a4f8f072afbda57270a82 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 28 Jul 2013 13:16:03 -0700 Subject: [PATCH] Improve performance of File::GetSize(). --- Common/FileUtil.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Common/FileUtil.cpp b/Common/FileUtil.cpp index 7fbe72e64..06dd86b02 100644 --- a/Common/FileUtil.cpp +++ b/Common/FileUtil.cpp @@ -107,6 +107,12 @@ bool Exists(const std::string &filename) return (result == 0); } +// Returns true if stat represents a directory +bool IsDirectory(const struct stat64 &file_info) +{ + return S_ISDIR(file_info.st_mode); +} + // Returns true if filename is a directory bool IsDirectory(const std::string &filename) { @@ -123,7 +129,7 @@ bool IsDirectory(const std::string &filename) return false; } - return S_ISDIR(file_info.st_mode); + return IsDirectory(file_info); } // Deletes a given filename, return true on success @@ -390,28 +396,23 @@ tm GetModifTime(const std::string &filename) // Returns the size of filename (64bit) u64 GetSize(const std::string &filename) { - if (!Exists(filename)) + struct stat64 file_info; + + int result = stat64(filename.c_str(), &file_info); + if (result != 0) { WARN_LOG(COMMON, "GetSize: failed %s: No such file", filename.c_str()); return 0; } - if (IsDirectory(filename)) + if (IsDirectory(file_info)) { WARN_LOG(COMMON, "GetSize: failed %s: is a directory", filename.c_str()); return 0; } - struct stat64 buf; - if (stat64(filename.c_str(), &buf) == 0) - { - DEBUG_LOG(COMMON, "GetSize: %s: %lld", - filename.c_str(), (long long)buf.st_size); - return buf.st_size; - } - ERROR_LOG(COMMON, "GetSize: Stat failed %s: %s", - filename.c_str(), GetLastErrorMsg()); - return 0; + DEBUG_LOG(COMMON, "GetSize: %s: %lld", filename.c_str(), (long long)file_info.st_size); + return file_info.st_size; } // Overloaded GetSize, accepts file descriptor