From 6934bc124ca7d5301cf9a302f549a8951fdef2f2 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 7 Mar 2022 20:08:54 -0800 Subject: [PATCH] Move temp save files to temporary directory Avoid excess writes to the project directory to dodge issues with remote file systems not fully writing data. wxRename works across disk boundaries (in theory) and falls back to wxCopy/wxRemove when it fails Fixes https://gitlab.com/kicad/code/kicad/issues/10747 --- eeschema/files-io.cpp | 19 ++++++++++--------- pagelayout_editor/files.cpp | 14 ++++++++------ pcbnew/files.cpp | 21 +++++++++------------ 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index 03848d7d4f..db40b8ffb8 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -62,6 +62,7 @@ #include #include #include +#include #include #include #include // For ::ResolvePossibleSymlinks @@ -693,9 +694,9 @@ bool SCH_EDIT_FRAME::saveSchematicFile( SCH_SHEET* aSheet, const wxString& aSave if( !IsWritable( schematicFileName ) ) return false; - wxFileName tempFile( schematicFileName ); - tempFile.SetName( wxT( "." ) + tempFile.GetName() ); - tempFile.SetExt( tempFile.GetExt() + wxT( "$" ) ); + wxStandardPaths& paths = wxStandardPaths::Get(); + wxString tempFile = wxFileName::CreateTempFileName( + paths.GetTempDir() + wxFileName::GetPathSeparator() + wxT( "eeschema" ) ); // Save wxLogTrace( traceAutoSave, "Saving file " + schematicFileName.GetFullPath() ); @@ -706,7 +707,7 @@ bool SCH_EDIT_FRAME::saveSchematicFile( SCH_SHEET* aSheet, const wxString& aSave try { - pi->Save( tempFile.GetFullPath(), aSheet, &Schematic() ); + pi->Save( tempFile, aSheet, &Schematic() ); success = true; } catch( const IO_ERROR& ioe ) @@ -717,11 +718,11 @@ bool SCH_EDIT_FRAME::saveSchematicFile( SCH_SHEET* aSheet, const wxString& aSave DisplayError( this, msg ); msg.Printf( _( "Failed to create temporary file '%s'." ), - tempFile.GetFullPath() ); + tempFile ); SetMsgPanel( wxEmptyString, msg ); // In case we started a file but didn't fully write it, clean up - wxRemoveFile( tempFile.GetFullPath() ); + wxRemoveFile( tempFile ); success = false; } @@ -729,18 +730,18 @@ bool SCH_EDIT_FRAME::saveSchematicFile( SCH_SHEET* aSheet, const wxString& aSave if( success ) { // Replace the original with the temporary file we just wrote - success = wxRenameFile( tempFile.GetFullPath(), schematicFileName.GetFullPath() ); + success = wxRenameFile( tempFile, schematicFileName.GetFullPath() ); if( !success ) { msg.Printf( _( "Error saving schematic file '%s'.\n" "Failed to rename temporary file '%s'." ), schematicFileName.GetFullPath(), - tempFile.GetFullPath() ); + tempFile ); DisplayError( this, msg ); msg.Printf( _( "Failed to rename temporary file '%s'." ), - tempFile.GetFullPath() ); + tempFile ); SetMsgPanel( wxEmptyString, msg ); } } diff --git a/pagelayout_editor/files.cpp b/pagelayout_editor/files.cpp index 02cff3259a..1a3f109737 100644 --- a/pagelayout_editor/files.cpp +++ b/pagelayout_editor/files.cpp @@ -37,6 +37,8 @@ #include "properties_frame.h" #include +#include +#include bool PL_EDITOR_FRAME::saveCurrentPageLayout() { @@ -288,23 +290,23 @@ bool PL_EDITOR_FRAME::SaveDrawingSheetFile( const wxString& aFullFileName ) { if( !aFullFileName.IsEmpty() ) { - wxFileName tempFile( aFullFileName ); - tempFile.SetName( wxT( "." ) + tempFile.GetName() ); - tempFile.SetExt( tempFile.GetExt() + wxT( "$" ) ); + wxStandardPaths& paths = wxStandardPaths::Get(); + wxString tempFile = wxFileName::CreateTempFileName( + paths.GetTempDir() + wxFileName::GetPathSeparator() + wxT( "pledit" ) ); try { - DS_DATA_MODEL::GetTheInstance().Save( tempFile.GetFullPath() ); + DS_DATA_MODEL::GetTheInstance().Save( tempFile ); } catch( const IO_ERROR& ) { // In case we started a file but didn't fully write it, clean up - wxRemoveFile( tempFile.GetFullPath() ); + wxRemoveFile( tempFile); return false; } - if( !wxRenameFile( tempFile.GetFullPath(), aFullFileName ) ) + if( !wxRenameFile( tempFile, aFullFileName ) ) return false; GetScreen()->SetContentModified( false ); diff --git a/pcbnew/files.cpp b/pcbnew/files.cpp index 38ec02039f..0e5bafc68c 100644 --- a/pcbnew/files.cpp +++ b/pcbnew/files.cpp @@ -1046,20 +1046,17 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool addToHistory, GetBoard()->SynchronizeNetsAndNetClasses(); } - wxFileName tempFile( aFileName ); + wxStandardPaths& paths = wxStandardPaths::Get(); + wxString tempFile = wxFileName::CreateTempFileName( + paths.GetTempDir() + wxFileName::GetPathSeparator() + wxT( "pcbnew" ) ); wxString upperTxt; wxString lowerTxt; - tempFile.SetName( wxT( "." ) + tempFile.GetName() ); - tempFile.SetExt( tempFile.GetExt() + wxT( "$" ) ); - try { PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::KICAD_SEXP ) ); - wxASSERT( tempFile.IsAbsolute() ); - - pi->Save( tempFile.GetFullPath(), GetBoard(), nullptr ); + pi->Save( tempFile, GetBoard(), nullptr ); } catch( const IO_ERROR& ioe ) { @@ -1067,26 +1064,26 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool addToHistory, pcbFileName.GetFullPath(), ioe.What() ) ); - lowerTxt.Printf( _( "Failed to create temporary file '%s'." ), tempFile.GetFullPath() ); + lowerTxt.Printf( _( "Failed to create temporary file '%s'." ), tempFile ); SetMsgPanel( upperTxt, lowerTxt ); // In case we started a file but didn't fully write it, clean up - wxRemoveFile( tempFile.GetFullPath() ); + wxRemoveFile( tempFile ); return false; } // If save succeeded, replace the original with what we just wrote - if( !wxRenameFile( tempFile.GetFullPath(), pcbFileName.GetFullPath() ) ) + if( !wxRenameFile( tempFile, pcbFileName.GetFullPath() ) ) { DisplayError( this, wxString::Format( _( "Error saving board file '%s'.\n" "Failed to rename temporary file '%s." ), pcbFileName.GetFullPath(), - tempFile.GetFullPath() ) ); + tempFile ) ); lowerTxt.Printf( _( "Failed to rename temporary file '%s'." ), - tempFile.GetFullPath() ); + tempFile ); SetMsgPanel( upperTxt, lowerTxt );