diff options
author | Alexey Edelev <alexey.edelev@qt.io> | 2023-09-05 12:03:49 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-09-12 21:40:11 +0000 |
commit | afbc0c7267f0058e2738eb0671c458ec02ab860d (patch) | |
tree | 7baec4deb27a05d027cf52eb62c2d8ea76ff3580 | |
parent | 35b3deeb587437396ce42b54dae5bbbb225b74b7 (diff) |
syncqt: Handle possible exceptions thrown by std::filesystem::create_directories
Wrap the directory creation logic with try/catch to handle possible
file system exceptions.
Pick-to: 6.5
Change-Id: I11ad4552dccfdc8cc8a4ec4912d0a15d0f9557c6
Reviewed-by: Amir Masoud Abdol <amir.abdol@qt.io>
(cherry picked from commit 7164cce881c3d65eb18749471ba5e358c7d5998a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/tools/syncqt/main.cpp | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/src/tools/syncqt/main.cpp b/src/tools/syncqt/main.cpp index 4471bcbc77..fbbb5f1665 100644 --- a/src/tools/syncqt/main.cpp +++ b/src/tools/syncqt/main.cpp @@ -140,8 +140,30 @@ std::filesystem::path normilizedPath(const std::string &path) { return std::filesystem::path(std::filesystem::weakly_canonical(path).generic_string()); } + +bool createDirectories(const std::string &path, std::string_view errorMsg, bool *exists = nullptr) +{ + bool result = true; + try { + if (!std::filesystem::exists(path)) { + if (exists) + *exists = false; + std::filesystem::create_directories(path); + } else { + if (exists) + *exists = true; + } + } catch (const std::filesystem::filesystem_error &fserr) { + result = false; + std::cerr << errorMsg << ": " << path << ".\n" + << fserr.code().message() << "(" << fserr.code().value() << "):" << fserr.what() + << std::endl; + } + return result; } +} // namespace utils + using FileStamp = std::filesystem::file_time_type; class CommandLineOptions @@ -732,9 +754,12 @@ public: bool skipCleanup = false) { bool result = true; - if (!std::filesystem::exists(outputDirectory)) { - std::filesystem::create_directories(outputDirectory); - } else if (!skipCleanup) { + bool outDirExists = false; + if (!utils::createDirectories(outputDirectory, "Unable to create staging directory", + &outDirExists)) + return false; + + if (outDirExists && !skipCleanup) { for (const auto &entry : std::filesystem::recursive_directory_iterator(outputDirectory)) { if (m_producedHeaders.find(entry.path().filename().generic_string()) @@ -845,8 +870,8 @@ public: else if (isPrivate) outputDir = m_commandLineArgs->privateIncludeDir(); - if (!std::filesystem::exists(outputDir)) - std::filesystem::create_directories(outputDir); + if (!utils::createDirectories(outputDir, "Unable to create output directory")) + return false; bool headerFileExists = std::filesystem::exists(headerFile); @@ -1718,8 +1743,9 @@ bool SyncScanner::writeIfDifferent(const std::string &outputFile, const std::str std::filesystem::path outputFilePath(outputFile); std::string outputDirectory = outputFilePath.parent_path().string(); - if (!std::filesystem::exists(outputDirectory)) - std::filesystem::create_directories(outputDirectory); + + if (!utils::createDirectories(outputDirectory, "Unable to create output directory")) + return false; auto expectedSize = buffer.size(); #ifdef _WINDOWS |