Refactor secure erase and add MMC erase engine
[platform/core/security/ode.git] / server / secure-erase.cpp
1 /*
2  *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 #include <fstream>
17 #include <vconf.h>
18 #include <unistd.h>
19
20 #include "engine/erase/mmc-engine.h"
21 #include "rmi/secure-erase.h"
22
23 #define ERASE_ENGINE MMCEraseEngine
24 #define PRIVILEGE_PLATFORM "http://tizen.org/privilege/internal/default/platform"
25
26 namespace ode {
27
28 namespace {
29
30 std::unique_ptr<ERASE_ENGINE> engine;
31
32 void dropCachePage(void)
33 {
34         std::ofstream file;
35
36         file.open("/proc/sys/vm/drop_caches");
37         if (file.fail()) {
38                 throw runtime::Exception("failed to access drop_caches file");
39         }
40
41         file << "3\n";
42         file.close();
43         ::sync();
44         return;
45 }
46
47 } /* namespace */
48
49 SecureErase::SecureErase(ODEControlContext &ctx) :
50         context(ctx)
51 {
52         context.expose(this, PRIVILEGE_PLATFORM, (int)(SecureErase::erase)(std::string));
53         context.expose(this, PRIVILEGE_PLATFORM, (int)(SecureErase::clean)(std::string));
54
55         engine.reset(new ERASE_ENGINE(ProgressBar([](int v) {
56                         ::vconf_set_str(VCONFKEY_ODE_ERASE_PROGRESS, std::to_string(v).c_str());
57                 }))
58         );
59 }
60
61 SecureErase::~SecureErase()
62 {
63 }
64
65 int SecureErase::erase(const std::string &name)
66 {
67         auto eraseWorker = [name, this]() {
68                 try {
69                         runtime::File file(name);
70                         if (file.isDevice()) {
71                                 engine->eraseDevice(name);
72                         } else {
73                                 engine->eraseFiles(name);
74                         }
75                         dropCachePage();
76                 } catch (runtime::Exception &e) {}
77         };
78
79         std::thread asyncWork(eraseWorker);
80         asyncWork.detach();
81
82         return 0;
83 }
84
85 int SecureErase::clean(const std::string &name)
86 {
87         auto cleanWorker = [name, this]() {
88                 try {
89                         engine->cleanDevice(name);
90                         dropCachePage();
91                 } catch (runtime::Exception &e) {}
92         };
93
94         std::thread asyncWork(cleanWorker);
95         asyncWork.detach();
96         return 0;
97 }
98
99 } // namespace ode