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>
This commit is contained in:
parent
35b3deeb58
commit
afbc0c7267
@ -140,8 +140,30 @@ std::filesystem::path normilizedPath(const std::string &path)
|
|||||||
{
|
{
|
||||||
return std::filesystem::path(std::filesystem::weakly_canonical(path).generic_string());
|
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;
|
using FileStamp = std::filesystem::file_time_type;
|
||||||
|
|
||||||
class CommandLineOptions
|
class CommandLineOptions
|
||||||
@ -732,9 +754,12 @@ public:
|
|||||||
bool skipCleanup = false)
|
bool skipCleanup = false)
|
||||||
{
|
{
|
||||||
bool result = true;
|
bool result = true;
|
||||||
if (!std::filesystem::exists(outputDirectory)) {
|
bool outDirExists = false;
|
||||||
std::filesystem::create_directories(outputDirectory);
|
if (!utils::createDirectories(outputDirectory, "Unable to create staging directory",
|
||||||
} else if (!skipCleanup) {
|
&outDirExists))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (outDirExists && !skipCleanup) {
|
||||||
for (const auto &entry :
|
for (const auto &entry :
|
||||||
std::filesystem::recursive_directory_iterator(outputDirectory)) {
|
std::filesystem::recursive_directory_iterator(outputDirectory)) {
|
||||||
if (m_producedHeaders.find(entry.path().filename().generic_string())
|
if (m_producedHeaders.find(entry.path().filename().generic_string())
|
||||||
@ -845,8 +870,8 @@ public:
|
|||||||
else if (isPrivate)
|
else if (isPrivate)
|
||||||
outputDir = m_commandLineArgs->privateIncludeDir();
|
outputDir = m_commandLineArgs->privateIncludeDir();
|
||||||
|
|
||||||
if (!std::filesystem::exists(outputDir))
|
if (!utils::createDirectories(outputDir, "Unable to create output directory"))
|
||||||
std::filesystem::create_directories(outputDir);
|
return false;
|
||||||
|
|
||||||
bool headerFileExists = std::filesystem::exists(headerFile);
|
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::filesystem::path outputFilePath(outputFile);
|
||||||
|
|
||||||
std::string outputDirectory = outputFilePath.parent_path().string();
|
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();
|
auto expectedSize = buffer.size();
|
||||||
#ifdef _WINDOWS
|
#ifdef _WINDOWS
|
||||||
|
Loading…
x
Reference in New Issue
Block a user