From 849367f0fa6de350d4d6ee9fbb1ed67b18f045fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Tue, 22 Dec 2015 21:56:02 +0100 Subject: [PATCH] Always fully clear temporary directories 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 https://github.com/umpirsky/Twig-Gettext-Extractor/issues/12 --- src/utility.cpp | 23 ++++++++--------------- src/utility.h | 1 - 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/utility.cpp b/src/utility.cpp index 63e2f636ef..362caef5f5 100644 --- a/src/utility.cpp +++ b/src/utility.cpp @@ -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; } diff --git a/src/utility.h b/src/utility.h index 63feb9368b..6098535032 100644 --- a/src/utility.h +++ b/src/utility.h @@ -195,7 +195,6 @@ class TempDirectory private: int m_counter; wxString m_dir; - wxArrayString m_files; static bool ms_keepFiles; };