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
101 changes: 51 additions & 50 deletions service/diskoperation/filesystems/ntfs.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-only

#include "ddlog.h"

Check warning on line 5 in service/diskoperation/filesystems/ntfs.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ddlog.h" not found.
#include "ntfs.h"
#include "filesystem.h"
#include "utils.h"

Check warning on line 8 in service/diskoperation/filesystems/ntfs.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "utils.h" not found.

#include <QDebug>

Expand All @@ -12,45 +13,45 @@

FS NTFS::getFilesystemSupport()
{
qDebug() << "[NTFS]::getFilesystemSupport - Enter";
qCDebug(serviceLog) << "[NTFS]::getFilesystemSupport - Enter";
FS fs(FS_NTFS);

fs.busy = FS::GPARTED;

if (!Utils::findProgramInPath("ntfsinfo").isEmpty()) {
qDebug() << "ntfsinfo found, enabling read support.";
qCDebug(serviceLog) << "ntfsinfo found, enabling read support.";
fs.read = FS::EXTERNAL;
}

if (!Utils::findProgramInPath("ntfslabel").isEmpty()) {
qDebug() << "ntfslabel found, enabling label and uuid support.";
qCDebug(serviceLog) << "ntfslabel found, enabling label and uuid support.";
fs .read_label = FS::EXTERNAL ;
fs .write_label = FS::EXTERNAL ;
fs.write_uuid = FS::EXTERNAL;
}

if (!Utils::findProgramInPath("mkntfs").isEmpty()) {
qDebug() << "mkntfs found, enabling create support.";
qCDebug(serviceLog) << "mkntfs found, enabling create support.";
fs.create = FS::EXTERNAL;
fs.create_with_label = FS::EXTERNAL;
}

//resizing is a delicate process ...
if (!Utils::findProgramInPath("ntfsresize").isEmpty()) {
qDebug() << "ntfsresize found, enabling check, grow, shrink, move support.";
qCDebug(serviceLog) << "ntfsresize found, enabling check, grow, shrink, move support.";
fs.check = FS::EXTERNAL;
fs.grow = FS::EXTERNAL;

if (fs.read) {//needed to determine a min file system size..
qDebug() << "Read support is available, enabling shrink.";
qCDebug(serviceLog) << "Read support is available, enabling shrink.";
fs.shrink = FS::EXTERNAL;
}

fs.move = FS::GPARTED;
}

if (!Utils::findProgramInPath("ntfsclone").isEmpty()) {
qDebug() << "ntfsclone found, enabling copy support.";
qCDebug(serviceLog) << "ntfsclone found, enabling copy support.";
fs.copy = FS::EXTERNAL;
}

Expand All @@ -61,108 +62,108 @@
// For GParted this means 2 MiB because smallest GUI unit is MiB.
m_fsLimits.min_size = 2 * MEBIBYTE;

qDebug() << "[NTFS]::getFilesystemSupport - Exit";
qCDebug(serviceLog) << "[NTFS]::getFilesystemSupport - Exit";
return fs;
}

