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>
31 #include <safe-buffer.h>
32 #include <buffer-conversion.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_PREFIX = "db-";
44 } // namespace anonymous
48 FileSystem::FileSystem(uid_t uid)
52 std::string FileSystem::getDBPath() const
55 ss << CKM_DATA_PATH << CKM_DB_PREFIX << m_uid;
59 std::string FileSystem::getDKEKPath() const {
61 ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_uid;
65 SafeBuffer FileSystem::getDomainKEK() const
67 std::ifstream is(getDKEKPath());
68 std::istreambuf_iterator<char> begin(is),end;
69 RawBuffer buffer(begin, end);
70 return toSafeBuffer(buffer);
73 bool FileSystem::saveDomainKEK(const SafeBuffer &buffer) const
75 std::ofstream os(getDKEKPath(), std::ios::out | std::ofstream::binary);
76 std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(os));
80 int FileSystem::init() {
82 if ((mkdir(CKM_DATA_PATH.c_str(), 0700)) && (errno != EEXIST)) {
84 LogError("Error in mkdir. Data directory could not be created. Errno: "
85 << err << " (" << strerror(err) << ")");
86 return -1; // TODO set up some error code
91 int FileSystem::removeUserData() const {
93 if (unlink(getDBPath().c_str())) {
96 LogError("Error in unlink user database: " << getDBPath()
97 << "Errno: " << errno << " " << strerror(err));
99 if (unlink(getDKEKPath().c_str())) {
102 LogError("Error in unlink user DKEK: " << getDKEKPath()
103 << "Errno: " << errno << " " << strerror(err));