Commit 60ec2001 authored by jan.koester's avatar jan.koester
Browse files

test

parent 08cc2897
Loading
Loading
Loading
Loading
Loading
+43 −3
Changes for src/backends/cluster.cpp: 43 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include <cstring>
#include <iostream>
#include <algorithm>
#include <fstream>

#include <crypto/sha.h>

@@ -105,11 +106,18 @@ namespace authdb {
        _Dirty = false;
    }

    ClusterBackend::ClusterBackend(const char *domain, size_t version)
    ClusterBackend::ClusterBackend(const char *domain, size_t version, const char *filepath)
        : _Domain(domain), _Pos(0), _Dirty(false),
          _ParityGroupId(0), _HasParityGroup(false)
    {
        // Try to fetch existing data from cluster
        if (filepath && filepath[0] != '\0')
            _FilePath = filepath;

        // Try to load from local file first
        loadFromFile();

        // Then try to fetch from cluster peers
        if (_Buffer.empty())
            fetchFromCluster();

        if (_Buffer.empty()) {
@@ -127,12 +135,15 @@ namespace authdb {

            // Push initial header to cluster
            pushToCluster();
            saveToFile();
        }
    }

    ClusterBackend::~ClusterBackend() {
        if (_Dirty)
        if (_Dirty) {
            pushToCluster();
            saveToFile();
        }
    }

    void ClusterBackend::read(unsigned char *src, size_t srcsize) {
@@ -158,6 +169,7 @@ namespace authdb {

    void ClusterBackend::unlock() {
        pushToCluster();
        saveToFile();
    }

    void ClusterBackend::setPos(size_t pos) {
@@ -260,4 +272,32 @@ namespace authdb {
        _Dirty = true;
    }

    void ClusterBackend::loadFromFile() {
        if (_FilePath.empty())
            return;

        std::ifstream in(_FilePath, std::ios::binary | std::ios::ate);
        if (!in.is_open())
            return;

        auto size = in.tellg();
        if (size <= 0)
            return;

        in.seekg(0, std::ios::beg);
        _Buffer.resize(static_cast<size_t>(size));
        in.read(reinterpret_cast<char*>(_Buffer.data()), size);
    }

    void ClusterBackend::saveToFile() {
        if (_FilePath.empty() || _Buffer.empty())
            return;

        std::ofstream out(_FilePath, std::ios::binary | std::ios::trunc);
        if (!out.is_open())
            return;

        out.write(reinterpret_cast<const char*>(_Buffer.data()), _Buffer.size());
    }

}