From 6e8979d8d83d53816f08583a69a262a4e93066bd Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Thu, 18 Apr 2019 22:00:04 +0100 Subject: [PATCH] Add try/catch block around writing a new library. Fixes: lp:1825203 * https://bugs.launchpad.net/kicad/+bug/1825203 (cherry picked from commit 2bcf38d2b690b45c9835f77bc5609c444f61aeeb) --- eeschema/libedit/lib_manager.cpp | 21 ++++++++++++--------- include/lib_table_base.h | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/eeschema/libedit/lib_manager.cpp b/eeschema/libedit/lib_manager.cpp index 354fc52b6a..3c73a6c4ba 100644 --- a/eeschema/libedit/lib_manager.cpp +++ b/eeschema/libedit/lib_manager.cpp @@ -645,14 +645,15 @@ bool LIB_MANAGER::addLibrary( const wxString& aFilePath, bool aCreate, SYMBOL_LI if( aCreate ) { - // CreateSymbolLib() fails if the file exists - if( wxFileName::Exists( aFilePath ) ) + try { - if( !wxRemoveFile( aFilePath ) ) - return false; + aTable->CreateSymbolLib( libName ); + } + catch( const IO_ERROR& e ) + { + aTable->RemoveRow( libRow ); + return false; } - - aTable->CreateSymbolLib( libName ); } m_frame.SyncLibraries( false ); @@ -682,10 +683,12 @@ std::set LIB_MANAGER::getOriginalParts( const wxString& aLibrary ) LIB_ALIAS* alias = symTable()->LoadSymbol( aLibrary, aliasName ); parts.insert( alias->GetPart() ); } - } catch( const IO_ERROR& e ) + } + catch( const IO_ERROR& e ) { - DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot enumerate " - "library \"%s\"" ), aLibrary ), e.What() ); + DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot enumerate library \"%s\"" ), + aLibrary ), + e.What() ); } return parts; diff --git a/include/lib_table_base.h b/include/lib_table_base.h index 4f72274980..716f4fd63d 100644 --- a/include/lib_table_base.h +++ b/include/lib_table_base.h @@ -414,6 +414,24 @@ public: */ bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false ); + /** + * Removes a row from the table. + * @param aRow is the row to remove + * @return true if the row was found (and removed) + */ + bool RemoveRow( LIB_TABLE_ROW* aRow ) + { + for( auto iter = rows.begin(); iter != rows.end(); ++iter ) + { + if( *iter == *aRow ) + { + rows.erase( iter, iter + 1 ); + return true; + } + } + return false; + } + /** * @return a #LIB_TABLE_ROW pointer if \a aURI is found in this table or in any chained * fallBack table fragments, else NULL.