Ext4Tool modifications: detect the filesystem and mkfs 14/129014/16
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Fri, 12 May 2017 08:46:51 +0000 (10:46 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Tue, 30 May 2017 09:47:12 +0000 (11:47 +0200)
Change-Id: I02444c6f56ee50acc6dde653a28490eadd4e0ee8

packaging/ode.spec
server/ext4-tool.cpp
server/ext4-tool.h

index 261b33f..fe3f58f 100755 (executable)
@@ -6,6 +6,7 @@ Source0: file://%{name}-%{version}.tar.gz
 Summary: Tizen device encryption and secure erase manager
 Group:   Security/Service
 Requires: systemd
+Requires: e2fsprogs
 BuildRequires: gcc
 BuildRequires: cmake
 BuildRequires: gettext-tools
index a8412b0..27770a0 100644 (file)
@@ -33,7 +33,11 @@ namespace ode {
 #define ODE_EXT2_MIN_BLOCK_LOG_SIZE    10      /* 1024 */
 #define ODE_EXT2_MIN_BLOCK_SIZE                (1 << ODE_EXT2_MIN_BLOCK_LOG_SIZE)
 
-static unsigned int divCeilSafely(unsigned int a, unsigned int b)
+#define ODE_EXT2_MAGIC                         0xEF53
+
+namespace {
+
+unsigned int divCeilSafely(unsigned int a, unsigned int b)
 {
        if (!a)
                return 0;
@@ -41,16 +45,67 @@ static unsigned int divCeilSafely(unsigned int a, unsigned int b)
        return ((a - 1) / b) + 1;
 }
 
+void execAndWait(const std::string &path, std::vector<std::string> &args)
+{
+       runtime::Process proc(path, args);
+
+       int ret = proc.execute();
+       if (ret < 0)
+               throw runtime::Exception(path + " failed for " + args.back());
+
+       ret = proc.waitForFinished();
+       if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0)
+               throw runtime::Exception(path + " failed for " + args.back());
+}
+
+} // namespace
+
 Ext4Tool::Ext4Tool(const std::string &src) :
        source(src), blockSize(0), totalBlockCount(0)
 {
+       if (!isExt(src))
+               throw runtime::Exception(source + " is not an ext2/3/4 filesystem");
+
+       readInfo();
+}
+
+Ext4Tool::~Ext4Tool()
+{
+}
+
+bool Ext4Tool::isExt(const std::string &src)
+{
+       unsigned short magic;
+       runtime::File device(src);
+       bool ret;
+
+       if (!device.exists())
+               throw runtime::Exception(src + " doesn't exist");
+
+       device.open(O_RDONLY);
+
+       device.lseek(ODE_SUPERBLOCK_OFFSET + 56, SEEK_SET);
+       device.read(&magic, 2);
+
+       if (magic == ODE_EXT2_MAGIC)
+               ret = true;
+       else
+               ret = false;
+
+       device.close();
+
+       return ret;
+}
+
+void Ext4Tool::readInfo()
+{
        unsigned int firstDataBlock = 0;
        unsigned int blocksPerGroup = 0;
        unsigned int clustersPerGroup = 0;
        runtime::File device(source);
 
-       if (device.exists() == false)
-               throw runtime::Exception("Source doesn't exist");
+       if (!device.exists())
+               throw runtime::Exception(source + " doesn't exist");
 
        device.open(O_RDONLY);
 
@@ -124,10 +179,6 @@ Ext4Tool::Ext4Tool(const std::string &src) :
        device.close();
 }
 
-Ext4Tool::~Ext4Tool()
-{
-}
-
 unsigned int Ext4Tool::getBlockSize()
 {
        return blockSize;
@@ -149,17 +200,30 @@ bool Ext4Tool::isUsedBlock(unsigned int blockIndex)
        return false;
 }
 
+void Ext4Tool::mkfs(const std::string &src)
+{
+       static const char *mkfsPath = "/sbin/mkfs.ext4";
+       std::vector<std::string> args = {
+               mkfsPath,
+               "-F",
+               "-q",
+               src
+       };
+
+       execAndWait(mkfsPath, args);
+}
+
 void Ext4Tool::forceCleanUp()
 {
+       static const char *fsckPath = "/sbin/fsck.ext4";
        std::vector<std::string> args = {
+               fsckPath,
                "-f",
                "-y",
                source
        };
 
-       runtime::Process proc("/sbin/e2fsck", args);
-       proc.execute();
-       proc.waitForFinished();
+       execAndWait(fsckPath, args);
 }
 
 } // namespace ode
index efad0fa..bbf2ce8 100644 (file)
@@ -20,6 +20,8 @@
 #include <string>
 #include <vector>
 
+#include <klay/filesystem.h>
+
 namespace ode {
 
 class Ext4Tool final {
@@ -37,10 +39,13 @@ public:
        unsigned int getBlockSize();
        unsigned int getTotalBlockCount();
        bool isUsedBlock(unsigned int blockIndex);
-
        void forceCleanUp();
 
+       static bool isExt(const std::string &src);
+       static void mkfs(const std::string &src);
+
 private:
+       void readInfo();
        std::string source;
        unsigned int blockSize, totalBlockCount;
        data bitmap;