File: smbcdiscoverer.cpp

package info (click to toggle)
kio-extras 4%3A25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 31,928 kB
  • sloc: cpp: 28,852; ansic: 3,084; perl: 1,048; xml: 116; sh: 92; python: 28; makefile: 9
file content (326 lines) | stat: -rw-r--r-- 10,706 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
    SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
    SPDX-FileCopyrightText: 2020-2021 Harald Sitter <sitter@kde.org>
*/

#include <QCoreApplication>
#include <QScopeGuard>
#include <QUrlQuery>

#include "smbcdiscoverer.h"

static QEvent::Type LoopEvent = QEvent::User;

class SMBCServerDiscovery : public SMBCDiscovery
{
public:
    SMBCServerDiscovery(const UDSEntry &entry)
        : SMBCDiscovery(entry)
    {
        m_entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
        m_entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH));
        m_entry.fastInsert(KIO::UDSEntry::UDS_URL, url());
        m_entry.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("application/x-smb-server"));
        m_entry.fastInsert(KIO::UDSEntry::UDS_ICON_NAME, QStringLiteral("network-server"));
    }

    QString url()
    {
        QUrl u("smb://");
        u.setHost(udsName());
        return u.url();
    }
};

class SMBCShareDiscovery : public SMBCDiscovery
{
public:
    SMBCShareDiscovery(const UDSEntry &entry)
        : SMBCDiscovery(entry)
    {
        m_entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
        m_entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
    }
};

class SMBCWorkgroupDiscovery : public SMBCDiscovery
{
public:
    SMBCWorkgroupDiscovery(const UDSEntry &entry)
        : SMBCDiscovery(entry)
    {
        m_entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
        m_entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, (S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH));
        m_entry.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("application/x-smb-workgroup"));
        m_entry.fastInsert(KIO::UDSEntry::UDS_URL, url());
    }

    QString url()
    {
        QUrl u("smb://");
        u.setHost(udsName());
        if (!u.isValid()) {
            // In the event that the workgroup contains bad characters, put it in a query instead.
            // This is transparently handled by SMBUrl when we get this as input again.
            // Also see documentation there.
            // https://bugs.kde.org/show_bug.cgi?id=204423
            u.setHost(QString());
            QUrlQuery q;
            q.addQueryItem("kio-workgroup", udsName());
            u.setQuery(q);
        }
        return u.url();
    }
};

class SMBCPrinterDiscovery : public SMBCDiscovery
{
public:
    SMBCPrinterDiscovery(const UDSEntry &entry)
        : SMBCDiscovery(entry)
    {
        m_entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, 0x0);
        m_entry.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/vnd.kde.kio.smb.printer"));
        // Relative to parent, but need to set it so we can append a marker. Subsequent stat calls wouldn't know that this is a printer
        // because libsmbc doesn't reflect that in the stat output.
        m_entry.fastInsert(KIO::UDSEntry::UDS_URL, udsName() + QStringLiteral("?kio-printer=true"));
    }
};

SMBCDiscovery::SMBCDiscovery(const UDSEntry &entry)
    : m_entry(entry)
    // cache the name, it may get accessed more than once
    , m_name(entry.stringValue(KIO::UDSEntry::UDS_NAME))
{
}

QString SMBCDiscovery::udsName() const
{
    return m_name;
}

KIO::UDSEntry SMBCDiscovery::toEntry() const
{
    return m_entry;
}

SMBCDiscoverer::SMBCDiscoverer(const SMBUrl &url, QEventLoop *loop, SMBWorker *worker)
    : m_url(url)
    , m_loop(loop)
    , m_worker(worker)
{
}

SMBCDiscoverer::~SMBCDiscoverer()
{
    if (m_dirFd > 0) {
        smbc_closedir(m_dirFd);
    }
}

void SMBCDiscoverer::start()
{
    queue();
}

bool SMBCDiscoverer::discoverNextFileInfo()
{
#ifdef HAVE_READDIRPLUS2
    // Readdirplus2 dir/file listing. Becomes noop when at end of data associated with dirfd.
    // If readdirplus2 isn't available the regular dirent listing is done.
    // readdirplus2 improves performance by giving us a stat without separate call (Samba>=4.12)
    struct stat st;
    const struct libsmb_file_info *fileInfo = smbc_readdirplus2(m_dirFd, &st);
    if (fileInfo) {
        const QString name = QString::fromUtf8(fileInfo->name);
        qCDebug(KIO_SMB_LOG) << "fileInfo"
                             << "name:" << name;
        if (name == ".") {
            return true;
        } else if (name == "..") {
            m_dirWasRoot = false;
            return true;
        }
        UDSEntry entry;
        entry.reserve(5); // Minimal size. stat will set at least 4 fields.
        entry.fastInsert(KIO::UDSEntry::UDS_NAME, name);

        m_url.addPath(name);
        m_worker->statToUDSEntry(m_url, st, entry); // won't produce useful error
        Q_EMIT newDiscovery(Discovery::Ptr(new SMBCDiscovery(entry)));
        m_url.cdUp();
        return true;
    }
#endif // HAVE_READDIRPLUS2
    return false;
}

