2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file FileSystem.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Sample service implementation.
24 #include <sys/types.h>
33 #include <dpl/errno_string.h>
34 #include <dpl/log/log.h>
36 #include <file-system.h>
40 static const std::string CKM_DATA_PATH = "/opt/data/ckm/";
41 static const std::string CKM_KEY_PREFIX = "key-";
42 static const std::string CKM_DB_KEY_PREFIX = "db-key-";
43 static const std::string CKM_DB_PREFIX = "db-";
44 static const std::string CKM_REMOVED_APP_PREFIX = "removed-app-";
46 } // namespace anonymous
50 FileSystem::FileSystem(uid_t uid)
54 std::string FileSystem::getDBPath() const
57 ss << CKM_DATA_PATH << CKM_DB_PREFIX << m_uid;
61 std::string FileSystem::getDKEKPath() const {
63 ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_uid;
67 std::string FileSystem::getDBDEKPath() const {
69 ss << CKM_DATA_PATH << CKM_DB_KEY_PREFIX << m_uid;
73 std::string FileSystem::getRemovedAppsPath() const {
75 ss << CKM_DATA_PATH << CKM_REMOVED_APP_PREFIX << m_uid;
79 RawBuffer FileSystem::loadFile(const std::string &path) const {
80 std::ifstream is(path);
85 std::istreambuf_iterator<char> begin(is),end;
86 std::vector<char> buff(begin,end); // This trick does not work with boost vector
88 RawBuffer buffer(buff.size());
89 memcpy(buffer.data(), buff.data(), buff.size());
93 RawBuffer FileSystem::getDKEK() const
95 return loadFile(getDKEKPath());
98 RawBuffer FileSystem::getDBDEK() const
100 return loadFile(getDBDEKPath());
103 bool FileSystem::saveFile(const std::string &path, const RawBuffer &buffer) const {
104 std::ofstream os(path, std::ios::out | std::ofstream::binary);
105 std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(os));
109 bool FileSystem::saveDKEK(const RawBuffer &buffer) const {
110 return saveFile(getDKEKPath(), buffer);
113 bool FileSystem::saveDBDEK(const RawBuffer &buffer) const {
114 return saveFile(getDBDEKPath(), buffer);
117 bool FileSystem::addRemovedApp(const std::string &smackLabel) const
119 std::ofstream outfile;
120 outfile.open(getRemovedAppsPath(), std::ios_base::app);
121 outfile << smackLabel << std::endl;
123 return !outfile.fail();
126 AppLabelVector FileSystem::clearRemovedsApps() const
129 AppLabelVector removedApps;
131 std::ifstream removedAppsFile(getRemovedAppsPath());
132 if (removedAppsFile.is_open()) {
133 while (! removedAppsFile.eof() ) {
134 getline (removedAppsFile,line);
136 removedApps.push_back(line);
138 removedAppsFile.close();
140 // truncate the contents
141 std::ofstream truncateFile;
142 truncateFile.open(getRemovedAppsPath(), std::ofstream::out | std::ofstream::trunc);
143 truncateFile.close();
147 int FileSystem::init() {
149 if ((mkdir(CKM_DATA_PATH.c_str(), 0700)) && (errno != EEXIST)) {
151 LogError("Error in mkdir. Data directory could not be created. Errno: "
152 << err << " (" << GetErrnoString(err) << ")");
153 return -1; // TODO set up some error code
158 UidVector FileSystem::getUIDsFromDBFile() {
160 std::unique_ptr<DIR, std::function<int(DIR*)>>
161 dirp(::opendir(CKM_DATA_PATH.c_str()), ::closedir);
165 LogError("Error in opendir. Data directory could not be read. Error: " << GetErrnoString(err));
169 size_t len = offsetof(struct dirent, d_name) + pathconf(CKM_DATA_PATH.c_str(), _PC_NAME_MAX) + 1;
170 std::unique_ptr<struct dirent, std::function<void(void*)>>
171 pEntry(static_cast<struct dirent*>(::malloc(len)), ::free);
174 LogError("Memory allocation failed.");
178 struct dirent* pDirEntry = NULL;
180 while ( (!readdir_r(dirp.get(), pEntry.get(), &pDirEntry)) && pDirEntry ) {
182 // Ignore files with diffrent prefix
183 if (strncmp(pDirEntry->d_name, CKM_KEY_PREFIX.c_str(), CKM_KEY_PREFIX.size())) {
187 // We find database. Let's extract user id.
189 uids.push_back(static_cast<uid_t>(std::stoi((pDirEntry->d_name)+CKM_KEY_PREFIX.size())));
190 } catch (const std::invalid_argument) {
191 LogError("Error in extracting uid from db file. Error=std::invalid_argument."
192 "This will be ignored.File=" << pDirEntry->d_name << "");
193 } catch(const std::out_of_range) {
194 LogError("Error in extracting uid from db file. Error=std::out_of_range."
195 "This will be ignored. File="<< pDirEntry->d_name << "");
202 int FileSystem::removeUserData() const {
203 int err, retCode = 0;
205 if (unlink(getDBPath().c_str())) {
208 LogError("Error in unlink user database: " << getDBPath()
209 << "Errno: " << errno << " " << GetErrnoString(err));
212 if (unlink(getDKEKPath().c_str())) {
215 LogError("Error in unlink user DKEK: " << getDKEKPath()
216 << "Errno: " << errno << " " << GetErrnoString(err));
219 if (unlink(getDBDEKPath().c_str())) {
222 LogError("Error in unlink user DBDEK: " << getDBDEKPath()
223 << "Errno: " << errno << " " << GetErrnoString(err));
226 if (unlink(getRemovedAppsPath().c_str())) {
229 LogError("Error in unlink user's Removed Apps File: " << getRemovedAppsPath()
230 << "Errno: " << errno << " " << GetErrnoString(err));