blob: 08b578aa984ec121d3a16a3ed3ae46f176b6e036 (
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
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
|
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "DevicesModel.h"
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <sys/stat.h>
NonnullRefPtr<DevicesModel> DevicesModel::create()
{
return adopt_ref(*new DevicesModel);
}
DevicesModel::DevicesModel()
{
}
DevicesModel::~DevicesModel()
{
}
int DevicesModel::row_count(const GUI::ModelIndex&) const
{
return m_devices.size();
}
int DevicesModel::column_count(const GUI::ModelIndex&) const
{
return Column::__Count;
}
String DevicesModel::column_name(int column) const
{
switch (column) {
case Column::Device:
return "Device";
case Column::Major:
return "Major";
case Column::Minor:
return "Minor";
case Column::ClassName:
return "Class";
case Column::Type:
return "Type";
default:
VERIFY_NOT_REACHED();
}
}
GUI::Variant DevicesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
{
VERIFY(is_valid(index));
if (role == GUI::ModelRole::TextAlignment) {
switch (index.column()) {
case Column::Device:
return Gfx::TextAlignment::CenterLeft;
case Column::Major:
return Gfx::TextAlignment::CenterRight;
case Column::Minor:
return Gfx::TextAlignment::CenterRight;
case Column::ClassName:
return Gfx::TextAlignment::CenterLeft;
case Column::Type:
return Gfx::TextAlignment::CenterLeft;
}
return {};
}
if (role == GUI::ModelRole::Sort) {
const DeviceInfo& device = m_devices[index.row()];
switch (index.column()) {
case Column::Device:
return device.path;
case Column::Major:
return device.major;
case Column::Minor:
return device.minor;
case Column::ClassName:
return device.class_name;
case Column::Type:
return device.type;
default:
VERIFY_NOT_REACHED();
}
}
if (role == GUI::ModelRole::Display) {
const DeviceInfo& device = m_devices[index.row()];
switch (index.column()) {
case Column::Device:
return device.path;
case Column::Major:
return device.major;
case Column::Minor:
return device.minor;
case Column::ClassName:
return device.class_name;
case Column::Type:
switch (device.type) {
case DeviceInfo::Type::Block:
return "Block";
case DeviceInfo::Type::Character:
return "Character";
default:
VERIFY_NOT_REACHED();
}
default:
VERIFY_NOT_REACHED();
}
}
return {};
}
void DevicesModel::update()
{
auto proc_devices = Core::File::construct("/proc/devices");
if (!proc_devices->open(Core::IODevice::OpenMode::ReadOnly))
VERIFY_NOT_REACHED();
auto json = JsonValue::from_string(proc_devices->read_all());
VERIFY(json.has_value());
m_devices.clear();
json.value().as_array().for_each([this](auto& value) {
JsonObject device = value.as_object();
DeviceInfo device_info;
device_info.major = device.get("major").to_uint();
device_info.minor = device.get("minor").to_uint();
device_info.class_name = device.get("class_name").to_string();
String type_str = device.get("type").to_string();
if (type_str == "block")
device_info.type = DeviceInfo::Type::Block;
else if (type_str == "character")
device_info.type = DeviceInfo::Type::Character;
else
VERIFY_NOT_REACHED();
m_devices.append(move(device_info));
});
auto fill_in_paths_from_dir = [this](const String& dir) {
Core::DirIterator dir_iter { dir, Core::DirIterator::Flags::SkipDots };
while (dir_iter.has_next()) {
auto path = dir_iter.next_full_path();
struct stat statbuf;
if (lstat(path.characters(), &statbuf) != 0) {
VERIFY_NOT_REACHED();
}
if (!S_ISBLK(statbuf.st_mode) && !S_ISCHR(statbuf.st_mode))
continue;
unsigned _major = major(statbuf.st_rdev);
unsigned _minor = minor(statbuf.st_rdev);
auto it = m_devices.find_if([_major, _minor](const auto& device_info) {
return device_info.major == _major && device_info.minor == _minor;
});
if (it != m_devices.end()) {
(*it).path = move(path);
}
}
};
fill_in_paths_from_dir("/dev");
fill_in_paths_from_dir("/dev/pts");
did_update();
}
|