Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions basestruct/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -86,15 +86,40 @@
return exitcode;
}

int Utils::executWithPipeCmd(const QString &strCmd, QString &outPut, QString &error)
int Utils::executWithInputOutputCmd(const QString &strCmd, const QStringList &strArg, const QString *inPut, QString &outPut, QString &error)
{
QStringList argList;
argList << "-c" << strCmd;
qDebug() << "Entering Utils::executWithInputOutputCmd with command:" << strCmd << strArg;
QProcess proc;
int exitCode;

proc.start(strCmd, strArg);

// 统一等待启动完成:无论是否需要写入 stdin,都检测 FailedToStart
// (程序不存在等场景),避免后续 readAll 取到空输出且 exitCode 为 0 的"假成功"
if (!proc.waitForStarted(-1)) {
error = proc.errorString();
return -1;
}

if (inPut) {
proc.write(inPut->toLocal8Bit());
proc.closeWriteChannel();
}

proc.waitForFinished(-1);
outPut = proc.readAllStandardOutput().data();
// 优先取子进程 stderr(含 cryptsetup/xfs_db 等的真实失败原因);
// 为空时回退到 QProcess 自身错误(如启动失败 execvp: ...)
QString stdErr = proc.readAllStandardError();
error = stdErr.isEmpty() ? proc.errorString() : stdErr;
exitCode = proc.exitCode();
proc.close();

return executeCmdWithArtList("/bin/bash", argList, outPut, error);
qDebug() << "Utils::executWithInputOutputCmd exitCode: " << exitCode;
return exitCode;
}

int Utils::executWithErrorCmd(const QString &strCmd, const QStringList &strArg, QString &outPut, QString &outPutError, QString &error)

Check warning on line 122 in basestruct/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'executWithErrorCmd' is never used.

Check warning on line 122 in basestruct/utils.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

