File 0002-Switch-to-ntfsinfo-to-read-NTFS-usage.patch of Package kpmcore

From a5bdd5a4eb44607fed3b0318ee64cc4f15abb122 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= <andrius@stikonas.eu>
Date: Fri, 24 Dec 2021 00:02:08 +0000
Subject: [PATCH] Switch to ntfsinfo to read NTFS usage.

BUG: 447248
---
 src/fs/ntfs.cpp                      | 28 +++++++++++++++++++---------
 src/util/externalcommand_whitelist.h |  1 +
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/fs/ntfs.cpp b/src/fs/ntfs.cpp
index 185fe05..e2a29a6 100644
--- a/src/fs/ntfs.cpp
+++ b/src/fs/ntfs.cpp
@@ -47,7 +47,8 @@ ntfs::ntfs(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QStr
 
 void ntfs::init()
 {
-    m_Shrink = m_Grow = m_Check = m_GetUsed = findExternal(QStringLiteral("ntfsresize")) ? cmdSupportFileSystem : cmdSupportNone;
+    m_Shrink = m_Grow = m_Check = findExternal(QStringLiteral("ntfsresize")) ? cmdSupportFileSystem : cmdSupportNone;
+    m_GetUsed = findExternal(QStringLiteral("ntfsinfo")) ? cmdSupportFileSystem : cmdSupportNone;
     m_GetLabel = cmdSupportCore;
     m_SetLabel = findExternal(QStringLiteral("ntfslabel")) ? cmdSupportFileSystem : cmdSupportNone;
     m_Create = findExternal(QStringLiteral("mkfs.ntfs")) ? cmdSupportFileSystem : cmdSupportNone;
@@ -97,18 +98,27 @@ int ntfs::maxLabelLength() const
 
 qint64 ntfs::readUsedCapacity(const QString& deviceNode) const
 {
-    ExternalCommand cmd(QStringLiteral("ntfsresize"), { QStringLiteral("--info"), QStringLiteral("--force"), QStringLiteral("--no-progress-bar"), deviceNode });
+    ExternalCommand cmd(QStringLiteral("ntfsinfo"), { QStringLiteral("--mft"), QStringLiteral("--force"), deviceNode });
 
     if (cmd.run(-1) && cmd.exitCode() == 0) {
-        qint64 usedBytes = -1;
-        QRegularExpression re(QStringLiteral("resize at (\\d+) bytes"));
-        QRegularExpressionMatch reUsedBytes = re.match(cmd.output());
+        QRegularExpression re(QStringLiteral("Cluster Size: (\\d+)"));
+        QRegularExpressionMatch reClusterSize = re.match(cmd.output());
+        qint64 clusterSize = reClusterSize.hasMatch() ? reClusterSize.captured(1).toLongLong() : -1;
+
+        QRegularExpression re2(QStringLiteral("Free Clusters: (\\d+)"));
+        QRegularExpressionMatch reFreeClusters = re2.match(cmd.output());
+        qint64 freeClusters = reFreeClusters.hasMatch() ? reFreeClusters.captured(1).toLongLong() : -1;
 
-        if (reUsedBytes.hasMatch())
-            usedBytes = reUsedBytes.captured(1).toLongLong();
+        QRegularExpression re3(QStringLiteral("Volume Size in Clusters: (\\d+)"));
+        QRegularExpressionMatch reVolumeSize = re3.match(cmd.output());
+        qint64 volumeSize = reVolumeSize.hasMatch() ? reVolumeSize.captured(1).toLongLong() : -1;
 
-        if (usedBytes > -1)
-            return usedBytes;
+        qint64 usedBytes = -1;
+        qDebug () << volumeSize << freeClusters << clusterSize;
+        if (clusterSize > -1 && freeClusters > -1 && volumeSize > -1) {
+            usedBytes = (volumeSize - freeClusters) * clusterSize;
+        }
+        return usedBytes;
     }
 
     return -1;
diff --git a/src/util/externalcommand_whitelist.h b/src/util/externalcommand_whitelist.h
index 17455a5..2f97447 100644
--- a/src/util/externalcommand_whitelist.h
+++ b/src/util/externalcommand_whitelist.h
@@ -74,6 +74,7 @@ QStringLiteral("nilfs-resize"),
 QStringLiteral("ntfsresize"),
 QStringLiteral("mkfs.ntfs"),
 QStringLiteral("ntfsclone"),
+QStringLiteral("ntfsinfo"),
 QStringLiteral("ntfslabel"),
 QStringLiteral("fsck.ocfs2"),
 QStringLiteral("mkfs.ocfs2"),
-- 
GitLab

openSUSE Build Service is sponsored by