Audacity  2.2.2
Functions
DirManager.cpp File Reference
#include "Audacity.h"
#include "DirManager.h"
#include "MemoryX.h"
#include <time.h>
#include <wx/defs.h>
#include <wx/app.h>
#include <wx/dir.h>
#include <wx/log.h>
#include <wx/filefn.h>
#include <wx/hash.h>
#include <wx/progdlg.h>
#include <wx/timer.h>
#include <wx/intl.h>
#include <wx/file.h>
#include <wx/filename.h>
#include <wx/object.h>
#include "AudacityApp.h"
#include "AudacityException.h"
#include "BlockFile.h"
#include "FileException.h"
#include "FileNames.h"
#include "blockfile/LegacyBlockFile.h"
#include "blockfile/LegacyAliasBlockFile.h"
#include "blockfile/SimpleBlockFile.h"
#include "blockfile/SilentBlockFile.h"
#include "blockfile/PCMAliasBlockFile.h"
#include "blockfile/ODPCMAliasBlockFile.h"
#include "blockfile/ODDecodeBlockFile.h"
#include "InconsistencyException.h"
#include "Internat.h"
#include "Project.h"
#include "Prefs.h"
#include "Sequence.h"
#include "widgets/Warning.h"
#include "widgets/MultiDialog.h"
#include "widgets/ErrorDialog.h"
#include "ondemand/ODManager.h"
#include "Track.h"

Go to the source code of this file.

Functions

wxMemorySize GetFreeMemory ()
 
static int RecursivelyEnumerate (wxString dirPath, wxArrayString &filePathArray, wxString dirspec, wxString filespec, bool bFiles, bool bDirs, int progress_count=0, int progress_bias=0, ProgressDialog *progress=NULL)
 
static int RecursivelyEnumerateWithProgress (wxString dirPath, wxArrayString &filePathArray, wxString dirspec, wxString filespec, bool bFiles, bool bDirs, int progress_count, const wxChar *message)
 
static int RecursivelyCountSubdirs (wxString dirPath)
 
static int RecursivelyRemoveEmptyDirs (wxString dirPath, int nDirCount=0, ProgressDialog *pProgress=NULL)
 
static void RecursivelyRemove (wxArrayString &filePathArray, int count, int bias, int flags, const wxChar *message=NULL)
 
static unsigned int hexchar_to_int (unsigned int x)
 

Function Documentation

wxMemorySize GetFreeMemory ( )

Definition at line 120 of file DirManager.cpp.

Referenced by DirManager::FillBlockfilesCache(), and SimpleBlockFile::GetCache().

121 {
122  wxMemorySize avail;
123 
124 #if defined(__WXMAC__)
125  mach_port_t port = mach_host_self();
126  mach_msg_type_number_t cnt = HOST_VM_INFO_COUNT;
127  vm_statistics_data_t stats;
128  vm_size_t pagesize = 0;
129 
130  memset(&stats, 0, sizeof(stats));
131 
132  host_page_size(port, &pagesize);
133  host_statistics(port, HOST_VM_INFO, (host_info_t) &stats, &cnt);
134  avail = stats.free_count * pagesize;
135 #else
136  avail = wxGetFreeMemory();
137 #endif
138 
139  return avail;
140 }
static unsigned int hexchar_to_int ( unsigned int  x)
inlinestatic

Definition at line 787 of file DirManager.cpp.

Referenced by DirManager::BalanceInfoAdd(), and DirManager::BalanceInfoDel().

788 {
789  if(x<48U)return 0;
790  if(x<58U)return x-48U;
791  if(x<65U)return 10U;
792  if(x<71U)return x-55U;
793  if(x<97U)return 10U;
794  if(x<103U)return x-87U;
795  return 15U;
796 }
static int RecursivelyCountSubdirs ( wxString  dirPath)
static

Definition at line 238 of file DirManager.cpp.

References name.

Referenced by DirManager::ProjectFSCK().

