fix: ui no longer blocks when volumes are unreachable + add support for network and fuse volumes

This commit is contained in:
2026-10-11 08:42:05 +02:00
parent 873b2eb01b
commit 3883917228
7 changed files with 226 additions and 53 deletions
+163 -40
View File
@@ -5,9 +5,12 @@
#include <qdir.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qfuturewatcher.h>
#include <qhash.h>
#include <qloggingcategory.h>
#include <qstorageinfo.h>
#include <qthreadpool.h>
#include <qtconcurrentrun.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
@@ -17,12 +20,27 @@ namespace ZShell::services {
namespace {
// A scan that finishes quickly is not worth showing as loading. Only a scan that
// stays in flight this long is reported, so the UI does not flicker every tick.
constexpr int kLoadingDelayMs = 300;
struct Accum {
quint64 usedBytes = 0;
quint64 totalBytes = 0;
bool hasRoot = false;
};
QThreadPool* storagePool() {
// Single thread: a scan blocked on an unreachable mount should not take a
// worker away from the global pool.
static QThreadPool* pool = [] {
auto* p = new QThreadPool();
p->setMaxThreadCount(1);
return p;
}();
return pool;
}
[[nodiscard]] QString sysfsRealPath(uint major, uint minor) {
const QString link =
QStringLiteral("/sys/dev/block/%1:%2").arg(major).arg(minor);
@@ -103,7 +121,14 @@ QStringList resolveByDevt(uint major, uint minor, int depth) {
} // namespace
Storage::Storage(QObject* parent) : TickingService(parent) {}
Storage::Storage(QObject* parent) : TickingService(parent) {
m_loadingTimer.setSingleShot(true);
QObject::connect(&m_loadingTimer, &QTimer::timeout, this, [this] {
if (m_watcher) {
setLoading(true);
}
});
}
qreal Storage::percentage() const {
qreal totalUsed = 0.0;
@@ -115,6 +140,18 @@ qreal Storage::percentage() const {
return totalSize > 0.0 ? totalUsed / totalSize : 0.0;
}
bool Storage::loading() const {
return m_loading;
}
void Storage::setLoading(bool value) {
if (m_loading == value) {
return;
}
m_loading = value;
Q_EMIT loadingChanged();
}
bool Storage::sameOrder(const QList<DiskInfo*>& a, const QList<DiskInfo*>& b) {
if (a.size() != b.size()) {
return false;
@@ -162,18 +199,41 @@ DiskInfo* Storage::primaryDisk() const {
bool Storage::isPseudoFs(QByteArrayView fsType) {
static constexpr const char* kPseudo[] = {
"tmpfs", "devtmpfs", "proc", "sysfs", "cgroup",
"cgroup2", "overlay", "squashfs", "devpts", "mqueue",
"ramfs", "rpc_pipefs", "autofs", "configfs", "debugfs",
"tracefs", "securityfs", "pstore", "bpf", "binfmt_misc",
"hugetlbfs", "fusectl", "efivarfs", "selinuxfs",
"tmpfs",
"devtmpfs",
"proc",
"sysfs",
"cgroup",
"cgroup2",
"overlay",
"squashfs",
"devpts",
"mqueue",
"ramfs",
"rpc_pipefs",
"autofs",
"configfs",
"debugfs",
"tracefs",
"securityfs",
"pstore",
"bpf",
"binfmt_misc",
"hugetlbfs",
"fusectl",
"efivarfs",
"selinuxfs",
// Real FUSE mounts (rclone, sshfs, ...) are reported like any other
// volume, so only the virtual ones are excluded here.
"fuse.gvfsd-fuse",
"fuse.portal",
};
for (const char* p : kPseudo) {
if (fsType == QByteArrayView(p)) {
return true;
}
}
return fsType.startsWith(QByteArrayView("fuse."));
return false;
}
QStringList Storage::resolveToPhysicalDisks(const QString& devicePath) {
@@ -190,18 +250,14 @@ QStringList Storage::resolveToPhysicalDisks(const QString& devicePath) {
return resolveByDevt(major(st.st_rdev), minor(st.st_rdev));
}
void Storage::tick() {
const qreal prevPercentage = percentage();
QHash<QString, Accum> byDisk;
// Multiple mounts can share a single backing filesystem (btrfs subvolumes,
// bind mounts, etc.) and each one reports identical bytesTotal/bytesAvailable.
// Dedupe by source device so the filesystem only contributes once per disk.
StorageScan Storage::scanVolumes() {
struct DeviceEntry {
quint64 totalBytes = 0;
quint64 usedBytes = 0;
bool hasRoot = false;
QByteArray device;
// Mount point of the first mount seen for this device. Used as the entry
// key when the device has no backing block device (network mount).
QString mount;
};
QHash<QByteArray, DeviceEntry> byDevice;
@@ -215,38 +271,120 @@ void Storage::tick() {
continue;
}
const QByteArray device = v.device();
const auto totalBytes = static_cast<quint64>(v.bytesTotal());
const auto availBytes = static_cast<quint64>(v.bytesAvailable());
const quint64 usedBytes =
totalBytes > availBytes ? totalBytes - availBytes : 0;
const bool isRoot = v.rootPath() == QStringLiteral("/");
DeviceEntry& e = byDevice[device];
e.device = device;
DeviceEntry& e = byDevice[v.device()];
e.totalBytes = totalBytes;
e.usedBytes = usedBytes;
e.hasRoot = e.hasRoot || isRoot;
if (e.mount.isEmpty()) {
e.mount = v.rootPath();
}
}
QHash<QString, Accum> byEntry;
// Multiple mounts can share a single backing filesystem (btrfs subvolumes,
// bind mounts, etc.) and each one reports identical bytesTotal/bytesAvailable.
// Dedupe by source device so the filesystem only contributes once per disk.
for (auto it = byDevice.constBegin(); it != byDevice.constEnd(); ++it) {
const DeviceEntry& e = it.value();
const QStringList disks =
resolveToPhysicalDisks(QString::fromLocal8Bit(e.device));
resolveToPhysicalDisks(QString::fromLocal8Bit(it.key()));
if (disks.isEmpty()) {
// Network mounts (and any other filesystem with no backing block
// device) have no physical disk to attribute the usage to, so the
// mount itself becomes the entry.
Accum& a = byEntry[e.mount];
a.usedBytes += e.usedBytes;
a.totalBytes += e.totalBytes;
a.hasRoot = a.hasRoot || e.hasRoot;
continue;
}
for (const QString& d : disks) {
if (d.startsWith(QStringLiteral("zram"))) {
continue;
}
Accum& a = byDisk[d];
Accum& a = byEntry[d];
a.usedBytes += e.usedBytes;
a.totalBytes += e.totalBytes;
a.hasRoot = a.hasRoot || e.hasRoot;
}
}
StorageScan scan;
scan.reserve(byEntry.size());
for (auto it = byEntry.constBegin(); it != byEntry.constEnd(); ++it) {
StorageEntry entry;
entry.mount = it.key();
entry.usedBytes = it.value().usedBytes;
entry.totalBytes = it.value().totalBytes;
entry.hasRoot = it.value().hasRoot;
scan.append(entry);
}
std::sort(
scan.begin(),
scan.end(),
[](const StorageEntry& a, const StorageEntry& b) {
if (a.hasRoot != b.hasRoot) {
return a.hasRoot;
}
return a.mount < b.mount;
});
return scan;
}
void Storage::tick() {
if (m_watcher) {
// Still waiting on a mount that has not answered. Starting another scan
// here would just queue blocked threads behind the first.
return;
}
beginScan();
}
void Storage::beginScan() {
m_loadingTimer.start(kLoadingDelayMs);
m_watcher = new QFutureWatcher<StorageScan>(this);
QObject::connect(
m_watcher,
&QFutureWatcher<StorageScan>::finished,
this,
&Storage::onScanFinished);
m_watcher->setFuture(
QtConcurrent::run(storagePool(), &Storage::scanVolumes));
}
void Storage::onScanFinished() {
QFutureWatcher<StorageScan>* watcher = m_watcher;
m_watcher = nullptr;
if (!watcher) {
return;
}
StorageScan scan = watcher->result();
watcher->deleteLater();
m_loadingTimer.stop();
// scan is already ordered (root disk first), so applying it in order keeps the
// QML model in the same order as before.
applyScan(scan);
setLoading(false);
}
void Storage::applyScan(const StorageScan& scan) {
const qreal prevPercentage = percentage();
QHash<QString, DiskInfo*> existing;
existing.reserve(m_disks.size());
for (DiskInfo* d : std::as_const(m_disks)) {
@@ -254,32 +392,17 @@ void Storage::tick() {
}
QList<DiskInfo*> next;
next.reserve(byDisk.size());
for (auto it = byDisk.constBegin(); it != byDisk.constEnd(); ++it) {
if (DiskInfo* survivor = existing.take(it.key())) {
survivor->update(
it.value().usedBytes,
it.value().totalBytes,
it.value().hasRoot);
next.reserve(scan.size());
for (const StorageEntry& e : scan) {
if (DiskInfo* survivor = existing.take(e.mount)) {
survivor->update(e.usedBytes, e.totalBytes, e.hasRoot);
next.append(survivor);
} else {
next.append(new DiskInfo(
it.key(),
it.value().usedBytes,
it.value().totalBytes,
it.value().hasRoot,
this));
e.mount, e.usedBytes, e.totalBytes, e.hasRoot, this));
}
}
std::sort(
next.begin(), next.end(), [](const DiskInfo* a, const DiskInfo* b) {
if (a->hasRoot() != b->hasRoot()) {
return a->hasRoot();
}
return a->mount() < b->mount();
});
bool manualCleared = false;
if (DiskInfo* m = m_manualPrimaryDisk.data();
m && existing.contains(m->mount())) {