From fb49e0da893f043c7b153b248763d5e13fa4c3af Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 30 Jul 2022 12:11:14 +0200 Subject: [PATCH] App: fixes #7277: Write to network drive fails on Windows --- src/App/Document.cpp | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/App/Document.cpp b/src/App/Document.cpp index f963ba961c..b881a00888 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -2614,15 +2614,37 @@ bool Document::saveToFile(const char* filename) const bool policy = App::GetApplication().GetParameterGroupByPath ("User parameter:BaseApp/Preferences/Document")->GetBool("BackupPolicy",true); - //realpath is canonical filename i.e. without symlink + auto canonical_path = [](const char* filename) { + try { #ifdef FC_OS_WIN32 - QString utf8Name = QString::fromUtf8(filename); - auto realpath = fs::weakly_canonical(fs::absolute(fs::path(utf8Name.toStdWString()))); - std::string nativePath = QString::fromStdWString(realpath.native()).toStdString(); + QString utf8Name = QString::fromUtf8(filename); + auto realpath = fs::weakly_canonical(fs::absolute(fs::path(utf8Name.toStdWString()))); + std::string nativePath = QString::fromStdWString(realpath.native()).toStdString(); #else - auto realpath = fs::weakly_canonical(fs::absolute(fs::path(filename))); - std::string nativePath = realpath.native(); + auto realpath = fs::weakly_canonical(fs::absolute(fs::path(filename))); + std::string nativePath = realpath.native(); #endif + // In case some folders in the path do not exist + auto parentPath = realpath.parent_path(); + fs::create_directories(parentPath); + + return nativePath; + } + catch (const std::exception&) { +#ifdef FC_OS_WIN32 + QString utf8Name = QString::fromUtf8(filename); + auto parentPath = fs::absolute(fs::path(utf8Name.toStdWString())).parent_path(); +#else + auto parentPath = fs::absolute(fs::path(filename)).parent_path(); +#endif + fs::create_directories(parentPath); + + return std::string(filename); + } + }; + + //realpath is canonical filename i.e. without symlink + std::string nativePath = canonical_path(filename); // make a tmp. file where to save the project data first and then rename to // the actual file name. This may be useful if overwriting an existing file @@ -2636,10 +2658,6 @@ bool Document::saveToFile(const char* filename) const Base::FileInfo tmp(fn); - // In case some folders in the path do not exist - auto parentPath = realpath.parent_path(); - fs::create_directories(parentPath); - // open extra scope to close ZipWriter properly { Base::ofstream file(tmp, std::ios::out | std::ios::binary);