Remove unnecessary code of secure-erase 68/151268/6
authors414kim <s414.kim@samsung.com>
Wed, 20 Sep 2017 08:58:54 +0000 (17:58 +0900)
committers414kim <s414.kim@samsung.com>
Fri, 22 Sep 2017 09:09:43 +0000 (18:09 +0900)
- remove reading /dev/zero code.
- changed mtab parsing code to use getmntent().

Change-Id: Ieee126dae6e33577ad9bdbb645c948db088eef3e
Signed-off-by: s414kim <s414.kim@samsung.com>
server/engine/erase/erase-engine.cpp
server/secure-erase.cpp

index a6824b5..ca508de 100644 (file)
@@ -31,20 +31,15 @@ EraseEngine::~EraseEngine()
 
 void EraseEngine::discardBlock(unsigned long long offset, blksize_t size)
 {
-       char zeros[ERASE_SIZE] = "";
+       char zero[ERASE_SIZE] = {};
        if (size > ERASE_SIZE) {
                throw runtime::Exception("[eraseBlock] : size is too big");
        }
 
-       runtime::File zero("/dev/zero");
-       zero.open(O_RDONLY);
-       zero.read(zeros, sizeof(zeros));
-       zero.close();
-
        runtime::File device(target);
        device.open(O_WRONLY);
        device.lseek(offset, SEEK_SET);
-       device.write(zeros, size);
+       device.write(zero, size);
        device.close();
 }
 
index a0c6cda..da67643 100644 (file)
 #include <fstream>
 #include <vconf.h>
 #include <unistd.h>
+#include <mntent.h>
 
 #include "ext4-tool.h"
 #include "engine/erase/erase-engine.h"
-#include "engine/erase/mmc-engine.h"
 #include "rmi/secure-erase.h"
-
 #define ERASE_ENGINE EraseEngine
 #define PRIVILEGE_PLATFORM "http://tizen.org/privilege/internal/default/platform"
 
@@ -33,61 +32,21 @@ namespace {
 typedef std::unordered_map<std::string, std::string> DeviceList;
 std::unique_ptr<ERASE_ENGINE> engine;
 
-const std::string evaluateTarget(const std::string &path)
+std::string findDevPath(const std::string &mntPath)
 {
-       DeviceList deviceList;
-       DeviceList::const_iterator deviceListIter;
-       std::string subStr(path);
-       subStr.append("//");
-       std::size_t pos = 0;
-       std::string mountInfo;
-       std::string name;
-       std::string source;
-       std::string type;
-
-       runtime::File resource(path);
-       if (!resource.exists()) {
-               throw runtime::Exception("target doesn't exist");
-       }
-
-       std::ifstream file("/etc/mtab");
-       if (file.fail()) {
-               throw runtime::Exception("failed to access /etc/mtab");
-       }
-
-       while (std::getline(file, mountInfo)) {
-               pos = mountInfo.find(" ");
-               name = mountInfo.substr(0, pos);
-               mountInfo.erase(0, pos+1);
-
-               pos = mountInfo.find(" ");
-               source = mountInfo.substr(0, pos);
-               mountInfo.erase(0, pos+1);
-
-               pos = mountInfo.find(" ");
-               type = mountInfo.substr(0, pos);
-               if (!type.compare("ext2") && !type.compare("ext3")
-                       && !type.compare("ext4")) {
-                       continue;
-               }
-               runtime::File device(name);
-               if (!device.exists()) {
+       std::string ret;
+       FILE* mtab = ::setmntent("/etc/mtab", "r");
+       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"))
                        continue;
-               }
-               if (device.isDevice()) {
-                       deviceList.insert(std::make_pair(source, name));
-               }
-       }
-
-       pos = subStr.size();
-       while ((pos = subStr.rfind('/', pos - 1)) != std::string::npos && pos != 0) {
-               std::string subPath = subStr.substr(0, pos);
-               deviceListIter = deviceList.find(subPath);
-               if (deviceListIter != deviceList.end()) {
-                       return deviceListIter->second;
+               if (mntPath == entry->mnt_dir) {
+                       ret = entry->mnt_fsname;
+                       break;
                }
        }
-       return "";
+       ::endmntent(mtab);
+       return ret;
 }
 } /* namespace */
 
@@ -110,8 +69,7 @@ int SecureErase::clean(const std::string &name)
 {
        auto cleanWorker = [name, this]() {
                try {
-                       std::string target;
-                       target = evaluateTarget(name);
+                       std::string target = findDevPath(name);
                        engine->cleanDevice(target);
                } catch (runtime::Exception &e) {}
        };