blob: ed36e2064c106c8c0041979c4f87f8769afee27a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#include "unencrypted_properties.h"
#include <sys/stat.h>
#include <dirent.h>
namespace properties {
const char* key = "key";
const char* ref = "ref";
const char* props = "props";
const char* is_default = "is_default";
}
namespace
{
const char* unencrypted_folder = "unencrypted";
}
std::string UnencryptedProperties::GetPath(const char* device)
{
return std::string() + device + "/" + unencrypted_folder;
}
UnencryptedProperties::UnencryptedProperties(const char* device)
: folder_(GetPath(device))
{
DIR* dir = opendir(folder_.c_str());
if (dir) {
closedir(dir);
} else {
folder_.clear();
}
}
UnencryptedProperties::UnencryptedProperties()
{
}
template<> std::string UnencryptedProperties::Get(const char* name,
std::string default_value) const
{
if (!OK()) return default_value;
std::ifstream i(folder_ + "/" + name, std::ios::binary);
if (!i) {
return default_value;
}
i.seekg(0, std::ios::end);
int length = i.tellg();
i.seekg(0, std::ios::beg);
if (length == -1) {
return default_value;
}
std::string s(length, 0);
i.read(&s[0], length);
if (!i) {
return default_value;
}
return s;
}
template<> bool UnencryptedProperties::Set(const char* name, std::string const& value)
{
if (!OK()) return false;
std::ofstream o(folder_ + "/" + name, std::ios::binary);
o << value;
return !o.fail();
}
UnencryptedProperties UnencryptedProperties::GetChild(const char* name) const
{
UnencryptedProperties up;
if (!OK()) return up;
std::string directory(folder_ + "/" + name);
if (mkdir(directory.c_str(), 700) == -1 && errno != EEXIST) {
return up;
}
up.folder_ = directory;
return up;
}
bool UnencryptedProperties::Remove(const char* name)
{
if (!OK()) return false;
if (remove((folder_ + "/" + name).c_str())
&& errno != ENOENT) {
return false;
}
return true;
}
bool UnencryptedProperties::OK() const
{
return !folder_.empty();
}
|