Audacity  2.2.0
DirManager.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  DirManager.h
6 
7  Dominic Mazzoni
8 
9 **********************************************************************/
10 
11 #ifndef _DIRMANAGER_
12 #define _DIRMANAGER_
13 
14 #include "MemoryX.h"
15 #include <wx/list.h>
16 #include <wx/string.h>
17 #include <wx/filename.h>
18 #include <wx/hashmap.h>
19 #include <wx/utils.h>
20 
21 #include "audacity/Types.h"
22 #include "xml/XMLTagHandler.h"
23 #include "wxFileNameWrapper.h"
24 
25 class wxHashTable;
26 class BlockArray;
27 class BlockFile;
28 class SequenceTest;
29 
30 #define FSCKstatus_CLOSE_REQ 0x1
31 #define FSCKstatus_CHANGED 0x2
32 #define FSCKstatus_SAVE_AUP 0x4 // used in combination with FSCKstatus_CHANGED
33 
34 WX_DECLARE_HASH_MAP(int, int, wxIntegerHash, wxIntegerEqual, DirHash);
35 
36 class BlockFile;
37 using BlockFilePtr = std::shared_ptr<BlockFile>;
38 
39 WX_DECLARE_HASH_MAP(wxString, std::weak_ptr<BlockFile>, wxStringHash,
40  wxStringEqual, BlockHash);
41 
42 wxMemorySize GetFreeMemory();
43 
44 enum {
45  kCleanTopDirToo = 1, // The top directory can be excluded from clean.
46  kCleanDirsOnlyIfEmpty = 2, // Otherwise non empty dirs may be removed.
47  kCleanFiles = 4, // Remove files
48  kCleanDirs = 8 // Remove dirs.
49 };
50 
51 
53  public:
54 
55  // MM: Construct DirManager
56  DirManager();
57 
58  virtual ~DirManager();
59 
60  static void SetTempDir(const wxString &_temp) { globaltemp = _temp; }
61 
62  // Returns true on success.
63  // If SetProject is told NOT to create the directory
64  // but it doesn't already exist, SetProject fails and returns false.
65  bool SetProject(wxString& newProjPath, wxString& newProjName, const bool bCreate);
66 
67  wxString GetProjectDataDir();
68  wxString GetProjectName();
69 
70  wxLongLong GetFreeDiskSpace();
71 
73  NewSimpleBlockFile(samplePtr sampleData,
74  size_t sampleLen,
75  sampleFormat format,
76  bool allowDeferredWrite = false);
77 
79  NewAliasBlockFile( const wxString &aliasedFile, sampleCount aliasStart,
80  size_t aliasLen, int aliasChannel);
81 
83  NewODAliasBlockFile( const wxString &aliasedFile, sampleCount aliasStart,
84  size_t aliasLen, int aliasChannel);
85 
87  NewODDecodeBlockFile( const wxString &aliasedFile, sampleCount aliasStart,
88  size_t aliasLen, int aliasChannel, int decodeType);
89 
91  bool ContainsBlockFile(const BlockFile *b) const;
93  bool ContainsBlockFile(const wxString &filepath) const;
94 
95  // Adds one to the reference count of the block file,
96  // UNLESS it is "locked", then it makes a NEW copy of
97  // the BlockFile.
98  // May throw an exception in case of disk space exhaustion, otherwise
99  // returns non-null.
100  BlockFilePtr CopyBlockFile(const BlockFilePtr &b);
101 
102  BlockFile *LoadBlockFile(const wxChar **attrs, sampleFormat format);
103  void SaveBlockFile(BlockFile *f, int depth, FILE *fp);
104 
105 #if LEGACY_PROJECT_FILE_SUPPORT
106  BlockFile *LoadBlockFile(wxTextFile * in, sampleFormat format);
107  void SaveBlockFile(BlockFile * f, wxTextFile * out);
108 #endif
109 
110  std::pair<bool, wxString> CopyToNewProjectDirectory(BlockFile *f);
111 
112  bool EnsureSafeFilename(const wxFileName &fName);
113 
114  void SetLoadingTarget(BlockArray *pArray, unsigned idx)
115  {
116  mLoadingTarget = pArray;
117  mLoadingTargetIdx = idx;
118  }
119  void SetLoadingFormat(sampleFormat format) { mLoadingFormat = format; }
120  void SetLoadingBlockLength(size_t len) { mLoadingBlockLen = len; }
121 
122  // Note: following affects only the loading of block files when opening a project
123  void SetLoadingMaxSamples(size_t max) { mMaxSamples = max; }
124 
125  bool HandleXMLTag(const wxChar *tag, const wxChar **attrs) override;
126  XMLTagHandler *HandleXMLChild(const wxChar * WXUNUSED(tag)) override
127  { return NULL; }
128  bool AssignFile(wxFileNameWrapper &filename, const wxString &value, bool check);
129 
130  // Clean the temp dir. Note that now where we have auto recovery the temp
131  // dir is not cleaned at start up anymore. But it is cleaned when the
132  // program is exited normally.
133  static void CleanTempDir();
134  static void CleanDir(
135  const wxString &path,
136  const wxString &dirSpec,
137  const wxString &fileSpec,
138  const wxString &msg,
139  int flags = 0);
140 
141  // Check the project for errors and possibly prompt user
142  // bForceError: Always show log error alert even if no errors are found here.
143  // Important when you know that there are already errors in the log.
144  // bAutoRecoverMode: Do not show any option dialogs for how to deal with errors found here.
145  // Too complicated during auto-recover. Just correct problems the "safest" way.
146  int ProjectFSCK(const bool bForceError, const bool bAutoRecoverMode);
147 
148  void FindMissingAliasedFiles(
149  BlockHash& missingAliasedFileAUFHash, // output: (.auf) AliasBlockFiles whose aliased files are missing
150  BlockHash& missingAliasedFilePathHash); // output: full paths of missing aliased files
151  void FindMissingAUFs(
152  BlockHash& missingAUFHash); // output: missing (.auf) AliasBlockFiles
153  void FindMissingAUs(
154  BlockHash& missingAUHash); // missing data (.au) blockfiles
155  // Find .au and .auf files that are not in the project.
156  void FindOrphanBlockFiles(
157  const wxArrayString& filePathArray, // input: all files in project directory
158  wxArrayString& orphanFilePathArray); // output: orphan files
159 
160 
161  // Remove all orphaned blockfiles without user interaction. This is
162  // generally safe, because orphaned blockfiles are not referenced by the
163  // project and thus worthless anyway.
164  void RemoveOrphanBlockfiles();
165 
166  // Get directory where data files are in. Note that projects are normally
167  // not interested in this information, but it is important for the
168  // auto-save functionality
169  wxString GetDataFilesDir() const;
170 
171  // This should only be used by the auto save functionality
172  void SetLocalTempDir(const wxString &path);
173 
174  // Do not DELETE any temporary files on exit. This is only called if
175  // auto recovery is cancelled and should be retried later
176  static void SetDontDeleteTempFiles() { dontDeleteTempFiles = true; }
177 
178  // Write all write-cached block files to disc, if any
179  void WriteCacheToDisk();
180 
181  // (Try to) fill cache of blockfiles, if caching is enabled (otherwise do
182  // nothing)
183  // A no-fail operation that does not throw
184  void FillBlockfilesCache();
185 
186  private:
187 
188  wxFileNameWrapper MakeBlockFileName();
189  wxFileNameWrapper MakeBlockFilePath(const wxString &value);
190 
191  BlockHash mBlockFileHash; // repository for blockfiles
192 
193  // Hashes for management of the sub-directory tree of _data
194  struct BalanceInfo
195  {
196  DirHash dirTopPool; // available toplevel dirs
197  DirHash dirTopFull; // full toplevel dirs
198  DirHash dirMidPool; // available two-level dirs
199  DirHash dirMidFull; // full two-level dirs
200  } mBalanceInfo;
201 
202  // Accessor for the balance info, may need to do a delayed update for
203  // deletion in case other threads DELETE block files
204  BalanceInfo &GetBalanceInfo();
205 
206  void BalanceInfoDel(const wxString&);
207  void BalanceInfoAdd(const wxString&);
208  void BalanceFileAdd(int);
209  int BalanceMidAdd(int, int);
210 
211  wxString projName;
212  wxString projPath;
213  wxString projFull;
214 
215  wxString lastProject;
216 
217  wxArrayString aliasList;
218 
221  sampleFormat mLoadingFormat;
223 
224  size_t mMaxSamples; // max samples per block
225 
226  unsigned long mLastBlockFileDestructionCount { 0 };
227 
228  static wxString globaltemp;
229  wxString mytemp;
230  static int numDirManagers;
231  static bool dontDeleteTempFiles;
232 
233  friend class SequenceTest;
234 };
235 
236 #endif
void SetLoadingMaxSamples(size_t max)
Definition: DirManager.h:123
wxString projName
Definition: DirManager.h:211
static wxString globaltemp
Definition: DirManager.h:228
Creates and manages BlockFile objects.
Definition: DirManager.h:52
size_t mMaxSamples
Definition: DirManager.h:224
A BlockFile is a chunk of immutable audio data.
Definition: BlockFile.h:56
size_t mLoadingBlockLen
Definition: DirManager.h:222
static int numDirManagers
Definition: DirManager.h:230
void SetLoadingTarget(BlockArray *pArray, unsigned idx)
Definition: DirManager.h:114
unsigned mLoadingTargetIdx
Definition: DirManager.h:220
wxString projPath
Definition: DirManager.h:212
wxMemorySize GetFreeMemory()
Definition: DirManager.cpp:119
WX_DECLARE_HASH_MAP(int, int, wxIntegerHash, wxIntegerEqual, DirHash)
Definition: wxFileNameWrapper.h:16
Definition: DirManager.h:45
DirHash dirTopPool
Definition: DirManager.h:196
wxString lastProject
Definition: DirManager.h:215
std::shared_ptr< BlockFile > BlockFilePtr
Definition: BlockFile.h:48
XMLTagHandler * HandleXMLChild(const wxChar *WXUNUSED(tag)) override
Definition: DirManager.h:126
void SetLoadingFormat(sampleFormat format)
Definition: DirManager.h:119
static bool dontDeleteTempFiles
Definition: DirManager.h:231
int format
Definition: ExportPCM.cpp:56
Definition: DirManager.h:47
BlockArray * mLoadingTarget
Definition: DirManager.h:219
virtual bool HandleXMLTag(const wxChar *tag, const wxChar **attrs)=0
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:69
sampleFormat mLoadingFormat
Definition: DirManager.h:221
BlockHash mBlockFileHash
Definition: DirManager.h:191
#define PROFILE_DLL_API
Definition: Audacity.h:169
Definition: DirManager.h:46
wxString projFull
Definition: DirManager.h:213
DirHash dirMidFull
Definition: DirManager.h:199
wxArrayString aliasList
Definition: DirManager.h:217
Definition: DirManager.h:48
Definition: DirManager.h:194
void SetLoadingBlockLength(size_t len)
Definition: DirManager.h:120
wxString mytemp
Definition: DirManager.h:229
DirHash dirTopFull
Definition: DirManager.h:197
Definition: Sequence.h:52
DirHash dirMidPool
Definition: DirManager.h:198
static void SetDontDeleteTempFiles()
Definition: DirManager.h:176
static void SetTempDir(const wxString &_temp)
Definition: DirManager.h:60