d52f447f34161aea104e02ae813e5f7b4bd11979
[platform/core/security/security-manager.git] / src / server / service / password-file-buffer.cpp
1 /*
2  *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18 /*
19  * @file        password-file-buffer.h
20  * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
21  * @version     1.0
22  * @brief       Implementation of PasswordFileBuffer, used for serialization in PasswordFile class
23  */
24
25 #include <password-file-buffer.h>
26
27 #include <fstream>
28 #include <iterator>
29
30 #include <dpl/log/log.h>
31
32 #include <security-server.h>
33 #include <password-exception.h>
34
35 #include <fcntl.h>
36
37 namespace SecurityServer
38 {
39     PasswordFileBuffer::PasswordFileBuffer(): m_bufferReadBytes(0) {}
40
41     void PasswordFileBuffer::Read(size_t num, void *bytes)
42     {
43         if(m_buffer.empty()) {
44             LogError("Buffer doesn't contain any data.");
45             Throw(PasswordException::NoData);
46         }
47
48         if((m_bufferReadBytes + num) > m_buffer.size()) {
49             LogError("Not enough buffer to read " << num << " data.");
50             Throw(PasswordException::OutOfData);
51         }
52
53         void* ret = memcpy(bytes, &m_buffer[m_bufferReadBytes], num);
54
55         if(ret == 0) {
56             LogError("Failed to read " << num << " bytes.");
57             Throw(PasswordException::MemoryError);
58         }
59
60         m_bufferReadBytes += num;
61     }
62
63     void PasswordFileBuffer::Write(size_t num, const void *bytes)
64     {
65         const char* buffer = static_cast<const char*>(bytes);
66         std::copy(buffer, buffer+num, std::back_inserter(m_buffer));
67     }
68
69     void PasswordFileBuffer::Save(const std::string &path)
70     {
71         std::ofstream file(path, std::ofstream::trunc);
72
73         if(!file.good()) {
74             LogError("Error while opening file stream.");
75             Throw(PasswordException::FStreamOpenError);
76         }
77
78         file.write(m_buffer.data(), m_buffer.size());
79         if(!file) {
80             LogError("Failed to write data.");
81             Throw(PasswordException::FStreamWriteError);
82         }
83         file.close();
84         int fd = open(path.c_str(), O_WRONLY | O_APPEND); fsync(fd); close(fd);
85     }
86
87     void PasswordFileBuffer::Load(const std::string &path)
88     {
89         std::ifstream file(path, std::ifstream::binary);
90
91         if(!file.good()) {
92             LogError("Error while opening file stream.");
93             Throw(PasswordException::FStreamOpenError);
94         }
95
96         //reset read bytes counter
97         m_bufferReadBytes = 0;
98
99         m_buffer.assign(std::istreambuf_iterator<char>(file),
100                         std::istreambuf_iterator<char>());
101
102         if(!file) {
103             LogError("Failed to read data. Failbit: " << file.fail() << ", Badbit: " << file.bad());
104             Throw(PasswordException::FStreamReadError);
105         }
106     }
107
108 } //namespace SecurityServer