From 1d2c9d8c6d5ae93bd57e3dc8f1b54c54c3422644 Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Tue, 3 Apr 2018 13:36:47 +0200 Subject: Autogen: Use std::istreambuf_iterator for file so string reading This adds a dedicated mutex for file reading and writing to cmQtAutoGenerator::FileSystem. The purpose of the change is to avoid that long files reads block cmsys based path computations, which are protected by an other mutex. --- Source/cmQtAutoGenerator.cxx | 49 ++++++++++++++++++++++++++------------------ Source/cmQtAutoGenerator.h | 5 +++++ 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Source/cmQtAutoGenerator.cxx b/Source/cmQtAutoGenerator.cxx index f4444e3..4aa1b1f 100644 --- a/Source/cmQtAutoGenerator.cxx +++ b/Source/cmQtAutoGenerator.cxx @@ -219,6 +219,20 @@ bool cmQtAutoGenerator::FileSystem::FileExists(std::string const& filename) return cmSystemTools::FileExists(filename); } +bool cmQtAutoGenerator::FileSystem::FileExists(std::string const& filename, + bool isFile) +{ + std::lock_guard lock(Mutex_); + return cmSystemTools::FileExists(filename, isFile); +} + +unsigned long cmQtAutoGenerator::FileSystem::FileLength( + std::string const& filename) +{ + std::lock_guard lock(Mutex_); + return cmSystemTools::FileLength(filename); +} + bool cmQtAutoGenerator::FileSystem::FileIsOlderThan( std::string const& buildFile, std::string const& sourceFile, std::string* error) @@ -248,35 +262,30 @@ bool cmQtAutoGenerator::FileSystem::FileRead(std::string& content, std::string* error) { bool success = false; - { - std::lock_guard lock(Mutex_); - if (cmSystemTools::FileExists(filename, true)) { - std::size_t const length = cmSystemTools::FileLength(filename); + if (FileExists(filename, true)) { + unsigned long const length = FileLength(filename); + { + std::lock_guard lock(Mutex_); cmsys::ifstream ifs(filename.c_str(), (std::ios::in | std::ios::binary)); if (ifs) { - if (length > 0) { - content.resize(length); - ifs.read(&content.front(), content.size()); - if (ifs) { - success = true; - } else { - content.clear(); - if (error != nullptr) { - error->append("Reading from the file failed."); - } - } + content.reserve(length); + content.assign(std::istreambuf_iterator{ ifs }, + std::istreambuf_iterator{}); + if (ifs) { + success = true; } else { - // Readable but empty file content.clear(); - success = true; + if (error != nullptr) { + error->append("Reading from the file failed."); + } } } else if (error != nullptr) { error->append("Opening the file for reading failed."); } - } else if (error != nullptr) { - error->append( - "The file does not exist, is not readable or is a directory."); } + } else if (error != nullptr) { + error->append( + "The file does not exist, is not readable or is a directory."); } return success; } diff --git a/Source/cmQtAutoGenerator.h b/Source/cmQtAutoGenerator.h index 0995b46..299e4c2 100644 --- a/Source/cmQtAutoGenerator.h +++ b/Source/cmQtAutoGenerator.h @@ -99,7 +99,12 @@ public: std::string GetFilePathChecksum(std::string const& filename); // -- File access + /// @brief Wrapper for cmSystemTools::FileExists bool FileExists(std::string const& filename); + /// @brief Wrapper for cmSystemTools::FileExists + bool FileExists(std::string const& filename, bool isFile); + /// @brief Wrapper for cmSystemTools::FileLength + unsigned long FileLength(std::string const& filename); bool FileIsOlderThan(std::string const& buildFile, std::string const& sourceFile, std::string* error = nullptr); -- cgit v0.12