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-13 04:51:24 +0000 |
commit | a6fb2596bbbc7a5095e05eb4af29bca1f12388ef (patch) | |
tree | 7e32257a0df0a52d2f2f925c3689661d141c4b63 | |
parent | 65030ebef217bcd1b8a7ddc19c6b40a5de691128 (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.
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>
(cherry picked from commit afbc0c7267f0058e2738eb0671c458ec02ab860d)
-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 a784ce311c..cf7bd6c182 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 @@ -715,9 +737,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()) @@ -821,8 +846,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); @@ -1690,8 +1715,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 |