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  bool MoveToNewProjectDirectory(BlockFile *f);
111  bool CopyToNewProjectDirectory(BlockFile *f);
112 
113  bool EnsureSafeFilename(const wxFileName &fName);
114 
115  void SetLoadingTarget(BlockArray *pArray, unsigned idx)
116  {
117  mLoadingTarget = pArray;
118  mLoadingTargetIdx = idx;
119  }
120  void SetLoadingFormat(sampleFormat format) { mLoadingFormat = format; }
121  void SetLoadingBlockLength(size_t len) { mLoadingBlockLen = len; }
122 
123  // Note: following affects only the loading of block files when opening a project
124  void SetLoadingMaxSamples(size_t max) { mMaxSamples = max; }
125 
126  bool HandleXMLTag(const wxChar *tag, const wxChar **attrs) override;
127  XMLTagHandler *HandleXMLChild(const wxChar * WXUNUSED(tag)) override
128  { return NULL; }
129  bool AssignFile(wxFileNameWrapper &filename, const wxString &value, bool check);
130 
131  // Clean the temp dir. Note that now where we have auto recovery the temp
132  // dir is not cleaned at start up anymore. But it is cleaned when the
133  // program is exited normally.
134  static void CleanTempDir();
135  static void CleanDir(
136  const wxString &path,
137  const wxString &dirSpec,
138  const wxString &fileSpec,
139  const wxString &msg,
140  int flags = 0);
141 
142  // Check the project for errors and possibly prompt user
143  // bForceError: Always show log error alert even if no errors are found here.
144  // Important when you know that there are already errors in the log.
145  // bAutoRecoverMode: Do not show any option dialogs for how to deal with errors found here.
146  // Too complicated during auto-recover. Just correct problems the "safest" way.
147  int ProjectFSCK(const bool bForceError, const bool bAutoRecoverMode);
148 
149  void FindMissingAliasedFiles(
150  BlockHash& missingAliasedFileAUFHash, // output: (.auf) AliasBlockFiles whose aliased files are missing
151  BlockHash& missingAliasedFilePathHash); // output: full paths of missing aliased files
152  void FindMissingAUFs(
153  BlockHash& missingAUFHash); // output: missing (.auf) AliasBlockFiles
154  void FindMissingAUs(
155  BlockHash& missingAUHash); // missing data (.au) blockfiles
156  // Find .au and .auf files that are not in the project.
157  void FindOrphanBlockFiles(
158  const wxArrayString& filePathArray, // input: all files in project directory
159  wxArrayString& orphanFilePathArray); // output: orphan files
160 
161 
162  // Remove all orphaned blockfiles without user interaction. This is
163  // generally safe, because orphaned blockfiles are not referenced by the
164  // project and thus worthless anyway.
165  void RemoveOrphanBlockfiles();
166 
167  // Get directory where data files are in. Note that projects are normally
168  // not interested in this information, but it is important for the
169  // auto-save functionality
170  wxString GetDataFilesDir() const;
171 
172  // This should only be used by the auto save functionality
173  void SetLocalTempDir(const wxString &path);
174 
175  // Do not DELETE any temporary files on exit. This is only called if
176  // auto recovery is cancelled and should be retried later
177  static void SetDontDeleteTempFiles() { dontDeleteTempFiles = true; }
178 
179  // Write all write-cached block files to disc, if any
180  void WriteCacheToDisk();
181 
182  // (Try to) fill cache of blockfiles, if caching is enabled (otherwise do
183  // nothing)
184  // A no-fail operation that does not throw
185  void FillBlockfilesCache();
186 
187  private:
188 
189  wxFileNameWrapper MakeBlockFileName();
190  wxFileNameWrapper MakeBlockFilePath(const wxString &value);
191 
192  bool MoveOrCopyToNewProjectDirectory(BlockFile *f, bool copy);
193 
194  BlockHash mBlockFileHash; // repository for blockfiles
195 
196  // Hashes for management of the sub-directory tree of _data
197  struct BalanceInfo
198  {
199  DirHash dirTopPool; // available toplevel dirs
200  DirHash dirTopFull; // full toplevel dirs
201  DirHash dirMidPool; // available two-level dirs
202  DirHash dirMidFull; // full two-level dirs
203  } mBalanceInfo;
204 
205  // Accessor for the balance info, may need to do a delayed update for
206  // deletion in case other threads DELETE block files
207  BalanceInfo &GetBalanceInfo();
208 
209  void BalanceInfoDel(const wxString&);
210  void BalanceInfoAdd(const wxString&);
211  void BalanceFileAdd(int);
212  int BalanceMidAdd(int, int);
213 
214  wxString projName;
215  wxString projPath;
216  wxString projFull;
217 
218  wxString lastProject;
219 
220  wxArrayString aliasList;
221 
224  sampleFormat mLoadingFormat;
226 
227  size_t mMaxSamples; // max samples per block
228 
229  unsigned long mLastBlockFileDestructionCount { 0 };
230 
231  static wxString globaltemp;
232  wxString mytemp;
233  static int numDirManagers;
234  static bool dontDeleteTempFiles;
235 
236  friend class SequenceTest;
237 };
238 
239 #endif
void SetLoadingMaxSamples(size_t max)
Definition: DirManager.h:124
wxString projName
Definition: DirManager.h:214
static wxString globaltemp
Definition: DirManager.h:231
Creates and manages BlockFile objects.
Definition: DirManager.h:52
size_t mMaxSamples
Definition: DirManager.h:227
A BlockFile is a chunk of immutable audio data.
Definition: BlockFile.h:56
size_t mLoadingBlockLen
Definition: DirManager.h:225
static int numDirManagers
Definition: DirManager.h:233
void SetLoadingTarget(BlockArray *pArray, unsigned idx)
Definition: DirManager.h:115
unsigned mLoadingTargetIdx
Definition: DirManager.h:223
wxString projPath
Definition: DirManager.h:215
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:199
wxString lastProject
Definition: DirManager.h:218
std::shared_ptr< BlockFile > BlockFilePtr
Definition: BlockFile.h:48
XMLTagHandler * HandleXMLChild(const wxChar *WXUNUSED(tag)) override
Definition: DirManager.h:127
void SetLoadingFormat(sampleFormat format)
Definition: DirManager.h:120
static bool dontDeleteTempFiles
Definition: DirManager.h:234
int format
Definition: ExportPCM.cpp:56
Definition: DirManager.h:47
BlockArray * mLoadingTarget
Definition: DirManager.h:222
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:224
BlockHash mBlockFileHash
Definition: DirManager.h:194
#define PROFILE_DLL_API
Definition: Audacity.h:171
Definition: DirManager.h:46
wxString projFull
Definition: DirManager.h:216
DirHash dirMidFull
Definition: DirManager.h:202
wxArrayString aliasList
Definition: DirManager.h:220
Definition: DirManager.h:48
Definition: DirManager.h:197
void SetLoadingBlockLength(size_t len)
Definition: DirManager.h:121
wxString mytemp
Definition: DirManager.h:232
DirHash dirTopFull
Definition: DirManager.h:200
Definition: Sequence.h:52
DirHash dirMidPool
Definition: DirManager.h:201
static void SetDontDeleteTempFiles()
Definition: DirManager.h:177
static void SetTempDir(const wxString &_temp)
Definition: DirManager.h:60