diff --git a/src/ProjectFileIO.cpp b/src/ProjectFileIO.cpp index 47ab343aa..936f0e58b 100644 --- a/src/ProjectFileIO.cpp +++ b/src/ProjectFileIO.cpp @@ -1029,25 +1029,38 @@ void ProjectFileIO::Compact(const std::shared_ptr &tracks, bool force // Must close the database to rename it if (CloseConnection()) { - // Rename the original to backup - if (wxRenameFile(origName, backName)) + // Only use the new file if it is actually smaller than the original. + // + // If the original file doesn't have anything to compact (original and new + // are basically identical), the file could grow by a few pages because of + // differences in how SQLite constructs the b-tree. + // + // In this case, just toss the new file and continue to use the original. + // + // Also, do this after closing the connection so that the -wal file + // gets cleaned up. + if (wxFileName::GetSize(tempName) < wxFileName::GetSize(origName)) { - // Rename the temporary to original - if (wxRenameFile(tempName, origName)) + // Rename the original to backup + if (wxRenameFile(origName, backName)) { - // Open the newly compacted original file - OpenConnection(origName); + // Rename the temporary to original + if (wxRenameFile(tempName, origName)) + { + // Open the newly compacted original file + OpenConnection(origName); - // Remove the old original file - wxRemoveFile(backName); + // Remove the old original file + wxRemoveFile(backName); - // Remember that we compacted - mWasCompacted = true; + // Remember that we compacted + mWasCompacted = true; - return; + return; + } + + wxRenameFile(backName, origName); } - - wxRenameFile(backName, origName); } OpenConnection(origName); diff --git a/src/menus/FileMenus.cpp b/src/menus/FileMenus.cpp index 81d75170b..7febc0146 100644 --- a/src/menus/FileMenus.cpp +++ b/src/menus/FileMenus.cpp @@ -162,9 +162,9 @@ void OnCompact(const CommandContext &context) int64_t total = projectFileIO.GetTotalUsage(); int64_t used = projectFileIO.GetCurrentUsage(currentTracks); - auto baseFile = wxFileName(projectFileIO.GetFileName()); - auto walFile = wxFileName(projectFileIO.GetFileName() + wxT("-wal")); - auto before = baseFile.GetSize() + walFile.GetSize(); + auto baseFile = projectFileIO.GetFileName(); + auto walFile = baseFile + wxT("-wal"); + auto before = wxFileName::GetSize(baseFile) + wxFileName::GetSize(walFile); int id = AudacityMessageBox( XO("Compacting this project will free up disk space by removing unused bytes within the file.\n\n" @@ -192,7 +192,7 @@ void OnCompact(const CommandContext &context) projectFileIO.Compact(currentTracks, true); - auto after = baseFile.GetSize() + walFile.GetSize(); + auto after = wxFileName::GetSize(baseFile) + wxFileName::GetSize(walFile); AudacityMessageBox( XO("Compacting actually freed %s of disk space.")