summaryrefslogtreecommitdiffstats
path: root/src/common-lib/filesystemmountwatcher.cpp
blob: 225ab2cd5bdb24ae0fe26651b740602a3525d3f7 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QSocketNotifier>
#include <QFileSystemWatcher>

#if defined(Q_OS_MACOS) || defined(Q_OS_IOS)
#  include <unistd.h>
#  include <sys/statvfs.h>
#  include <sys/mount.h>
#elif defined(Q_OS_LINUX)
#  include <mntent.h>
#endif
#include <qplatformdefs.h>

#include "filesystemmountwatcher.h"

QT_BEGIN_NAMESPACE_AM


class FileSystemMountWatcherPrivate
{
public:
    FileSystemMountWatcherPrivate()
    {
#if defined(Q_OS_LINUX)
        if (s_mountTabFile.isEmpty()) {
            s_mountTabFile = "/proc/self/mounts";

            m_procMountsFd = QT_OPEN(s_mountTabFile.constData(), O_RDONLY);
            if (m_procMountsFd >= 0) {
                m_procMountsNotifier = new QSocketNotifier(m_procMountsFd, QSocketNotifier::Exception);
                QObject::connect(m_procMountsNotifier, &QSocketNotifier::activated,
                                 m_procMountsNotifier, [this]() { mountsChanged(); });
            }
        } else {
            m_autoTestMountTabWatcher = new QFileSystemWatcher({ QString::fromLocal8Bit(s_mountTabFile) });
            QObject::connect(m_autoTestMountTabWatcher, &QFileSystemWatcher::fileChanged,
                             m_autoTestMountTabWatcher, [this]() { mountsChanged(); });
        }
#endif
    }

    ~FileSystemMountWatcherPrivate()
    {
        delete m_autoTestMountTabWatcher;
        delete m_procMountsNotifier;
        if (m_procMountsFd >= 0)
            QT_CLOSE(m_procMountsFd);
    }

    bool add(FileSystemMountWatcher *watcher, const QString &mountPoint)
    {
        if ((!m_procMountsNotifier && !m_autoTestMountTabWatcher) || !watcher || mountPoint.isEmpty())
            return false;

        m_mountPoints.insert(mountPoint, watcher);

        if (m_mountPoints.size() == 1)
            m_mounts = currentMountPoints(); // we need to know from where we started
        return true;
    }

    bool remove(FileSystemMountWatcher *watcher, const QString &mountPoint)
    {
        if ((!m_procMountsNotifier && !m_autoTestMountTabWatcher) || !watcher || mountPoint.isEmpty())
            return false;

        m_mountPoints.remove(mountPoint, watcher);
        return true;
    }

    void attach()
    {
        m_ref.ref();
    }

    bool detach(FileSystemMountWatcher *watcher)
    {
        m_mountPoints.removeIf([=](decltype(m_mountPoints)::iterator it) {
            return it.value() == watcher;
        });
        return !m_ref.deref();
    }

    static QMultiMap<QString, QString> currentMountPoints()
    {
        QMultiMap<QString, QString> result;
#if defined(Q_OS_WIN) || defined (Q_OS_QNX)
        return result; // no mounts on Windows, not supported on QNX

#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
        struct statfs *sfs = nullptr;
        int count = getmntinfo(&sfs, MNT_NOWAIT);

        for (int i = 0; i < count; ++i, ++sfs) {
            result.insert(QString::fromLocal8Bit(sfs->f_mntonname), QString::fromLocal8Bit(sfs->f_mntfromname));
        }
#else
        FILE *pm = fopen(s_mountTabFile.constData(), "r");
        if (!pm)
            return result;

#  if defined(Q_OS_ANDROID)
        while (struct mntent *mntPtr = getmntent(pm)) {
            result.insert(QString::fromLocal8Bit(mntPtr->mnt_dir),
                          QString::fromLocal8Bit(mntPtr->mnt_fsname));
        }
#  else
        static const size_t pathMax = static_cast<size_t>(pathconf("/", _PC_PATH_MAX)) * 2 + 1024;  // quite big, but better be safe than sorry
        QScopedArrayPointer<char> strBuf(new char[pathMax]);
        struct mntent mntBuf;

        while (getmntent_r(pm, &mntBuf, strBuf.data(), int(pathMax) - 1)) {
            result.insert(QString::fromLocal8Bit(mntBuf.mnt_dir),
                          QString::fromLocal8Bit(mntBuf.mnt_fsname));
        }
#  endif
        fclose(pm);
#endif

        return result;
    }

    void mountsChanged()
    {
        if (m_mountPoints.isEmpty())
            return;

        auto newMounts = currentMountPoints();

        for (auto it = m_mountPoints.cbegin(); it != m_mountPoints.cend(); ++it) {
            const QString mp = it.key();
            bool isMounted = newMounts.contains(mp);
            bool wasMounted = m_mounts.contains(mp);

            if (isMounted != wasMounted)
                emit it.value()->mountChanged(mp, newMounts.value(mp));
        }

        m_mounts = newMounts;
    }

private:
    QAtomicInt m_ref;

    static QByteArray s_mountTabFile;

    int m_procMountsFd = -1;
    QSocketNotifier *m_procMountsNotifier = nullptr;
    QFileSystemWatcher *m_autoTestMountTabWatcher = nullptr;
    QMultiMap<QString, FileSystemMountWatcher *> m_mountPoints;
    QMultiMap<QString, QString> m_mounts;

    friend class FileSystemMountWatcher;
};

QByteArray FileSystemMountWatcherPrivate::s_mountTabFile;


FileSystemMountWatcherPrivate *FileSystemMountWatcher::d = nullptr;

FileSystemMountWatcher::FileSystemMountWatcher(QObject *parent)
    : QObject(parent)
{
    if (!d)
        d = new FileSystemMountWatcherPrivate();
    d->attach();
}

FileSystemMountWatcher::~FileSystemMountWatcher()
{
    if (d->detach(this)) {
        delete d;
        d = nullptr;
    }
}

QMultiMap<QString, QString> FileSystemMountWatcher::currentMountPoints()
{
    // if we have an active cache, then use it
    if (d->m_procMountsNotifier)
        return d->m_mounts;
    return FileSystemMountWatcherPrivate::currentMountPoints();
}

void FileSystemMountWatcher::addMountPoint(const QString &directory)
{
    d->add(this, directory);
}

void FileSystemMountWatcher::removeMountPoint(const QString &directory)
{
    d->remove(this, directory);
}

bool FileSystemMountWatcher::setMountTabFileForTesting(const QString &mtabFile)
{
    if (d)
        return false; // too late
#if defined(Q_OS_LINUX)
    FileSystemMountWatcherPrivate::s_mountTabFile = mtabFile.toLocal8Bit();
    return true;
#else
    Q_UNUSED(mtabFile)
    return false;
#endif
}

QT_END_NAMESPACE_AM

#include "moc_filesystemmountwatcher.cpp"