diff options
author | Sebastian Holtermann <sebholt@xwmw.org> | 2018-04-03 11:36:47 (GMT) |
---|---|---|
committer | Sebastian Holtermann <sebholt@xwmw.org> | 2018-04-03 15:20:30 (GMT) |
commit | 1d2c9d8c6d5ae93bd57e3dc8f1b54c54c3422644 (patch) | |
tree | 397c0fc29d36f80eba9df44b46e41c0611138c4d /Source | |
parent | ccc38fa509301a135e68542f9965f3ea9f0547b7 (diff) | |
download | CMake-1d2c9d8c6d5ae93bd57e3dc8f1b54c54c3422644.zip CMake-1d2c9d8c6d5ae93bd57e3dc8f1b54c54c3422644.tar.gz CMake-1d2c9d8c6d5ae93bd57e3dc8f1b54c54c3422644.tar.bz2 |
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.
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmQtAutoGenerator.cxx | 49 | ||||
-rw-r--r-- | 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<std::mutex> lock(Mutex_); + return cmSystemTools::FileExists(filename, isFile); +} + +unsigned long cmQtAutoGenerator::FileSystem::FileLength( + std::string const& filename) +{ + std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<char>{ ifs }, + std::istreambuf_iterator<char>{}); + 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); |