From 00eeec35b428944436dc4a934509105dfa4a52de Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Thu, 3 Jul 2025 19:41:25 +0300 Subject: [PATCH] _fixes --- .../Engine/Platform/Apple/AppleFileSystem.cpp | 9 +- .../Engine/Platform/Linux/LinuxFileSystem.cpp | 10 +- .../Engine/Platform/Unix/UnixFileSystem.cpp | 117 ++++-------------- Source/Engine/Platform/Unix/UnixFileSystem.h | 1 - 4 files changed, 25 insertions(+), 112 deletions(-) diff --git a/Source/Engine/Platform/Apple/AppleFileSystem.cpp b/Source/Engine/Platform/Apple/AppleFileSystem.cpp index 5cccbe3d8..450507009 100644 --- a/Source/Engine/Platform/Apple/AppleFileSystem.cpp +++ b/Source/Engine/Platform/Apple/AppleFileSystem.cpp @@ -7,10 +7,6 @@ #include "Engine/Platform/File.h" #include "Engine/Core/Types/String.h" #include "Engine/Core/Types/StringView.h" -#include "Engine/Core/Types/TimeSpan.h" -#include "Engine/Core/Collections/Array.h" -#include "Engine/Core/Math/Math.h" -#include "Engine/Core/Log.h" #include "Engine/Utilities/StringConverter.h" #include #include @@ -18,7 +14,6 @@ #include #include #include -#include #include #include @@ -72,8 +67,8 @@ bool AppleFileSystem::CopyFile(const StringView& dst, const StringView& src) return false; } - out_error: - cachedError = errno; +out_error: + cachedError = errno; close(srcFile); if (dstFile >= 0) close(dstFile); diff --git a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp index 4452068c7..9b31dc28f 100644 --- a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp +++ b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp @@ -4,22 +4,16 @@ #include "LinuxFileSystem.h" #include "Engine/Platform/File.h" -#include "Engine/Platform/StringUtils.h" -#include "Engine/Core/Types/String.h" #include "Engine/Core/Types/StringBuilder.h" -#include "Engine/Core/Types/StringView.h" -#include "Engine/Core/Types/TimeSpan.h" #include "Engine/Core/Collections/Array.h" #include "Engine/Core/Math/Math.h" #include "Engine/Core/Log.h" #include "Engine/Utilities/StringConverter.h" -#include #include #include #include #include #include -#include #include #include #include @@ -228,8 +222,8 @@ bool LinuxFileSystem::CopyFile(const StringView& dst, const StringView& src) return false; } - out_error: - cachedError = errno; +out_error: + cachedError = errno; close(srcFile); if (dstFile >= 0) close(dstFile); diff --git a/Source/Engine/Platform/Unix/UnixFileSystem.cpp b/Source/Engine/Platform/Unix/UnixFileSystem.cpp index 9242ccd89..1fb47f0f5 100644 --- a/Source/Engine/Platform/Unix/UnixFileSystem.cpp +++ b/Source/Engine/Platform/Unix/UnixFileSystem.cpp @@ -4,31 +4,28 @@ #include "UnixFileSystem.h" #include "Engine/Platform/File.h" -#include "Engine/Platform/StringUtils.h" -#include "Engine/Core/Types/String.h" -#include "Engine/Core/Types/StringBuilder.h" -#include "Engine/Core/Types/StringView.h" #include "Engine/Core/Types/TimeSpan.h" #include "Engine/Core/Collections/Array.h" #include "Engine/Core/Math/Math.h" #include "Engine/Core/Log.h" #include "Engine/Utilities/StringConverter.h" -#include #include -#include #include #include #include #include -#include -#include -#include + +#if PLATFORM_MAC || PLATFORM_IOS +typedef StringAsANSI<> UnixString; +#else +typedef StringAsUTF8<> UnixString; +#endif const DateTime UnixEpoch(1970, 1, 1); bool UnixFileSystem::CreateDirectory(const StringView& path) { - const StringAsUTF8<> pathAnsi(*path, path.Length()); + const UnixString pathAnsi(*path, path.Length()); // Skip if already exists struct stat fileInfo; @@ -119,7 +116,7 @@ bool DeleteUnixPathTree(const char* path) bool UnixFileSystem::DeleteDirectory(const String& path, bool deleteContents) { - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); if (deleteContents) { return DeleteUnixPathTree(pathANSI.Get()); @@ -133,7 +130,7 @@ bool UnixFileSystem::DeleteDirectory(const String& path, bool deleteContents) bool UnixFileSystem::DirectoryExists(const StringView& path) { struct stat fileInfo; - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); if (stat(pathANSI.Get(), &fileInfo) != -1) { return S_ISDIR(fileInfo.st_mode); @@ -143,8 +140,8 @@ bool UnixFileSystem::DirectoryExists(const StringView& path) bool UnixFileSystem::DirectoryGetFiles(Array& results, const String& path, const Char* searchPattern, DirectorySearchOption option) { - const StringAsUTF8<> pathANSI(*path, path.Length()); - const StringAsUTF8<> searchPatternANSI(searchPattern); + const UnixString pathANSI(*path, path.Length()); + const UnixString searchPatternANSI(searchPattern); // Check if use only top directory if (option == DirectorySearchOption::TopDirectoryOnly) @@ -158,7 +155,7 @@ bool UnixFileSystem::GetChildDirectories(Array& results, const String& p DIR* dir; struct stat statPath, statEntry; struct dirent* entry; - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); const char* pathStr = pathANSI.Get(); // Stat for the path @@ -214,7 +211,7 @@ bool UnixFileSystem::GetChildDirectories(Array& results, const String& p bool UnixFileSystem::FileExists(const StringView& path) { struct stat fileInfo; - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); if (stat(pathANSI.Get(), &fileInfo) != -1) { return S_ISREG(fileInfo.st_mode); @@ -224,7 +221,7 @@ bool UnixFileSystem::FileExists(const StringView& path) bool UnixFileSystem::DeleteFile(const StringView& path) { - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); return unlink(pathANSI.Get()) != 0; } @@ -232,7 +229,7 @@ uint64 UnixFileSystem::GetFileSize(const StringView& path) { struct stat fileInfo; fileInfo.st_size = 0; - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); if (stat(pathANSI.Get(), &fileInfo) != -1) { // Check for directories @@ -246,7 +243,7 @@ uint64 UnixFileSystem::GetFileSize(const StringView& path) bool UnixFileSystem::IsReadOnly(const StringView& path) { - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); if (access(pathANSI.Get(), W_OK) == -1) { return errno == EACCES; @@ -256,7 +253,7 @@ bool UnixFileSystem::IsReadOnly(const StringView& path) bool UnixFileSystem::SetReadOnly(const StringView& path, bool isReadOnly) { - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); struct stat fileInfo; if (stat(pathANSI.Get(), &fileInfo) != -1) { @@ -283,15 +280,15 @@ bool UnixFileSystem::MoveFile(const StringView& dst, const StringView& src, bool if (overwrite) { - unlink(StringAsUTF8<>(*dst, dst.Length()).Get()); + unlink(UnixString(*dst, dst.Length()).Get()); } - if (rename(StringAsUTF8<>(*src, src.Length()).Get(), StringAsUTF8<>(*dst, dst.Length()).Get()) != 0) + if (rename(UnixString(*src, src.Length()).Get(), UnixString(*dst, dst.Length()).Get()) != 0) { if (errno == EXDEV) { if (!CopyFile(dst, src)) { - unlink(StringAsUTF8<>(*src, src.Length()).Get()); + unlink(UnixString(*src, src.Length()).Get()); return false; } } @@ -300,78 +297,6 @@ bool UnixFileSystem::MoveFile(const StringView& dst, const StringView& src, bool return false; } -bool UnixFileSystem::CopyFile(const StringView& dst, const StringView& src) -{ - const StringAsUTF8<> srcANSI(*src, src.Length()); - const StringAsUTF8<> dstANSI(*dst, dst.Length()); - - int srcFile, dstFile; - char buffer[4096]; - ssize_t readSize; - int cachedError; - - srcFile = open(srcANSI.Get(), O_RDONLY); - if (srcFile < 0) - return true; - dstFile = open(dstANSI.Get(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - if (dstFile < 0) - goto out_error; - - // first try the kernel method - struct stat statBuf; - fstat(srcFile, &statBuf); - readSize = 1; - while (readSize > 0) - { - readSize = sendfile(dstFile, srcFile, 0, statBuf.st_size); - } - // sendfile could fail for example if the input file is not nmap'able - // in this case we fall back to the read/write loop - if (readSize < 0) - { - while (readSize = read(srcFile, buffer, sizeof(buffer)), readSize > 0) - { - char* ptr = buffer; - ssize_t writeSize; - - do - { - writeSize = write(dstFile, ptr, readSize); - if (writeSize >= 0) - { - readSize -= writeSize; - ptr += writeSize; - } - else if (errno != EINTR) - { - goto out_error; - } - } while (readSize > 0); - } - } - - if (readSize == 0) - { - if (close(dstFile) < 0) - { - dstFile = -1; - goto out_error; - } - close(srcFile); - - // Success - return false; - } - -out_error: - cachedError = errno; - close(srcFile); - if (dstFile >= 0) - close(dstFile); - errno = cachedError; - return true; -} - bool UnixFileSystem::getFilesFromDirectoryTop(Array& results, const char* path, const char* searchPattern) { size_t pathLength; @@ -532,7 +457,7 @@ bool UnixFileSystem::getFilesFromDirectoryAll(Array& results, const char DateTime UnixFileSystem::GetFileLastEditTime(const StringView& path) { struct stat fileInfo; - const StringAsUTF8<> pathANSI(*path, path.Length()); + const UnixString pathANSI(*path, path.Length()); if (stat(pathANSI.Get(), &fileInfo) == -1) { return DateTime::MinValue(); diff --git a/Source/Engine/Platform/Unix/UnixFileSystem.h b/Source/Engine/Platform/Unix/UnixFileSystem.h index 1c6dd0f01..4c7256aca 100644 --- a/Source/Engine/Platform/Unix/UnixFileSystem.h +++ b/Source/Engine/Platform/Unix/UnixFileSystem.h @@ -25,7 +25,6 @@ public: static bool IsReadOnly(const StringView& path); static bool SetReadOnly(const StringView& path, bool isReadOnly); static bool MoveFile(const StringView& dst, const StringView& src, bool overwrite = false); - static bool CopyFile(const StringView& dst, const StringView& src); static DateTime GetFileLastEditTime(const StringView& path); private: