summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-11-01 06:01:21 -0500
committerJohn Wiegley <johnw@newartisans.com>2009-11-01 06:01:21 -0500
commit977e7db164cfe8572a5e0080746c34f5a7b1729e (patch)
tree90b8da365c8b9481293e8087ad934c71ec7ce565
parentde3893a08a9d85e672a09f21f20bc3148a4933b8 (diff)
downloadfork-ledger-977e7db164cfe8572a5e0080746c34f5a7b1729e.tar.gz
fork-ledger-977e7db164cfe8572a5e0080746c34f5a7b1729e.tar.bz2
fork-ledger-977e7db164cfe8572a5e0080746c34f5a7b1729e.zip
Improved archive header logic for cache files
-rw-r--r--src/archive.cc86
-rw-r--r--src/archive.h10
-rw-r--r--src/session.cc4
3 files changed, 71 insertions, 29 deletions
diff --git a/src/archive.cc b/src/archive.cc
index a37bad90..b3aff7d7 100644
--- a/src/archive.cc
+++ b/src/archive.cc
@@ -42,7 +42,8 @@
#include "post.h"
#include "xact.h"
-#define ARCHIVE_VERSION 0x03000001
+#define LEDGER_MAGIC 0x4c454447
+#define ARCHIVE_VERSION 0x03000002
//BOOST_IS_ABSTRACT(ledger::scope_t)
BOOST_CLASS_EXPORT(ledger::scope_t)
@@ -63,25 +64,64 @@ template void ledger::journal_t::serialize(boost::archive::binary_iarchive&,
const unsigned int);
namespace ledger {
-void archive_t::read_header()
+namespace {
+ bool read_header_bits(std::istream& in) {
+ uint32_t bytes;
+
+ assert(sizeof(uint32_t) == 4);
+ in.read(reinterpret_cast<char *>(&bytes), sizeof(uint32_t));
+ if (bytes != LEDGER_MAGIC) {
+ DEBUG("archive.journal", "Magic bytes not present");
+ return false;
+ }
+
+ in.read(reinterpret_cast<char *>(&bytes), sizeof(uint32_t));
+ if (bytes != ARCHIVE_VERSION) {
+ DEBUG("archive.journal", "Archive version mismatch");
+ return false;
+ }
+
+ return true;
+ }
+
+ void write_header_bits(std::ostream& out) {
+ uint32_t bytes;
+
+ assert(sizeof(uint32_t) == 4);
+ bytes = LEDGER_MAGIC;
+ out.write(reinterpret_cast<char *>(&bytes), sizeof(uint32_t));
+
+ bytes = ARCHIVE_VERSION;
+ out.write(reinterpret_cast<char *>(&bytes), sizeof(uint32_t));
+ }
+}
+
+bool archive_t::read_header()
{
- if (exists(file)) {
- // Open the stream, read the version number and the list of sources
- ifstream stream(file, std::ios::binary);
- boost::archive::binary_iarchive iarchive(stream);
+ uintmax_t size = file_size(file);
+ if (size < 8)
+ return false;
- DEBUG("archive.journal", "Reading header from archive");
- iarchive >> *this;
+ // Open the stream, read the version number and the list of sources
+ ifstream stream(file, std::ios::binary);
+ if (! read_header_bits(stream))
+ return false;
+
+ boost::archive::binary_iarchive iarchive(stream);
- DEBUG("archive.journal",
- "Version number: " << std::hex << version << std::dec);
- DEBUG("archive.journal", "Number of sources: " << sources.size());
+ DEBUG("archive.journal", "Reading header from archive");
+ iarchive >> *this;
+
+ DEBUG("archive.journal",
+ "Version number: " << std::hex << ARCHIVE_VERSION << std::dec);
+ DEBUG("archive.journal", "Number of sources: " << sources.size());
#if defined(DEBUG_ON)
- foreach (const journal_t::fileinfo_t& i, sources)
- DEBUG("archive.journal", "Loaded source: " << *i.filename);
+ foreach (const journal_t::fileinfo_t& i, sources)
+ DEBUG("archive.journal", "Loaded source: " << *i.filename);
#endif
- }
+
+ return true;
}
bool archive_t::should_load(const std::list<path>& data_files)
@@ -95,8 +135,8 @@ bool archive_t::should_load(const std::list<path>& data_files)
return false;
}
- if (version != ARCHIVE_VERSION) {
- DEBUG("archive.journal", "No, it fails the version check");
+ if (! read_header()) {
+ DEBUG("archive.journal", "No, header failed to read");
return false;
}
@@ -205,10 +245,9 @@ void archive_t::save(shared_ptr<journal_t> journal)
{
INFO_START(archive, "Saved journal file cache");
- ofstream archive(file, std::ios::binary);
- boost::archive::binary_oarchive oa(archive);
+ ofstream stream(file, std::ios::binary);
- version = ARCHIVE_VERSION;
+ write_header_bits(stream);
sources = journal->sources;
#if defined(DEBUG_ON)
@@ -216,8 +255,10 @@ void archive_t::save(shared_ptr<journal_t> journal)
DEBUG("archive.journal", "Saving source: " << *i.filename);
#endif
- DEBUG("archive.journal",
- "Creating archive with version " << std::hex << version << std::dec);
+ boost::archive::binary_oarchive oa(stream);
+
+ DEBUG("archive.journal", "Creating archive with version "
+ << std::hex << ARCHIVE_VERSION << std::dec);
oa << *this;
DEBUG("archive.journal",
@@ -232,6 +273,9 @@ bool archive_t::load(shared_ptr<journal_t> journal)
INFO_START(archive, "Read cached journal file");
ifstream stream(file, std::ios::binary);
+ if (! read_header_bits(stream))
+ return false;
+
boost::archive::binary_iarchive iarchive(stream);
// Skip past the archive header, it was already read in before
diff --git a/src/archive.h b/src/archive.h
index 77272dbe..cd778a03 100644
--- a/src/archive.h
+++ b/src/archive.h
@@ -58,7 +58,6 @@ namespace ledger {
class archive_t
{
path file;
- uint32_t version;
std::list<journal_t::fileinfo_t> sources;
@@ -66,19 +65,17 @@ public:
archive_t() {
TRACE_CTOR(archive_t, "");
}
- archive_t(const path& _file)
- : file(_file), version(0) {
+ archive_t(const path& _file) : file(_file) {
TRACE_CTOR(archive_t, "const path&");
}
- archive_t(const archive_t& ar)
- : file(ar.file), version(0) {
+ archive_t(const archive_t& ar) : file(ar.file) {
TRACE_CTOR(archive_t, "copy");
}
~archive_t() {
TRACE_DTOR(archive_t);
}
- void read_header();
+ bool read_header();
bool should_load(const std::list<path>& data_files);
bool should_save(shared_ptr<journal_t> journal);
@@ -94,7 +91,6 @@ private:
template<class Archive>
void serialize(Archive & ar, const unsigned int /* version */) {
- ar & version;
ar & sources;
}
#endif // HAVE_BOOST_SERIALIZATION
diff --git a/src/session.cc b/src/session.cc
index f3209fe7..3f29716d 100644
--- a/src/session.cc
+++ b/src/session.cc
@@ -130,15 +130,17 @@ std::size_t session_t::read_data(const string& master_account)
price_db_path = resolve_path(HANDLER(price_db_).str());
optional<archive_t> cache;
+#if 1
+ // jww (2009-11-01): The binary caching feature is disabled for now.
if (HANDLED(cache_) && master_account.empty()) {
cache = archive_t(HANDLED(cache_).str());
- cache->read_header();
if (price_db_path) {
HANDLER(file_).data_files.push_back(*price_db_path);
populated_price_db = true;
}
}
+#endif
if (! (cache &&
cache->should_load(HANDLER(file_).data_files) &&