mirror of
https://github.com/edisionnano/QDiskInfo.git
synced 2026-04-19 19:53:20 +03:00
Chekck /usr/sbin for smartctl
Some distributions like OpenSUSE place binaries which are unsafe (i.e. pertain to storage devices) in /usr/sbin which is not in PATH
This commit is contained in:
@@ -98,21 +98,43 @@ void MainWindow::updateNavigationButtons(int currentIndex)
|
|||||||
nextButton->setEnabled(currentIndex < buttonGroup->buttons().size() - 1);
|
nextButton->setEnabled(currentIndex < buttonGroup->buttons().size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString getSmartctlPath() {
|
||||||
|
QStringList paths = QString::fromLocal8Bit(qgetenv("PATH")).split(QDir::listSeparator(), Qt::SkipEmptyParts);
|
||||||
|
|
||||||
|
paths << "/usr/sbin" << "/usr/local/sbin";
|
||||||
|
|
||||||
|
for (const QString &path : paths) {
|
||||||
|
QString absolutePath = QDir(path).absoluteFilePath("smartctl");
|
||||||
|
if (QFile::exists(absolutePath) && QFileInfo(absolutePath).isExecutable()) {
|
||||||
|
return absolutePath;
|
||||||
|
qDebug() << absolutePath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::scanDevices()
|
void MainWindow::scanDevices()
|
||||||
{
|
{
|
||||||
QString output = getSmartctlOutput({"--scan", "--json"}, false);
|
QString output = getSmartctlOutput({"--scan", "--json"}, false);
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
|
QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
|
||||||
QJsonObject jsonObj = doc.object();
|
QJsonObject jsonObj = doc.object();
|
||||||
devices = jsonObj["devices"].toArray();
|
devices = jsonObj["devices"].toArray();
|
||||||
|
QString smartctlPath = getSmartctlPath();
|
||||||
QStringList commandList;
|
QStringList commandList;
|
||||||
|
|
||||||
for (const QJsonValue &value : std::as_const(devices)) {
|
for (const QJsonValue &value : std::as_const(devices)) {
|
||||||
QJsonObject device = value.toObject();
|
QJsonObject device = value.toObject();
|
||||||
QString deviceName = device["name"].toString();
|
QString deviceName = device["name"].toString();
|
||||||
commandList.append(QString("smartctl --all --json %1").arg(deviceName));
|
commandList.append(QString(smartctlPath + " --all --json %1").arg(deviceName));
|
||||||
}
|
}
|
||||||
QString command = commandList.join(" ; ");
|
QString command = commandList.join(" ; ");
|
||||||
|
|
||||||
|
if (smartctlPath.isEmpty()) {
|
||||||
|
QMessageBox::critical(this, tr("KDiskInfo Error"), tr("smartctl was not found, please install it!"));
|
||||||
|
QTimer::singleShot(0, qApp, &QApplication::quit);
|
||||||
|
}
|
||||||
|
|
||||||
QString allDevicesOutput = getSmartctlOutput({"sh", "-c", command}, true);
|
QString allDevicesOutput = getSmartctlOutput({"sh", "-c", command}, true);
|
||||||
|
|
||||||
int startIndex = 0;
|
int startIndex = 0;
|
||||||
@@ -725,38 +747,19 @@ void MainWindow::addSmartAttributesTable(const QJsonArray &attributes)
|
|||||||
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool commandExists(const QString &command) {
|
|
||||||
QStringList paths = QString::fromLocal8Bit(qgetenv("PATH")).split(QDir::listSeparator(), Qt::SkipEmptyParts);
|
|
||||||
for (const QString &path : paths) {
|
|
||||||
QString absolutePath = QDir(path).absoluteFilePath(command);
|
|
||||||
if (QFile::exists(absolutePath) && QFileInfo(absolutePath).isExecutable()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString MainWindow::getSmartctlOutput(const QStringList &arguments, bool root)
|
QString MainWindow::getSmartctlOutput(const QStringList &arguments, bool root)
|
||||||
{
|
{
|
||||||
QProcess process;
|
QProcess process;
|
||||||
QString command;
|
QString command;
|
||||||
QStringList commandArgs;
|
|
||||||
|
|
||||||
if (root) {
|
if (root) {
|
||||||
command = "pkexec";
|
command = "pkexec";
|
||||||
commandArgs = QStringList();
|
|
||||||
} else {
|
} else {
|
||||||
command = "smartctl";
|
command = getSmartctlPath();
|
||||||
commandArgs = QStringList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!commandExists("smartctl")) {
|
if (!getSmartctlPath().isEmpty()) {
|
||||||
QMessageBox::critical(this, tr("KDiskInfo Error"), tr("smartctl was not found, please install it!"));
|
process.start(command, arguments);
|
||||||
QTimer::singleShot(0, qApp, &QApplication::quit);
|
|
||||||
} else {
|
|
||||||
commandArgs.append(arguments);
|
|
||||||
process.start(command, commandArgs);
|
|
||||||
process.waitForFinished(-1);
|
process.waitForFinished(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -765,13 +768,18 @@ QString MainWindow::getSmartctlOutput(const QStringList &arguments, bool root)
|
|||||||
if (initializing) {
|
if (initializing) {
|
||||||
QTimer::singleShot(0, qApp, &QApplication::quit);
|
QTimer::singleShot(0, qApp, &QApplication::quit);
|
||||||
}
|
}
|
||||||
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root && !initializing) {
|
if (root && !initializing) {
|
||||||
clearButtonGroup();
|
clearButtonGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
return process.readAllStandardOutput();
|
if (process.isOpen()) {
|
||||||
|
return process.readAllStandardOutput();
|
||||||
|
} else {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString MainWindow::toTitleCase(const QString& sentence) {
|
QString MainWindow::toTitleCase(const QString& sentence) {
|
||||||
@@ -896,15 +904,16 @@ void MainWindow::on_actionUse_Fahrenheit_toggled(bool enabled)
|
|||||||
QString MainWindow::initiateSelfTest(const QString &testType, const QString &deviceNode)
|
QString MainWindow::initiateSelfTest(const QString &testType, const QString &deviceNode)
|
||||||
{
|
{
|
||||||
QProcess process;
|
QProcess process;
|
||||||
QString command = "pkexec";
|
QString command = getSmartctlPath();
|
||||||
QStringList arguments;
|
QStringList arguments;
|
||||||
arguments << "smartctl" << "--json" << "-t" << testType << deviceNode;
|
arguments << command << "--json" << "-t" << testType << deviceNode;
|
||||||
|
|
||||||
process.start(command, arguments);
|
process.start("pkexec", arguments);
|
||||||
process.waitForFinished(-1);
|
process.waitForFinished(-1);
|
||||||
|
|
||||||
QString output = process.readAllStandardOutput();
|
if (process.isOpen()) {
|
||||||
QString errorOutput = process.readAllStandardError();
|
return process.readAllStandardOutput();
|
||||||
|
} else {
|
||||||
return output;
|
return QString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user