Bug 1271 - Move the Audacity temp dir to a location not scanned by cleanup apps (Residual)

The residual issue here was that an old cfg could go on using the unsafe path.  So we check for the unsafe path at init and silently substitute the good path.  If the user attempts to re-instate the unsafe path we tell them no, with an informative message.  This change was made more complex by windows allowing different strings for the same path, specifically C:\Users\JAMESC~1\AppData\Local\Temp\audacity_temp contains the shortening '~' so in the function that tests 'IsTempDirectoryNameOK' we use GetLongPath() to always compare the expanded names.

I also changed directory prefs to add SessionData rather than audacity_temp on the new directory name, on windows when choosing a new temp directory.
This commit is contained in:
James Crook 2015-12-26 14:56:47 +00:00
parent 0629c31538
commit 94afd8265b
3 changed files with 51 additions and 6 deletions

View File

@ -1504,6 +1504,24 @@ void AudacityApp::OnReceiveCommand(AppCommandEvent &event)
mCmdHandler->OnReceiveCommand(event);
}
// We now disallow temp directory name that puts it where cleaner apps will
// try to clean out the files.
bool AudacityApp::IsTempDirectoryNameOK( const wxString & Name ){
#ifndef __WXMSW__
return true;
#else
wxFileName tmpFile;
tmpFile.AssignTempFileName(wxT("nn"));
// use Long Path to expand out any abbreviated long substrings.
wxString BadPath = tmpFile.GetLongPath();
::wxRemoveFile(tmpFile.GetFullPath());
BadPath = BadPath.BeforeLast( '\\' ) + "\\";
wxFileName cmpFile( Name );
wxString NameCanonical = cmpFile.GetLongPath( ) + "\\";
return !(NameCanonical.StartsWith( BadPath ));
#endif
}
bool AudacityApp::InitTempDir()
{
// We need to find a temp directory location.
@ -1523,8 +1541,9 @@ bool AudacityApp::InitTempDir()
wxLogNull logNo;
// Try temp dir that was stored in prefs first
if (tempFromPrefs != wxT("")) {
if( !IsTempDirectoryNameOK( tempFromPrefs ) ){
;// Bad name? Don't try and use it.
} else if (tempFromPrefs != wxT("")) {
if (wxDirExists(tempFromPrefs))
temp = tempFromPrefs;
else if (wxMkdir(tempFromPrefs, 0755))
@ -1555,7 +1574,11 @@ bool AudacityApp::InitTempDir()
if (temp == wxT("")) {
// Failed
wxMessageBox(_("Audacity could not find a place to store temporary files.\nPlease enter an appropriate directory in the preferences dialog."));
if( !IsTempDirectoryNameOK( tempFromPrefs ) ) {
wxMessageBox(_("Audacity could not find a safe place to store temporary files.\nAudacity needs a place where automatic cleanup programs won't delete the temporary files.\nPlease enter an appropriate directory in the preferences dialog."));
} else {
wxMessageBox(_("Audacity could not find a place to store temporary files.\nPlease enter an appropriate directory in the preferences dialog."));
}
// Only want one page of the preferences
DirectoriesPrefsFactory directoriesPrefsFactory;

View File

@ -181,6 +181,7 @@ class AudacityApp:public wxApp {
const wxArrayString & pathList,
wxArrayString &results,
int flags = wxDIR_FILES);
static bool IsTempDirectoryNameOK( const wxString & Name );
FileHistory *GetRecentFiles() {return mRecentFiles;}
void AddFileToHistory(const wxString & name);

View File

@ -125,10 +125,18 @@ void DirectoriesPrefs::PopulateOrExchange(ShuttleGui & S)
void DirectoriesPrefs::OnChooseTempDir(wxCommandEvent & e)
{
wxString oldTempDir = gPrefs->Read(wxT("/Directories/TempDir"), wxGetApp().defaultTempDir);
// Because we went through InitTempDir() during initialisation,
// the old temp directory name in prefs should already be OK. Just in case there is
// some way we hadn't thought of for it to be not OK,
// we avoid prompting with it in that case and use the suggested default instead.
if( !AudacityApp::IsTempDirectoryNameOK( oldTempDir ) )
oldTempDir = wxGetApp().defaultTempDir;
wxDirDialog dlog(this,
_("Choose a location to place the temporary directory"),
gPrefs->Read(wxT("/Directories/TempDir"),
wxGetApp().defaultTempDir));
oldTempDir );
int retval = dlog.ShowModal();
if (retval != wxID_CANCEL && dlog.GetPath() != wxT("")) {
wxFileName tmpDirPath;
@ -139,8 +147,13 @@ void DirectoriesPrefs::OnChooseTempDir(wxCommandEvent & e)
// ending in a directory with the same name as what we'd add should be OK
// already)
wxString newDirName;
#if defined(__WXMSW__) || defined(__WXMAC__)
#if defined(__WXMAC__)
newDirName = wxT("audacity_temp");
#elif defined(__WXMSW__)
// Clearing Bug 1271 residual issue. Let's NOT have temp in the name.
// [Arguably we should do this for all platforms, not just Windows
// which has cleaners that clean up temp data. For another day].
newDirName = wxT("SessionData");
#else
newDirName = wxT(".audacity_temp");
#endif
@ -191,6 +204,14 @@ bool DirectoriesPrefs::Validate()
wxFileName tempDir;
tempDir.SetPath(mTempDir->GetValue());
if( !AudacityApp::IsTempDirectoryNameOK( tempDir.GetPath() ) ) {
wxMessageBox(
wxString::Format(_("Directory %s is not suitable (at risk of being cleaned out)"),
tempDir.GetPath().c_str()),
_("Error"),
wxOK | wxICON_ERROR);
return false;
}
if (!tempDir.DirExists()) {
int ans = wxMessageBox(
wxString::Format(_("Directory %s does not exist. Create it?"),