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
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);

View File

@ -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.")