2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 Intel Corporation
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-04-12 22:58:32 +00:00
|
|
|
|
|
|
|
#include <QtCore/QStorageInfo>
|
|
|
|
|
|
|
|
void printVolumes(const QList<QStorageInfo> &volumes, int (*printer)(const char *, ...))
|
|
|
|
{
|
|
|
|
// Sample output:
|
|
|
|
// Filesystem (Type) Size Available BSize Label Mounted on
|
|
|
|
// /dev/sda2 (ext4) RO 388480 171218 1024 /boot
|
|
|
|
// /dev/mapper/system-root (btrfs) RW
|
|
|
|
// 214958080 39088272 4096 /
|
|
|
|
// /dev/disk1s2 (hfs) RW 488050672 419909696 4096 Macintosh HD2 /Volumes/Macintosh HD2
|
|
|
|
|
2023-09-22 17:58:13 +00:00
|
|
|
int fsColumnWidth = 25;
|
|
|
|
int labelColumnWidth = 20;
|
|
|
|
for (const QStorageInfo &info : volumes) {
|
|
|
|
int len = 3 + info.device().size() + info.fileSystemType().size();
|
|
|
|
fsColumnWidth = qMax(fsColumnWidth, len);
|
|
|
|
if (QString subvol = info.subvolume(); !subvol.isEmpty())
|
|
|
|
labelColumnWidth = qMax(labelColumnWidth, int(subvol.size() + strlen("subvol=")));
|
|
|
|
else
|
|
|
|
labelColumnWidth = qMax(labelColumnWidth, int(info.name().size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
printer("%*s Size Available BSize %*s Mounted on\n",
|
|
|
|
-fsColumnWidth, "Filesystem (Type)",
|
|
|
|
-labelColumnWidth, "Label");
|
QStorageInfo manual test: port away from Q_FOREACH
In main.cpp, the loop is over a local variable which would be const
were it not for the multi-step initialization that I didn't want to
change. The loop body clearly doesn't modify the container, so port to
ranged for loop with std::as_const().
In printvolumes.cpp, the loop _does_ invoke unknown code (through the
function pointer passed as the second argument), but, as could be
expected, the two users of the function don't pass functions that know
about `volumes`:
- in the tst_QStorageInfo auto-test, an rvalue `volumes` is passed,
so we don't need to analyze the qInfoPrinter function passed there,
as it cannot possibly reference the temporary
- and in main.cpp of the manual test, we just pass printf (which is
technically UB (taking the address of a standard library function),
but I don't care right now).
Pick-to: 6.6 6.5
Task-number: QTBUG-115839
Change-Id: Ibcd10a0e0b3229d8f2a1d98545d8fa6d473a0f75
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
2023-08-08 08:17:15 +00:00
|
|
|
for (const QStorageInfo &info : volumes) {
|
2016-04-12 22:58:32 +00:00
|
|
|
QByteArray fsAndType = info.device();
|
|
|
|
if (info.fileSystemType() != fsAndType)
|
|
|
|
fsAndType += " (" + info.fileSystemType() + ')';
|
|
|
|
|
2023-09-22 17:58:13 +00:00
|
|
|
printer("%*s R%c ", -fsColumnWidth, fsAndType.constData(), info.isReadOnly() ? 'O' : 'W');
|
2016-06-21 00:53:42 +00:00
|
|
|
printer("%10llu %10llu %5u ", info.bytesTotal() / 1024, info.bytesFree() / 1024, info.blockSize());
|
|
|
|
if (!info.subvolume().isEmpty())
|
2023-09-22 17:58:13 +00:00
|
|
|
printer("subvol=%*s ", -labelColumnWidth + int(strlen("subvol=")), qPrintable(info.subvolume()));
|
2016-06-21 00:53:42 +00:00
|
|
|
else
|
2023-09-22 17:58:13 +00:00
|
|
|
printer("%*s ", -labelColumnWidth, qPrintable(info.name()));
|
2016-06-21 00:53:42 +00:00
|
|
|
printer("%s\n", qPrintable(info.rootPath()));
|
2016-04-12 22:58:32 +00:00
|
|
|
}
|
|
|
|
}
|