Recursively delete directories in the project tree

Ensure we iterate over all the sub directories and files inside of them
when deleting a directory from the project tree.

Fixes: lp:1852357
* https://bugs.launchpad.net/kicad/+bug/1852357
This commit is contained in:
Ian McInerney 2019-11-15 00:25:56 +00:00
parent 56949252b1
commit 88e55bbf2d
3 changed files with 83 additions and 11 deletions

View File

@ -378,3 +378,67 @@ wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
{
return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
}
bool DeleteDirectory( const wxString& aDirName, bool aRecurse, bool aIncludeHidden )
{
int hiddenFlag = ( aIncludeHidden ? wxDIR_HIDDEN : 0 );
wxDir mainDir( aDirName );
if( !mainDir.IsOpened() )
return false;
if( mainDir.HasSubDirs() && !aRecurse )
{
mainDir.Close();
return false;
}
// Get the name from wxWidgets so that we are sure all the separators are correct
wxString fullDirName = mainDir.GetNameWithSep();
wxString dir;
bool valid = mainDir.GetFirst( &dir, wxEmptyString, wxDIR_DIRS | hiddenFlag );
// Iterate over the subdirectories to recursively delete them and their contents
while( valid )
{
dir.Prepend( fullDirName );
// This call will also delete the actual directory, so we don't have to
if( !DeleteDirectory( dir, true, aIncludeHidden ) )
{
mainDir.Close();
return false;
}
valid = mainDir.GetNext( &dir );
}
wxString file;
valid = mainDir.GetFirst( &file, wxEmptyString, wxDIR_FILES | hiddenFlag );
// Iterate over the files to remove all of them from the directory
while( valid )
{
file.Prepend( fullDirName );
if( !wxRemoveFile( file ) )
{
mainDir.Close();
return false;
}
valid = mainDir.GetNext( &dir );
}
mainDir.Close();
// Now delete the actual directory
if( !wxRmdir( aDirName ) )
{
mainDir.Close();
return false;
}
return true;
}

View File

@ -152,4 +152,21 @@ wxString FindKicadFile( const wxString& shortname );
*/
extern wxString QuoteFullPath( wxFileName& fn, wxPathFormat format = wxPATH_NATIVE );
/**
* Delete a directory and all of its contents recursively.
* This function ensures that all contents of subdirectories are deleted before deleting
* the directory. If recursion is disabled, then the existence of subdirectories will
* cause the deletion to fail and the function to return false.
*
* Note that if hidden files/folders exist in the directory, and aIncludeHidden is false,
* then the directory may not be deleted.
*
* @param aRecurse specifies if subdirectories should also be deleted
* @param aIncludeHidden specifies if hidden files/directories should be deleted as well
* @return true if the directory could be deleted
*
*/
bool DeleteDirectory( const wxString& aDirName, bool aRecurse = true, bool aIncludeHidden = true );
#endif /* __INCLUDE__GESTFICH_H__ */

View File

@ -115,7 +115,7 @@ bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check )
if( !wxRenameFile( GetFileName(), newFile, false ) )
{
wxMessageDialog( m_parent, _( "Unable to rename file ... " ), _( "Permission error?" ),
wxMessageDialog( m_parent, _( "Unable to rename file ... " ), _( "Permission error?" ),
wxICON_ERROR | wxOK );
return false;
}
@ -138,16 +138,7 @@ void TREEPROJECT_ITEM::Delete()
if( !wxDirExists( GetFileName() ) )
success = wxRemoveFile( GetFileName() );
else
{
wxArrayString filelist;
wxDir::GetAllFiles( GetFileName(), &filelist );
for( unsigned int i = 0; i < filelist.Count(); i++ )
wxRemoveFile( filelist[i] );
success = wxRmdir( GetFileName() );
}
success = DeleteDirectory( GetFileName() );
if( success )
m_parent->Delete( GetId() );