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 <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 ());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -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,26 +130,27 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Datafile::dump () const
|
||||
|
@ -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 ()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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};
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <FS.h>
|
||||
#include <AtomicFile.h>
|
||||
#include <format.h>
|
||||
#include <Journal.h>
|
||||
#include <TransactionsFactory.h>
|
||||
|
@ -54,20 +54,10 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Journal::recordConfigAction (const std::string& before, const std::string& after)
|
||||
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -37,6 +37,8 @@ class Journal
|
|||
{
|
||||
public:
|
||||
Journal() = default;
|
||||
Journal(const Journal&) = delete;
|
||||
Journal& operator= (const Journal&) = delete;
|
||||
|
||||
void initialize(const std::string&);
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <cassert>
|
||||
#include <cerrno>
|
||||
#include <inttypes.h>
|
||||
#include <AtomicFile.h>
|
||||
#include <JSON.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -123,9 +124,18 @@ 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 ())
|
||||
try
|
||||
{
|
||||
AtomicFile::read (file, contents);
|
||||
if (contents.length ())
|
||||
{
|
||||
parse (contents, nest);
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string Rules::file () const
|
||||
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue