Commit 4f673529 authored by Adrien Béraud's avatar Adrien Béraud Committed by Guillaume Roguez

ringaccount: use loadFile, saveFile from fileutils

Refs #67228

Change-Id: Ic13ea4d0b27de7fe7829f2b5b43171eba68e5d0c
parent 755f8694
......@@ -423,35 +423,6 @@ RingAccount::checkIdentityPath()
loadIdentity();
}
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()) {
RING_ERR("Could not write data to %s", path.c_str());
return;
}
file.write((char*)data.data(), data.size());
}
std::pair<std::shared_ptr<dht::crypto::Certificate>, dht::crypto::Identity>
RingAccount::loadIdentity()
{
......@@ -877,7 +848,7 @@ void RingAccount::doUnregister(std::function<void(bool)> released_cb)
void
RingAccount::registerCA(const dht::crypto::Certificate& crt)
{
saveFile(caPath_ + DIR_SEPARATOR_STR + crt.getPublicKey().getId().toString(), crt.getPacked());
fileutils::saveFile(caPath_ + DIR_SEPARATOR_STR + crt.getPublicKey().getId().toString(), crt.getPacked());
}
bool
......@@ -887,7 +858,7 @@ RingAccount::unregisterCA(const dht::InfoHash& crt_id)
bool deleted = false;
for (const auto& ca_path : cas) {
try {
dht::crypto::Certificate tmp_crt(loadFile(ca_path));
dht::crypto::Certificate tmp_crt(fileutils::loadFile(ca_path));
if (tmp_crt.getPublicKey().getId() == crt_id)
deleted &= remove(ca_path.c_str()) == 0;
} catch (const std::exception&) {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment