Refactor mtab related functions
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 14 Nov 2017 13:33:42 +0000 (14:33 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 23 Nov 2017 11:52:07 +0000 (12:52 +0100)
Change-Id: I28ba2ddbe1ea5140e53368acff2946790a016896

server/engine/encryption/ecryptfs-engine.cpp
server/internal-encryption.cpp
server/misc.cpp
server/misc.h
server/secure-erase.cpp
tests/CMakeLists.txt

index 682bbdd..4b87d9a 100644 (file)
@@ -30,6 +30,8 @@
 
 #include "ecryptfs-engine.h"
 
+#include "misc.h"
+
 #define OPTION_ONLY_NEW_FILE                   (1 << 0)
 #define OPTION_EXCEPT_FOR_MEDIA_FILE   (1 << 1)
 
@@ -353,17 +355,13 @@ void EcryptfsEngine::umount()
 
 bool EcryptfsEngine::isMounted()
 {
-       std::ifstream file("/proc/mounts");
-       std::string line;
-
-       while (std::getline(file, line)) {
-               std::stringstream info(line);
-               std::string src, dest, type;
-
-               info >> src >> dest >> type;
-               if (type == "ecryptfs" && src == source && dest == destination) {
+       Mtab mtab;
+       struct ::mntent* entry;
+       while ((entry = mtab.next()) != NULL) {
+               if (std::string("ecryptfs") == entry->mnt_type &&
+                       source == entry->mnt_fsname &&
+                       destination == entry->mnt_dir)
                        return true;
-               }
        }
 
        return false;
index 0c07c0e..77da03b 100644 (file)
@@ -358,7 +358,7 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
        auto encryptWorker = [MasterKey, options, this]() {
                try {
                        std::string source = engine->getSource();
-                       std::string mntPath = findMntPath(source);
+                       std::string mntPath = findMountPointByDevice(source);
 
                        if (!mntPath.empty()) {
                                INFO(SINK, "Closing all known systemd services that might be using internal storage.");
@@ -375,7 +375,7 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
                                        }
                                        stopDependedSystemdServices();
                                }
-                               mntPath = findMntPath(source);
+                               mntPath = findMountPointByDevice(source);
                        }
 
                        showProgressUI("Encrypting");
index 80a6458..5533432 100644 (file)
  */
 #include <signal.h>
 #include <sys/types.h>
-#include <stdio.h>
-#include <mntent.h>
 
 #include <klay/file-user.h>
+#include <klay/filesystem.h>
+#include <klay/exception.h>
 
 #include "logger.h"
 #include "misc.h"
 
 namespace ode {
 
-std::string findMntPath(const std::string &devPath)
+Mtab::Mtab() {
+       mtab = ::setmntent("/proc/self/mounts", "r");
+       if (mtab == NULL)
+               throw runtime::Exception("Opening mtab failed");
+}
+
+Mtab::~Mtab() {
+       ::endmntent(mtab);
+}
+
+struct ::mntent* Mtab::next() {
+       return ::getmntent(mtab);
+}
+
+
+std::string findMountPointByDevice(const std::string &devPath)
 {
        std::string ret;
 
-       FILE* mtab = ::setmntent("/etc/mtab", "r");
+       Mtab mtab;
        struct ::mntent* entry = NULL;
-       while ((entry = ::getmntent(mtab)) != NULL) {
+       while ((entry = mtab.next()) != NULL) {
                if (devPath == entry->mnt_fsname) {
                        ret = entry->mnt_dir;
                        break;
                }
        }
-       ::endmntent(mtab);
 
        return ret;
 }
index 3b71f49..13c380e 100644 (file)
 #define __MISC_H__
 
 #include <string>
+#include <stdio.h>
+#include <mntent.h>
 
 namespace ode {
 
-std::string findMntPath(const std::string &devPath);
+class Mtab {
+public:
+       Mtab();
+       ~Mtab();
+
+       struct ::mntent* next();
+
+private:
+       FILE* mtab;
+};
+
+std::string findMountPointByDevice(const std::string &devPath);
 void killDependentApplications(const std::string &mntPath);
 
 } // namespace ode
index d11af7e..e7a66cb 100644 (file)
@@ -17,8 +17,8 @@
 #include <fstream>
 #include <vconf.h>
 #include <unistd.h>
-#include <mntent.h>
 
+#include "misc.h"
 #include "secure-erase.h"
 
 namespace ode {
@@ -28,22 +28,25 @@ namespace {
 typedef std::unordered_map<std::string, std::string> DeviceList;
 const char *PRIVILEGE_PLATFORM = "http://tizen.org/privilege/internal/default/platform";
 
-std::string findDevPath(const std::string &mntPath)
+std::string findDeviceByMountPoint(const std::string &mntPath)
 {
        std::string ret;
-       FILE* mtab = ::setmntent("/etc/mtab", "r");
+       Mtab mtab;
        struct ::mntent* entry = NULL;
-       while ((entry = ::getmntent(mtab)) != NULL) {
-               if (strcmp(entry->mnt_type, "ext2") && strcmp(entry->mnt_type, "ext3") && strcmp(entry->mnt_type, "ext4"))
+       while ((entry = mtab.next()) != NULL) {
+               if (strcmp(entry->mnt_type, "ext2") &&
+                       strcmp(entry->mnt_type, "ext3") &&
+                       strcmp(entry->mnt_type, "ext4"))
                        continue;
+
                if (mntPath == entry->mnt_dir) {
                        ret = entry->mnt_fsname;
                        break;
                }
        }
-       ::endmntent(mtab);
        return ret;
 }
+
 } /* namespace */
 
 SecureEraseServer::SecureEraseServer(ServerContext &srv) :
@@ -65,7 +68,7 @@ int SecureEraseServer::clean(const std::string &name)
 {
        auto cleanWorker = [name, this]() {
                try {
-                       std::string target = findDevPath(name);
+                       std::string target = findDeviceByMountPoint(name);
                        engine->cleanDevice(target);
                } catch (runtime::Exception &e) {}
        };
index a9d1c08..72650e9 100755 (executable)
@@ -28,6 +28,7 @@ SET(TEST_SRC  main.cpp
                                ../server/key-manager/anti-forensics.cpp
                                ../server/ext4-tool.cpp
                                ../server/progress-bar.cpp
+                               ../server/misc.cpp
 )
 
 ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_SRC})