1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-16 16:10:06 +02:00

AUP3: Do not use compacted file if it grows

This can happen because the new file is constructed differently
than the original and SQLite may introduce additional b-tree
pages as a result.
This commit is contained in:
Leland Lucius 2020-08-02 08:03:41 -05:00
parent 9eabb40524
commit 96931a8b0c
2 changed files with 30 additions and 17 deletions

View File

@ -1029,25 +1029,38 @@ void ProjectFileIO::Compact(const std::shared_ptr<TrackList> &tracks, bool force
// Must close the database to rename it // Must close the database to rename it
if (CloseConnection()) if (CloseConnection())
{ {
// Rename the original to backup // Only use the new file if it is actually smaller than the original.
if (wxRenameFile(origName, backName)) //
// 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 // Rename the original to backup
if (wxRenameFile(tempName, origName)) if (wxRenameFile(origName, backName))
{ {
// Open the newly compacted original file // Rename the temporary to original
OpenConnection(origName); if (wxRenameFile(tempName, origName))
{
// Open the newly compacted original file
OpenConnection(origName);
// Remove the old original file // Remove the old original file
wxRemoveFile(backName); wxRemoveFile(backName);
// Remember that we compacted // Remember that we compacted
mWasCompacted = true; mWasCompacted = true;
return; return;
}
wxRenameFile(backName, origName);
} }
wxRenameFile(backName, origName);
} }
OpenConnection(origName); OpenConnection(origName);

View File

@ -162,9 +162,9 @@ void OnCompact(const CommandContext &context)
int64_t total = projectFileIO.GetTotalUsage(); int64_t total = projectFileIO.GetTotalUsage();
int64_t used = projectFileIO.GetCurrentUsage(currentTracks); int64_t used = projectFileIO.GetCurrentUsage(currentTracks);
auto baseFile = wxFileName(projectFileIO.GetFileName()); auto baseFile = projectFileIO.GetFileName();
auto walFile = wxFileName(projectFileIO.GetFileName() + wxT("-wal")); auto walFile = baseFile + wxT("-wal");
auto before = baseFile.GetSize() + walFile.GetSize(); auto before = wxFileName::GetSize(baseFile) + wxFileName::GetSize(walFile);
int id = AudacityMessageBox( int id = AudacityMessageBox(
XO("Compacting this project will free up disk space by removing unused bytes within the file.\n\n" 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); projectFileIO.Compact(currentTracks, true);
auto after = baseFile.GetSize() + walFile.GetSize(); auto after = wxFileName::GetSize(baseFile) + wxFileName::GetSize(walFile);
AudacityMessageBox( AudacityMessageBox(
XO("Compacting actually freed %s of disk space.") XO("Compacting actually freed %s of disk space.")