void NTFS::setUsedSectors( Partition & partition )
{
qDebug() << "[NTFS]::setUsedSectors - Enter";
qCDebug(serviceLog) << "[NTFS]::setUsedSectors - Enter";
QString output, error, strmatch;

Check warning on line 72 in service/diskoperation/filesystems/ntfs.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The scope of the variable 'strmatch' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:\012void f(int x)\012{\012 int i = 0;\012 if (x) {\012 // it's safe to move 'int i = 0;' here\012 for (int n = 0; n < 10; ++n) {\012 // it is possible but not safe to move 'int i = 0;' here\012 do_something(&i);\012 }\012 }\012}\012When you see this message it is always safe to reduce the variable scope 1 level.

m_blocksSize = m_numOfFreeOrUsedBlocks = m_totalNumOfBlock = -1;
QString cmd = QString("ntfsinfo --mft --force %1").arg(partition.getPath());

if (!Utils::executCmd(cmd, output, error)) {
qDebug() << "ntfsinfo executed successfully.";
qCDebug(serviceLog) << "ntfsinfo executed successfully.";
strmatch = "Cluster Size:";
int index = output.indexOf(strmatch);
if (index >= 0 && index < output.length()) {
m_blocksSize = Utils::regexpLabel(output, QString("(?<=Cluster Size:).*(?=\n)")).trimmed().toLong();
qDebug() << "Found Cluster Size:" << m_blocksSize;
qCDebug(serviceLog) << "Found Cluster Size:" << m_blocksSize;
}

strmatch = "Volume Size in Clusters:";
index = output.indexOf(strmatch);
if (index >= 0 && index < output.length()) {
m_totalNumOfBlock = Utils::regexpLabel(output, QString("(?<=Volume Size in Clusters:).*(?=\n)")).trimmed().toLong();
qDebug() << "Found Volume Size in Clusters:" << m_totalNumOfBlock;
qCDebug(serviceLog) << "Found Volume Size in Clusters:" << m_totalNumOfBlock;
}

strmatch = "Free Clusters:";
index = output.indexOf(strmatch);
if (index >= 0 && index < output.length()) {
m_numOfFreeOrUsedBlocks = Utils::regexpLabel(output, QString("(?<=Free Clusters:).*(?=[(])")).trimmed().toLong();
qDebug() << "Found Free Clusters:" << m_numOfFreeOrUsedBlocks;
qCDebug(serviceLog) << "Found Free Clusters:" << m_numOfFreeOrUsedBlocks;
}
} else {
qDebug() << "ntfsinfo execution failed. Error:" << error;
qCDebug(serviceLog) << "ntfsinfo execution failed. Error:" << error;
}

if (m_blocksSize > -1 && m_totalNumOfBlock > -1 && m_numOfFreeOrUsedBlocks > -1) {
qDebug() << "Calculating and setting sector usage.";
qCDebug(serviceLog) << "Calculating and setting sector usage.";
m_totalNumOfBlock = m_totalNumOfBlock * (m_blocksSize / partition.m_sectorSize);
m_numOfFreeOrUsedBlocks = m_numOfFreeOrUsedBlocks * (m_blocksSize / partition.m_sectorSize);

partition.setSectorUsage(m_totalNumOfBlock, m_numOfFreeOrUsedBlocks);
partition.m_fsBlockSize = m_blocksSize;
}

qDebug() << "[NTFS]::setUsedSectors - Exit";
qCDebug(serviceLog) << "[NTFS]::setUsedSectors - Exit";
}

void NTFS::readLabel( Partition & partition )
{
qDebug() << "[NTFS]::readLabel - Enter";
qCDebug(serviceLog) << "[NTFS]::readLabel - Enter";
QString output, error;
if (!Utils::executCmd(QString("ntfslabel --force %1").arg(partition.getPath()), output, error)) {
qDebug() << "Successfully read label:" << output.trimmed();
qCDebug(serviceLog) << "Successfully read label:" << output.trimmed();
partition.setFilesystemLabel(output.trimmed());
} else {
qDebug() << "Failed to read label. Error:" << error;
qCDebug(serviceLog) << "Failed to read label. Error:" << error;
}
qDebug() << "[NTFS]::readLabel - Exit";
qCDebug(serviceLog) << "[NTFS]::readLabel - Exit";
}

bool NTFS::writeLabel( const Partition & partition)
{
qDebug() << "[NTFS]::writeLabel - Enter";
qCDebug(serviceLog) << "[NTFS]::writeLabel - Enter";
QString output, error;
int exitcode = Utils::executCmd(QString("ntfslabel --force %1 %2").arg(partition.getPath()).arg(partition.getFileSystemLabel()), output, error);
bool success = (exitcode == 0);
qDebug() << "[NTFS]::writeLabel - Exit with status:" << success;
qCDebug(serviceLog) << "[NTFS]::writeLabel - Exit with status:" << success;
return success;
}

void NTFS::readUuid(Partition & partition)
{
qDebug() << "[NTFS]::readUuid - Enter/Exit";
qCDebug(serviceLog) << "[NTFS]::readUuid - Enter/Exit";
}

bool NTFS::writeUuid( const Partition & partition)
{
qDebug() << "[NTFS]::writeUuid - Enter";
qCDebug(serviceLog) << "[NTFS]::writeUuid - Enter";
QString output, error;
int exitcode = Utils::executCmd(QString("ntfslabel --new-serial %1").arg(partition.getPath()), output, error);
bool success = (exitcode == 0 || error.compare("Unknown error") == 0);
qDebug() << "[NTFS]::writeUuid - Exit with status:" << success;
qCDebug(serviceLog) << "[NTFS]::writeUuid - Exit with status:" << success;
return success;
}

bool NTFS::create(const Partition & newPartition)
{
qDebug() << "[NTFS]::create - Enter";
qCDebug(serviceLog) << "[NTFS]::create - Enter";
QString output, error;
int exitcode = -1;
if (newPartition.getFileSystemLabel().isEmpty() || newPartition.getFileSystemLabel() == " ") {
qDebug() << "Creating NTFS without label on" << newPartition.getPath();
qCDebug(serviceLog) << "Creating NTFS without label on" << newPartition.getPath();
exitcode = Utils::executCmd(QString("mkntfs -Q -v -F %1").arg(newPartition.getPath()), output, error);
} else {
qDebug() << "Creating NTFS with label" << newPartition.getFileSystemLabel() << "on" << newPartition.getPath();
qCDebug(serviceLog) << "Creating NTFS with label" << newPartition.getFileSystemLabel() << "on" << newPartition.getPath();
exitcode = Utils::executCmd(QString("mkntfs -Q -v -F %1 -L %2").arg(newPartition.getPath()).arg(newPartition.getFileSystemLabel()),output, error);
}
bool success = (exitcode == 0 && error.compare("Unknown error") == 0);
qDebug() << "[NTFS]::create - Exit with status:" << success;
qCDebug(serviceLog) << "[NTFS]::create - Exit with status:" << success;
return success;
}

Expand All @@ -179,33 +180,33 @@

bool NTFS::resize(const Partition &partitionNew, bool fillPartition)
{
// qDebug() << "[NTFS]::resize(Partition) - Enter, partition:" << partitionNew.getPath() << "fill:" << fillPartition;
// qCDebug(serviceLog) << "[NTFS]::resize(Partition) - Enter, partition:" << partitionNew.getPath() << "fill:" << fillPartition;
double d = Utils::sectorToUnit(partitionNew.getSectorLength(), partitionNew.m_sectorSize, UNIT_BYTE);
bool result = resize(partitionNew.getPath(), QString::number(((long long)d / 1000)), fillPartition);
// qDebug() << "[NTFS]::resize(Partition) - Exit with status:" << result;
// qCDebug(serviceLog) << "[NTFS]::resize(Partition) - Exit with status:" << result;
return result;
}

bool NTFS::resize(const QString &path, const QString &sizeByte, bool fillPartition)
{
// qDebug() << "[NTFS]::resize(path) - Enter, path:" << path << "size(k):" << sizeByte << "fill:" << fillPartition;
// qCDebug(serviceLog) << "[NTFS]::resize(path) - Enter, path:" << path << "size(k):" << sizeByte << "fill:" << fillPartition;
// bool success;
QString output, error;
QString size, cmd;
if (!fillPartition) {
size = QString(" -s %1k").arg(sizeByte);
// qDebug() << "Shrinking filesystem, size arg:" << size;
// qCDebug(serviceLog) << "Shrinking filesystem, size arg:" << size;
}

//Utils::executCmd(QString("umount %1").arg(path));

cmd = "ntfsresize --force --force" + size ;
// Real resize
cmd = QString("%1 %2").arg(cmd).arg(path);
// qDebug() << "Executing ntfsresize command:" << cmd;
// qCDebug(serviceLog) << "Executing ntfsresize command:" << cmd;
// success = !Utils::executCmd(cmd, output, error);
bool success = !Utils::executCmd(cmd, output, error);
// qDebug() << "[NTFS]::resize(path) - Exit with status:" << success << "output:" << output << "error:" << error;
// qCDebug(serviceLog) << "[NTFS]::resize(path) - Exit with status:" << success << "output:" << output << "error:" << error;
return success;
}

Expand All @@ -220,59 +221,59 @@

bool NTFS::checkRepair(const Partition &partition)
{
qDebug() << "[NTFS]::checkRepair - Enter";
qCDebug(serviceLog) << "[NTFS]::checkRepair - Enter";

bool result = checkRepair(partition.getPath());
qDebug() << "[NTFS]::checkRepair - return " << result;
qCDebug(serviceLog) << "[NTFS]::checkRepair - return " << result;
return result;
}

bool NTFS::checkRepair(const QString &devpath)
{
qDebug() << "[NTFS]::checkRepair(path) - Enter";
qCDebug(serviceLog) << "[NTFS]::checkRepair(path) - Enter";
QString output, error;
int exitcode = Utils::executCmd(QString("ntfsresize -i -f -v %1").arg(devpath), output, error);
bool success = (exitcode == 0 || error.compare("Unknown error") == 0);
qDebug() << "[NTFS]::checkRepair(path) - Exit with status:" << success;
qCDebug(serviceLog) << "[NTFS]::checkRepair(path) - Exit with status:" << success;
return success;
}

FS_Limits NTFS::getFilesystemLimits(const Partition &partition)
{
// qDebug() << "[NTFS]::getFilesystemLimits(Partition) - Enter, for partition:" << partition.getPath();
// qCDebug(serviceLog) << "[NTFS]::getFilesystemLimits(Partition) - Enter, for partition:" << partition.getPath();
FS_Limits limits = getFilesystemLimits(partition.getPath());
// qDebug() << "[NTFS]::getFilesystemLimits(Partition) - Exit";
// qCDebug(serviceLog) << "[NTFS]::getFilesystemLimits(Partition) - Exit";
return limits;
}

FS_Limits NTFS::getFilesystemLimits(const QString &path)
{
qDebug() << "[NTFS]::getFilesystemLimits(path) - Enter, for path:" << path;
qCDebug(serviceLog) << "[NTFS]::getFilesystemLimits(path) - Enter, for path:" << path;
m_fsLimits = FS_Limits {-1, -1};
QString cmd, output, error;
//Utils::executCmd(QString("umount %1").arg(path), output, error);
cmd = QString("ntfsresize -m -f %1").arg(path);
qDebug() << "Executing ntfsresize command:" << cmd;
qCDebug(serviceLog) << "Executing ntfsresize command:" << cmd;
if (Utils::executCmd(cmd, output, error) != 0 && error.compare("Unknown error") != 0) {
qDebug() << "ntfsresize execution failed. Error:" << error;
qCDebug(serviceLog) << "ntfsresize execution failed. Error:" << error;
return m_fsLimits;
}

foreach (QString str, output.split("\n")) {

Check warning on line 262 in service/diskoperation/filesystems/ntfs.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 'str' is used to iterate by value. It could be declared as a const reference which is usually faster and recommended in C++.
// qDebug()<<"getFilesystemLimits ntfs"<<str;
// qCDebug(serviceLog)<<"getFilesystemLimits ntfs"<<str;
if (str.contains("Minsize (in MB):")) {
// qDebug() << "Found Minsize line:" << str;
// qCDebug(serviceLog) << "Found Minsize line:" << str;
auto list = str.split(":");
//qDebug()<<"getFilesystemLimits ntfs list"<<list;
//qCDebug(serviceLog)<<"getFilesystemLimits ntfs list"<<list;
if (list.count() == 2) {
m_fsLimits.min_size = list[1].toLongLong() * 1000 * 1000; //转换为byte
//qDebug()<<"getFilesystemLimits ntfs min_size"<<tmp.min_size;
//qCDebug(serviceLog)<<"getFilesystemLimits ntfs min_size"<<tmp.min_size;
m_fsLimits.max_size = 0;
return m_fsLimits;
}
}
}
qDebug() << "[NTFS]::getFilesystemLimits(path) - Exit, limits not found.";
qCDebug(serviceLog) << "[NTFS]::getFilesystemLimits(path) - Exit, limits not found.";
return m_fsLimits;
}

Expand Down
Loading
Loading