shepherd/src/memdb.h

61 lines
1.7 KiB
C
Raw Normal View History

2019-03-28 10:49:59 +00:00
//
// Created by jedi on 3/12/19.
//
#include <string>
#include <sys/stat.h>
#include <iostream>
#include <filesystem>
#include "spmemvfs.h"
#include "crypto.h"
#ifndef SHEPHERD_MEMDB_H
#define SHEPHERD_MEMDB_H
namespace shepherd{
class memdb{
private:
spmemvfs_db_t db_;
spmembuffer_t * mem_;
bytes buf_;
std::string path_;
std::string secret_;
void load(){
if(std::filesystem::exists(path_)){
buf_ = crypto::load(path_,secret_);
if(buf_.size()==0){
std::cerr << "no dbfile found" << std::endl;
exit(1);
}
}else{
std::cerr << "Error: No such file" << std::endl;
buf_ = crypto::load("/home/jedi/.shepherd/passwd.db");
}
mem_->total = mem_->used = buf_.size();
mem_->data = (char*) buf_.data();
}
public:
void save(){
bytes buf(mem_->data, &mem_->data[mem_->used]);
crypto::save(path_,buf,secret_);
}
memdb(const std::string &path, const std::string &secret):path_(path),secret_(secret){
mem_ = (spmembuffer_t*)calloc( sizeof( spmembuffer_t ), 1 );
spmemvfs_env_init();
load();
spmemvfs_open_db( &db_, path.c_str(), mem_ );
}
sqlite3* handle(){
return db_.handle;
}
~memdb(){
save();
spmemvfs_close_db( &db_ );
spmemvfs_env_fini();
}
};
}
#endif //SHEPHERD_MEMDB_H