-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeitem.cpp
181 lines (155 loc) · 4.72 KB
/
nodeitem.cpp
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
181
#include "nodeitem.h"
#include <QMutexLocker>
#include <algorithm>
#include <stdlib.h>
#include "valueitem.h"
NodeItem::NodeItem(HiveItem *parent, int index, QString name,
hive_h *hive, hive_node_h node) :
HiveItem(parent, index, name),
m_hive(hive), m_nodeHandle(node),
m_loadedChildren(false), m_loadedValues(false)
{
qDebug() << "Created" << *this;
}
NodeItem::NodeItem(HiveItem *parent, int index,
hive_h *hive, hive_node_h node) :
NodeItem(parent, index, getNodeName(hive, node), hive, node)
{
}
NodeItem::~NodeItem()
{
QMutexLocker locker{&m_childrenMutex};
qDeleteAll(m_children);
qDeleteAll(m_values);
if (m_nodeHandle == hivex_root(m_hive))
{
qDebug() << "Closing Hive" << m_hive;
hivex_close(m_hive);
}
}
HiveItem *NodeItem::childItem(int index)
{
loadChildren();
QMutexLocker locker{&m_childrenMutex};
HiveItem *child = static_cast<HiveItem *>(m_children.value(index, nullptr));
if (!child)
{
qWarning() << "Could not load child of index" << index << "for" << *this;
}
return child;
}
int NodeItem::childCount() const
{
size_t numChildren = hivex_node_nr_children(m_hive, m_nodeHandle);
// qDebug() << *this << "has" << numChildren << "children";
return numChildren;
}
bool NodeItem::isNode() const
{
return true;
}
QList<HiveItem *> NodeItem::getValues()
{
loadValues();
QMutexLocker locker{&m_valuesMutex};
return m_values;
}
void NodeItem::loadChildren()
{
QMutexLocker locker{&m_childrenMutex};
if (m_loadedChildren)
{
return;
}
qDebug() << "Loading children of" << *this;
qDebug() << "Deleting current children";
qDeleteAll(m_children);
hive_node_h *nodes = hivex_node_children(m_hive, m_nodeHandle);
if (!nodes)
{
qWarning() << "Could not load child nodes of" << *this;
return;
}
qDebug() << "Loading child nodes";
int childIndex = 0;
for (hive_node_h *childNode = nodes; *childNode; ++childNode)
{
qDebug() << "Loading child node of index" << childIndex
<< "hive " << m_hive << "node" << *childNode;
m_children.append(new NodeItem{static_cast<HiveItem *>(this),
childIndex, m_hive, *childNode});
++childIndex;
}
free(nodes);
nodes = nullptr;
qDebug() << "Sorting child nodes";
std::stable_sort(m_children.begin(), m_children.end(),
[](NodeItem * const &a, NodeItem * const &b) {
return (a && b) ? QString::compare(a->name(), b->name(), Qt::CaseInsensitive) < 0 : false;
});
qDebug() << "Updating child indexes";
childIndex = 0;
for (const auto &child : m_children)
child->updateIndex(childIndex++);
m_loadedChildren = true;
}
void NodeItem::loadValues()
{
QMutexLocker locker{&m_childrenMutex};
if (m_loadedValues)
{
return;
}
qDebug() << "Loading values of" << *this;
qDebug() << "Deleting current values";
qDeleteAll(m_values);
hive_value_h *values = hivex_node_values(m_hive, m_nodeHandle);
if (!values)
{
qWarning() << "Could not load values of" << *this;
return;
}
int index = 0;
for (hive_value_h *value = values; *value; ++value)
{
qDebug() << "Loading value of index" << index
<< "hive" << m_hive << "value" << *value;
m_values.append(static_cast<HiveItem *>(
new ValueItem(this, index, m_hive, *value)));
++index;
}
free(values);
values = nullptr;
qDebug() << "Sorting values";
std::stable_sort(m_values.begin(), m_values.end(),
[](HiveItem * const &a, HiveItem * const &b) {
return (a && b) ? QString::compare(a->name(), b->name(), Qt::CaseInsensitive) < 0 : false;
});
qDebug() << "Updating value indexes";
index = 0;
for (const auto &value : m_values)
value->updateIndex(index++);
m_loadedValues = true;
}
QString NodeItem::getNodeName(hive_h *hive, hive_node_h node)
{
char *name = hivex_node_name(hive, node);
if (name)
{
QString qName = QString::fromUtf8(name, hivex_node_name_len(hive, node));
qDebug() << "Name of node" << node << "in hive" << hive << "is" << qName;
free(name);
return qName;
}
qWarning() << "Could not load node name of hive" << hive << "node" << node;
return QString{};
}
QDebug operator<<(QDebug dbg, const NodeItem &node)
{
QDebugStateSaver saver{dbg};
Q_UNUSED(saver);
dbg.nospace() << "NodeItem(name: " << node.name()
<< ", hive: " << node.m_hive
<< ", node: " << node.m_nodeHandle << ")";
return dbg;
}