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>
bb10
Alexey Edelev 2023-09-05 12:03:49 +02:00
parent 5ce4aecd49
commit 7164cce881
1 changed files with 33 additions and 7 deletions

View File

@ -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