From 6463e12576116d775c210b9f050364d55d121146 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 28 Dec 2017 16:58:42 -0500 Subject: [PATCH] Move MyCopyFile into class FileNames --- src/DirManager.cpp | 35 ++++------------------------------- src/FileNames.cpp | 26 ++++++++++++++++++++++++++ src/FileNames.h | 4 ++++ 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/DirManager.cpp b/src/DirManager.cpp index e08064940..7efff54b6 100644 --- a/src/DirManager.cpp +++ b/src/DirManager.cpp @@ -91,6 +91,7 @@ #include "AudacityException.h" #include "BlockFile.h" #include "FileException.h" +#include "FileNames.h" #include "blockfile/LegacyBlockFile.h" #include "blockfile/LegacyAliasBlockFile.h" #include "blockfile/SimpleBlockFile.h" @@ -1161,34 +1162,6 @@ bool DirManager::ContainsBlockFile(const wxString &filepath) const BlockFilePtr{ it->second.lock() }; } -namespace { - bool MyCopyFile(const wxString& file1, const wxString& file2, - bool overwrite = true) - { -#ifdef __WXMSW__ - - // workaround not needed - return wxCopyFile(file1, file2, overwrite); - -#else - // PRL: Compensate for buggy wxCopyFile that returns false success, - // which was a cause of case 4 in comment 10 of - // http://bugzilla.audacityteam.org/show_bug.cgi?id=1759 - // Destination file was created, but was empty - // Bug was introduced after wxWidgets 2.8.12 at commit - // 0597e7f977c87d107e24bf3e95ebfa3d60efc249 of wxWidgets repo - - bool existed = wxFileExists(file2); - bool result = wxCopyFile(file1, file2, overwrite) && - wxFile{ file1 }.Length() == wxFile{ file2 }.Length(); - if (!result && !existed) - wxRemoveFile(file2); - return result; - -#endif - } -} - // Adds one to the reference count of the block file, // UNLESS it is "locked", then it makes a NEW copy of // the BlockFile. @@ -1232,7 +1205,7 @@ BlockFilePtr DirManager::CopyBlockFile(const BlockFilePtr &b) //a summary file, so we should check before we copy. if(b->IsSummaryAvailable()) { - if( !MyCopyFile(fn.GetFullPath(), + if( !FileNames::CopyFile(fn.GetFullPath(), newFile.GetFullPath()) ) // Disk space exhaustion, maybe throw FileException{ @@ -1384,7 +1357,7 @@ std::pair DirManager::CopyToNewProjectDirectory(BlockFile *f) bool summaryExisted = f->IsSummaryAvailable(); auto oldPath = oldFileNameRef.GetFullPath(); if (summaryExisted) { - auto success = MyCopyFile(oldPath, newPath); + auto success = FileNames::CopyFile(oldPath, newPath); if (!success) return { false, {} }; } @@ -1415,7 +1388,7 @@ std::pair DirManager::CopyToNewProjectDirectory(BlockFile *f) //if it doesn't, we can assume it was written to the NEW name, which is fine. if (oldFileName.FileExists()) { - bool ok = MyCopyFile(oldPath, newPath); + bool ok = FileNames::CopyFile(oldPath, newPath); if (!ok) return { false, {} }; } diff --git a/src/FileNames.cpp b/src/FileNames.cpp index 6743b3739..041ba11d4 100644 --- a/src/FileNames.cpp +++ b/src/FileNames.cpp @@ -43,6 +43,32 @@ used throughout Audacity into this one place. static wxString gDataDir; +bool FileNames::CopyFile( + const wxString& file1, const wxString& file2, bool overwrite) +{ +#ifdef __WXMSW__ + + // workaround not needed + return wxCopyFile(file1, file2, overwrite); + +#else + // PRL: Compensate for buggy wxCopyFile that returns false success, + // which was a cause of case 4 in comment 10 of + // http://bugzilla.audacityteam.org/show_bug.cgi?id=1759 + // Destination file was created, but was empty + // Bug was introduced after wxWidgets 2.8.12 at commit + // 0597e7f977c87d107e24bf3e95ebfa3d60efc249 of wxWidgets repo + + bool existed = wxFileExists(file2); + bool result = wxCopyFile(file1, file2, overwrite) && + wxFile{ file1 }.Length() == wxFile{ file2 }.Length(); + if (!result && !existed) + wxRemoveFile(file2); + return result; + +#endif +} + wxString FileNames::MkDir(const wxString &Str) { // Behaviour of wxFileName::DirExists() and wxFileName::MkDir() has diff --git a/src/FileNames.h b/src/FileNames.h index 4d290b6ef..47989606c 100644 --- a/src/FileNames.h +++ b/src/FileNames.h @@ -23,6 +23,10 @@ class wxArrayString; class AUDACITY_DLL_API FileNames { public: + // This exists to compensate for bugs in wxCopyFile: + static bool CopyFile( + const wxString& file1, const wxString& file2, bool overwrite = true); + static wxString MkDir(const wxString &Str); static wxString TempDir();