void SMBCDiscoverer::discoverNext()
{
    // Poor man's concurrency. smbc isn't thread safe so we'd hold up other
    // discoverers until we are done. While that will likely happen anyway
    // because smbc_opendir (usually?) blocks until it actually has all
    // the data to loop on, meaning the actual looping after open is fairly
    // fast. Even so, there's benefit in letting other discoverers do
    // their work in the meantime because they may do more atomic
    // requests that are async and can take a while due to network latency.
    // To get somewhat reasonable behavior we simulate an async smbc discovery
    // by posting loop events to the eventloop and each loop run we process
    // a single dirent.
    // This effectively unblocks the eventloop between iterations.
    // Once we are out of entries this discoverer is considered finished.

    // Always queue a new iteration when returning so we don't forget to.
    auto autoQueue = qScopeGuard([this] {
        queue();
    });

    if (m_dirFd == -1) {
        init();
        Q_ASSERT(m_dirFd || m_finished);
        return;
    }

    static bool printersOnly = qEnvironmentVariableIntValue("KIO_SMB_PRINTERS") > 0;

    if (!printersOnly && discoverNextFileInfo()) {
        return;
    }

    qCDebug(KIO_SMB_LOG) << "smbc_readdir ";
    struct smbc_dirent *dirp = smbc_readdir(m_dirFd);
    if (dirp == nullptr) {
        qCDebug(KIO_SMB_LOG) << "done with smbc";
        stop();
        return;
    }

    // We cannot trust dirp->commentlen as it might be with or without the NUL character
    // See KDE bug #111430 and Samba bug #3030.
    // This further complicates things because name is really declared as name[1]
    // which is insofar a problem as in Qt6 QByteArrayView knows to handle this properly
    // for the fromUtf8 conversion resulting in only a single character getting extracted.
    // To mitigate both problems we a) always rely on the terminal NUL detection and
    // b) explicitly decay name to a pointer such that QString has to obey a).
    const QString name = QString::fromUtf8(std::addressof(dirp->name[0]));
    const QString comment = QString::fromUtf8(dirp->comment);

    qCDebug(KIO_SMB_LOG) << "dirent "
                         << "name:" << name << "comment:" << comment << "type:" << dirp->smbc_type;

    UDSEntry entry;
    // Minimal potential size. The actual size depends on this function,
    // possibly the stat function, and lastly the Discovery objects themselves.
    // The smallest will be a ShareDiscovery with 5 fields.
    entry.reserve(5);
    entry.fastInsert(KIO::UDSEntry::UDS_NAME, name);
    entry.fastInsert(KIO::UDSEntry::UDS_COMMENT, comment);
    // Ensure system shares are marked hidden.
    if (name.endsWith(QLatin1Char('$'))) {
        entry.fastInsert(KIO::UDSEntry::UDS_HIDDEN, 1);
    }

    if (printersOnly) {
        if (dirp->smbc_type == SMBC_PRINTER_SHARE) {
            Q_EMIT newDiscovery(Discovery::Ptr(new SMBCPrinterDiscovery(entry)));
        }
        return;
    }

#if !defined(HAVE_READDIRPLUS2)
    // . and .. are always of the dir type so they are of no consequence outside
    // actual dir listing and that'd be done by readdirplus2 already
    if (name == ".") {
        // Skip the "." entry
        // Mind the way m_currentUrl is handled in the loop
    } else if (name == "..") {
        m_dirWasRoot = false;
    } else if (dirp->smbc_type == SMBC_FILE || dirp->smbc_type == SMBC_DIR) {
        // Set stat information
        m_url.addPath(name);
        const int statErr = m_worker->browse_stat_path(m_url, entry);
        if (statErr != 0) {
            // The entry can disappear in the time span between
            // listing and the stat call. There's nothing we or the user
            // can do about it. Log the incident and move on with listing.
            qCWarning(KIO_SMB_LOG) << "Failed to stat" << m_url << statErr;
        } else {
            Q_EMIT newDiscovery(Discovery::Ptr(new SMBCDiscovery(entry)));
        }
        m_url.cdUp();
    }
#endif // HAVE_READDIRPLUS2

    if (dirp->smbc_type == SMBC_SERVER) {
        Q_EMIT newDiscovery(Discovery::Ptr(new SMBCServerDiscovery(entry)));
    } else if (dirp->smbc_type == SMBC_FILE_SHARE) {
        Q_EMIT newDiscovery(Discovery::Ptr(new SMBCShareDiscovery(entry)));
    } else if (dirp->smbc_type == SMBC_WORKGROUP) {
        Q_EMIT newDiscovery(Discovery::Ptr(new SMBCWorkgroupDiscovery(entry)));
    } else {
        qCDebug(KIO_SMB_LOG) << "SMBC_UNKNOWN :" << name;
    }
}

void SMBCDiscoverer::customEvent(QEvent *event)
{
    if (event->type() == LoopEvent) {
        if (!m_finished) {
            discoverNext();
        }
        return;
    }
    QObject::customEvent(event);
}

void SMBCDiscoverer::stop()
{
    m_finished = true;
    Q_EMIT finished();
}

bool SMBCDiscoverer::isFinished() const
{
    return m_finished;
}

bool SMBCDiscoverer::dirWasRoot() const
{
    return m_dirWasRoot;
}

int SMBCDiscoverer::error() const
{
    return m_error;
}

void SMBCDiscoverer::init()
{
    Q_ASSERT(m_dirFd < 0);

    m_dirFd = smbc_opendir(m_url.toSmbcUrl());
    if (m_dirFd >= 0) {
        m_error = 0;
    } else {
        m_error = errno;
        stop();
    }

    qCDebug(KIO_SMB_LOG) << "open" << m_url.toSmbcUrl() << "url-type:" << m_url.getType() << "dirfd:" << m_dirFd << "errNum:" << m_error;

    return;
}

void SMBCDiscoverer::queue()
{
    if (m_finished) {
        return;
    }

    // Queue low priority events. For server discovery (that is: other discoverers run as well)
    // we want the modern discoverers to be peferred. For other discoveries only
    // SMBC is running on the loop and so the priority has no negative impact.
    QCoreApplication::postEvent(this, new QEvent(LoopEvent), Qt::LowEventPriority);
}

#include "moc_smbcdiscoverer.cpp"