From: s414kim Date: Wed, 20 Sep 2017 08:58:54 +0000 (+0900) Subject: Remove unnecessary code of secure-erase X-Git-Tag: submit/tizen_4.0/20171018.042033~14 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7c30174ce6acfc5fd4bee77dc2002fdde2fa2470;p=platform%2Fcore%2Fsecurity%2Fode.git Remove unnecessary code of secure-erase - remove reading /dev/zero code. - changed mtab parsing code to use getmntent(). Change-Id: Ieee126dae6e33577ad9bdbb645c948db088eef3e Signed-off-by: s414kim --- diff --git a/server/engine/erase/erase-engine.cpp b/server/engine/erase/erase-engine.cpp index a6824b5..ca508de 100644 --- a/server/engine/erase/erase-engine.cpp +++ b/server/engine/erase/erase-engine.cpp @@ -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(); } diff --git a/server/secure-erase.cpp b/server/secure-erase.cpp index a0c6cda..da67643 100644 --- a/server/secure-erase.cpp +++ b/server/secure-erase.cpp @@ -17,12 +17,11 @@ #include #include #include +#include #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 DeviceList; std::unique_ptr 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) {} };