-
Notifications
You must be signed in to change notification settings - Fork 0
/
hddtest.cpp
436 lines (378 loc) · 14.1 KB
/
hddtest.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*******************************************************************************
*
* HDDTest the graphical drive benchmarking tool.
* Copyright (C) 2011 Vladimír Matěna <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
********************************************************************************/
#include <QMessageBox>
#include <iostream>
#include "hddtest.h"
#include "ui_hddtest.h"
#include "testwidget.h"
#include "seeker.h"
HDDTestWidget::HDDTestWidget(QWidget *parent) :
QDialog(parent), ui(new Ui::HDDTestWidget) {
ui->setupUi(this);
// connect access warning and error signals
connect(&device, SIGNAL(accessWarning()), this, SLOT(device_accessWarning()));
connect(&refDevice, SIGNAL(accessWarning()), this, SLOT(refDevice_accessWarning()));
connect(&device, SIGNAL(operationError()), this, SLOT(device_operationError()));
connect(&refDevice, SIGNAL(operationError()), this, SLOT(device_operationError()));
// connect device refresh signal
connect(&device, SIGNAL(udisksUpdate()), this, SLOT(device_list_refresh()), Qt::QueuedConnection);
device_list_refresh();
// Initialize tests
ui->filerwwidget->SetDevice(&device);
ui->filestructurewidget->SetDevice(&device);
ui->readblockwidget->SetDevice(&device);
ui->readcontwidget->SetDevice(&device);
ui->readrndwidget->SetDevice(&device);
ui->seekwidget->SetDevice(&device);
ui->smallfileswidget->SetDevice(&device);
// set icons to buttons
ui->open->setIcon(QIcon::fromTheme("document-open", QIcon("icon:/icon/document-open.png")));
ui->save->setIcon(QIcon::fromTheme("document-save", QIcon("icon:/icon/document-save.png")));
ui->about->setIcon(QIcon::fromTheme("help-about", QIcon("icon:/icon/help-about.png")));
}
HDDTestWidget::~HDDTestWidget() {
delete ui;
}
void HDDTestWidget::on_drive_currentIndexChanged(QString) {
QVariant data = ui->drive->itemData(ui->drive->currentIndex());
switch(data.value<Device::Item>().type) {
case Device::Item::Type::DEVICE:
EraseResults(TestWidget::RESULTS);
device.Open(data.value<Device::Item>(), true);
ReloadTests(false);
break;
case Device::Item::Type::RESULT:
device.Open(data.value<Device::Item>(), true);
EraseResults(TestWidget::RESULTS);
OpenResultFile(data.value<Device::Item>().path, TestWidget::RESULTS);
ReloadTests(true);
break;
default:
// this should not happend
std::cerr << "WARNING: Bad item selected in device combo box." << std::endl;
break;
}
}
void HDDTestWidget::on_reference_currentIndexChanged(QString ) {
QVariant data = ui->reference->itemData(ui->reference->currentIndex());
switch(data.value<Device::Item>().type) {
case Device::Item::Type::RESULT:
refDevice.Open(data.value<Device::Item>(), true);
EraseResults(TestWidget::REFERENCE);
OpenResultFile(data.value<Device::Item>().path, TestWidget::REFERENCE);
UpdateInfo(TestWidget::REFERENCE);
break;
case Device::Item::Type::NOTHING:
EraseResults(TestWidget::REFERENCE);
UpdateInfo(TestWidget::REFERENCE);
break;
default:
// this should not happend
std::cerr << "WARNING: Device selected in reference combo box." << std::endl;
break;
}
}
void HDDTestWidget::device_accessWarning() {
#ifndef NDEBUG
return;
#endif
QString user = QString::fromLatin1(getenv("USER"));
QMessageBox box;
box.setText("You are running HDDTest as user: " + user +
" most probably you do not have rights for HDDTest to operate properly." +
" You you should have right to do following with device you want to be tested:" +
"\n\tRead block device." +
"\n\tRead and write device`s filesystem." +
"\n\tAdvice device not to be cached." +
"\n\tDrop system caches." +
"\nFailing to do so will cause HDDTest to show incorrect results.");
box.setInformativeText("Continue at your own risk.");
box.exec();
}
void HDDTestWidget::device_operationError() {
bool running = false;
if(ui->filerwwidget->testState == TestWidget::STARTED) {
ui->filerwwidget->StopTest();
running = true;
}
if(ui->filestructurewidget->testState == TestWidget::STARTED) {
ui->filestructurewidget->StopTest();
running = true;
}
if(ui->readblockwidget->testState == TestWidget::STARTED) {
ui->readblockwidget->StopTest();
running = true;
}
if(ui->readcontwidget->testState == TestWidget::STARTED) {
ui->readcontwidget->StopTest();
running = true;
}
if(ui->readrndwidget->testState == TestWidget::STARTED) {
ui->readrndwidget->StopTest();
running = true;
}
if(ui->seekwidget->testState == TestWidget::STARTED) {
ui->seekwidget->StopTest();
running = true;
}
if(ui->smallfileswidget->testState == TestWidget::STARTED) {
ui->smallfileswidget->StopTest();
running = true;
}
if(running) {
QMessageBox box;
box.setText(QString() + "An error occured while running test. I/O operation failed in test function." +
" This can be caused by missing permissions for operating device you are testing" +
" as well as hardware failture. Please check following:" +
"\n\tDo you have sufficent rights to read block device." +
"\n\tDo you have sufficent rights to read/write devices filesystem." +
"\n\tIs the device big enouh for selected test type." +
"\n\tIf you are running filesystem test check free space on target filesystem." +
"\n\tCheck you system logs for I/O errors indicating hardware failture.");
box.setInformativeText("Running test will be stopped.");
box.exec();
}
}
void HDDTestWidget::refDevice_accessWarning() {}
void HDDTestWidget::UpdateInfo(TestWidget::DataSet dataset) {
if(dataset == TestWidget::RESULTS) {
// update info tab - tested device
ui->model->setText(device.model);
ui->serial->setText(device.serial);
ui->firmware->setText(device.firmware);
ui->size->setText(Def::FormatSize(device.size));
ui->mountpoint->setText(device.mountpoint);
ui->fstype->setText(device.fstype);
ui->fsoptions->setText(device.fsoptions);
ui->kernel->setText(device.kernel);
} else if(dataset == TestWidget::REFERENCE) {
// update info tab - reference devices
ui->reference_model->setText(refDevice.model);
ui->reference_serial->setText(refDevice.serial);
ui->reference_firmware->setText(refDevice.firmware);
ui->reference_size->setText(Def::FormatSize(refDevice.size));
ui->reference_mountpoint->setText(refDevice.mountpoint);
ui->reference_fstype->setText(refDevice.fstype);
ui->reference_fsoptions->setText(refDevice.fsoptions);
ui->reference_kernel->setText(refDevice.kernel);
} else {
std::cerr << "WARNING: Cannot update info for unknown dataset." << std::endl;
}
}
void HDDTestWidget::ReloadTests(bool loaded) {
// check test modes - FS, Valid
bool fs = device.fs;
bool valid = device.size > 0;
UpdateInfo(TestWidget::RESULTS);
// Raw device test
ui->readblockwidget->SetStartEnabled(!loaded && valid);
ui->readcontwidget->SetStartEnabled(!loaded && valid);
ui->readrndwidget->SetStartEnabled(!loaded && valid);
ui->seekwidget->SetStartEnabled(!loaded && valid);
// Filesystem tests
ui->smallfileswidget->SetStartEnabled(!loaded && fs);
ui->filerwwidget->SetStartEnabled(!loaded && fs);
ui->filestructurewidget->SetStartEnabled(!loaded && fs);
}
void HDDTestWidget::EraseResults(TestWidget::DataSet dataset) {
if(dataset == TestWidget::RESULTS) {
device.EraseDriveInfo();
} else {
refDevice.EraseDriveInfo();
}
ui->readblockwidget->EraseResults(dataset);
ui->readcontwidget->EraseResults(dataset);
ui->readrndwidget->EraseResults(dataset);
ui->seekwidget->EraseResults(dataset);
ui->smallfileswidget->EraseResults(dataset);
ui->filerwwidget->EraseResults(dataset);
ui->filestructurewidget->EraseResults(dataset);
}
void HDDTestWidget::on_save_clicked() {
QString filename = QFileDialog::getSaveFileName(this, tr("Save results"), "", tr("Results (*.hddtest)"));
if(filename.length() > 0) {
// Create base document for tests results
QDomDocument doc("HddTest");
// Create base element
QDomElement results = doc.createElement("Results");
doc.appendChild(results);
// save drive info
results.appendChild(device.WriteInfo(doc));
// save tests results
results.appendChild(ui->filerwwidget->WriteResults(doc));
results.appendChild(ui->filestructurewidget->WriteResults(doc));
results.appendChild(ui->readblockwidget->WriteResults(doc));
results.appendChild(ui->readcontwidget->WriteResults(doc));
results.appendChild(ui->readrndwidget->WriteResults(doc));
results.appendChild(ui->seekwidget->WriteResults(doc));
results.appendChild(ui->smallfileswidget->WriteResults(doc));
// write document to file
QFile file(filename);
file.open(QIODevice::WriteOnly);
QTextStream stream(&file);
stream << doc.toString();
file.close();
}
}
void HDDTestWidget::on_open_clicked() {
QString filename = QFileDialog::getOpenFileName(
this,
tr("Open Saved results"),
"",
tr("Results (*.hddtest)"));
if(!filename.isNull()) {
// add open result to drive selection
ui->drive->addItem(QIcon::fromTheme("document-open", QIcon(":icon/icon/document-open.png")),
filename,
QVariant::fromValue(Device::Item::Saved(filename)));
// add open result to reference selection
ui->reference->addItem(QIcon::fromTheme("document-open", QIcon(":icon/icon/document-open.png")),
filename,
QVariant::fromValue(Device::Item::Saved(filename)));
// open new result in reference seection
ui->reference->setCurrentIndex(ui->reference->count() - 1);
}
}
void HDDTestWidget::on_about_clicked() {
About about;
about.exec();
}
void HDDTestWidget::OpenResultFile(QString filename, TestWidget::DataSet dataset) {
if(filename.length() == 0) {
std::cerr << "WARNING: no filename given for results file" << std::endl;
return;
}
// open file
QFile file(filename);
if(!file.open(QIODevice::ReadOnly)) {
std::cerr << "Cannot open results file" << std::endl;
file.close();
return;
}
// Open document
QDomDocument doc("HddTest");
QString err;
int row,col;
if(!doc.setContent(&file, &err, &row, &col)) {
file.close();
std::cerr << "Cannot set XML document context to file" << std::endl;
return;
}
file.close();
QDomElement root = doc.documentElement();
// restore device info
(dataset == TestWidget::REFERENCE)?refDevice.ReadInfo(root):device.ReadInfo(root);
// restore tests result
ui->seekwidget->RestoreResults(root, dataset);
ui->filerwwidget->RestoreResults(root, dataset);
ui->filestructurewidget->RestoreResults(root, dataset);
ui->smallfileswidget->RestoreResults(root, dataset);
ui->readblockwidget->RestoreResults(root, dataset);
ui->readrndwidget->RestoreResults(root, dataset);
ui->readcontwidget->RestoreResults(root, dataset);
}
void HDDTestWidget::closeEvent(QCloseEvent *ev) {
// Check whenever something is in progress
bool running = false;
if(ui->filerwwidget->testState == TestWidget::STARTED)
running = true;
if(ui->filestructurewidget->testState == TestWidget::STARTED)
running = true;
if(ui->readblockwidget->testState == TestWidget::STARTED)
running = true;
if(ui->readcontwidget->testState == TestWidget::STARTED)
running = true;
if(ui->readrndwidget->testState == TestWidget::STARTED)
running = true;
if(ui->seekwidget->testState == TestWidget::STARTED)
running = true;
if(ui->smallfileswidget->testState == TestWidget::STARTED)
running = true;
if(running) {
// Refuse to close and report to user
ev->ignore();
QMessageBox box;
box.setText("Cannot close application since test is running.");
box.exec();
} else {
ev->accept(); // close
}
}
void HDDTestWidget::device_list_refresh() {
// save selection
QString selectedDrive = ui->drive->currentText();
QString selectedReference = ui->reference->currentText();
// Improves performance and sychromization
ui->drive->blockSignals(true);
ui->reference->blockSignals(true);
// Clear device lists
ui->drive->clear();
ui->reference->clear();
// Add drive selection to results combo box
QList<Device::Item> devList = device.GetDevices();
for(int i = 0; i < devList.size(); ++i)
ui->drive->addItem(QIcon(":icon/icon/hdd.svg"), devList[i].label, QVariant::fromValue(devList[i]));
// Add nothing operation to reference combo box
ui->drive->insertSeparator(ui->drive->count());
ui->reference->addItem(
QIcon::fromTheme("process-stop", QIcon(":icon/icon/process-stop.png")),
"Nothing",
QVariant::fromValue(Device::Item::None()));
ui->reference->insertSeparator(ui->reference->count());
// Add saved resutls for both combo boxes
QFileInfoList savedList = QDir(":reference/reference").entryInfoList(QStringList("*.hddtest"), QDir::Files);
for(int i = 0; i < savedList.size(); ++i) {
QString label = savedList[i].fileName();
QString path = savedList[i].absoluteFilePath();
Device::Item item = Device::Item::Saved(path);
ui->drive->addItem(
QIcon::fromTheme("document-open", QIcon(":icon/icon/document-open.png")),
label,
QVariant::fromValue(item));
ui->reference->addItem(
QIcon::fromTheme("document-open", QIcon(":icon/icon/document-open.png")),
label,
QVariant::fromValue(item));
}
// add separators behind the list in order to separate open results later
ui->drive->insertSeparator(ui->drive->count());
ui->reference->insertSeparator(ui->reference->count());
// unblock signals
ui->drive->blockSignals(false);
ui->reference->blockSignals(false);
// restore selection
int driveIndex = 0;
if(!selectedDrive.isEmpty()) {
driveIndex = ui->drive->findText(selectedDrive);
if(driveIndex < 0) {
driveIndex = 0;
}
}
ui->drive->setCurrentIndex(driveIndex);
on_drive_currentIndexChanged("");
int referenceIndex = 0;
if(!selectedReference.isEmpty()) {
referenceIndex = ui->reference->findText(selectedReference);
if(referenceIndex < 0) {
referenceIndex = 0;
}
}
ui->reference->setCurrentIndex(referenceIndex);
}