Datafile: Remove empty files

If calling undo on an operation the removes many entries from the
datafiles, there would be many 0 length size files remaining in the data
directory. Now they will be removed if they are empty so they will not
longer need to be checked when parsing the database.

Signed-off-by: Shaun Ruffell <sruffell@sruffell.net>
This commit is contained in:
Shaun Ruffell 2020-06-06 20:41:48 -05:00 committed by lauft
parent b2a8fe6f55
commit b0efdbc9ee

View file

@ -148,23 +148,30 @@ void Datafile::commit ()
if (_dirty)
{
AtomicFile file (_file);
if (file.open ())
if (_lines.size () > 0)
{
// Sort the intervals by ascending start time.
std::sort (_lines.begin (), _lines.end ());
// Write out all the lines.
file.truncate ();
for (auto& line : _lines)
if (file.open ())
{
file.write_raw (line + '\n');
}
// Sort the intervals by ascending start time.
std::sort (_lines.begin (), _lines.end ());
_dirty = false;
// Write out all the lines.
file.truncate ();
for (auto& line : _lines)
{
file.write_raw (line + '\n');
}
_dirty = false;
}
else
{
throw format ("Could not write to data file {1}", _file._data);
}
}
else
{
throw format ("Could not write to data file {1}", _file._data);
file.remove ();
}
}
}