The function 'executWithErrorCmd' is never used.
{
qDebug() << "Utils::executWithErrorCmd cmd: " << strCmd;
qDebug() << "Utils::executWithErrorCmd argList: " << strArg;
Expand Down
8 changes: 5 additions & 3 deletions basestruct/utils.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -55,13 +55,15 @@ class Utils
static int executCmd(const QString &strCmd);

/**
* @brief 执行外部命令,使用 /bin/bash -c 执行管道命令
* @brief 执行外部命令,并提供输入输出
* @param strCmd:命令
* @param strArg:参数
* @param inPut:命令输入
* @param outPut:命令输出
* @param error:错误信息
* @return 非0失败
*/
static int executWithPipeCmd(const QString &strCmd, QString &outPut, QString &error);
static int executWithInputOutputCmd(const QString &strCmd, const QStringList &strArg, const QString *inPut, QString &outPut, QString &error);

/**
* @brief 执行外部命令
Expand Down
18 changes: 11 additions & 7 deletions service/diskoperation/DeviceStorage.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -504,14 +504,18 @@ void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model)
}
}

cmd = "udevadm info " + devicePath + " | grep ID_MODEL=";
proc.start("bash", QStringList() << "-c" << cmd);
proc.start("udevadm", QStringList() << "info" << devicePath);
proc.waitForFinished(-1);
outPut = proc.readAllStandardOutput();
if (!outPut.isEmpty()) {
auto idModel = outPut.split("=", QString::SkipEmptyParts);
if (idModel.count() > 1)
model = idModel.at(1).trimmed();
QString key = "ID_MODEL=";
int start = outPut.indexOf(key);
if (start != -1) {
start += key.length(); // 移动到等号后面
int end = outPut.indexOf('\n', start); // 找到行尾
if (end == -1)
end = outPut.length(); // 如果是最后一行

model = outPut.mid(start, end - start).trimmed();
if (!model.isEmpty())
return;
}
Expand Down
13 changes: 7 additions & 6 deletions service/diskoperation/filesystems/xfs.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -47,12 +47,13 @@ FS XFS::getFilesystemSupport()

void XFS::setUsedSectors(Partition &partition)
{
QString output, error, strmatch, strcmd;
QString output, error;
m_blocksSize = m_numOfFreeOrUsedBlocks = m_totalNumOfBlock = -1;
strcmd = QString("xfs_db -r -c 'sb 0' -c 'print blocksize' -c 'print dblocks'"
" -c 'print fdblocks' %1")
.arg(partition.getPath());
if (Utils::executWithPipeCmd(strcmd, output, error) == 0) {
QStringList args;
args << "-r" << "-c" << "sb 0" << "-c" << "print blocksize"
<< "-c" << "print dblocks" << "-c" << "print fdblocks"
<< partition.getPath();
if (Utils::executWithInputOutputCmd("xfs_db", args, nullptr, output, error) == 0) {
auto strList = output.split("\n");
foreach (auto &item, strList) {
auto value = item.split("=", QString::SkipEmptyParts);
Expand Down
69 changes: 20 additions & 49 deletions service/diskoperation/luksoperator/luksoperator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright (C) 2022 ~ 2022 Deepin Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -476,20 +475,13 @@ bool LUKSOperator::isLUKS(QString devPath)

bool LUKSOperator::format(const LUKS_INFO &luks)
{
QProcess proc;
QStringList options;
options << "-c" << QString("echo -n '%1' | cryptsetup --cipher %2 --key-size 256 --hash sha256 luksFormat --label=%3 %4 -q")
.arg(luks.m_decryptStr)
.arg(Utils::getCipherStr(luks.m_crypt))
.arg(luks.m_fileSystemLabel)
.arg(luks.m_devicePath);
proc.start("/bin/bash", options);
proc.waitForFinished(-1);
proc.waitForReadyRead();
QString outPut = proc.readAllStandardOutput();
QString error = proc.errorString();
proc.close();
bool success = (proc.exitCode() == 0);
QString outPut, error;
QStringList args;
args << "--cipher" << Utils::getCipherStr(luks.m_crypt)
<< "--key-size" << "256" << "--hash" << "sha256"
<< "luksFormat" << QString("--label=%1").arg(luks.m_fileSystemLabel)
<< luks.m_devicePath << "-q";
bool success = (Utils::executWithInputOutputCmd("cryptsetup", args, &luks.m_decryptStr, outPut, error) == 0);
if (!success) {
qCritical() << Q_FUNC_INFO << "output:" << outPut << "\terror:" << error;
}
Expand All @@ -498,32 +490,18 @@ bool LUKSOperator::format(const LUKS_INFO &luks)

bool LUKSOperator::open(const LUKS_INFO &luks)
{
QProcess proc;
QStringList options;
options << "-c" << QString("echo -n '%1' | cryptsetup open %2 %3 -q")
.arg(luks.m_decryptStr)
.arg(luks.m_devicePath)
.arg(luks.m_mapper.m_dmName);
proc.start("/bin/bash", options);
proc.waitForFinished(-1);
proc.waitForReadyRead();//
proc.close();
return proc.exitCode() == 0;
QString outPut, error;
QStringList args;
args << "open" << luks.m_devicePath << luks.m_mapper.m_dmName << "-q";
return (Utils::executWithInputOutputCmd("cryptsetup", args, &luks.m_decryptStr, outPut, error) == 0);
}

bool LUKSOperator::testKey(const LUKS_INFO &luks)
{
QProcess proc;
QStringList options;
options << "-c" << QString("echo -n '%1' | cryptsetup open --test-passphrase %2 %3 -q")
.arg(luks.m_decryptStr)
.arg(luks.m_devicePath)
.arg(luks.m_mapper.m_dmName);
proc.start("/bin/bash", options);
proc.waitForFinished(-1);
proc.waitForReadyRead();//
proc.close();
return proc.exitCode() == 0;
QString outPut, error;
QStringList args;
args << "open" << "--test-passphrase" << luks.m_devicePath << luks.m_mapper.m_dmName << "-q";
return (Utils::executWithInputOutputCmd("cryptsetup", args, &luks.m_decryptStr, outPut, error) == 0);
}

bool LUKSOperator::close(const LUKS_INFO &luks)
Expand Down Expand Up @@ -597,17 +575,10 @@ bool LUKSOperator::addKeyFile(const LUKS_INFO &luks)
}

//添加key
QProcess proc;
QStringList options;
options << "-c" << QString("echo -n '%1' | cryptsetup luksAddKey %2 %3 -q")
.arg(luks.m_decryptStr)
.arg(luks.m_devicePath)
.arg(filePath);
proc.start("/bin/bash", options);
proc.waitForFinished();
proc.waitForReadyRead();//
proc.close();
return proc.exitCode() == 0;
QString outPut, error;
QStringList args;
args << "luksAddKey" << luks.m_devicePath << filePath << "-q";
return (Utils::executWithInputOutputCmd("cryptsetup", args, &luks.m_decryptStr, outPut, error) == 0);
}

bool LUKSOperator::deleteKeyFile(const LUKS_INFO &luks)
Expand Down
14 changes: 9 additions & 5 deletions service/diskoperation/partedcore.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

Expand Down Expand Up @@ -432,8 +432,10 @@ bool PartedCore::detectionPartitionTableError(const QString &devicePath)
bool needRewrite = gptIsExpanded(devicePath);
if (needRewrite) {
QString outPutFix, errorFix;
QString cmdFix = QString("echo w | fdisk %1").arg(devicePath);
Utils::executWithPipeCmd(cmdFix, outPutFix, errorFix);
QStringList args;
args << devicePath;
QString inPut("w\n");
Utils::executWithInputOutputCmd("fdisk", args, &inPut, outPutFix, errorFix);
qDebug() << __FUNCTION__ << "createPartition Partition Table Rewrite Done";
return false;
}
Expand Down Expand Up @@ -3335,8 +3337,10 @@ void PartedCore::reWritePartition(const QString &devicePath)
bool needRewrite = gptIsExpanded(devicePath);
if (needRewrite) {
QString outPutFix, errorFix;
QString cmdFix = QString("echo w | fdisk %1").arg(devicePath);
Utils::executWithPipeCmd(cmdFix, outPutFix, errorFix);
QStringList args;
args << devicePath;
QString inPut("w\n");
Utils::executWithInputOutputCmd("fdisk", args, &inPut, outPutFix, errorFix);
qDebug() << __FUNCTION__ << "createPartition Partition Table Rewrite Done";
return;
}
Expand Down
41 changes: 23 additions & 18 deletions service/watcher.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -8,27 +8,39 @@
#include <unistd.h>
#include <QDebug>
#include <QTime>
#include <QThread>

Check warning on line 11 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QThread> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 11 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QThread> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QProcess>

Check warning on line 12 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 12 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QString>

Check warning on line 13 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QString> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 13 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QString> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QRegularExpression>

Check warning on line 14 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 14 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QCoreApplication>

Check warning on line 15 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCoreApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in service/watcher.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QCoreApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.


namespace DiskManager {

/**
* @brief 执行外部命令
* @param strCmd:外部命令字符串
* @param outPut:命令控制台输出
* @param error:错误信息
* @return exitcode:退出码
* @brief 检测前端进程 deepin-diskmanager 是否在运行
*
* 仅启动 ps 一个子进程并在代码内完成过滤,避免使用 shell 解释器,也不再串联 awk/grep 子进程。
* 等价于管道 `ps -eo cmd | grep -w deepin-diskmanager$`。
*
*/
void Watcher::executCmd(const QString &strCmd, QString &outPut, QString &error)
bool Watcher::isFrontEndRunning(QString &error)
{
QProcess proc;
proc.start("bash", QStringList() << "-c" << strCmd);
proc.start("ps", QStringList() << "-eo" << "cmd");
proc.waitForFinished(-1);
outPut = proc.readAllStandardOutput();
error = proc.readAllStandardError();

static const QRegularExpression rxMatch("(?:^|[^\\w])deepin-diskmanager$");
const QStringList lines = QString::fromLocal8Bit(proc.readAllStandardOutput()).split('\n');
for (const QString &line : lines) {
const QString exe = line.trimmed().section(' ', 0, 0, QString::SectionSkipEmpty);
if (rxMatch.match(exe).hasMatch()) {
return true;
}
}

return false;
}

void Watcher::exit()
Expand All @@ -41,18 +53,12 @@
void Watcher::run()
{
bool isrun = false;
QString cmd, outPut, error;
//先判断后台服务进程是否存在,如果存在可能是强制退出导致,应先退出后台程序再重新启动磁盘管理器
cmd = QString("ps -eo pid,cmd |awk '{print $2}' |grep -w deepin-diskmanager$");
QString error;

while (!stoped) {
QThread::msleep(500); //0.5 second
executCmd(cmd, outPut, error);
int ret = 0;
ret = outPut.length();
if (ret) {
if (isFrontEndRunning(error)) {
//这里表示前端在运行当中
// qDebug() << "Set to true!!!!!";
isrun = true;
} else {
//这里表示,前端启动过,但是现在已经关闭了
Expand All @@ -61,7 +67,6 @@
_exit(0);
}
}
// qDebug() << "Sleep !!! == " << ret << " " << outPut << " " << outPut.length() << " "<< error;
}
}

Expand Down
4 changes: 2 additions & 2 deletions service/watcher.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

Expand All @@ -24,9 +24,9 @@
*/
void run();

void executCmd(const QString &strCmd, QString &outPut, QString &error);
bool isFrontEndRunning(QString &error);

public Q_SLOTS:

Check warning on line 29 in service/watcher.h

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.

Check warning on line 29 in service/watcher.h

View workflow job for this annotation

GitHub Actions / static-check / static-check

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.
void exit();

private:
Expand Down
Loading