From f023e270a16f79378dfb8d7e28ba07d236a9b1f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Wed, 12 Nov 2014 18:25:18 -0500
Subject: [PATCH] fileutils: add loadFile, saveFile

Change-Id: Ia65ebf8b9b593a8b56dde8db7b9c14e86d8903ac
---
 daemon/src/dht/dhtaccount.cpp | 55 +++++------------------------------
 daemon/src/fileutils.cpp      | 31 ++++++++++++++++++++
 daemon/src/fileutils.h        |  3 ++
 3 files changed, 42 insertions(+), 47 deletions(-)

diff --git a/daemon/src/dht/dhtaccount.cpp b/daemon/src/dht/dhtaccount.cpp
index 8ff471d021..8c22c2c3fc 100644
--- a/daemon/src/dht/dhtaccount.cpp
+++ b/daemon/src/dht/dhtaccount.cpp
@@ -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>
diff --git a/daemon/src/fileutils.cpp b/daemon/src/fileutils.cpp
index 02a009aa85..c2776351c0 100644
--- a/daemon/src/fileutils.cpp
+++ b/daemon/src/fileutils.cpp
@@ -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)
 {
diff --git a/daemon/src/fileutils.h b/daemon/src/fileutils.h
index 17602d6f13..5379f0d143 100644
--- a/daemon/src/fileutils.h
+++ b/daemon/src/fileutils.h
@@ -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;
-- 
GitLab