Skip to content

Commit

Permalink
Always fully clear temporary directories
Browse files Browse the repository at this point in the history
Make sure TempDirectory dtor always removes Poedit's temporary directory
completely, without errors. There were two subtle bugs in the old code:

1. Poedit would try to rmdir the directory even with --keep-temp-files.
This would fail because the files weren't deleted.

2. If something created unexpected extra files, rmdir would fail too.

Fix both by recursively deleting the directory with its content, instead
of doing the same thing manually, but only for recognized files. As a
bonus, make the code slightly simpler.

See umpirsky/Twig-Gettext-Extractor#12
  • Loading branch information
vslavik committed Dec 22, 2015
1 parent fce5a0d commit 849367f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
23 changes: 8 additions & 15 deletions src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,40 +135,33 @@ TempDirectory::TempDirectory() : m_counter(0)

TempDirectory::~TempDirectory()
{
if ( m_dir.empty() )
return;

Clear();

wxLogTrace("poedit.tmp", "removing temp dir %s", m_dir.c_str());
wxFileName::Rmdir(m_dir);
}

void TempDirectory::Clear()
{
if ( m_dir.empty() )
return;

if ( ms_keepFiles )
{
wxLogTrace("poedit.tmp", "keeping temp files in %s", m_dir.c_str());
return;
}

for ( wxArrayString::const_iterator i = m_files.begin(); i != m_files.end(); ++i )
{
if ( wxFileName::FileExists(*i) )
{
wxLogTrace("poedit.tmp", "removing temp file %s", i->c_str());
wxRemoveFile(*i);
}
}
wxLogTrace("poedit.tmp", "removing temp dir %s", m_dir.c_str());
wxFileName::Rmdir(m_dir, wxPATH_RMDIR_RECURSIVE);

m_dir.clear();
}

wxString TempDirectory::CreateFileName(const wxString& suffix)
{
wxASSERT( !m_dir.empty() );
wxString s = wxString::Format("%s%c%d%s",
m_dir.c_str(), wxFILE_SEP_PATH,
m_counter++,
suffix.c_str());
m_files.push_back(s);
wxLogTrace("poedit.tmp", "new temp file %s", s.c_str());
return s;
}
Expand Down
1 change: 0 additions & 1 deletion src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ class TempDirectory
private:
int m_counter;
wxString m_dir;
wxArrayString m_files;

static bool ms_keepFiles;
};
Expand Down

0 comments on commit 849367f

Please sign in to comment.