Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstorageinfo_p.h
Go to the documentation of this file.
1// Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QSTORAGEINFO_P_H
6#define QSTORAGEINFO_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qloggingcategory.h>
20#include <QtCore/qsystemdetection.h>
21#include <QtCore/qtenvironmentvariables.h>
22#include <QtCore/private/qglobal_p.h>
23#include "qstorageinfo.h"
24
25#ifdef Q_OS_UNIX
26#include <sys/types.h> // dev_t
27#endif
28
29QT_BEGIN_NAMESPACE
30
31Q_DECLARE_LOGGING_CATEGORY(lcStorageInfo)
32
33class QStorageInfoPrivate : public QSharedData
34{
35public:
36 QStorageInfoPrivate() = default;
37
38 void doStat();
39
40 static QList<QStorageInfo> mountedVolumes();
41
42 static QStorageInfo root()
43 {
44#ifdef Q_OS_WIN
45 return QStorageInfo(QDir::fromNativeSeparators(QFile::decodeName(qgetenv("SystemDrive"))));
46#else
47 return QStorageInfo(QStringLiteral("/"));
48#endif
49 }
50
51protected:
52#if defined(Q_OS_WIN)
53 void initRootPath();
54 void retrieveVolumeInfo();
55 void retrieveDiskFreeSpace();
56 bool queryStorageProperty();
57 void queryFileFsSectorSizeInformation();
58#elif defined(Q_OS_DARWIN)
59 void initRootPath();
60 void retrievePosixInfo();
61 void retrieveUrlProperties(bool initRootPath = false);
62 void retrieveLabel();
63#elif defined(Q_OS_LINUX)
64 void retrieveVolumeInfo();
65
66 // used with the Linux 6.8+ API
67 static QList<QStorageInfo> listMounts();
68
69public:
70 struct MountInfo {
71 QString mountPoint;
72 QByteArray fsType;
73 QByteArray device;
74 QByteArray fsRoot;
75 dev_t stDev = 0;
76 quint64 mntid = 0;
77 };
78
79 void setFromMountInfo(MountInfo &&info)
80 {
81 rootPath = std::move(info.mountPoint);
82 fileSystemType = std::move(info.fsType);
83 device = std::move(info.device);
84 subvolume = std::move(info.fsRoot);
85 }
86
87 explicit QStorageInfoPrivate(MountInfo &&info)
88 {
89 setFromMountInfo(std::move(info));
90 }
91
92#elif defined(Q_OS_UNIX)
93 void initRootPath();
94 void retrieveVolumeInfo();
95#endif
96
97#ifdef Q_OS_UNIX
98 // Common helper functions
99 template <typename String>
100 static bool isParentOf(const String &parent, const QString &dirName)
101 {
102 return dirName.startsWith(parent) &&
103 (dirName.size() == parent.size() || dirName.at(parent.size()) == u'/' ||
104 parent.size() == 1);
105 }
106 static inline bool shouldIncludeFs(const QString &mountDir, const QByteArray &fsType);
107#endif
108
109public:
110 QString rootPath;
111 QByteArray device;
112 QByteArray subvolume;
113 QByteArray fileSystemType;
114 QString name;
115
116 qint64 bytesTotal = -1;
117 qint64 bytesFree = -1;
118 qint64 bytesAvailable = -1;
119 int blockSize = -1;
120
121 bool readOnly = false;
122 bool ready = false;
123 bool valid = false;
124};
125
126#ifdef Q_OS_UNIX
127bool QStorageInfoPrivate::shouldIncludeFs(const QString &mountDir, const QByteArray &fsType)
128{
129#if defined(Q_OS_ANDROID)
130 // "rootfs" is the filesystem type of "/" on Android
131 static constexpr char RootFsStr[] = "";
132#else
133 // "rootfs" is a type of ramfs on Linux, used in the initrd on some distros
134 static constexpr char RootFsStr[] = "rootfs";
135#endif
136
137 using namespace Qt::StringLiterals;
138 /*
139 * This function implements a heuristic algorithm to determine whether a
140 * given mount should be reported to the user. Our objective is to list
141 * only entries that the end-user would find useful.
142 *
143 * We therefore ignore:
144 * - mounted in /dev, /proc, /sys: special mounts
145 * (this will catch /sys/fs/cgroup, /proc/sys/fs/binfmt_misc, /dev/pts,
146 * some of which are tmpfs on Linux)
147 * - mounted in /var/run or /var/lock: most likely pseudofs
148 * (on earlier systemd versions, /var/run was a bind-mount of /run, so
149 * everything would be unnecessarily duplicated)
150 * - filesystem type is "rootfs": artifact of the root-pivot on some Linux
151 * initrd
152 * - if the filesystem total size is zero, it's a pseudo-fs (not checked here).
153 */
154
155 if (isParentOf("/dev"_L1, mountDir)
156 || isParentOf("/proc"_L1, mountDir)
157 || isParentOf("/sys"_L1, mountDir)
158 || isParentOf("/var/run"_L1, mountDir)
159 || isParentOf("/var/lock"_L1, mountDir)) {
160 return false;
161 }
162
163 if (!fsType.isEmpty() && fsType == RootFsStr)
164 return false;
165
166 // size checking in QStorageInfo::mountedVolumes()
167 return true;
168}
169#endif // Q_OS_UNIX
170
171QT_END_NAMESPACE
172
173#endif // QSTORAGEINFO_P_H
Combined button and popup list for selecting options.
Q_GLOBAL_STATIC(DefaultRoleNames, qDefaultRoleNames, { { Qt::DisplayRole, "display" }, { Qt::DecorationRole, "decoration" }, { Qt::EditRole, "edit" }, { Qt::ToolTipRole, "toolTip" }, { Qt::StatusTipRole, "statusTip" }, { Qt::WhatsThisRole, "whatsThis" }, }) const QHash< int
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)