Unify and correct Autosave functions

Autosave was not working correctly, partly due to different
implementations in eeschema and pcbnew and partly due to a mistaken
refactor at some point during v5 development.  This unifies the expected
autosave prefix to _autosave- for both pcbnew and eeschema.  It also
unifies the expected suffix for the backup files to -bak.

Fixes: lp:1820433
* https://bugs.launchpad.net/kicad/+bug/1820433

(cherry picked from commit 078320e2fb)
This commit is contained in:
Seth Hillbrand 2019-03-18 05:22:33 -07:00
parent 4facd57cb7
commit 168fa09f5f
8 changed files with 45 additions and 66 deletions

View File

@ -582,16 +582,14 @@ bool EDA_BASE_FRAME::IsWritable( const wxFileName& aFileName )
}
void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName,
const wxString& aBackupFileExtension )
void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
{
wxCHECK_RET( aFileName.IsOk(), wxT( "Invalid file name!" ) );
wxCHECK_RET( !aBackupFileExtension.IsEmpty(), wxT( "Invalid backup file extension!" ) );
wxFileName autoSaveFileName = aFileName;
// Check for auto save file.
autoSaveFileName.SetName( AUTOSAVE_PREFIX_FILENAME + aFileName.GetName() );
autoSaveFileName.SetName( GetAutoSaveFilePrefix() + aFileName.GetName() );
wxLogTrace( traceAutoSave,
wxT( "Checking for auto save file " ) + autoSaveFileName.GetFullPath() );
@ -615,18 +613,14 @@ void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName,
{
// Get the backup file name.
wxFileName backupFileName = aFileName;
backupFileName.SetExt( aBackupFileExtension );
backupFileName.SetExt( aFileName.GetExt() + GetBackupSuffix() );
// If an old backup file exists, delete it. If an old copy of the file exists, rename
// it to the backup file name
if( aFileName.FileExists() )
{
// Remove the old file backup file.
if( backupFileName.FileExists() )
wxRemoveFile( backupFileName.GetFullPath() );
// Rename the old file to the backup file name.
if( !wxRenameFile( aFileName.GetFullPath(), backupFileName.GetFullPath() ) )
if( !wxRenameFile( aFileName.GetFullPath(), backupFileName.GetFullPath(), true ) )
{
msg.Printf( _( "Could not create backup file \"%s\"" ),
GetChars( backupFileName.GetFullPath() ) );

View File

@ -135,7 +135,7 @@ bool SCH_EDIT_FRAME::SaveEEFile( SCH_SCREEN* aScreen, bool aSaveUnderNewName,
{
// Delete auto save file.
wxFileName autoSaveFileName = schematicFileName;
autoSaveFileName.SetName( AUTOSAVE_PREFIX_FILENAME + schematicFileName.GetName() );
autoSaveFileName.SetName( GetAutoSaveFilePrefix() + schematicFileName.GetName() );
if( autoSaveFileName.FileExists() )
{
@ -289,6 +289,9 @@ bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
g_RootSheet = NULL; // Force CreateScreens() to build new empty project on load failure.
SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
// This will rename the file if there is an autosave and the user want to recover
CheckForAutoSaveFile( fullFileName );
try
{
g_RootSheet = pi->Load( fullFileName, &Kiway() );
@ -745,8 +748,8 @@ bool SCH_EDIT_FRAME::doAutoSave()
tmpFileName = fn = screen->GetFileName();
// Auto save file name is the normal file name prefixed with AUTOSAVE_PREFIX_FILENAME.
fn.SetName( AUTOSAVE_PREFIX_FILENAME + fn.GetName() );
// Auto save file name is the normal file name prefixed with GetAutoSavePrefix().
fn.SetName( GetAutoSaveFilePrefix() + fn.GetName() );
screen->SetFileName( fn.GetFullPath() );

View File

@ -690,7 +690,7 @@ void SCH_DRAW_PANEL::OnKeyEvent( wxKeyEvent& event )
void SCH_DRAW_PANEL::onPaint( wxPaintEvent& aEvent )
{
if( !m_gal->IsInitialized() )
if( !m_gal->IsInitialized() || !m_gal->IsVisible() )
// The first wxPaintEvent can be fired at startup before the GAL engine is fully initialized
// (depending on platforms). Do nothing in this case
return;

View File

@ -689,8 +689,8 @@ void SCH_EDIT_FRAME::OnCloseWindow( wxCloseEvent& aEvent )
{
fn = Prj().AbsolutePath( screen->GetFileName() );
// Auto save file name is the normal file name prepended with AUTOSAVE_PREFIX_FILENAME.
fn.SetName( AUTOSAVE_PREFIX_FILENAME + fn.GetName() );
// Auto save file name is the normal file name prepended with GetAutoSaveFilePrefix().
fn.SetName( GetAutoSaveFilePrefix() + fn.GetName() );
if( fn.FileExists() && fn.IsFileWritable() )
wxRemoveFile( fn.GetFullPath() );

View File

@ -60,18 +60,6 @@
#define CREATE_BACKUP_FILE true
#define NO_BACKUP_FILE false
/**
* Prefix to create filenames for schematic files or other difile when auto-saved
* to retrieve a crash.
*
* The auto-saved filenames are AUTOSAVE_PREFIX_FILENAME + \<sourcefilename\>
* where \<sourcefilename\> is the flename without path of the auto-saved file
* Warning: avoid any special charactoer like / \\ \$ \% which can create issues on Unix
* or Window with filenames or env var expansion.
*/
#define AUTOSAVE_PREFIX_FILENAME wxT( "_saved_" )
class EDA_ITEM;
class EDA_RECT;
class EDA_DRAW_PANEL;
@ -159,6 +147,24 @@ protected:
///> Default style flags used for wxAUI toolbars
static constexpr int KICAD_AUI_TB_STYLE = wxAUI_TB_DEFAULT_STYLE | wxAUI_TB_PLAIN_BACKGROUND;
/**
* Function GetBackupSuffix
* @return the suffix to be appended to the file extension on backup
*/
static wxString GetBackupSuffix()
{
return wxT( "-bak" );
}
/**
* Function GetAutoSaveFilePrefix
* @return the string to prepend to a file name for automatic save.
*/
static wxString GetAutoSaveFilePrefix()
{
return wxT( "_autosave-" );
}
/**
* Function onAutoSaveTimer
* handles the auto save timer event.
@ -386,10 +392,8 @@ public:
* is removed.
* </p>
* @param aFileName A wxFileName object containing the file name to check.
* @param aBackupFileExtension A wxString object containing the backup file extension
* used to create the backup file name.
*/
void CheckForAutoSaveFile( const wxFileName& aFileName, const wxString& aBackupFileExtension );
void CheckForAutoSaveFile( const wxFileName& aFileName );
/**
* Function ShowChangedLanguage

View File

@ -62,16 +62,6 @@
#define USE_INSTRUMENTATION 0
static const wxChar backupSuffix[] = wxT( "-bak" );
static const wxChar autosavePrefix[] = wxT( "_autosave-" );
wxString PCB_EDIT_FRAME::GetAutoSaveFilePrefix()
{
return wxString( autosavePrefix );
}
/**
* Function AskLoadBoardFileName
* puts up a wxFileDialog asking for a BOARD filename to open.
@ -265,12 +255,12 @@ bool PCB_EDIT_FRAME::Files_io_from_id( int id )
if( id == ID_MENU_RECOVER_BOARD_AUTOSAVE )
{
wxString rec_name = wxString( autosavePrefix ) + fn.GetName();
wxString rec_name = GetAutoSaveFilePrefix() + fn.GetName();
fn.SetName( rec_name );
}
else
{
wxString backup_ext = fn.GetExt()+ backupSuffix;
wxString backup_ext = fn.GetExt() + GetBackupSuffix();
fn.SetExt( backup_ext );
}
@ -381,10 +371,6 @@ IO_MGR::PCB_FILE_T plugin_type( const wxString& aFileName, int aCtl )
// both legacy and eagle share a common file extension.
pluginType = ( aCtl & KICTL_EAGLE_BRD ) ? IO_MGR::EAGLE : IO_MGR::LEGACY;
}
else if( fn.GetExt().CmpNoCase( IO_MGR::GetFileExtension( IO_MGR::LEGACY ) + backupSuffix ) == 0 )
{
pluginType = IO_MGR::LEGACY;
}
else if( fn.GetExt().CmpNoCase( IO_MGR::GetFileExtension( IO_MGR::PCAD ) ) == 0 )
{
pluginType = IO_MGR::PCAD;
@ -478,6 +464,9 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
PLUGIN::RELEASER pi( IO_MGR::PluginFind( pluginType ) );
// This will rename the file if there is an autosave and the user want to recover
CheckForAutoSaveFile( fullFileName );
try
{
PROPERTIES props;
@ -541,11 +530,6 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
GetScreen()->ClrModify();
{
wxFileName fn = fullFileName;
CheckForAutoSaveFile( fullFileName, fn.GetExt() );
}
if( pluginType == IO_MGR::LEGACY &&
loadedBoard->GetFileFormatVersionAtLoad() < LEGACY_BOARD_FILE_VERSION )
{
@ -618,12 +602,12 @@ bool PCB_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
}
static wxString create_backup_file( const wxString& aFileName )
wxString PCB_EDIT_FRAME::createBackupFile( const wxString& aFileName )
{
wxFileName fn = aFileName;
wxFileName backupFileName = aFileName;
backupFileName.SetExt( fn.GetExt() + backupSuffix );
backupFileName.SetExt( fn.GetExt() + GetBackupSuffix() );
// If an old backup file exists, delete it. If an old board file exists,
// rename it to the backup file name.
@ -674,11 +658,9 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool aCreateBackupF
wxString backupFileName;
// aCreateBackupFile == false is mainly used to write autosave files
// or new files in save as... command
if( aCreateBackupFile )
{
backupFileName = create_backup_file( aFileName );
backupFileName = createBackupFile( aFileName );
}
GetBoard()->SynchronizeNetsAndNetClasses();
@ -729,7 +711,7 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool aCreateBackupF
// Delete auto save file on successful save.
wxFileName autoSaveFileName = pcbFileName;
autoSaveFileName.SetName( wxString( autosavePrefix ) + pcbFileName.GetName() );
autoSaveFileName.SetName( GetAutoSaveFilePrefix() + pcbFileName.GetName() );
if( autoSaveFileName.FileExists() )
wxRemoveFile( autoSaveFileName.GetFullPath() );
@ -816,7 +798,7 @@ bool PCB_EDIT_FRAME::doAutoSave()
wxFileName autoSaveFileName = tmpFileName;
// Auto save file name is the board file name prepended with autosaveFilePrefix string.
autoSaveFileName.SetName( wxString( autosavePrefix ) + autoSaveFileName.GetName() );
autoSaveFileName.SetName( GetAutoSaveFilePrefix() + autoSaveFileName.GetName() );
if( !autoSaveFileName.IsOk() )
return false;

View File

@ -611,7 +611,7 @@ void PCB_EDIT_FRAME::OnCloseWindow( wxCloseEvent& Event )
// Delete the auto save file if it exists.
wxFileName fn = GetBoard()->GetFileName();
// Auto save file name is the normal file name prefixed with '_autosave'.
// Auto save file name is the normal file name prefixed with 'GetAutoSaveFilePrefix()'.
fn.SetName( GetAutoSaveFilePrefix() + fn.GetName() );
// When the auto save feature does not have write access to the board file path, it falls

View File

@ -113,6 +113,8 @@ protected:
void createPopUpBlockMenu( wxMenu* menu );
void createPopUpMenuForMarkers( MARKER_PCB* aMarker, wxMenu* aPopMenu );
wxString createBackupFile( const wxString& aFileName );
/**
* an helper function to enable some menus only active when the display
* is switched to GAL mode and which do nothing in legacy mode
@ -308,12 +310,6 @@ public:
*/
void UpdateUserInterface();
/**
* Function GetAutoSaveFilePrefix
* @return the string to prepend to a file name for automatic save.
*/
static wxString GetAutoSaveFilePrefix();
/**
* Execute a remote command send by Eeschema via a socket,
* port KICAD_PCB_PORT_SERVICE_NUMBER (currently 4242)