239 {
240  bool bContinue;
241  int nCount = 0;
242  wxDir dir(dirPath);
243  if (dir.IsOpened() && dir.HasSubDirs())
244  {
245  wxString name;
246  bContinue = dir.GetFirst(&name, wxEmptyString, wxDIR_DIRS);
247  while (bContinue)
248  {
249  nCount++;
250  wxString subdirPath = dirPath + wxFILE_SEP_PATH + name;
251  nCount += RecursivelyCountSubdirs(subdirPath);
252  bContinue = dir.GetNext(&name);
253  }
254  }
255  return nCount;
256 }
static int RecursivelyCountSubdirs(wxString dirPath)
Definition: DirManager.cpp:238
const wxChar * name
Definition: Distortion.cpp:94
static int RecursivelyEnumerate ( wxString  dirPath,
wxArrayString &  filePathArray,
wxString  dirspec,
wxString  filespec,
bool  bFiles,
bool  bDirs,
int  progress_count = 0,
int  progress_bias = 0,
ProgressDialog progress = NULL 
)
static

Definition at line 162 of file DirManager.cpp.

References name.

Referenced by DirManager::CleanDir(), and RecursivelyEnumerateWithProgress().

170 {
171  int count=0;
172  bool cont;
173 
174  wxDir dir(dirPath);
175  if(dir.IsOpened()){
176  wxString name;
177 
178  // Don't DELETE files from a selective top level, e.g. if handed "projects*" as the
179  // directory specifier.
180  if (bFiles && dirspec.IsEmpty() ){
181  cont= dir.GetFirst(&name, filespec, wxDIR_FILES | wxDIR_HIDDEN | wxDIR_NO_FOLLOW);
182  while ( cont ){
183  wxString filepath = dirPath + wxFILE_SEP_PATH + name;
184 
185  count++;
186  filePathArray.Add(filepath);
187 
188  cont = dir.GetNext(&name);
189 
190  if (progress)
191  progress->Update(count + progress_bias,
192  progress_count);
193  }
194  }
195 
196  cont= dir.GetFirst(&name, dirspec, wxDIR_DIRS | wxDIR_NO_FOLLOW);
197  while ( cont ){
198  wxString subdirPath = dirPath + wxFILE_SEP_PATH + name;
199  count += RecursivelyEnumerate(
200  subdirPath, filePathArray, wxEmptyString,filespec,
201  bFiles, bDirs,
202  progress_count, count + progress_bias,
203  progress);
204  cont = dir.GetNext(&name);
205  }
206  }
207 
208  if (bDirs) {
209  filePathArray.Add(dirPath);
210  count++;
211  }
212 
213  return count;
214 }
static int RecursivelyEnumerate(wxString dirPath, wxArrayString &filePathArray, wxString dirspec, wxString filespec, bool bFiles, bool bDirs, int progress_count=0, int progress_bias=0, ProgressDialog *progress=NULL)
Definition: DirManager.cpp:162
ProgressResult Update(int value, const wxString &message=wxEmptyString)
const wxChar * name
Definition: Distortion.cpp:94
static int RecursivelyEnumerateWithProgress ( wxString  dirPath,
wxArrayString &  filePathArray,
wxString  dirspec,
wxString  filespec,
bool  bFiles,
bool  bDirs,
int  progress_count,
const wxChar *  message 
)
static

Definition at line 216 of file DirManager.cpp.

References _(), Maybe< X >::create(), and RecursivelyEnumerate().

Referenced by DirManager::ProjectFSCK(), and DirManager::RemoveOrphanBlockfiles().

223 {
224  Maybe<ProgressDialog> progress{};
225 
226  if (message)
227  progress.create( _("Progress"), message );
228 
229  int count = RecursivelyEnumerate(
230  dirPath, filePathArray, dirspec,filespec,
231  bFiles, bDirs,
232  progress_count, 0,
233  progress.get());
234 
235  return count;
236 }
static int RecursivelyEnumerate(wxString dirPath, wxArrayString &filePathArray, wxString dirspec, wxString filespec, bool bFiles, bool bDirs, int progress_count=0, int progress_bias=0, ProgressDialog *progress=NULL)
Definition: DirManager.cpp:162
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
void create(Args &&...args)
Definition: MemoryX.h:632
static void RecursivelyRemove ( wxArrayString &  filePathArray,
int  count,
int  bias,
int  flags,
const wxChar *  message = NULL 
)
static

Definition at line 303 of file DirManager.cpp.

References _(), Maybe< X >::create(), kCleanDirs, kCleanDirsOnlyIfEmpty, kCleanFiles, and name.

Referenced by DirManager::CleanDir().

