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
66public:
67 struct MountInfo {
68 QString mountPoint;
69 QByteArray fsType;
70 QByteArray device;
71 QByteArray fsRoot;
72 dev_t stDev = 0;
73 quint64 mntid = 0;
74 };
75
76 void setFromMountInfo(MountInfo &&info)
77 {
78 rootPath = std::move(info.mountPoint);
79 fileSystemType = std::move(info.fsType);
80 device = std::move(info.device);
81 subvolume = std::move(info.fsRoot);
82 }
83
84 QStorageInfoPrivate(MountInfo &&info)
85 {
86 setFromMountInfo(std::move(info));
87 }
88
89#elif defined(Q_OS_UNIX)
90 void initRootPath();
91 void retrieveVolumeInfo();
92#endif
93
94#ifdef Q_OS_UNIX
95 // Common helper functions
96 template <typename String>
97 static bool isParentOf(const String &parent, const QString &dirName)
98 {
99 return dirName.startsWith(parent) &&
100 (dirName.size() == parent.size() || dirName.at(parent.size()) == u'/' ||
101 parent.size() == 1);
102 }
103 static inline bool shouldIncludeFs(const QString &mountDir, const QByteArray &fsType);
104#endif
105
106public:
107 QString rootPath;
108 QByteArray device;
109 QByteArray subvolume;
110 QByteArray fileSystemType;
111 QString name;
112
113 qint64 bytesTotal = -1;
114 qint64 bytesFree = -1;
115 qint64 bytesAvailable = -1;
116 int blockSize = -1;
117
118 bool readOnly = false;
119 bool ready = false;
120 bool valid = false;
121};
122
123#ifdef Q_OS_UNIX
124bool QStorageInfoPrivate::shouldIncludeFs(const QString &mountDir, const QByteArray &fsType)
125{
126#if defined(Q_OS_ANDROID)
127 // "rootfs" is the filesystem type of "/" on Android
128 static constexpr char RootFsStr[] = "";
129#else
130 // "rootfs" is a type of ramfs on Linux, used in the initrd on some distros
131 static constexpr char RootFsStr[] = "rootfs";
132#endif
133
134 using namespace Qt::StringLiterals;
135 /*
136 * This function implements a heuristic algorithm to determine whether a
137 * given mount should be reported to the user. Our objective is to list
138 * only entries that the end-user would find useful.
139 *
140 * We therefore ignore:
141 * - mounted in /dev, /proc, /sys: special mounts
142 * (this will catch /sys/fs/cgroup, /proc/sys/fs/binfmt_misc, /dev/pts,
143 * some of which are tmpfs on Linux)
144 * - mounted in /var/run or /var/lock: most likely pseudofs
145 * (on earlier systemd versions, /var/run was a bind-mount of /run, so
146 * everything would be unnecessarily duplicated)
147 * - filesystem type is "rootfs": artifact of the root-pivot on some Linux
148 * initrd
149 * - if the filesystem total size is zero, it's a pseudo-fs (not checked here).
150 */
151
152 if (isParentOf("/dev"_L1, mountDir)
153 || isParentOf("/proc"_L1, mountDir)
154 || isParentOf("/sys"_L1, mountDir)
155 || isParentOf("/var/run"_L1, mountDir)
156 || isParentOf("/var/lock"_L1, mountDir)) {
157 return false;
158 }
159
160 if (!fsType.isEmpty() && fsType == RootFsStr)
161 return false;
162
163 // size checking in QStorageInfo::mountedVolumes()
164 return true;
165}
166#endif // Q_OS_UNIX
167
168QT_END_NAMESPACE
169
170#endif // QSTORAGEINFO_P_H
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)