Skip to content
Snippets Groups Projects
Commit f023e270 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

fileutils: add loadFile, saveFile

Change-Id: Ia65ebf8b9b593a8b56dde8db7b9c14e86d8903ac
parent aef76520
Branches
Tags
No related merge requests found
......@@ -345,35 +345,11 @@ DHTAccount::checkIdentityPath()
dht::crypto::Identity
DHTAccount::loadIdentity() const
{
std::vector<char> buffer;
std::vector<char> buffer_crt;
std::vector<uint8_t> buffer;
std::vector<uint8_t> buffer_crt;
try {
{
std::ifstream file(privkeyPath_, std::ios::binary);
if (!file)
throw std::runtime_error("Can't read private key file.");
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
if (size > std::numeric_limits<unsigned>::max())
throw std::runtime_error("Can't read private key file.");
buffer.resize(size);
file.seekg(0, std::ios::beg);
if (!file.read(buffer.data(), size))
throw std::runtime_error("Can't load private key.");
}
{
std::ifstream file(certPath_, std::ios::binary);
if (!file)
throw std::runtime_error("Can't read certificate file.");
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
if (size > std::numeric_limits<unsigned>::max())
throw std::runtime_error("Can't read certificate file.");
buffer_crt.resize(size);
file.seekg(0, std::ios::beg);
if (!file.read(buffer_crt.data(), size))
throw std::runtime_error("Can't load certificate.");
}
buffer = fileutils::loadFile(privkeyPath_);
buffer_crt = fileutils::loadFile(certPath_);
}
catch (const std::exception& e) {
SFL_ERR("Error loading identity: %s", e.what());
......@@ -421,25 +397,10 @@ DHTAccount::loadIdentity() const
void
DHTAccount::saveIdentity(const dht::crypto::Identity id) const
{
if (id.first) {
auto buffer = id.first->serialize();
std::ofstream file(privkeyPath_, std::ios::trunc | std::ios::binary);
if (!file.is_open()) {
SFL_ERR("Could not write key to %s", privkeyPath_.c_str());
return;
}
file.write((char*)buffer.data(), buffer.size());
}
if (id.second) {
auto buffer = id.second->getPacked();
std::ofstream file(certPath_, std::ios::trunc | std::ios::binary);
if (!file.is_open()) {
SFL_ERR("Could not write key to %s", certPath_.c_str());
return;
}
file.write((char*)buffer.data(), buffer.size());
}
if (id.first)
fileutils::saveFile(privkeyPath_, id.first->serialize());
if (id.second)
fileutils::saveFile(certPath_, id.second->getPacked());
}
template <typename T>
......
......@@ -54,6 +54,8 @@
#include <sstream>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <limits>
#include <cstdlib>
#include <cstring>
......@@ -211,6 +213,35 @@ bool isDirectoryWritable(const std::string &directory)
return access(directory.c_str(), W_OK) == 0;
}
std::vector<uint8_t>
loadFile(const std::string& path)
{
std::vector<uint8_t> buffer;
std::ifstream file(path, std::ios::binary);
if (!file)
throw std::runtime_error("Can't read file: "+path);
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
if (size > std::numeric_limits<unsigned>::max())
throw std::runtime_error("File is too big: "+path);
buffer.resize(size);
file.seekg(0, std::ios::beg);
if (!file.read((char*)buffer.data(), size))
throw std::runtime_error("Can't load file: "+path);
return buffer;
}
void
saveFile(const std::string& path, const std::vector<uint8_t>& data)
{
std::ofstream file(path, std::ios::trunc | std::ios::binary);
if (!file.is_open()) {
SFL_ERR("Could not write data to %s", path.c_str());
return;
}
file.write((char*)data.data(), data.size());
}
static size_t
dirent_buf_size(DIR * dirp)
{
......
......@@ -65,6 +65,9 @@ namespace fileutils {
*/
std::vector<std::string> readDirectory(const std::string &dir);
std::vector<uint8_t> loadFile(const std::string& path);
void saveFile(const std::string& path, const std::vector<uint8_t>& data);
struct FileHandle {
int fd;
const std::string name;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment