| 1 |
// Copyright (C) 2016 Intel Corporation |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtCore/QStorageInfo> |
| 5 |
|
| 6 |
void printVolumes(const QList<QStorageInfo> &volumes, int (*printer)(const char *, ...)) |
| 7 |
{ |
| 8 |
// Sample output: |
| 9 |
// Filesystem (Type) Size Available BSize Label Mounted on |
| 10 |
// /dev/sda2 (ext4) RO 388480 171218 1024 /boot |
| 11 |
// /dev/mapper/system-root (btrfs) RW |
| 12 |
// 214958080 39088272 4096 / |
| 13 |
// /dev/disk1s2 (hfs) RW 488050672 419909696 4096 Macintosh HD2 /Volumes/Macintosh HD2 |
| 14 |
|
| 15 |
int fsColumnWidth = 25; |
| 16 |
int labelColumnWidth = 20; |
| 17 |
for (const QStorageInfo &info : volumes) { |
| 18 |
int len = 3 + info.device().size() + info.fileSystemType().size(); |
| 19 |
fsColumnWidth = qMax(fsColumnWidth, len); |
| 20 |
if (QString subvol = info.subvolume(); !subvol.isEmpty()) |
| 21 |
labelColumnWidth = qMax(labelColumnWidth, int(subvol.size() + strlen("subvol="))); |
| 22 |
else |
| 23 |
labelColumnWidth = qMax(labelColumnWidth, int(info.name().size())); |
| 24 |
} |
| 25 |
|
| 26 |
printer("%*s Size Available BSize %*s Mounted on\n", |
| 27 |
-fsColumnWidth, "Filesystem (Type)", |
| 28 |
-labelColumnWidth, "Label"); |
| 29 |
for (const QStorageInfo &info : volumes) { |
| 30 |
QByteArray fsAndType = info.device(); |
| 31 |
if (info.fileSystemType() != fsAndType) |
| 32 |
fsAndType += " (" + info.fileSystemType() + ')'; |
| 33 |
|
| 34 |
printer("%*s R%c ", -fsColumnWidth, fsAndType.constData(), info.isReadOnly() ? 'O' : 'W'); |
| 35 |
printer("%10llu %10llu %5u ", info.bytesTotal() / 1024, info.bytesFree() / 1024, info.blockSize()); |
| 36 |
if (!info.subvolume().isEmpty()) |
| 37 |
printer("subvol=%*s ", -labelColumnWidth + int(strlen("subvol=")), qPrintable(info.subvolume())); |
| 38 |
else |
| 39 |
printer("%*s ", -labelColumnWidth, qPrintable(info.name())); |
| 40 |
printer("%s\n", qPrintable(info.rootPath())); |
| 41 |
} |
| 42 |
} |