-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageDevice.cpp
More file actions
180 lines (155 loc) · 3.04 KB
/
Copy pathStorageDevice.cpp
File metadata and controls
180 lines (155 loc) · 3.04 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "StorageDevice.h"
#include <libopi/DiskHelper.h>
#include <libutils/FileUtils.h>
#include <utility>
namespace KGP
{
StorageDevice::StorageDevice(const string &devicename)
{
using namespace Utils;
if( File::DirExists("/sys/class/block/"s + devicename) )
{
this->device = OPI::DiskHelper::StorageDevice(devicename);
}
else
{
this->device = File::GetFileName(File::RealPath(devicename));
}
if( this->device.is_null() )
{
throw std::runtime_error("Unable to locate storage device: "s + devicename);
}
}
list<StorageDevice> StorageDevice::Devices()
{
list<StorageDevice> devices;
json jdevs = OPI::DiskHelper::StorageDevices();
for(const auto& jdev: jdevs)
{
devices.emplace_back( StorageDevice(jdev) );
}
return devices;
}
string StorageDevice::DeviceName() const
{
return this->device["devname"].get<string>();
}
string StorageDevice::SysPath() const
{
return this->device["syspath"].get<string>();
}
string StorageDevice::DevicePath() const
{
return this->device["devpath"].get<string>();
}
string StorageDevice::LVMPath() const
{
return this->device["dm-path"].get<string>();
}
string StorageDevice::LUKSPath() const
{
return this->LVMPath();
}
string StorageDevice::Model() const
{
return this->device["model"].get<string>();
}
list<string> StorageDevice::MountPoint() const
{
list<string> mps;
for( const auto& mp : this->device["mountpoint"])
{
mps.emplace_back(mp.get<string>());
}
return mps;
}
uint64_t StorageDevice::Blocks() const
{
return this->device["blocks"];
}
uint64_t StorageDevice::Size() const
{
return this->device["size"];
}
list<StorageDevice> StorageDevice::Partitions() const
{
list<StorageDevice> parts;
if( ! this->Is(StorageDevice::Partition) )
{
for(const auto& part: this->device["partitions"])
{
parts.emplace_back(StorageDevice(part));
}
}
return parts;
}
bool StorageDevice::Is(StorageDevice::Characteristic c) const
{
switch (c)
{
case Mounted:
return this->device["mounted"];
break;
case Partition:
return this->device["partition"];
break;
case Physical:
return this->device["isphysical"];
break;
case ReadOnly:
return this->device["readonly"];
break;
case Removable:
return this->device["removable"];
break;
case RootDevice:
{
if( ! this->device["mounted"] )
{
return false;
}
for( const auto& mp : this->device["mountpoint"] )
{
if( mp.get<string>() == "/")
{
return true;
}
}
return false;
break;
}
case BootDevice:
{
if( this->device.contains("partitions") )
{
for(const auto& part: this->device["partitions"])
{
for(const auto& mp : part["mountpoint"])
{
if( mp.get<string>() == "/")
{
return true;
}
}
}
}
return false;
break;
}
case DeviceMapper:
return this->device["dm"];
break;
case LVMDevice:
return this->device["dm-type"].get<string>() == "lvm";
break;
case LUKSDevice:
return this->device["dm-type"].get<string>() == "luks";
break;
}
// Should never get here
return false;
}
StorageDevice::StorageDevice(json dev): device(std::move(dev))
{
}
}