1
0
mirror of https://github.com/cookiengineer/audacity synced 2026-03-10 00:15:31 +01:00

Don't copy wxFileName often, it's not lightweight! Use wxFileNameWrapper...

... which is a new class that defines moves.
This commit is contained in:
Paul Licameli
2016-04-11 00:16:29 -04:00
committed by Paul Licameli
parent b0ef9c4e82
commit b6fdffbab2
23 changed files with 291 additions and 208 deletions

View File

@@ -560,9 +560,9 @@ void DirManager::SetLocalTempDir(const wxString &path)
mytemp = path;
}
wxFileName DirManager::MakeBlockFilePath(const wxString &value) {
wxFileNameWrapper DirManager::MakeBlockFilePath(const wxString &value) {
wxFileName dir;
wxFileNameWrapper dir;
dir.AssignDir(GetDataFilesDir());
if(value.GetChar(0)==wxT('d')){
@@ -571,7 +571,8 @@ wxFileName DirManager::MakeBlockFilePath(const wxString &value) {
wxString subdir=value.Mid(0,location);
dir.AppendDir(subdir);
if(!dir.DirExists())dir.Mkdir();
if(!dir.DirExists())
dir.Mkdir();
}
if(value.GetChar(0)==wxT('e')){
@@ -588,14 +589,14 @@ wxFileName DirManager::MakeBlockFilePath(const wxString &value) {
wxLogSysError(_("mkdir in DirManager::MakeBlockFilePath failed."));
}
}
return dir;
return std::move(dir);
}
bool DirManager::AssignFile(wxFileName &fileName,
bool DirManager::AssignFile(wxFileNameWrapper &fileName,
const wxString &value,
bool diskcheck)
{
wxFileName dir=MakeBlockFilePath(value);
wxFileNameWrapper dir{ MakeBlockFilePath(value) };
if(diskcheck){
// verify that there's no possible collision on disk. If there
@@ -755,9 +756,9 @@ void DirManager::BalanceInfoDel(const wxString &file)
// only determines appropriate filename and subdir balance; does not
// perform maintainence
wxFileName DirManager::MakeBlockFileName()
wxFileNameWrapper DirManager::MakeBlockFileName()
{
wxFileName ret;
wxFileNameWrapper ret;
wxString baseFileName;
unsigned int filenum,midnum,topnum,midkey;
@@ -863,7 +864,7 @@ wxFileName DirManager::MakeBlockFileName()
// FIXME: Might we get here without midkey having been set?
// Seemed like a possible problem in these changes in .aup directory hierarchy.
BalanceFileAdd(midkey);
return ret;
return std::move(ret);
}
BlockFile *DirManager::NewSimpleBlockFile(
@@ -871,13 +872,14 @@ BlockFile *DirManager::NewSimpleBlockFile(
sampleFormat format,
bool allowDeferredWrite)
{
wxFileName fileName = MakeBlockFileName();
wxFileNameWrapper filePath{ MakeBlockFileName() };
const wxString fileName{ filePath.GetName() };
BlockFile *newBlockFile =
new SimpleBlockFile(fileName, sampleData, sampleLen, format,
new SimpleBlockFile(std::move(filePath), sampleData, sampleLen, format,
allowDeferredWrite);
mBlockFileHash[fileName.GetName()]=newBlockFile;
mBlockFileHash[fileName]=newBlockFile;
return newBlockFile;
}
@@ -886,13 +888,15 @@ BlockFile *DirManager::NewAliasBlockFile(
const wxString &aliasedFile, sampleCount aliasStart,
sampleCount aliasLen, int aliasChannel)
{
wxFileName fileName = MakeBlockFileName();
wxFileNameWrapper filePath{ MakeBlockFileName() };
const wxString fileName = filePath.GetName();
BlockFile *newBlockFile =
new PCMAliasBlockFile(fileName,
aliasedFile, aliasStart, aliasLen, aliasChannel);
new PCMAliasBlockFile(std::move(filePath),
wxFileNameWrapper{aliasedFile},
aliasStart, aliasLen, aliasChannel);
mBlockFileHash[fileName.GetName()]=newBlockFile;
mBlockFileHash[fileName]=newBlockFile;
aliasList.Add(aliasedFile);
return newBlockFile;
@@ -902,13 +906,14 @@ BlockFile *DirManager::NewODAliasBlockFile(
const wxString &aliasedFile, sampleCount aliasStart,
sampleCount aliasLen, int aliasChannel)
{
wxFileName fileName = MakeBlockFileName();
wxFileNameWrapper filePath{ MakeBlockFileName() };
const wxString fileName{ filePath.GetName() };
BlockFile *newBlockFile =
new ODPCMAliasBlockFile(fileName,
aliasedFile, aliasStart, aliasLen, aliasChannel);
new ODPCMAliasBlockFile(std::move(filePath),
wxFileNameWrapper{aliasedFile}, aliasStart, aliasLen, aliasChannel);
mBlockFileHash[fileName.GetName()]=newBlockFile;
mBlockFileHash[fileName]=newBlockFile;
aliasList.Add(aliasedFile);
return newBlockFile;
@@ -918,13 +923,14 @@ BlockFile *DirManager::NewODDecodeBlockFile(
const wxString &aliasedFile, sampleCount aliasStart,
sampleCount aliasLen, int aliasChannel, int decodeType)
{
wxFileName fileName = MakeBlockFileName();
wxFileNameWrapper filePath{ MakeBlockFileName() };
const wxString fileName{ filePath.GetName() };
BlockFile *newBlockFile =
new ODDecodeBlockFile(fileName,
aliasedFile, aliasStart, aliasLen, aliasChannel, decodeType);
new ODDecodeBlockFile(std::move(filePath),
wxFileNameWrapper{aliasedFile}, aliasStart, aliasLen, aliasChannel, decodeType);
mBlockFileHash[fileName.GetName()]=newBlockFile;
mBlockFileHash[fileName]=newBlockFile;
aliasList.Add(aliasedFile); //OD TODO: check to see if we need to remove this when done decoding.
//I don't immediately see a place where aliased files remove when a file is closed.
@@ -970,10 +976,12 @@ BlockFile *DirManager::CopyBlockFile(BlockFile *b)
if (!fn.IsOk())
// Block files with uninitialized filename (i.e. SilentBlockFile)
// just need an in-memory copy.
b2 = b->Copy(wxFileName());
b2 = b->Copy(wxFileNameWrapper{});
else
{
wxFileName newFile = MakeBlockFileName();
wxFileNameWrapper newFile{ MakeBlockFileName() };
const wxString newName{newFile.GetName()};
const wxString newPath{ newFile.GetFullPath() };
// We assume that the NEW file should have the same extension
// as the existing file
@@ -988,13 +996,13 @@ BlockFile *DirManager::CopyBlockFile(BlockFile *b)
return NULL;
}
b2 = b->Copy(newFile);
b2 = b->Copy(std::move(newFile));
if (b2 == NULL)
return NULL;
mBlockFileHash[newFile.GetName()]=b2;
aliasList.Add(newFile.GetFullPath());
mBlockFileHash[newName]=b2;
aliasList.Add(newPath);
}
return b2;
@@ -1115,23 +1123,23 @@ bool DirManager::MoveOrCopyToNewProjectDirectory(BlockFile *f, bool copy)
return true;
}
wxFileName newFileName;
wxFileNameWrapper newFileName;
if (!this->AssignFile(newFileName, oldFileName.GetFullName(), false))
return false;
if (newFileName != oldFileName) {
//check to see that summary exists before we copy.
bool summaryExisted = f->IsSummaryAvailable();
auto oldPath = oldFileName.GetFullPath();
auto newPath = newFileName.GetFullPath();
if (summaryExisted) {
auto oldPath = oldFileName.GetFullPath();
auto newPath = newFileName.GetFullPath();
auto success = copy
? wxCopyFile(oldPath, newPath)
: wxRenameFile(oldPath, newPath);
if (!success)
return false;
}
f->SetFileName(newFileName);
f->SetFileName(std::move(newFileName));
//there is a small chance that the summary has begun to be computed on a different thread with the
//original filename. we need to catch this case by waiting for it to finish and then copy.
@@ -1145,9 +1153,7 @@ bool DirManager::MoveOrCopyToNewProjectDirectory(BlockFile *f, bool copy)
//if it doesn't, we can assume it was written to the NEW name, which is fine.
if (oldFileName.FileExists())
{
auto oldPath = oldFileName.GetFullPath();
bool ok = wxCopyFile(oldPath,
newFileName.GetFullPath());
bool ok = wxCopyFile(oldPath, newPath);
if(ok && !copy)
wxRemoveFile(oldPath);
else if (!ok)
@@ -1201,12 +1207,13 @@ void DirManager::Deref(BlockFile * f)
}
}
bool DirManager::EnsureSafeFilename(wxFileName fName)
bool DirManager::EnsureSafeFilename(const wxFileName &fName)
{
// Quick check: If it's not even in our alias list,
// then the file name is A-OK.
if (aliasList.Index(fName.GetFullPath()) == wxNOT_FOUND)
const wxString fullPath{fName.GetFullPath()};
if (aliasList.Index(fullPath) == wxNOT_FOUND)
return true;
/* i18n-hint: 'old' is part of a filename used when a file is renamed. */
@@ -1214,7 +1221,7 @@ bool DirManager::EnsureSafeFilename(wxFileName fName)
/* i18n-hint: e.g. Try to go from "mysong.wav" to "mysong-old1.wav". */
// Keep trying until we find a filename that doesn't exist.
wxFileName renamedFileName = fName;
wxFileNameWrapper renamedFileName{ fName };
int i = 0;
do {
i++;
@@ -1227,11 +1234,12 @@ bool DirManager::EnsureSafeFilename(wxFileName fName)
// Test creating a file by that name to make sure it will
// be possible to do the rename
wxFile testFile(renamedFileName.GetFullPath(), wxFile::write);
const wxString renamedFullPath{ renamedFileName.GetFullPath() };
wxFile testFile(renamedFullPath, wxFile::write);
if (!testFile.IsOpened()) {
{ // need braces to avoid compiler warning about ambiguous else, see the macro
wxLogSysError(_("Unable to open/create test file."),
renamedFileName.GetFullPath().c_str());
renamedFullPath.c_str());
}
return false;
}
@@ -1239,16 +1247,16 @@ bool DirManager::EnsureSafeFilename(wxFileName fName)
// Close the file prior to renaming.
testFile.Close();
if (!wxRemoveFile(renamedFileName.GetFullPath())) {
if (!wxRemoveFile(renamedFullPath)) {
/* i18n-hint: %s is the name of a file.*/
{ // need braces to avoid compiler warning about ambiguous else, see the macro
wxLogSysError(_("Unable to remove '%s'."),
renamedFileName.GetFullPath().c_str());
renamedFullPath.c_str());
}
return false;
}
wxPrintf(_("Renamed file: %s\n"), renamedFileName.GetFullPath().c_str());
wxPrintf(_("Renamed file: %s\n"), renamedFullPath.c_str());
// Go through our block files and see if any indeed point to
// the file we're concerned about. If so, point the block file
@@ -1263,18 +1271,18 @@ bool DirManager::EnsureSafeFilename(wxFileName fName)
// don't worry, we don't rely on this cast unless IsAlias is true
AliasBlockFile *ab = (AliasBlockFile*)b;
// don't worry, we don't rely on this cast unless ISDataAvailable is false
// which means that it still needs to access the file.
ODDecodeBlockFile *db = (ODDecodeBlockFile*)b;
if (b->IsAlias() && ab->GetAliasedFileName() == fName) {
needToRename = true;
//ODBlocks access the aliased file on another thread, so we need to pause them before this continues.
ab->LockRead();
}
//now for encoded OD blocks (e.g. flac)
// don't worry, we don't rely on this cast unless ISDataAvailable is false
// which means that it still needs to access the file.
ODDecodeBlockFile *db = (ODDecodeBlockFile*)b;
if (!b->IsDataAvailable() && db->GetEncodedAudioFilename() == fName) {
else if (!b->IsDataAvailable() && db->GetEncodedAudioFilename() == fName) {
needToRename = true;
//ODBlocks access the aliased file on another thread, so we need to pause them before this continues.
@@ -1285,8 +1293,8 @@ bool DirManager::EnsureSafeFilename(wxFileName fName)
}
if (needToRename) {
if (!wxRenameFile(fName.GetFullPath(),
renamedFileName.GetFullPath()))
if (!wxRenameFile(fullPath,
renamedFullPath))
{
// ACK!!! The renaming was unsuccessful!!!
// (This shouldn't happen, since we tried creating a
@@ -1312,8 +1320,8 @@ bool DirManager::EnsureSafeFilename(wxFileName fName)
// Print error message and cancel the export
wxLogSysError(_("Unable to rename '%s' to '%s'."),
fName.GetFullPath().c_str(),
renamedFileName.GetFullPath().c_str());
fullPath.c_str(),
renamedFullPath.c_str());
return false;
}
else
@@ -1328,13 +1336,13 @@ bool DirManager::EnsureSafeFilename(wxFileName fName)
if (b->IsAlias() && ab->GetAliasedFileName() == fName)
{
ab->ChangeAliasedFileName(renamedFileName);
ab->ChangeAliasedFileName(std::move(renamedFileName));
ab->UnlockRead();
wxPrintf(_("Changed block %s to new alias name\n"), b->GetFileName().GetFullName().c_str());
}
if (!b->IsDataAvailable() && db->GetEncodedAudioFilename() == fName) {
db->ChangeAudioFile(renamedFileName);
else if (!b->IsDataAvailable() && db->GetEncodedAudioFilename() == fName) {
db->ChangeAudioFile(std::move(renamedFileName));
db->UnlockRead();
}
++iter;
@@ -1342,8 +1350,8 @@ bool DirManager::EnsureSafeFilename(wxFileName fName)
}
aliasList.Remove(fName.GetFullPath());
aliasList.Add(renamedFileName.GetFullPath());
aliasList.Remove(fullPath);
aliasList.Add(renamedFullPath);
}
// Success!!! Either we successfully renamed the file,
@@ -1479,9 +1487,9 @@ _("Project check of \"%s\" folder \
// This is done, eventually, in PCMAliasBlockFile::ReadData()
// and ODPCMAliasBlockFile::ReadData, in the stack of b->Recover().
// There, if the mAliasedFileName is bad, it zeroes the data.
wxFileName dummy;
wxFileNameWrapper dummy;
dummy.Clear();
b->ChangeAliasedFileName(dummy);
b->ChangeAliasedFileName(std::move(dummy));
b->Recover();
nResult = FSCKstatus_CHANGED | FSCKstatus_SAVE_AUP;
}
@@ -1699,7 +1707,7 @@ void DirManager::FindMissingAliasedFiles(
BlockFile *b = iter->second;
if (b->IsAlias())
{
wxFileName aliasedFileName = ((AliasBlockFile*)b)->GetAliasedFileName();
const wxFileName &aliasedFileName = ((AliasBlockFile*)b)->GetAliasedFileName();
wxString aliasedFileFullPath = aliasedFileName.GetFullPath();
// wxEmptyString can happen if user already chose to "replace... with silence".
if ((aliasedFileFullPath != wxEmptyString) &&
@@ -1730,13 +1738,13 @@ void DirManager::FindMissingAUFs(
BlockHash::iterator iter = mBlockFileHash.begin();
while (iter != mBlockFileHash.end())
{
wxString key = iter->first;
const wxString &key = iter->first;
BlockFile *b = iter->second;
if (b->IsAlias() && b->IsSummaryAvailable())
{
/* don't look in hash; that might find files the user moved
that the Blockfile abstraction can't find itself */
wxFileName fileName = MakeBlockFilePath(key);
wxFileNameWrapper fileName{ MakeBlockFilePath(key) };
fileName.SetName(key);
fileName.SetExt(wxT("auf"));
if (!fileName.FileExists())
@@ -1756,11 +1764,11 @@ void DirManager::FindMissingAUs(
BlockHash::iterator iter = mBlockFileHash.begin();
while (iter != mBlockFileHash.end())
{
wxString key = iter->first;
const wxString &key = iter->first;
BlockFile *b = iter->second;
if (!b->IsAlias())
{
wxFileName fileName = MakeBlockFilePath(key);
wxFileNameWrapper fileName{ MakeBlockFilePath(key) };
fileName.SetName(key);
fileName.SetExt(wxT("au"));
if (!fileName.FileExists())
@@ -1783,13 +1791,14 @@ void DirManager::FindOrphanBlockFiles(
for (size_t i = 0; i < filePathArray.GetCount(); i++)
{
wxFileName fullname = filePathArray[i];
const wxFileName &fullname = filePathArray[i];
wxString basename = fullname.GetName();
const wxString ext{fullname.GetExt()};
if ((mBlockFileHash.find(basename) == mBlockFileHash.end()) && // is orphan
// Consider only Audacity data files.
// Specifically, ignore <branding> JPG and <import> OGG ("Save Compressed Copy").
(fullname.GetExt().IsSameAs(wxT("au")) ||
fullname.GetExt().IsSameAs(wxT("auf"))))
(ext.IsSameAs(wxT("au")) ||
ext.IsSameAs(wxT("auf"))))
{
if (!clipboardDM) {
TrackList *clipTracks = AudacityProject::GetClipboardTracks();