mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
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:
parent
8e99c07d85
commit
4b3a907cbb
7 changed files with 47 additions and 36 deletions
|
@ -33,6 +33,7 @@
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <shared.h>
|
#include <shared.h>
|
||||||
#include <timew.h>
|
#include <timew.h>
|
||||||
|
#include <AtomicFile.h>
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
Database::iterator::iterator (files_iterator fbegin, files_iterator fend) :
|
Database::iterator::iterator (files_iterator fbegin, files_iterator fend) :
|
||||||
|
@ -257,7 +258,7 @@ void Database::commit ()
|
||||||
file.commit ();
|
file.commit ();
|
||||||
}
|
}
|
||||||
|
|
||||||
File::write (_location + "/tags.data", _tagInfoDatabase.toJson ());
|
AtomicFile::write (_location + "/tags.data", _tagInfoDatabase.toJson ());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -32,11 +32,12 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <AtomicFile.h>
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void Datafile::initialize (const std::string& name)
|
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.
|
// From the name, which is of the form YYYY-MM.data, extract the YYYY and MM.
|
||||||
auto basename = _file.name ();
|
auto basename = _file.name ();
|
||||||
|
@ -129,24 +130,25 @@ void Datafile::commit ()
|
||||||
// The _dirty flag indicates that the file needs to be written.
|
// The _dirty flag indicates that the file needs to be written.
|
||||||
if (_dirty)
|
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.
|
// Sort the intervals by ascending start time.
|
||||||
std::sort (_lines.begin (), _lines.end ());
|
std::sort (_lines.begin (), _lines.end ());
|
||||||
|
|
||||||
// Write out all the lines.
|
// Write out all the lines.
|
||||||
|
file.truncate ();
|
||||||
for (auto& line : _lines)
|
for (auto& line : _lines)
|
||||||
_file.write_raw (line + '\n');
|
{
|
||||||
|
file.write_raw (line + '\n');
|
||||||
|
}
|
||||||
|
|
||||||
_file.close ();
|
|
||||||
_dirty = false;
|
_dirty = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
throw format ("Could not write to data file {1}", _file._data);
|
throw format ("Could not write to data file {1}", _file._data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,21 +170,20 @@ std::string Datafile::dump () const
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void Datafile::load_lines ()
|
void Datafile::load_lines ()
|
||||||
{
|
{
|
||||||
if (_file.open ())
|
AtomicFile file (_file);
|
||||||
|
if (file.open ())
|
||||||
{
|
{
|
||||||
_file.lock ();
|
|
||||||
|
|
||||||
// Load the data.
|
// Load the data.
|
||||||
std::vector <std::string> read_lines;
|
std::vector <std::string> read_lines;
|
||||||
_file.read (read_lines);
|
file.read (read_lines);
|
||||||
_file.close ();
|
file.close ();
|
||||||
|
|
||||||
// Append the lines that were read.
|
// Append the lines that were read.
|
||||||
for (auto& line : read_lines)
|
for (auto& line : read_lines)
|
||||||
_lines.push_back (line);
|
_lines.push_back (line);
|
||||||
|
|
||||||
_lines_loaded = true;
|
_lines_loaded = true;
|
||||||
debug (format ("{1}: {2} intervals", _file.name (), read_lines.size ()));
|
debug (format ("{1}: {2} intervals", file.name (), read_lines.size ()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ private:
|
||||||
void load_lines ();
|
void load_lines ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
File _file {};
|
Path _file {};
|
||||||
bool _dirty {false};
|
bool _dirty {false};
|
||||||
std::vector <std::string> _lines {};
|
std::vector <std::string> _lines {};
|
||||||
bool _lines_loaded {false};
|
bool _lines_loaded {false};
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
//
|
//
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <FS.h>
|
#include <AtomicFile.h>
|
||||||
#include <format.h>
|
#include <format.h>
|
||||||
#include <Journal.h>
|
#include <Journal.h>
|
||||||
#include <TransactionsFactory.h>
|
#include <TransactionsFactory.h>
|
||||||
|
@ -54,19 +54,9 @@ void Journal::endTransaction ()
|
||||||
throw "Call to end non-existent transaction";
|
throw "Call to end non-existent transaction";
|
||||||
}
|
}
|
||||||
|
|
||||||
File undo (_location);
|
AtomicFile::append (_location, _currentTransaction->toString ());
|
||||||
|
|
||||||
if (undo.open ())
|
_currentTransaction.reset ();
|
||||||
{
|
|
||||||
undo.append (_currentTransaction->toString());
|
|
||||||
|
|
||||||
undo.close ();
|
|
||||||
_currentTransaction.reset ();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw format ("Unable to write the undo transaction to {1}", undo._data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -102,7 +92,7 @@ void Journal::recordUndoAction (
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
Transaction Journal::popLastTransaction ()
|
Transaction Journal::popLastTransaction ()
|
||||||
{
|
{
|
||||||
File undo (_location);
|
AtomicFile undo (_location);
|
||||||
|
|
||||||
std::vector <std::string> read_lines;
|
std::vector <std::string> read_lines;
|
||||||
undo.read (read_lines);
|
undo.read (read_lines);
|
||||||
|
@ -125,8 +115,8 @@ Transaction Journal::popLastTransaction ()
|
||||||
Transaction last = transactions.back ();
|
Transaction last = transactions.back ();
|
||||||
transactions.pop_back ();
|
transactions.pop_back ();
|
||||||
|
|
||||||
File::remove (undo._data);
|
|
||||||
undo.open ();
|
undo.open ();
|
||||||
|
undo.truncate ();
|
||||||
|
|
||||||
for (auto& transaction : transactions)
|
for (auto& transaction : transactions)
|
||||||
{
|
{
|
||||||
|
|
|
@ -37,6 +37,8 @@ class Journal
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Journal() = default;
|
Journal() = default;
|
||||||
|
Journal(const Journal&) = delete;
|
||||||
|
Journal& operator= (const Journal&) = delete;
|
||||||
|
|
||||||
void initialize(const std::string&);
|
void initialize(const std::string&);
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <AtomicFile.h>
|
||||||
#include <JSON.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.
|
// Read the file, then parse the contents.
|
||||||
std::string contents;
|
std::string contents;
|
||||||
if (File::read (file, contents) && contents.length ())
|
try
|
||||||
parse (contents, nest);
|
{
|
||||||
|
AtomicFile::read (file, contents);
|
||||||
|
if (contents.length ())
|
||||||
|
{
|
||||||
|
parse (contents, nest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -624,7 +634,9 @@ bool Rules::setConfigVariable (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (change)
|
if (change)
|
||||||
File::write (rules.file (), lines);
|
{
|
||||||
|
AtomicFile::write (rules.file (), lines);
|
||||||
|
}
|
||||||
|
|
||||||
return change;
|
return change;
|
||||||
}
|
}
|
||||||
|
@ -648,7 +660,7 @@ int Rules::unsetConfigVariable (
|
||||||
|
|
||||||
// Read config file as lines of text.
|
// Read config file as lines of text.
|
||||||
std::vector <std::string> lines;
|
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:
|
// If there is a non-comment line containing the entry in flattened form:
|
||||||
// a.b.c = value
|
// a.b.c = value
|
||||||
|
@ -703,7 +715,9 @@ int Rules::unsetConfigVariable (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (change)
|
if (change)
|
||||||
File::write (rules.file (), lines);
|
{
|
||||||
|
AtomicFile::write (rules.file (), lines);
|
||||||
|
}
|
||||||
|
|
||||||
if (change && found)
|
if (change && found)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <cmake.h>
|
#include <cmake.h>
|
||||||
|
#include <AtomicFile.h>
|
||||||
#include <CLI.h>
|
#include <CLI.h>
|
||||||
#include <Database.h>
|
#include <Database.h>
|
||||||
#include <Rules.h>
|
#include <Rules.h>
|
||||||
|
@ -96,6 +97,8 @@ int main (int argc, const char** argv)
|
||||||
|
|
||||||
// Save any outstanding changes.
|
// Save any outstanding changes.
|
||||||
database.commit ();
|
database.commit ();
|
||||||
|
|
||||||
|
AtomicFile::finalize_all ();
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (const std::string& error)
|
catch (const std::string& error)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue