Menu

[eb790e]: / ext4_utils / unencrypted_properties.h  Maximize  Restore  History

Download this file

75 lines (60 with data), 2.3 kB

 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
#include <string>
#include <fstream>
// key names for properties we use
namespace properties {
extern const char* key;
extern const char* ref;
extern const char* props;
extern const char* is_default;
}
/**
* Class to store data on the unencrypted folder of a device.
* Note that the folder must exist before this class is constructed.
* All names must be valid single level (no '/') file or directory names
* Data is organized hierarchically so we can get a child folder
*/
class UnencryptedProperties
{
public:
// Get path of folder. Must create before using any properties
// This is to allow proper setting of SELinux policy
static std::string GetPath(const char* device);
// Opens properties folder on named device.
// If folder does not exist, OK will return false, all
// getters will return default properties and setters will fail.
UnencryptedProperties(const char* device);
// Get named object. Return default if object does not exist or error.
template<typename t> t Get(const char* name, t default_value = t()) const;
// Set named object. Return true if success, false otherwise
template<typename t> bool Set(const char* name, t const& value);
// Get child properties
UnencryptedProperties GetChild(const char* name) const;
// Remove named object
bool Remove(const char* name);
// Does folder exist?
bool OK() const;
private:
UnencryptedProperties();
std::string folder_;
};
template<typename t> t UnencryptedProperties::Get(const char* name,
t default_value) const
{
if (!OK()) return default_value;
t value = default_value;
std::ifstream(folder_ + "/" + name) >> value;
return value;
}
template<typename t> bool UnencryptedProperties::Set(const char* name,
t const& value)
{
if (!OK()) return false;
std::ofstream o(folder_ + "/" + name);
o << value;
return !o.fail();
}
// Specialized getters/setters for strings
template<> std::string UnencryptedProperties::Get(const char* name,
std::string default_value) const;
template<> bool UnencryptedProperties::Set(const char* name,
std::string const& value);
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.