From 5705672957934ff6cdb01e9625d4c5bb0bc3741e Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Wed, 2 Aug 2023 19:20:50 +0200 Subject: [PATCH] Added a getFileSize() function to FileSystemUtil Also modernized createEmptyFile() to use std::filesystem::path --- es-core/src/utils/FileSystemUtil.cpp | 45 +++++++++++++++------------- es-core/src/utils/FileSystemUtil.h | 4 ++- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/es-core/src/utils/FileSystemUtil.cpp b/es-core/src/utils/FileSystemUtil.cpp index dbbb0246b..b80a46b1a 100644 --- a/es-core/src/utils/FileSystemUtil.cpp +++ b/es-core/src/utils/FileSystemUtil.cpp @@ -23,7 +23,6 @@ #include "utils/PlatformUtil.h" #include "utils/StringUtil.h" -#include #include #include #include @@ -569,6 +568,21 @@ namespace Utils return "."; } + long getFileSize(const std::filesystem::path& path) + { + try { +#if defined(_WIN64) + return static_cast(std::filesystem::file_size( + Utils::String::stringToWideString(path.generic_string()))); +#else + return static_cast(std::filesystem::file_size(path)); +#endif + } + catch (...) { + return -1; + } + } + std::string expandHomePath(const std::string& path) { // Expand home path if ~ is used. @@ -758,31 +772,22 @@ namespace Utils #endif } - bool createEmptyFile(const std::string& path) + bool createEmptyFile(const std::filesystem::path& path) { + const std::filesystem::path cleanPath {path.lexically_normal().make_preferred()}; if (exists(path)) { + LOG(LogError) << "Couldn't create target file \"" << cleanPath.string() + << "\" as it already exists"; + return false; + } #if defined(_WIN64) - LOG(LogError) << "Couldn't create target file \"" - << Utils::String::replace(path, "/", "\\") - << "\" as it already exists"; - return false; - } - - std::ofstream targetFile {Utils::String::stringToWideString(path).c_str(), + std::ofstream targetFile {Utils::String::stringToWideString(cleanPath.string()).c_str(), std::ios::binary}; - if (targetFile.fail()) { - LOG(LogError) << "Couldn't create target file \"" - << Utils::String::replace(path, "/", "\\") #else - LOG(LogError) << "Couldn't create target file \"" << path - << "\" as it already exists"; - return false; - } - - std::ofstream targetFile {path, std::ios::binary}; - if (targetFile.fail()) { - LOG(LogError) << "Couldn't create target file \"" << path + std::ofstream targetFile {cleanPath, std::ios::binary}; #endif + if (targetFile.fail()) { + LOG(LogError) << "Couldn't create target file \"" << cleanPath.string() << "\", permission problems?"; targetFile.close(); return false; diff --git a/es-core/src/utils/FileSystemUtil.h b/es-core/src/utils/FileSystemUtil.h index 546719f24..973dcf9a1 100644 --- a/es-core/src/utils/FileSystemUtil.h +++ b/es-core/src/utils/FileSystemUtil.h @@ -11,6 +11,7 @@ #ifndef ES_CORE_UTILS_FILE_SYSTEM_UTIL_H #define ES_CORE_UTILS_FILE_SYSTEM_UTIL_H +#include #include #include @@ -42,6 +43,7 @@ namespace Utils std::string getFileName(const std::string& path); std::string getStem(const std::string& path); std::string getExtension(const std::string& path); + long getFileSize(const std::filesystem::path& path); std::string expandHomePath(const std::string& path); std::string resolveRelativePath(const std::string& path, const std::string& relativeTo, @@ -59,7 +61,7 @@ namespace Utils bool renameFile(const std::string& sourcePath, const std::string& destinationPath, bool overwrite); - bool createEmptyFile(const std::string& path); + bool createEmptyFile(const std::filesystem::path& path); bool removeFile(const std::string& path); bool removeDirectory(const std::string& path); bool createDirectory(const std::string& path);