Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QSTORAGEINFO_P_H
5#define QSTORAGEINFO_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qloggingcategory.h>
19#include <QtCore/qsystemdetection.h>
20#include <QtCore/qtenvironmentvariables.h>
21#include <QtCore/private/qglobal_p.h>
22#include "qstorageinfo.h"
23
24#ifdef Q_OS_UNIX
25#include <sys/types.h> // dev_t
26#endif
27
29
30inline Q_LOGGING_CATEGORY(lcStorageInfo, "qt.core.qstorageinfo", QtWarningMsg)
31
32class QStorageInfoPrivate : public QSharedData
33{
34public:
35 QStorageInfoPrivate() = default;
36
37 void doStat();
38
39 static QList<QStorageInfo> mountedVolumes();
40
41 static QStorageInfo root()
42 {
43#ifdef Q_OS_WIN
45#else
46 return QStorageInfo(QStringLiteral("/"));
47#endif
48 };
49
50protected:
51#if defined(Q_OS_WIN)
52 void initRootPath();
53 void retrieveVolumeInfo();
54 void retrieveDiskFreeSpace();
55 bool queryStorageProperty();
56 void queryFileFsSectorSizeInformation();
57#elif defined(Q_OS_DARWIN)
58 void initRootPath();
59 void retrievePosixInfo();
60 void retrieveUrlProperties(bool initRootPath = false);
61 void retrieveLabel();
62#elif defined(Q_OS_LINUX)
63 void retrieveVolumeInfo();
64
65public:
66 struct MountInfo {
67 QString mountPoint;
68 QByteArray fsType;
70 QByteArray fsRoot;
71 dev_t stDev = 0;
72 };
73
74 void setFromMountInfo(MountInfo &&info)
75 {
76 rootPath = std::move(info.mountPoint);
77 fileSystemType = std::move(info.fsType);
78 device = std::move(info.device);
79 subvolume = std::move(info.fsRoot);
80 }
81
82 QStorageInfoPrivate(MountInfo &&info)
83 {
84 setFromMountInfo(std::move(info));
85 }
86
87#elif defined(Q_OS_UNIX)
88 void initRootPath();
89 void retrieveVolumeInfo();
90#endif
91
92public:
93 QString rootPath;
95 QByteArray subvolume;
96 QByteArray fileSystemType;
98
99 qint64 bytesTotal = -1;
100 qint64 bytesFree = -1;
101 qint64 bytesAvailable = -1;
102 int blockSize = -1;
103
104 bool readOnly = false;
105 bool ready = false;
106 bool valid = false;
107};
108
109// Common helper functions
110template <typename String>
111static bool isParentOf(const String &parent, const QString &dirName)
112{
113 return dirName.startsWith(parent) &&
114 (dirName.size() == parent.size() || dirName.at(parent.size()) == u'/' ||
115 parent.size() == 1);
116}
117
118static inline bool shouldIncludeFs(const QString &mountDir, const QByteArray &fsType)
119{
120#if defined(Q_OS_ANDROID)
121 // "rootfs" is the filesystem type of "/" on Android
122 static constexpr char RootFsStr[] = "";
123#else
124 // "rootfs" is a type of ramfs on Linux, used in the initrd on some distros
125 static constexpr char RootFsStr[] = "rootfs";
126#endif
127
128 using namespace Qt::StringLiterals;
129 /*
130 * This function implements a heuristic algorithm to determine whether a
131 * given mount should be reported to the user. Our objective is to list
132 * only entries that the end-user would find useful.
133 *
134 * We therefore ignore:
135 * - mounted in /dev, /proc, /sys: special mounts
136 * (this will catch /sys/fs/cgroup, /proc/sys/fs/binfmt_misc, /dev/pts,
137 * some of which are tmpfs on Linux)
138 * - mounted in /var/run or /var/lock: most likely pseudofs
139 * (on earlier systemd versions, /var/run was a bind-mount of /run, so
140 * everything would be unnecessarily duplicated)
141 * - filesystem type is "rootfs": artifact of the root-pivot on some Linux
142 * initrd
143 * - if the filesystem total size is zero, it's a pseudo-fs (not checked here).
144 */
145
146 if (isParentOf("/dev"_L1, mountDir)
147 || isParentOf("/proc"_L1, mountDir)
148 || isParentOf("/sys"_L1, mountDir)
149 || isParentOf("/var/run"_L1, mountDir)
150 || isParentOf("/var/lock"_L1, mountDir)) {
151 return false;
152 }
153
154 if (!fsType.isEmpty() && fsType == RootFsStr)
155 return false;
156
157 // size checking in QStorageInfo::mountedVolumes()
158 return true;
159}
160
162
163#endif // QSTORAGEINFO_P_H
IOBluetoothDevice * device
\inmodule QtCore
Definition qbytearray.h:57
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
static QString fromNativeSeparators(const QString &pathName)
Definition qdir.cpp:962
static QString decodeName(const QByteArray &localFileName)
This does the reverse of QFile::encodeName() using localFileName.
Definition qfile.h:162
\inmodule QtCore
Definition qshareddata.h:19
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5455
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
Definition qstring.h:1226
Combined button and popup list for selecting options.
const int blockSize
@ QtWarningMsg
Definition qlogging.h:31
#define Q_LOGGING_CATEGORY(name,...)
GLuint name
static QString retrieveLabel(const QStorageInfoPrivate &d, quint64 deviceId)
QStorageInfoPrivate::MountInfo MountInfo
static bool shouldIncludeFs(const QString &mountDir, const QByteArray &fsType)
QT_BEGIN_NAMESPACE static Q_LOGGING_CATEGORY(lcStorageInfo, "qt.core.qstorageinfo", QtWarningMsg) class QStorageInfoPrivate bool isParentOf(const String &parent, const QString &dirName)
#define QStringLiteral(str)
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
long long qint64
Definition qtypes.h:60
QHostInfo info
[0]