summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qstorageinfo_p.h
blob: 3af4b81ca4b4266c77bbd0a2cb01e62e1cc10e97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QSTORAGEINFO_P_H
#define QSTORAGEINFO_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/qloggingcategory.h>
#include <QtCore/qsystemdetection.h>
#include <QtCore/qtenvironmentvariables.h>
#include <QtCore/private/qglobal_p.h>
#include "qstorageinfo.h"

#ifdef Q_OS_UNIX
#include <sys/types.h> // dev_t
#endif

QT_BEGIN_NAMESPACE

inline Q_LOGGING_CATEGORY(lcStorageInfo, "qt.core.qstorageinfo", QtWarningMsg)

class QStorageInfoPrivate : public QSharedData
{
public:
    QStorageInfoPrivate() = default;

    void doStat();

    static QList<QStorageInfo> mountedVolumes();

    static QStorageInfo root()
    {
#ifdef Q_OS_WIN
        return QStorageInfo(QDir::fromNativeSeparators(QFile::decodeName(qgetenv("SystemDrive"))));
#else
        return QStorageInfo(QStringLiteral("/"));
#endif
    };

protected:
#if defined(Q_OS_WIN)
    void initRootPath();
    void retrieveVolumeInfo();
    void retrieveDiskFreeSpace();
    bool queryStorageProperty();
    void queryFileFsSectorSizeInformation();
#elif defined(Q_OS_DARWIN)
    void initRootPath();
    void retrievePosixInfo();
    void retrieveUrlProperties(bool initRootPath = false);
    void retrieveLabel();
#elif defined(Q_OS_LINUX)
    void retrieveVolumeInfo();

public:
    struct MountInfo {
        QString mountPoint;
        QByteArray fsType;
        QByteArray device;
        QByteArray fsRoot;
        dev_t stDev = 0;
    };

    void setFromMountInfo(MountInfo &&info)
    {
        rootPath = std::move(info.mountPoint);
        fileSystemType = std::move(info.fsType);
        device = std::move(info.device);
        subvolume = std::move(info.fsRoot);
    }

    QStorageInfoPrivate(MountInfo &&info)
    {
        setFromMountInfo(std::move(info));
    }

#elif defined(Q_OS_UNIX)
    void initRootPath();
    void retrieveVolumeInfo();
#endif

public:
    QString rootPath;
    QByteArray device;
    QByteArray subvolume;
    QByteArray fileSystemType;
    QString name;

    qint64 bytesTotal = -1;
    qint64 bytesFree = -1;
    qint64 bytesAvailable = -1;
    int blockSize = -1;

    bool readOnly = false;
    bool ready = false;
    bool valid = false;
};

// Common helper functions
template <typename String>
static bool isParentOf(const String &parent, const QString &dirName)
{
    return dirName.startsWith(parent) &&
            (dirName.size() == parent.size() || dirName.at(parent.size()) == u'/' ||
             parent.size() == 1);
}

static inline bool shouldIncludeFs(const QString &mountDir, const QByteArray &fsType)
{
#if defined(Q_OS_ANDROID)
    // "rootfs" is the filesystem type of "/" on Android
    static constexpr char RootFsStr[] = "";
#else
    // "rootfs" is a type of ramfs on Linux, used in the initrd on some distros
    static constexpr char RootFsStr[] = "rootfs";
#endif

    using namespace Qt::StringLiterals;
    /*
     * This function implements a heuristic algorithm to determine whether a
     * given mount should be reported to the user. Our objective is to list
     * only entries that the end-user would find useful.
     *
     * We therefore ignore:
     *  - mounted in /dev, /proc, /sys: special mounts
     *    (this will catch /sys/fs/cgroup, /proc/sys/fs/binfmt_misc, /dev/pts,
     *    some of which are tmpfs on Linux)
     *  - mounted in /var/run or /var/lock: most likely pseudofs
     *    (on earlier systemd versions, /var/run was a bind-mount of /run, so
     *    everything would be unnecessarily duplicated)
     *  - filesystem type is "rootfs": artifact of the root-pivot on some Linux
     *    initrd
     *  - if the filesystem total size is zero, it's a pseudo-fs (not checked here).
     */

    if (isParentOf("/dev"_L1, mountDir)
        || isParentOf("/proc"_L1, mountDir)
        || isParentOf("/sys"_L1, mountDir)
        || isParentOf("/var/run"_L1, mountDir)
        || isParentOf("/var/lock"_L1, mountDir)) {
        return false;
    }

    if (!fsType.isEmpty() && fsType == RootFsStr)
        return false;

    // size checking in QStorageInfo::mountedVolumes()
    return true;
}

QT_END_NAMESPACE

#endif // QSTORAGEINFO_P_H