diff options
author | Alexey Edelev <alexey.edelev@qt.io> | 2023-09-05 12:03:49 +0200 |
---|---|---|
committer | Alexey Edelev <alexey.edelev@qt.io> | 2023-09-12 16:33:20 +0200 |
commit | 7164cce881c3d65eb18749471ba5e358c7d5998a (patch) | |
tree | a1b4363e7ec03e9378e44022e9195927ea6d86c3 /src/tools | |
parent | 5ce4aecd495667a50dd121930f9ca39dc07cfc36 (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.6 6.5
Change-Id: I11ad4552dccfdc8cc8a4ec4912d0a15d0f9557c6
Reviewed-by: Amir Masoud Abdol <amir.abdol@qt.io>
Diffstat (limited to 'src/tools')
-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 f2a0f65fb0..ee19820da3 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 @@ -747,9 +769,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()) @@ -867,8 +892,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); @@ -1737,8 +1762,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 |