Use AtomicFile for data, tags, undo, and config files

Now changes accross all of these files either happen all together or not
at all.

Related to issue #155
This commit is contained in:
Shaun Ruffell 2020-02-16 12:52:12 -06:00 committed by lauft
parent 8e99c07d85
commit 4b3a907cbb
7 changed files with 47 additions and 36 deletions

View file

@ -33,6 +33,7 @@
#include <iomanip>
#include <shared.h>
#include <timew.h>
#include <AtomicFile.h>
////////////////////////////////////////////////////////////////////////////////
Database::iterator::iterator (files_iterator fbegin, files_iterator fend) :
@ -257,7 +258,7 @@ void Database::commit ()
file.commit ();
}
File::write (_location + "/tags.data", _tagInfoDatabase.toJson ());
AtomicFile::write (_location + "/tags.data", _tagInfoDatabase.toJson ());
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -32,11 +32,12 @@
#include <sstream>
#include <cassert>
#include <stdlib.h>
#include <AtomicFile.h>
////////////////////////////////////////////////////////////////////////////////
void Datafile::initialize (const std::string& name)
{
_file = File (name);
_file = Path (name);
// From the name, which is of the form YYYY-MM.data, extract the YYYY and MM.
auto basename = _file.name ();
@ -129,24 +130,25 @@ void Datafile::commit ()
// The _dirty flag indicates that the file needs to be written.
if (_dirty)
{
if (_file.open ())
AtomicFile file (_file);
if (file.open ())
{
_file.lock ();
_file.truncate ();
_file.append (std::string ("")); // Seek to EOF.
// Sort the intervals by ascending start time.
std::sort (_lines.begin (), _lines.end ());
// Write out all the lines.
file.truncate ();
for (auto& line : _lines)
_file.write_raw (line + '\n');
{
file.write_raw (line + '\n');
}
_file.close ();
_dirty = false;
}
else
{
throw format ("Could not write to data file {1}", _file._data);
}
}
}
@ -168,21 +170,20 @@ std::string Datafile::dump () const
////////////////////////////////////////////////////////////////////////////////
void Datafile::load_lines ()
{
if (_file.open ())
AtomicFile file (_file);
if (file.open ())
{
_file.lock ();
// Load the data.
std::vector <std::string> read_lines;
_file.read (read_lines);
_file.close ();
file.read (read_lines);
file.close ();
// Append the lines that were read.
for (auto& line : read_lines)
_lines.push_back (line);
_lines_loaded = true;
debug (format ("{1}: {2} intervals", _file.name (), read_lines.size ()));
debug (format ("{1}: {2} intervals", file.name (), read_lines.size ()));
}
}

View file

@ -53,7 +53,7 @@ private:
void load_lines ();
private:
File _file {};
Path _file {};
bool _dirty {false};
std::vector <std::string> _lines {};
bool _lines_loaded {false};

View file

@ -24,7 +24,7 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <FS.h>
#include <AtomicFile.h>
#include <format.h>
#include <Journal.h>
#include <TransactionsFactory.h>
@ -54,19 +54,9 @@ void Journal::endTransaction ()
throw "Call to end non-existent transaction";
}
File undo (_location);
AtomicFile::append (_location, _currentTransaction->toString ());
if (undo.open ())
{
undo.append (_currentTransaction->toString());
undo.close ();
_currentTransaction.reset ();
}
else
{
throw format ("Unable to write the undo transaction to {1}", undo._data);
}
_currentTransaction.reset ();
}
////////////////////////////////////////////////////////////////////////////////
@ -102,7 +92,7 @@ void Journal::recordUndoAction (
////////////////////////////////////////////////////////////////////////////////
Transaction Journal::popLastTransaction ()
{
File undo (_location);
AtomicFile undo (_location);
std::vector <std::string> read_lines;
undo.read (read_lines);
@ -125,8 +115,8 @@ Transaction Journal::popLastTransaction ()
Transaction last = transactions.back ();
transactions.pop_back ();
File::remove (undo._data);
undo.open ();
undo.truncate ();
for (auto& transaction : transactions)
{

View file

@ -37,6 +37,8 @@ class Journal
{
public:
Journal() = default;
Journal(const Journal&) = delete;
Journal& operator= (const Journal&) = delete;
void initialize(const std::string&);

View file

@ -34,6 +34,7 @@
#include <cassert>
#include <cerrno>
#include <inttypes.h>
#include <AtomicFile.h>
#include <JSON.h>
////////////////////////////////////////////////////////////////////////////////
@ -123,8 +124,17 @@ void Rules::load (const std::string& file, int nest /* = 1 */)
// Read the file, then parse the contents.
std::string contents;
if (File::read (file, contents) && contents.length ())
parse (contents, nest);
try
{
AtomicFile::read (file, contents);
if (contents.length ())
{
parse (contents, nest);
}
}
catch (...)
{
}
}
////////////////////////////////////////////////////////////////////////////////
@ -624,7 +634,9 @@ bool Rules::setConfigVariable (
}
if (change)
File::write (rules.file (), lines);
{
AtomicFile::write (rules.file (), lines);
}
return change;
}
@ -648,7 +660,7 @@ int Rules::unsetConfigVariable (
// Read config file as lines of text.
std::vector <std::string> lines;
File::read (rules.file (), lines);
AtomicFile::read (rules.file (), lines);
// If there is a non-comment line containing the entry in flattened form:
// a.b.c = value
@ -703,7 +715,9 @@ int Rules::unsetConfigVariable (
}
if (change)
File::write (rules.file (), lines);
{
AtomicFile::write (rules.file (), lines);
}
if (change && found)
return 0;

View file

@ -25,6 +25,7 @@
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <AtomicFile.h>
#include <CLI.h>
#include <Database.h>
#include <Rules.h>
@ -96,6 +97,8 @@ int main (int argc, const char** argv)
// Save any outstanding changes.
database.commit ();
AtomicFile::finalize_all ();
}
catch (const std::string& error)