305 {
306  bool bFiles= (flags & kCleanFiles) != 0;
307  bool bDirs = (flags & kCleanDirs) != 0;
308  bool bDirsMustBeEmpty = (flags & kCleanDirsOnlyIfEmpty) != 0;
309  Maybe<ProgressDialog> progress{};
310 
311 
312  if (message)
313  progress.create( _("Progress"), message );
314 
315  auto nn = filePathArray.size();
316  for (unsigned int i = 0; i < nn; i++) {
317  const wxChar *file = filePathArray[i];
318  if (bFiles)
319  ::wxRemoveFile(file);
320  if (bDirs) {
321  // continue will go to the next item, and skip
322  // attempting to delete the directory.
323  if( bDirsMustBeEmpty ){
324  wxDir dir( file );
325  if( !dir.IsOpened() )
326  continue;
327  if( dir.HasFiles() )
328  continue;
329  if( dir.HasSubDirs() )
330  continue;
331  }
332 
333 #ifdef __WXMSW__
334  if (!bFiles)
335  ::wxRemoveFile(file); // See note above about wxRmdir sometimes incorrectly failing on Windows.
336 #endif
337 
338  if (! ::wxRmdir(file) ) {
339  wxDir dir(file);
340  if(dir.IsOpened()) {
341  wxLogMessage(file + wxString(" still contains:"));
342  wxString name;
343  auto cont = dir.GetFirst(&name);
344  while ( cont ) {
345  wxLogMessage(file + wxString(wxFILE_SEP_PATH) + name );
346  cont = dir.GetNext(&name);
347  }
348  }
349  else
350  wxLogMessage(wxString("Can't enumerate directory ") + file);
351  }
352  }
353  if (progress)
354  progress->Update(i + bias, count);
355  }
356 }
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop"))), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom"))), OnMoveTrack) void TrackMenuTable::OnSetName(wxCommandEvent &)
const wxChar * name
Definition: Distortion.cpp:94
void create(Args &&...args)
Definition: MemoryX.h:632
static int RecursivelyRemoveEmptyDirs ( wxString  dirPath,
int  nDirCount = 0,
ProgressDialog pProgress = NULL 
)
static

Definition at line 258 of file DirManager.cpp.

References name.

Referenced by DirManager::ProjectFSCK().

261 {
262  bool bContinue;
263  wxDir dir(dirPath);
264  int nCount = 0;
265  if (dir.IsOpened())
266  {
267  if (dir.HasSubDirs())
268  {
269  wxString name;
270  bContinue = dir.GetFirst(&name, wxEmptyString, wxDIR_DIRS);
271  while (bContinue)
272  {
273  wxString subdirPath = dirPath + wxFILE_SEP_PATH + name;
274  nCount += RecursivelyRemoveEmptyDirs(subdirPath, nDirCount, pProgress);
275  bContinue = dir.GetNext(&name);
276  }
277  }
278  // Have to recheck dir.HasSubDirs() again, in case they all were deleted in recursive calls.
279  if (!dir.HasSubDirs() && !dir.HasFiles() && (dirPath.Right(5) != wxT("_data")))
280  {
281  // No subdirs or files. It's empty so DELETE it.
282  // Vaughan, 2010-07-07:
283  // Note that, per http://src.chromium.org/svn/trunk/src/base/file_util_win.cc, among others,
284  // "Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting
285  // an empty directory..." Supposedly fixed in Vista and up.
286  // I discovered this on WinXP. I tried several other Windows SDK functions (e.g., _rmdir
287  // and RemoveDirectory), and they all give same results.
288  // I noticed dirs get deleted in RecursivelyRemove, maybe because it doesn't
289  // consider whether the path is a directory or a file and wxRemoveFile()'s it first.
290  // Tried it here on WinXP, but no joy. Leave the code in case it works on other Win systems.
291  #ifdef __WXMSW__
292  ::wxRemoveFile(dirPath);
293  #endif
294  ::wxRmdir(dirPath);
295  }
296  nCount++; // Count dirPath in progress.
297  if (pProgress)
298  pProgress->Update(nCount, nDirCount);
299  }
300  return nCount;
301 }
static int RecursivelyRemoveEmptyDirs(wxString dirPath, int nDirCount=0, ProgressDialog *pProgress=NULL)
Definition: DirManager.cpp:258
ProgressResult Update(int value, const wxString &message=wxEmptyString)
const wxChar * name
Definition: Distortion.cpp:94