1
0
mirror of https://github.com/cookiengineer/audacity synced 2026-01-08 09:27:11 +01:00

Add fileSpec to CleanDir, so can be selective about files to delete.

Note that CleanDir may be used on a temp directory, deleting entire projects, and in that case
we delete all files too, but do not want to delete files that are at the top level, only ones inside
projects.
This commit is contained in:
James Crook
2017-02-20 12:58:11 +00:00
parent 0a518b65d1
commit 89c44f9cd6
2 changed files with 39 additions and 15 deletions

View File

@@ -140,11 +140,16 @@ wxMemorySize GetFreeMemory()
// //
// Behavior of RecursivelyEnumerate is tailored to our uses and not // Behavior of RecursivelyEnumerate is tailored to our uses and not
// entirely straightforward. It recurs depth-first from the passed- // entirely straightforward. We use it only for recursing into
// Audacity projects, but beware that it may be applied to a directory
// that contains other things too, for example a temp directory.
// It recurses depth-first from the passed-
// in directory into its subdirs according to optional dirspec // in directory into its subdirs according to optional dirspec
// pattern, building a list of directories and (optionally) files // pattern, building a list of directories and (optionally) files
// in the listed order. The dirspec is not applied to // in the listed order.
// subdirs of subdirs. Files in the passed-in directory will not be // The dirspec is not applied to subdirs of subdirs.
// The filespec is applied to all files in subdirectories.
// Files in the passed-in directory will not be
// enumerated. Also, the passed-in directory is the last entry added // enumerated. Also, the passed-in directory is the last entry added
// to the list. // to the list.
// JKC: Using flag wxDIR_NO_FOLLOW to NOT follow symbolic links. // JKC: Using flag wxDIR_NO_FOLLOW to NOT follow symbolic links.
@@ -153,6 +158,7 @@ wxMemorySize GetFreeMemory()
static int RecursivelyEnumerate(wxString dirPath, static int RecursivelyEnumerate(wxString dirPath,
wxArrayString& filePathArray, // output: all files in dirPath tree wxArrayString& filePathArray, // output: all files in dirPath tree
wxString dirspec, wxString dirspec,
wxString filespec,
bool bFiles, bool bDirs, bool bFiles, bool bDirs,
int progress_count = 0, int progress_count = 0,
int progress_bias = 0, int progress_bias = 0,
@@ -165,8 +171,10 @@ static int RecursivelyEnumerate(wxString dirPath,
if(dir.IsOpened()){ if(dir.IsOpened()){
wxString name; wxString name;
if (bFiles){ // Don't delete files from a selective top level, e.g. if handed "projects*" as the
cont= dir.GetFirst(&name, dirspec, wxDIR_FILES | wxDIR_HIDDEN | wxDIR_NO_FOLLOW); // directory specifier.
if (bFiles && dirspec.IsEmpty() ){
cont= dir.GetFirst(&name, filespec, wxDIR_FILES | wxDIR_HIDDEN | wxDIR_NO_FOLLOW);
while ( cont ){ while ( cont ){
wxString filepath = dirPath + wxFILE_SEP_PATH + name; wxString filepath = dirPath + wxFILE_SEP_PATH + name;
@@ -185,7 +193,7 @@ static int RecursivelyEnumerate(wxString dirPath,
while ( cont ){ while ( cont ){
wxString subdirPath = dirPath + wxFILE_SEP_PATH + name; wxString subdirPath = dirPath + wxFILE_SEP_PATH + name;
count += RecursivelyEnumerate( count += RecursivelyEnumerate(
subdirPath, filePathArray, wxEmptyString, subdirPath, filePathArray, wxEmptyString,filespec,
bFiles, bDirs, bFiles, bDirs,
progress_count, count + progress_bias, progress_count, count + progress_bias,
progress); progress);
@@ -204,6 +212,7 @@ static int RecursivelyEnumerate(wxString dirPath,
static int RecursivelyEnumerateWithProgress(wxString dirPath, static int RecursivelyEnumerateWithProgress(wxString dirPath,
wxArrayString& filePathArray, // output: all files in dirPath tree wxArrayString& filePathArray, // output: all files in dirPath tree
wxString dirspec, wxString dirspec,
wxString filespec,
bool bFiles, bool bDirs, bool bFiles, bool bDirs,
int progress_count, int progress_count,
const wxChar* message) const wxChar* message)
@@ -214,7 +223,7 @@ static int RecursivelyEnumerateWithProgress(wxString dirPath,
progress.create( _("Progress"), message ); progress.create( _("Progress"), message );
int count = RecursivelyEnumerate( int count = RecursivelyEnumerate(
dirPath, filePathArray, dirspec, dirPath, filePathArray, dirspec,filespec,
bFiles, bDirs, bFiles, bDirs,
progress_count, 0, progress_count, 0,
progress.get()); progress.get());
@@ -400,14 +409,20 @@ DirManager::~DirManager()
// static // static
// This is quite a dangerous function. In the temp dir it will delete every directory
// recursively, that has 'project*' as the name - EVEN if it happens not to be an Audacity
// project but just something else called project.
void DirManager::CleanTempDir() void DirManager::CleanTempDir()
{ {
CleanDir(globaltemp, wxT("project*"), _("Cleaning up temporary files")); CleanDir(globaltemp, wxT("project*"), wxEmptyString, _("Cleaning up temporary files"));
} }
// static // static
void DirManager::CleanDir( void DirManager::CleanDir(
const wxString &path, const wxString &dirSpec, const wxString &msg, const wxString &path,
const wxString &dirSpec,
const wxString &fileSpec,
const wxString &msg,
bool removeTop) bool removeTop)
{ {
if (dontDeleteTempFiles) if (dontDeleteTempFiles)
@@ -416,9 +431,9 @@ void DirManager::CleanDir(
wxArrayString filePathArray, dirPathArray; wxArrayString filePathArray, dirPathArray;
int countFiles = int countFiles =
RecursivelyEnumerate(path, filePathArray, dirSpec, true, false); RecursivelyEnumerate(path, filePathArray, dirSpec, fileSpec, true, false);
int countDirs = int countDirs =
RecursivelyEnumerate(path, dirPathArray, dirSpec, false, true); RecursivelyEnumerate(path, dirPathArray, dirSpec, fileSpec, false, true);
// Subtract 1 because we don't want to DELETE the global temp directory, // Subtract 1 because we don't want to DELETE the global temp directory,
// which this will find and list last. // which this will find and list last.
@@ -566,7 +581,11 @@ bool DirManager::SetProject(wxString& newProjPath, wxString& newProjName, const
// and mercilessly remove them, in addition to removing the directories. // and mercilessly remove them, in addition to removing the directories.
CleanDir( CleanDir(
cleanupLoc1, wxEmptyString, _("Cleaning up cache directories"), true); cleanupLoc1,
wxEmptyString, // EmptyString => ALL directories.
// If the next line were wxEmptyString, ALL files would be removed.
".DS_Store", // Other project files should already have been removed.
_("Cleaning up cache directories"), true);
//This destroys the empty dirs of the OD block files, which are yet to come. //This destroys the empty dirs of the OD block files, which are yet to come.
//Dont know if this will make the project dirty, but I doubt it. (mchinen) //Dont know if this will make the project dirty, but I doubt it. (mchinen)
@@ -1505,7 +1524,8 @@ int DirManager::ProjectFSCK(const bool bForceError, const bool bAutoRecoverMode)
RecursivelyEnumerateWithProgress( RecursivelyEnumerateWithProgress(
dirPath, dirPath,
filePathArray, // output: all files in project directory tree filePathArray, // output: all files in project directory tree
wxEmptyString, wxEmptyString, // All dirs
wxEmptyString, // All files
true, false, true, false,
mBlockFileHash.size(), // rough guess of how many BlockFiles will be found/processed, for progress mBlockFileHash.size(), // rough guess of how many BlockFiles will be found/processed, for progress
_("Inspecting project file data")); _("Inspecting project file data"));
@@ -1927,7 +1947,8 @@ void DirManager::RemoveOrphanBlockfiles()
RecursivelyEnumerateWithProgress( RecursivelyEnumerateWithProgress(
dirPath, dirPath,
filePathArray, // output: all files in project directory tree filePathArray, // output: all files in project directory tree
wxEmptyString, wxEmptyString, // All dirs
wxEmptyString, // All files
true, false, true, false,
mBlockFileHash.size(), // rough guess of how many BlockFiles will be found/processed, for progress mBlockFileHash.size(), // rough guess of how many BlockFiles will be found/processed, for progress
_("Inspecting project file data")); _("Inspecting project file data"));

View File

@@ -123,7 +123,10 @@ class PROFILE_DLL_API DirManager final : public XMLTagHandler {
// program is exited normally. // program is exited normally.
static void CleanTempDir(); static void CleanTempDir();
static void CleanDir( static void CleanDir(
const wxString &path, const wxString &dirSpec, const wxString &msg, const wxString &path,
const wxString &dirSpec,
const wxString &fileSpec,
const wxString &msg,
bool removeTop = false); bool removeTop = false);
// Check the project for errors and possibly prompt user // Check the project for errors and possibly prompt user