From d6392c33ad1a87e2498859977fe21e8f47bf29a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Tue, 10 Apr 2018 16:12:15 +0200 Subject: [PATCH] Core/Directory: Fix GetResultSize and IsResultDirectory methods on Posix --- ChangeLog.md | 1 + src/Nazara/Core/Posix/DirectoryImpl.cpp | 36 ++++++++++++++++++------- src/Nazara/Core/Posix/DirectoryImpl.hpp | 1 + 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index fc9d90771..e5bb304b7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -82,6 +82,7 @@ Nazara Engine: - Fix copy and move semantic on HandledObject and ObjectHandle - Add support for emissive and normal maps in .mtl loader using custom keywords ([map_]emissive and [map_]normal) - Music, Sound and SoundEmitter are now movable +- Fixed Directory::GetResultSize and Directory::IsResultDirectory on Posix systems Nazara Development Kit: - Added ImageWidget (#139) diff --git a/src/Nazara/Core/Posix/DirectoryImpl.cpp b/src/Nazara/Core/Posix/DirectoryImpl.cpp index e52328def..c1ba495a2 100644 --- a/src/Nazara/Core/Posix/DirectoryImpl.cpp +++ b/src/Nazara/Core/Posix/DirectoryImpl.cpp @@ -3,8 +3,11 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include +#include #include +#include #include #include #include @@ -13,9 +16,9 @@ namespace Nz { - DirectoryImpl::DirectoryImpl(const Directory* parent) + DirectoryImpl::DirectoryImpl(const Directory* parent) : + m_parent(parent) { - NazaraUnused(parent); } void DirectoryImpl::Close() @@ -30,19 +33,32 @@ namespace Nz UInt64 DirectoryImpl::GetResultSize() const { - struct stat64 resulststat; - stat64(m_result->d_name, &resulststat); + if (S_ISREG(m_result->d_type)) + { + String path = m_parent->GetPath(); + std::size_t pathSize = path.GetSize(); - return static_cast(resulststat.st_size); + std::size_t resultNameSize = std::strlen(m_result->d_name); + + std::size_t fullNameSize = pathSize + 1 + resultNameSize; + StackArray fullName = NazaraStackAllocationNoInit(char, fullNameSize + 1); + std::memcpy(&fullName[0], path.GetConstBuffer(), pathSize * sizeof(char)); + fullName[pathSize] = '/'; + std::memcpy(&fullName[pathSize + 1], m_result->d_name, resultNameSize * sizeof(char)); + fullName[fullNameSize] = '\0'; + + struct stat64 results; + stat64(fullName.data(), &results); + + return results.st_size; + } + else + return 0; } bool DirectoryImpl::IsResultDirectory() const { - struct stat64 filestats; - if (stat64(m_result->d_name, &filestats) == -1) // error - return false; - - return S_ISDIR(filestats.st_mode); + return S_ISDIR(m_result->d_type); } bool DirectoryImpl::NextResult() diff --git a/src/Nazara/Core/Posix/DirectoryImpl.hpp b/src/Nazara/Core/Posix/DirectoryImpl.hpp index 50d1b96aa..907947371 100644 --- a/src/Nazara/Core/Posix/DirectoryImpl.hpp +++ b/src/Nazara/Core/Posix/DirectoryImpl.hpp @@ -43,6 +43,7 @@ namespace Nz static bool Remove(const String& dirPath); private: + const Directory* m_parent; DIR* m_handle; dirent64* m_result; };