diff --git a/autotests/jobremotetest.cpp b/autotests/jobremotetest.cpp index 74690277..f56c3118 100644 --- a/autotests/jobremotetest.cpp +++ b/autotests/jobremotetest.cpp @@ -1,469 +1,529 @@ /* This file is part of the KDE project Copyright (C) 2004-2006 David Faure Copyright (C) 2008 Norbert Frese This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "jobremotetest.h" #include #include #include #include #include #include #include #include #include #include #include #include #include //#include "kiotesthelper.h" // createTestFile etc. QTEST_MAIN(JobRemoteTest) QDateTime s_referenceTimeStamp; // The code comes partly from jobtest.cpp static QUrl remoteTmpUrl() { QString customDir(qgetenv("KIO_JOBREMOTETEST_REMOTETMP")); if (customDir.isEmpty()) { return QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + '/'); } else { // Could be a path or a URL return QUrl::fromUserInput(customDir + '/'); } } static QString localTmpDir() { #ifdef Q_OS_WIN return QDir::tempPath() + "/jobremotetest/"; #else // This one needs to be on another partition return QStringLiteral("/tmp/jobremotetest/"); #endif } static bool myExists(const QUrl &url) { KIO::Job *job = KIO::stat(url, KIO::StatJob::DestinationSide, 0, KIO::HideProgressInfo); job->setUiDelegate(nullptr); return job->exec(); } static bool myMkdir(const QUrl &url) { KIO::Job *job = KIO::mkdir(url, -1); job->setUiDelegate(nullptr); return job->exec(); } void JobRemoteTest::initTestCase() { QStandardPaths::setTestModeEnabled(true); // To avoid a runtime dependency on klauncher qputenv("KDE_FORK_SLAVES", "yes"); s_referenceTimeStamp = QDateTime::currentDateTime().addSecs(-30); // 30 seconds ago // Start with a clean base dir cleanupTestCase(); QUrl url = remoteTmpUrl(); if (!myExists(url)) { const bool ok = url.isLocalFile() ? QDir().mkpath(url.toLocalFile()) : myMkdir(url); if (!ok) { qFatal("couldn't create %s", qPrintable(url.toString())); } } const bool ok = QDir().mkpath(localTmpDir()); if (!ok) { qFatal("couldn't create %s", qPrintable(localTmpDir())); } } static void delDir(const QUrl &pathOrUrl) { KIO::Job *job = KIO::del(pathOrUrl, KIO::HideProgressInfo); job->setUiDelegate(nullptr); job->exec(); } void JobRemoteTest::cleanupTestCase() { delDir(remoteTmpUrl()); delDir(QUrl::fromLocalFile(localTmpDir())); } void JobRemoteTest::enterLoop() { QEventLoop eventLoop; connect(this, &JobRemoteTest::exitLoop, &eventLoop, &QEventLoop::quit); eventLoop.exec(QEventLoop::ExcludeUserInputEvents); } ///// void JobRemoteTest::putAndGet() { QUrl u(remoteTmpUrl()); u.setPath(u.path() + "putAndGetFile"); KIO::TransferJob *job = KIO::put(u, 0600, KIO::Overwrite | KIO::HideProgressInfo); quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago job->setModificationTime(mtime); job->setUiDelegate(nullptr); connect(job, &KJob::result, this, &JobRemoteTest::slotResult); connect(job, &KIO::TransferJob::dataReq, this, &JobRemoteTest::slotDataReq); m_result = -1; m_dataReqCount = 0; enterLoop(); QVERIFY(m_result == 0); // no error m_result = -1; KIO::StoredTransferJob *getJob = KIO::storedGet(u, KIO::NoReload, KIO::HideProgressInfo); getJob->setUiDelegate(nullptr); connect(getJob, &KJob::result, this, &JobRemoteTest::slotGetResult); enterLoop(); QCOMPARE(m_result, 0); // no error QCOMPARE(m_data, QByteArray("This is a test for KIO::put()\n")); //QCOMPARE( m_data.size(), 11 ); } void JobRemoteTest::slotGetResult(KJob *job) { m_result = job->error(); m_data = static_cast(job)->data(); emit exitLoop(); } void JobRemoteTest::slotDataReq(KIO::Job *, QByteArray &data) { // Really not the way you'd write a slotDataReq usually :) switch (m_dataReqCount++) { case 0: data = "This is a test for "; break; case 1: data = "KIO::put()\n"; break; case 2: data = QByteArray(); break; } } void JobRemoteTest::slotResult(KJob *job) { m_result = job->error(); emit exitLoop(); } //// void JobRemoteTest::openFileWriting() { m_rwCount = 0; QUrl u(remoteTmpUrl()); u.setPath(u.path() + "openFileWriting"); fileJob = KIO::open(u, QIODevice::WriteOnly); fileJob->setUiDelegate(nullptr); connect(fileJob, &KJob::result, this, &JobRemoteTest::slotResult); connect(fileJob, &KIO::FileJob::data, this, &JobRemoteTest::slotFileJobData); connect(fileJob, &KIO::FileJob::open, this, &JobRemoteTest::slotFileJobOpen); connect(fileJob, &KIO::FileJob::written, this, &JobRemoteTest::slotFileJobWritten); connect(fileJob, &KIO::FileJob::position, this, &JobRemoteTest::slotFileJobPosition); connect(fileJob, QOverload::of(&KIO::FileJob::close), this, &JobRemoteTest::slotFileJobClose); m_result = -1; m_closeSignalCalled = false; enterLoop(); QEXPECT_FAIL("", "Needs fixing in kio_file", Abort); QVERIFY(m_result == 0); // no error KIO::StoredTransferJob *getJob = KIO::storedGet(u, KIO::NoReload, KIO::HideProgressInfo); getJob->setUiDelegate(nullptr); connect(getJob, &KJob::result, this, &JobRemoteTest::slotGetResult); enterLoop(); QCOMPARE(m_result, 0); // no error QVERIFY(m_closeSignalCalled); // close signal called. qDebug() << "m_data: " << m_data; QCOMPARE(m_data, QByteArray("test....test....test....test....test....test....end")); } void JobRemoteTest::slotFileJobData(KIO::Job *job, const QByteArray &data) { Q_UNUSED(job); Q_UNUSED(data); } void JobRemoteTest::slotFileJobRedirection(KIO::Job *job, const QUrl &url) { Q_UNUSED(job); Q_UNUSED(url); } void JobRemoteTest::slotFileJobMimetype(KIO::Job *job, const QString &type) { Q_UNUSED(job); Q_UNUSED(type); } void JobRemoteTest::slotFileJobOpen(KIO::Job *job) { Q_UNUSED(job); fileJob->seek(0); } void JobRemoteTest::slotFileJobWritten(KIO::Job *job, KIO::filesize_t written) { Q_UNUSED(job); Q_UNUSED(written); if (m_rwCount > 5) { fileJob->close(); } else { fileJob->seek(m_rwCount * 8); m_rwCount++; } } void JobRemoteTest::slotFileJobPosition(KIO::Job *job, KIO::filesize_t offset) { Q_UNUSED(job); Q_UNUSED(offset); const QByteArray data("test....end"); fileJob->write(data); } void JobRemoteTest::slotFileJobClose(KIO::Job *job) { Q_UNUSED(job); m_closeSignalCalled = true; - qDebug() << "+++++++++ closed"; + qDebug() << "+++++++++ filejob closed"; } //// void JobRemoteTest::openFileReading() { QUrl u(remoteTmpUrl()); u.setPath(u.path() + "openFileReading"); const QByteArray putData("test1test2test3test4test5"); KIO::StoredTransferJob *putJob = KIO::storedPut(putData, u, 0600, KIO::Overwrite | KIO::HideProgressInfo ); quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago putJob->setModificationTime(mtime); putJob->setUiDelegate(nullptr); connect(putJob, &KJob::result, this, &JobRemoteTest::slotResult); m_result = -1; enterLoop(); QVERIFY(m_result == 0); // no error m_rwCount = 4; m_data = QByteArray(); fileJob = KIO::open(u, QIODevice::ReadOnly); fileJob->setUiDelegate(nullptr); connect(fileJob, &KJob::result, this, &JobRemoteTest::slotResult); connect(fileJob, &KIO::FileJob::data, this, &JobRemoteTest::slotFileJob2Data); connect(fileJob, &KIO::FileJob::open, this, &JobRemoteTest::slotFileJob2Open); connect(fileJob, &KIO::FileJob::written, this, &JobRemoteTest::slotFileJob2Written); connect(fileJob, &KIO::FileJob::position, this, &JobRemoteTest::slotFileJob2Position); + // Can reuse this slot (same for all tests). connect(fileJob, QOverload::of(&KIO::FileJob::close), - this, &JobRemoteTest::slotFileJob2Close); + this, &JobRemoteTest::slotFileJobClose); m_result = -1; m_closeSignalCalled = false; enterLoop(); QVERIFY(m_result == 0); // no error QVERIFY(m_closeSignalCalled); // close signal called. qDebug() << "resulting m_data: " << QString(m_data); QCOMPARE(m_data, QByteArray("test5test4test3test2test1")); } void JobRemoteTest::slotFileJob2Data(KIO::Job *job, const QByteArray &data) { Q_UNUSED(job); qDebug() << "m_rwCount = " << m_rwCount << " data: " << data; m_data.append(data); if (m_rwCount < 0) { fileJob->close(); } else { fileJob->seek(m_rwCount-- * 5); } } void JobRemoteTest::slotFileJob2Redirection(KIO::Job *job, const QUrl &url) { Q_UNUSED(job); Q_UNUSED(url); } void JobRemoteTest::slotFileJob2Mimetype(KIO::Job *job, const QString &type) { Q_UNUSED(job); qDebug() << "mimetype: " << type; } void JobRemoteTest::slotFileJob2Open(KIO::Job *job) { Q_UNUSED(job); fileJob->seek(m_rwCount-- * 5); } void JobRemoteTest::slotFileJob2Written(KIO::Job *job, KIO::filesize_t written) { Q_UNUSED(job); Q_UNUSED(written); } void JobRemoteTest::slotFileJob2Position(KIO::Job *job, KIO::filesize_t offset) { Q_UNUSED(job); qDebug() << "position : " << offset << " -> read (5)"; fileJob->read(5); } -void JobRemoteTest::slotFileJob2Close(KIO::Job *job) -{ - Q_UNUSED(job); - m_closeSignalCalled = true; - qDebug() << "+++++++++ job2 closed"; -} - //// void JobRemoteTest::slotMimetype(KIO::Job *job, const QString &type) { QVERIFY(job != nullptr); m_mimetype = type; } void JobRemoteTest::openFileRead0Bytes() { QUrl u(remoteTmpUrl()); u.setPath(u.path() + "openFileReading"); const QByteArray putData("Doesn't matter"); KIO::StoredTransferJob *putJob = KIO::storedPut(putData, u, 0600, KIO::Overwrite | KIO::HideProgressInfo ); quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago putJob->setModificationTime(mtime); putJob->setUiDelegate(nullptr); connect(putJob, &KJob::result, this, &JobRemoteTest::slotResult); m_result = -1; enterLoop(); QVERIFY(m_result == 0); // no error m_data = QByteArray(); fileJob = KIO::open(u, QIODevice::ReadOnly); fileJob->setUiDelegate(nullptr); connect(fileJob, &KJob::result, this, &JobRemoteTest::slotResult); connect(fileJob, &KIO::FileJob::data, this, &JobRemoteTest::slotFileJob3Data); connect(fileJob, &KIO::FileJob::open, this, &JobRemoteTest::slotFileJob3Open); // Can reuse this slot (it's a noop). connect(fileJob, &KIO::FileJob::written, this, &JobRemoteTest::slotFileJob2Written); connect(fileJob, &KIO::FileJob::position, this, &JobRemoteTest::slotFileJob3Position); // Can reuse this as well. connect(fileJob, QOverload::of(&KIO::FileJob::close), - this, &JobRemoteTest::slotFileJob3Close); + this, &JobRemoteTest::slotFileJobClose); m_result = -1; m_closeSignalCalled = false; enterLoop(); // Previously reading 0 bytes would cause both data() and error() being emitted... QVERIFY(m_result == 0); // no error QVERIFY(m_closeSignalCalled); // close signal called. } void JobRemoteTest::slotFileJob3Open(KIO::Job *job) { Q_UNUSED(job); fileJob->seek(0); } void JobRemoteTest::slotFileJob3Position(KIO::Job *job, KIO::filesize_t offset) { Q_UNUSED(job); qDebug() << "position : " << offset << " -> read (0)"; fileJob->read(0); } void JobRemoteTest::slotFileJob3Data(KIO::Job *job, const QByteArray& data) { Q_UNUSED(job); QVERIFY(data.isEmpty()); fileJob->close(); } -void JobRemoteTest::slotFileJob3Close(KIO::Job *job) +void JobRemoteTest::openFileTruncating() +{ + QUrl u(remoteTmpUrl()); + u.setPath(u.path() + "openFileTruncating"); + + const QByteArray putData("test1"); + + KIO::StoredTransferJob *putJob = KIO::storedPut(putData, + u, + 0600, KIO::Overwrite | KIO::HideProgressInfo + ); + + quint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch(); // Use second granularity, supported on all filesystems + QDateTime mtime = QDateTime::fromSecsSinceEpoch(secsSinceEpoch - 30); // 30 seconds ago + putJob->setModificationTime(mtime); + putJob->setUiDelegate(nullptr); + connect(putJob, &KJob::result, + this, &JobRemoteTest::slotResult); + m_result = -1; + enterLoop(); + QVERIFY(m_result == 0); // no error + + m_truncatedFile.setFileName(u.toLocalFile()); + QVERIFY(m_truncatedFile.exists()); + QVERIFY(m_truncatedFile.open(QIODevice::ReadOnly)); + fileJob = KIO::open(u, QIODevice::ReadWrite); + + fileJob->setUiDelegate(nullptr); + connect(fileJob, &KJob::result, + this, &JobRemoteTest::slotResult); + connect(fileJob, &KIO::FileJob::open, + this, &JobRemoteTest::slotFileJob4Open); + connect(fileJob, &KIO::FileJob::truncated, + this, &JobRemoteTest::slotFileJob4Truncated); + // Can reuse this slot (same for all tests). + connect(fileJob, QOverload::of(&KIO::FileJob::close), + this, &JobRemoteTest::slotFileJobClose); + m_result = -1; + m_closeSignalCalled = false; + + enterLoop(); + QVERIFY(m_result == 0); // no error + QVERIFY(m_closeSignalCalled); // close signal called. +} + +void JobRemoteTest::slotFileJob4Open(KIO::Job *job) { Q_UNUSED(job); - m_closeSignalCalled = true; - qDebug() << "+++++++++ job2 closed"; + fileJob->truncate(10); + qDebug() << "Truncating file to 10"; +} + +void JobRemoteTest::slotFileJob4Truncated(KIO::Job *job, KIO::filesize_t length) +{ + Q_UNUSED(job); + if(length == 10) { + m_truncatedFile.seek(0); + QCOMPARE(m_truncatedFile.readAll(), QByteArray("test1\x00\x00\x00\x00\x00", 10)); + fileJob->truncate(4); + qDebug() << "Truncating file to 4"; + } else if(length == 4) { + m_truncatedFile.seek(0); + QCOMPARE(m_truncatedFile.readAll(), QByteArray("test")); + fileJob->truncate(0); + qDebug() << "Truncating file to 0"; + } else { + m_truncatedFile.seek(0); + QCOMPARE(m_truncatedFile.readAll(), QByteArray()); + fileJob->close(); + qDebug() << "Truncating file finished"; + } } diff --git a/autotests/jobremotetest.h b/autotests/jobremotetest.h index 5d24d346..2c4ca811 100644 --- a/autotests/jobremotetest.h +++ b/autotests/jobremotetest.h @@ -1,99 +1,102 @@ /* This file is part of the KDE project Copyright (C) 2004 David Faure Copyright (C) 2008 Norbert Frese This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* Please set KIO_JOBREMOTETEST_REMOTETMP to test other protocols than kio_file. Don't forget the trailing slash! */ #ifndef JOBREMOTETEST_H #define JOBREMOTETEST_H #include #include #include class JobRemoteTest : public QObject { Q_OBJECT public: JobRemoteTest() {} private Q_SLOTS: void initTestCase(); void cleanupTestCase(); // Remote tests void putAndGet(); void openFileWriting(); void openFileReading(); void openFileRead0Bytes(); + void openFileTruncating(); //void calculateRemainingSeconds(); Q_SIGNALS: void exitLoop(); protected Q_SLOTS: //void slotEntries( KIO::Job*, const KIO::UDSEntryList& lst ); void slotGetResult(KJob *); void slotDataReq(KIO::Job *, QByteArray &); void slotResult(KJob *); void slotMimetype(KIO::Job *, const QString &); void slotFileJobData(KIO::Job *job, const QByteArray &data); void slotFileJobRedirection(KIO::Job *job, const QUrl &url); void slotFileJobMimetype(KIO::Job *job, const QString &type); void slotFileJobOpen(KIO::Job *job); void slotFileJobWritten(KIO::Job *job, KIO::filesize_t written); void slotFileJobPosition(KIO::Job *job, KIO::filesize_t offset); void slotFileJobClose(KIO::Job *job); void slotFileJob2Data(KIO::Job *job, const QByteArray &data); void slotFileJob2Redirection(KIO::Job *job, const QUrl &url); void slotFileJob2Mimetype(KIO::Job *job, const QString &type); void slotFileJob2Open(KIO::Job *job); void slotFileJob2Written(KIO::Job *job, KIO::filesize_t written); void slotFileJob2Position(KIO::Job *job, KIO::filesize_t offset); - void slotFileJob2Close(KIO::Job *job); void slotFileJob3Open(KIO::Job *job); void slotFileJob3Position(KIO::Job *job, KIO::filesize_t offset); void slotFileJob3Data(KIO::Job *job, const QByteArray &data); - void slotFileJob3Close(KIO::Job *job); + + void slotFileJob4Open(KIO::Job *job); + void slotFileJob4Truncated(KIO::Job *job, KIO::filesize_t length); private: void enterLoop(); enum { AlreadyExists = 1 }; int m_result; bool m_closeSignalCalled; + QFile m_truncatedFile; QByteArray m_data; QStringList m_names; int m_dataReqCount; QString m_mimetype; // openReadWrite test KIO::FileJob *fileJob; int m_rwCount; }; #endif diff --git a/src/core/commands_p.h b/src/core/commands_p.h index a8ddeba2..c365cdf4 100644 --- a/src/core/commands_p.h +++ b/src/core/commands_p.h @@ -1,75 +1,76 @@ /* This file is part of the KDE libraries Copyright (C) 2000-2013 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KIO_COMMANDS_P_H #define KIO_COMMANDS_P_H #include "kiocore_export.h" namespace KIO { /** * @internal * Commands that can be invoked by a job. */ enum Command { CMD_HOST = '0', // 48 CMD_CONNECT = '1', // 49 CMD_DISCONNECT = '2', // 50 CMD_SLAVE_STATUS = '3', // 51 CMD_SLAVE_CONNECT = '4', // 52 CMD_SLAVE_HOLD = '5', // 53 CMD_NONE = 'A', // 65 CMD_TESTDIR = 'B', // 66 CMD_GET = 'C', // 67 CMD_PUT = 'D', // 68 CMD_STAT = 'E', // 69 CMD_MIMETYPE = 'F', // 70 CMD_LISTDIR = 'G', // 71 CMD_MKDIR = 'H', // 72 CMD_RENAME = 'I', // 73 CMD_COPY = 'J', // 74 CMD_DEL = 'K', // 75 CMD_CHMOD = 'L', // 76 CMD_SPECIAL = 'M', // 77 CMD_SETMODIFICATIONTIME = 'N', // 78 CMD_REPARSECONFIGURATION = 'O', // 79 CMD_META_DATA = 'P', // 80 CMD_SYMLINK = 'Q', // 81 CMD_SUBURL = 'R', // 82 Inform the slave about the url it is streaming on. CMD_MESSAGEBOXANSWER = 'S', // 83 CMD_RESUMEANSWER = 'T', // 84 CMD_CONFIG = 'U', // 85 CMD_MULTI_GET = 'V', // 86 CMD_SETLINKDEST = 'W', // 87 CMD_OPEN = 'X', // 88 CMD_CHOWN = 'Y', // 89 CMD_READ = 'Z', // 90 CMD_WRITE = 91, CMD_SEEK = 92, CMD_CLOSE = 93, CMD_HOST_INFO = 94, - CMD_FILESYSTEMFREESPACE = 95 + CMD_FILESYSTEMFREESPACE = 95, + CMD_TRUNCATE = 96 // Add new ones here once a release is done, to avoid breaking binary compatibility. // Note that protocol-specific commands shouldn't be added here, but should use special. }; } // namespace #endif diff --git a/src/core/filejob.cpp b/src/core/filejob.cpp index b02ad33e..7021406c 100644 --- a/src/core/filejob.cpp +++ b/src/core/filejob.cpp @@ -1,229 +1,250 @@ /* * This file is part of the KDE libraries * Copyright (c) 2006 Allan Sandfeld Jensen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License version 2 as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * **/ #include "filejob.h" #include "slavebase.h" #include "scheduler.h" #include "slave.h" #include "job_p.h" class KIO::FileJobPrivate: public KIO::SimpleJobPrivate { public: FileJobPrivate(const QUrl &url, const QByteArray &packedArgs) : SimpleJobPrivate(url, CMD_OPEN, packedArgs), m_open(false), m_size(0) {} bool m_open; QString m_mimetype; KIO::filesize_t m_size; void slotRedirection(const QUrl &url); void slotData(const QByteArray &data); void slotMimetype(const QString &mimetype); void slotOpen(); void slotWritten(KIO::filesize_t); void slotFinished(); void slotPosition(KIO::filesize_t); + void slotTruncated(KIO::filesize_t); void slotTotalSize(KIO::filesize_t); /** * @internal * Called by the scheduler when a @p slave gets to * work on this job. * @param slave the slave that starts working on this job */ void start(Slave *slave) override; Q_DECLARE_PUBLIC(FileJob) static inline FileJob *newJob(const QUrl &url, const QByteArray &packedArgs) { FileJob *job = new FileJob(*new FileJobPrivate(url, packedArgs)); job->setUiDelegate(KIO::createDefaultJobUiDelegate()); return job; } }; using namespace KIO; FileJob::FileJob(FileJobPrivate &dd) : SimpleJob(dd) { } FileJob::~FileJob() { } void FileJob::read(KIO::filesize_t size) { Q_D(FileJob); if (!d->m_open) { return; } KIO_ARGS << size; d->m_slave->send(CMD_READ, packedArgs); } void FileJob::write(const QByteArray &_data) { Q_D(FileJob); if (!d->m_open) { return; } d->m_slave->send(CMD_WRITE, _data); } void FileJob::seek(KIO::filesize_t offset) { Q_D(FileJob); if (!d->m_open) { return; } KIO_ARGS << KIO::filesize_t(offset); d->m_slave->send(CMD_SEEK, packedArgs); } +void FileJob::truncate(KIO::filesize_t length) +{ + Q_D(FileJob); + if (!d->m_open) { + return; + } + + KIO_ARGS << KIO::filesize_t(length); + d->m_slave->send(CMD_TRUNCATE, packedArgs); +} + void FileJob::close() { Q_D(FileJob); if (!d->m_open) { return; } d->m_slave->send(CMD_CLOSE); // ### close? } KIO::filesize_t FileJob::size() { Q_D(FileJob); if (!d->m_open) { return 0; } return d->m_size; } // Slave sends data void FileJobPrivate::slotData(const QByteArray &_data) { Q_Q(FileJob); emit q_func()->data(q, _data); } void FileJobPrivate::slotRedirection(const QUrl &url) { Q_Q(FileJob); //qDebug() << url; emit q->redirection(q, url); } void FileJobPrivate::slotMimetype(const QString &type) { Q_Q(FileJob); m_mimetype = type; emit q->mimetype(q, m_mimetype); } void FileJobPrivate::slotPosition(KIO::filesize_t pos) { Q_Q(FileJob); emit q->position(q, pos); } +void FileJobPrivate::slotTruncated(KIO::filesize_t length) +{ + Q_Q(FileJob); + emit q->truncated(q, length); +} + void FileJobPrivate::slotTotalSize(KIO::filesize_t t_size) { m_size = t_size; Q_Q(FileJob); q->setTotalAmount(KJob::Bytes, m_size); } void FileJobPrivate::slotOpen() { Q_Q(FileJob); m_open = true; emit q->open(q); } void FileJobPrivate::slotWritten(KIO::filesize_t t_written) { Q_Q(FileJob); emit q->written(q, t_written); } void FileJobPrivate::slotFinished() { Q_Q(FileJob); //qDebug() << this << m_url; m_open = false; emit q->close(q); // Return slave to the scheduler slaveDone(); // Scheduler::doJob(this); q->emitResult(); } void FileJobPrivate::start(Slave *slave) { Q_Q(FileJob); q->connect(slave, SIGNAL(data(QByteArray)), SLOT(slotData(QByteArray))); q->connect(slave, SIGNAL(redirection(QUrl)), SLOT(slotRedirection(QUrl))); q->connect(slave, SIGNAL(mimeType(QString)), SLOT(slotMimetype(QString))); q->connect(slave, SIGNAL(open()), SLOT(slotOpen())); q->connect(slave, SIGNAL(finished()), SLOT(slotFinished())); q->connect(slave, SIGNAL(position(KIO::filesize_t)), SLOT(slotPosition(KIO::filesize_t))); + q->connect(slave, SIGNAL(truncated(KIO::filesize_t)), + SLOT(slotTruncated(KIO::filesize_t))); + q->connect(slave, SIGNAL(written(KIO::filesize_t)), SLOT(slotWritten(KIO::filesize_t))); q->connect(slave, SIGNAL(totalSize(KIO::filesize_t)), SLOT(slotTotalSize(KIO::filesize_t))); SimpleJobPrivate::start(slave); } FileJob *KIO::open(const QUrl &url, QIODevice::OpenMode mode) { // Send decoded path and encoded query KIO_ARGS << url << mode; return FileJobPrivate::newJob(url, packedArgs); } #include "moc_filejob.cpp" diff --git a/src/core/filejob.h b/src/core/filejob.h index 01418199..73e2a0f4 100644 --- a/src/core/filejob.h +++ b/src/core/filejob.h @@ -1,197 +1,219 @@ /* * This file is part of the KDE libraries * Copyright (c) 2006 Allan Sandfeld Jensen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License version 2 as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * **/ #ifndef KIO_FILEJOB_H #define KIO_FILEJOB_H #include "kiocore_export.h" #include "simplejob.h" namespace KIO { class FileJobPrivate; /** * @class KIO::FileJob filejob.h * * The file-job is an asynchronous version of normal file handling. - * It allows block-wise reading and writing, and allows seeking. Results are returned through signals. + * It allows block-wise reading and writing, and allows seeking and truncation. Results are returned through signals. * * Should always be created using KIO::open(QUrl) */ class KIOCORE_EXPORT FileJob : public SimpleJob { Q_OBJECT public: ~FileJob(); /** * This function attempts to read up to \p size bytes from the URL passed to * KIO::open() and returns the bytes received via the data() signal. * * The read operation commences at the current file offset, and the file * offset is incremented by the number of bytes read, but this change in the * offset does not result in the position() signal being emitted. * * If the current file offset is at or past the end of file (i.e. EOD), no * bytes are read, and the data() signal returns an empty QByteArray. * * On error the data() signal is not emitted. To catch errors please connect * to the result() signal. * * @param size the requested amount of data to read * */ void read(KIO::filesize_t size); /** * This function attempts to write all the bytes in \p data to the URL * passed to KIO::open() and returns the bytes written received via the * written() signal. * * The write operation commences at the current file offset, and the file * offset is incremented by the number of bytes read, but this change in the * offset does not result in the position() being emitted. * * On error the written() signal is not emitted. To catch errors please * connect to the result() signal. * * @param data the data to write */ void write(const QByteArray &data); /** * Closes the file-slave * * The slave emits close() and result(). */ void close(); /** * Seek * * The slave emits position() on successful seek to the specified \p offset. * * On error the position() signal is not emitted. To catch errors please * connect to the result() signal. * * @param offset the position from start to go to */ void seek(KIO::filesize_t offset); + /** + * Truncate + * + * The slave emits truncated() on successful truncation to the specified \p length. + * + * On error the truncated() signal is not emitted. To catch errors please + * connect to the result() signal. + * + * @param length the desired length to truncate to + * @since 5.66 + */ + void truncate(KIO::filesize_t length); + /** * Size * * @return the file size */ KIO::filesize_t size(); Q_SIGNALS: /** * Data from the slave has arrived. Emitted after read(). * * Unless a read() request was sent for 0 bytes, End of data (EOD) has been * reached if data.size() == 0 * * @param job the job that emitted this signal * @param data data received from the slave. * */ void data(KIO::Job *job, const QByteArray &data); /** * Signals the file is a redirection. * Follow this url manually to reach data * @param job the job that emitted this signal * @param url the new URL */ void redirection(KIO::Job *job, const QUrl &url); /** * Mimetype determined. * @param job the job that emitted this signal * @param type the mime type */ void mimetype(KIO::Job *job, const QString &type); /** * File is open, metadata has been determined and the * file-slave is ready to receive commands. * @param job the job that emitted this signal */ void open(KIO::Job *job); /** * \p written bytes were written to the file. Emitted after write(). * @param job the job that emitted this signal * @param written bytes written. */ void written(KIO::Job *job, KIO::filesize_t written); /** * File is closed and will accept no more commands * @param job the job that emitted this signal */ void close(KIO::Job *job); /** * The file has reached this position. Emitted after seek(). * @param job the job that emitted this signal * @param offset the new position */ void position(KIO::Job *job, KIO::filesize_t offset); + /** + * The file has been truncated to this point. Emitted after truncate(). + * @param job the job that emitted this signal + * @param length the new length of the file + * @since 5.66 + */ + void truncated(KIO::Job *job, KIO::filesize_t length); + protected: FileJob(FileJobPrivate &dd); private: Q_PRIVATE_SLOT(d_func(), void slotRedirection(const QUrl &)) Q_PRIVATE_SLOT(d_func(), void slotData(const QByteArray &data)) Q_PRIVATE_SLOT(d_func(), void slotMimetype(const QString &mimetype)) Q_PRIVATE_SLOT(d_func(), void slotOpen()) Q_PRIVATE_SLOT(d_func(), void slotWritten(KIO::filesize_t)) Q_PRIVATE_SLOT(d_func(), void slotFinished()) Q_PRIVATE_SLOT(d_func(), void slotPosition(KIO::filesize_t)) + Q_PRIVATE_SLOT(d_func(), void slotTruncated(KIO::filesize_t)) Q_PRIVATE_SLOT(d_func(), void slotTotalSize(KIO::filesize_t)) Q_DECLARE_PRIVATE(FileJob) }; /** * Open ( random access I/O ) * * The file-job emits open() when opened * * On error the open() signal is not emitted. To catch errors please * connect to the result() signal. * * @param url the URL of the file * @param mode the access privileges: see \ref OpenMode * * @return The file-handling job. It will never return 0. Errors are handled asynchronously * (emitted as signals). */ KIOCORE_EXPORT FileJob *open(const QUrl &url, QIODevice::OpenMode mode); } // namespace #endif diff --git a/src/core/global.h b/src/core/global.h index cb79d00d..5660a067 100644 --- a/src/core/global.h +++ b/src/core/global.h @@ -1,382 +1,383 @@ /* This file is part of the KDE libraries Copyright (C) 2000-2005 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KIO_GLOBAL_H #define KIO_GLOBAL_H #include "kiocore_export.h" #include #include // for QFile::Permissions #include #include "metadata.h" // for source compat #include "jobtracker.h" // for source compat class QUrl; class QTime; #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) // on windows ssize_t is not defined, only SSIZE_T exists #include typedef SSIZE_T ssize_t; #endif /** * @short A namespace for KIO globals * */ namespace KIO { /// 64-bit file offset typedef qlonglong fileoffset_t; /// 64-bit file size typedef qulonglong filesize_t; /** * Converts @p size from bytes to the string representation. * * @param size size in bytes * @return converted size as a string - e.g. 123.4 KiB , 12.0 MiB */ KIOCORE_EXPORT QString convertSize(KIO::filesize_t size); /** * Converts a size to a string representation * Not unlike QString::number(...) * * @param size size in bytes * @return converted size as a string - e.g. 123456789 */ KIOCORE_EXPORT QString number(KIO::filesize_t size); /** * Converts size from kibi-bytes (2^10) to the string representation. * * @param kibSize size in kibi-bytes (2^10) * @return converted size as a string - e.g. 123.4 KiB , 12.0 MiB */ KIOCORE_EXPORT QString convertSizeFromKiB(KIO::filesize_t kibSize); /** * Calculates remaining time in seconds from total size, processed size and speed. * * @param totalSize total size in bytes * @param processedSize processed size in bytes * @param speed speed in bytes per second * @return calculated remaining time in seconds */ KIOCORE_EXPORT unsigned int calculateRemainingSeconds(KIO::filesize_t totalSize, KIO::filesize_t processedSize, KIO::filesize_t speed); /** * Convert @p seconds to a string representing number of days, hours, minutes and seconds * * @param seconds number of seconds to convert * @return string representation in a locale depending format */ KIOCORE_EXPORT QString convertSeconds(unsigned int seconds); #if KIOCORE_ENABLE_DEPRECATED_SINCE(3, 4) /** * Calculates remaining time from total size, processed size and speed. * * @param totalSize total size in bytes * @param processedSize processed size in bytes * @param speed speed in bytes per second * @return calculated remaining time * @deprecated Since 3.4, use calculateRemainingSeconds() instead, as QTime is limited to 23:59:59 */ KIOCORE_DEPRECATED_VERSION(3, 4, "Use KIO::calculateRemainingSeconds(KIO::filesize_t, KIO::filesize_t, KIO::filesize_t") KIOCORE_EXPORT QTime calculateRemaining(KIO::filesize_t totalSize, KIO::filesize_t processedSize, KIO::filesize_t speed); #endif /** * Helper for showing information about a set of files and directories * @param items the number of items (= @p files + @p dirs + number of symlinks :) * @param files the number of files * @param dirs the number of dirs * @param size the sum of the size of the @p files * @param showSize whether to show the size in the result * @return the summary string */ KIOCORE_EXPORT QString itemsSummaryString(uint items, uint files, uint dirs, KIO::filesize_t size, bool showSize); /** * Encodes (from the text displayed to the real filename) * This translates '/' into a "unicode fraction slash", QChar(0x2044). * Used by KIO::link, for instance. * @param str the file name to encode * @return the encoded file name */ KIOCORE_EXPORT QString encodeFileName(const QString &str); /** * Decodes (from the filename to the text displayed) * This doesn't do anything anymore, it used to do the opposite of encodeFileName * when encodeFileName was using %2F for '/'. * @param str the file name to decode * @return the decoded file name */ KIOCORE_EXPORT QString decodeFileName(const QString &str); #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 61) /** * Given a directory path and a filename (which usually exists already), * this function returns a suggested name for a file that doesn't exist * in that directory. The existence is only checked for local urls though. * The suggested file name is of the form "foo 1", "foo 2" etc. * @since 5.0 * @deprecated since 5.61, use KFileUtils::suggestName() from KCoreAddons */ KIOCORE_DEPRECATED_VERSION(5, 61, "Use KFileUtils::suggestName(const QUrl &, const QString &) from KCoreAddons") KIOCORE_EXPORT QString suggestName(const QUrl &baseURL, const QString &oldName); #endif /** * Error codes that can be emitted by KIO. */ enum Error { ERR_CANNOT_OPEN_FOR_READING = KJob::UserDefinedError + 1, ERR_CANNOT_OPEN_FOR_WRITING = KJob::UserDefinedError + 2, ERR_CANNOT_LAUNCH_PROCESS = KJob::UserDefinedError + 3, ERR_INTERNAL = KJob::UserDefinedError + 4, ERR_MALFORMED_URL = KJob::UserDefinedError + 5, ERR_UNSUPPORTED_PROTOCOL = KJob::UserDefinedError + 6, ERR_NO_SOURCE_PROTOCOL = KJob::UserDefinedError + 7, ERR_UNSUPPORTED_ACTION = KJob::UserDefinedError + 8, ERR_IS_DIRECTORY = KJob::UserDefinedError + 9, ///< ... where a file was expected ERR_IS_FILE = KJob::UserDefinedError + 10, ///< ... where a directory was expected (e.g. listing) ERR_DOES_NOT_EXIST = KJob::UserDefinedError + 11, ERR_FILE_ALREADY_EXIST = KJob::UserDefinedError + 12, ERR_DIR_ALREADY_EXIST = KJob::UserDefinedError + 13, ERR_UNKNOWN_HOST = KJob::UserDefinedError + 14, ERR_ACCESS_DENIED = KJob::UserDefinedError + 15, ERR_WRITE_ACCESS_DENIED = KJob::UserDefinedError + 16, ERR_CANNOT_ENTER_DIRECTORY = KJob::UserDefinedError + 17, ERR_PROTOCOL_IS_NOT_A_FILESYSTEM = KJob::UserDefinedError + 18, ERR_CYCLIC_LINK = KJob::UserDefinedError + 19, ERR_USER_CANCELED = KJob::KilledJobError, ERR_CYCLIC_COPY = KJob::UserDefinedError + 21, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_CREATE_SOCKET = KJob::UserDefinedError + 22, ///< @deprecated Since 5.0, use ERR_CANNOT_CREATE_SOCKET #endif ERR_CANNOT_CREATE_SOCKET = KJob::UserDefinedError + 22, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_CONNECT = KJob::UserDefinedError + 23, ///< @deprecated Since 5.0, use ERR_CANNOT_CONNECT #endif ERR_CANNOT_CONNECT = KJob::UserDefinedError + 23, ERR_CONNECTION_BROKEN = KJob::UserDefinedError + 24, ERR_NOT_FILTER_PROTOCOL = KJob::UserDefinedError + 25, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_MOUNT = KJob::UserDefinedError + 26, ///< @deprecated Since 5.0, use ERR_CANNOT_MOUNT #endif ERR_CANNOT_MOUNT = KJob::UserDefinedError + 26, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_UNMOUNT = KJob::UserDefinedError + 27, ///< @deprecated Since 5.0, use ERR_CANNOT_UNMOUNT #endif ERR_CANNOT_UNMOUNT = KJob::UserDefinedError + 27, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_READ = KJob::UserDefinedError + 28, ///< @deprecated Since 5.0, use ERR_CANNOT_READ #endif ERR_CANNOT_READ = KJob::UserDefinedError + 28, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_WRITE = KJob::UserDefinedError + 29, ///< @deprecated Since 5.0, use ERR_CANNOT_WRITE #endif ERR_CANNOT_WRITE = KJob::UserDefinedError + 29, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_BIND = KJob::UserDefinedError + 30, ///< @deprecated Since 5.0, use ERR_CANNOT_BIND #endif ERR_CANNOT_BIND = KJob::UserDefinedError + 30, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_LISTEN = KJob::UserDefinedError + 31, ///< @deprecated Since 5.0, use ERR_CANNOT_LISTEN #endif ERR_CANNOT_LISTEN = KJob::UserDefinedError + 31, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_ACCEPT = KJob::UserDefinedError + 32, ///< @deprecated Since 5.0, use ERR_CANNOT_ACCEPT #endif ERR_CANNOT_ACCEPT = KJob::UserDefinedError + 32, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_LOGIN = KJob::UserDefinedError + 33, ///< @deprecated Since 5.0, use ERR_CANNOT_LOGIN #endif ERR_CANNOT_LOGIN = KJob::UserDefinedError + 33, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_STAT = KJob::UserDefinedError + 34, ///< @deprecated Since 5.0, use ERR_CANNOT_STAT #endif ERR_CANNOT_STAT = KJob::UserDefinedError + 34, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_CLOSEDIR = KJob::UserDefinedError + 35, ///< @deprecated Since 5.0, use ERR_CANNOT_CLOSEDIR #endif ERR_CANNOT_CLOSEDIR = KJob::UserDefinedError + 35, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_MKDIR = KJob::UserDefinedError + 37, ///< @deprecated Since 5.0, use ERR_CANNOT_MKDIR #endif ERR_CANNOT_MKDIR = KJob::UserDefinedError + 37, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_RMDIR = KJob::UserDefinedError + 38, ///< @deprecated Since 5.0, use ERR_CANNOT_RMDIR #endif ERR_CANNOT_RMDIR = KJob::UserDefinedError + 38, ERR_CANNOT_RESUME = KJob::UserDefinedError + 39, ERR_CANNOT_RENAME = KJob::UserDefinedError + 40, ERR_CANNOT_CHMOD = KJob::UserDefinedError + 41, ERR_CANNOT_DELETE = KJob::UserDefinedError + 42, // The text argument is the protocol that the dead slave supported. // This means for example: file, ftp, http, ... ERR_SLAVE_DIED = KJob::UserDefinedError + 43, ERR_OUT_OF_MEMORY = KJob::UserDefinedError + 44, ERR_UNKNOWN_PROXY_HOST = KJob::UserDefinedError + 45, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) ERR_COULD_NOT_AUTHENTICATE = KJob::UserDefinedError + 46, ///< @deprecated Since 5.0, use ERR_CANNOT_AUTHENTICATE #endif ERR_CANNOT_AUTHENTICATE = KJob::UserDefinedError + 46, ERR_ABORTED = KJob::UserDefinedError + 47, ///< Action got aborted from application side ERR_INTERNAL_SERVER = KJob::UserDefinedError + 48, ERR_SERVER_TIMEOUT = KJob::UserDefinedError + 49, ERR_SERVICE_NOT_AVAILABLE = KJob::UserDefinedError + 50, ERR_UNKNOWN = KJob::UserDefinedError + 51, // (was a warning) ERR_CHECKSUM_MISMATCH = 52, ERR_UNKNOWN_INTERRUPT = KJob::UserDefinedError + 53, ERR_CANNOT_DELETE_ORIGINAL = KJob::UserDefinedError + 54, ERR_CANNOT_DELETE_PARTIAL = KJob::UserDefinedError + 55, ERR_CANNOT_RENAME_ORIGINAL = KJob::UserDefinedError + 56, ERR_CANNOT_RENAME_PARTIAL = KJob::UserDefinedError + 57, ERR_NEED_PASSWD = KJob::UserDefinedError + 58, ERR_CANNOT_SYMLINK = KJob::UserDefinedError + 59, ERR_NO_CONTENT = KJob::UserDefinedError + 60, ///< Action succeeded but no content will follow. ERR_DISK_FULL = KJob::UserDefinedError + 61, ERR_IDENTICAL_FILES = KJob::UserDefinedError + 62, ///< src==dest when moving/copying ERR_SLAVE_DEFINED = KJob::UserDefinedError + 63, ///< for slave specified errors that can be ///< rich text. Email links will be handled ///< by the standard email app and all hrefs ///< will be handled by the standard browser. ///< Copyright (C) 2003 Waldo Bastian Copyright 2012 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kprotocolinfo.h" #include "kprotocolinfo_p.h" #include "kprotocolinfofactory_p.h" #include #include #include #include #include // // Internal functions: // KProtocolInfoPrivate::KProtocolInfoPrivate(const QString &path) { KConfig sconfig(path, KConfig::SimpleConfig); KConfigGroup config(&sconfig, "Protocol"); m_name = config.readEntry("protocol"); m_exec = config.readPathEntry("exec", QString()); m_isSourceProtocol = config.readEntry("source", true); m_isHelperProtocol = config.readEntry("helper", false); m_supportsReading = config.readEntry("reading", false); m_supportsWriting = config.readEntry("writing", false); m_supportsMakeDir = config.readEntry("makedir", false); m_supportsDeleting = config.readEntry("deleting", false); m_supportsLinking = config.readEntry("linking", false); m_supportsMoving = config.readEntry("moving", false); m_supportsOpening = config.readEntry("opening", false); + m_supportsTruncating = config.readEntry("truncating", false); m_canCopyFromFile = config.readEntry("copyFromFile", false); m_canCopyToFile = config.readEntry("copyToFile", false); m_canRenameFromFile = config.readEntry("renameFromFile", false); m_canRenameToFile = config.readEntry("renameToFile", false); m_canDeleteRecursive = config.readEntry("deleteRecursive", false); const QString fnu = config.readEntry("fileNameUsedForCopying", "FromURL"); m_fileNameUsedForCopying = KProtocolInfo::FromUrl; if (fnu == QLatin1String("Name")) { m_fileNameUsedForCopying = KProtocolInfo::Name; } else if (fnu == QLatin1String("DisplayName")) { m_fileNameUsedForCopying = KProtocolInfo::DisplayName; } m_listing = config.readEntry("listing", QStringList()); // Many .protocol files say "Listing=false" when they really mean "Listing=" (i.e. unsupported) if (m_listing.count() == 1 && m_listing.first() == QLatin1String("false")) { m_listing.clear(); } m_supportsListing = (m_listing.count() > 0); m_defaultMimetype = config.readEntry("defaultMimetype"); m_determineMimetypeFromExtension = config.readEntry("determineMimetypeFromExtension", true); m_archiveMimeTypes = config.readEntry("archiveMimetype", QStringList()); m_icon = config.readEntry("Icon"); m_config = config.readEntry("config", m_name); m_maxSlaves = config.readEntry("maxInstances", 1); m_maxSlavesPerHost = config.readEntry("maxInstancesPerHost", 0); QString tmp = config.readEntry("input"); if (tmp == QLatin1String("filesystem")) { m_inputType = KProtocolInfo::T_FILESYSTEM; } else if (tmp == QLatin1String("stream")) { m_inputType = KProtocolInfo::T_STREAM; } else { m_inputType = KProtocolInfo::T_NONE; } tmp = config.readEntry("output"); if (tmp == QLatin1String("filesystem")) { m_outputType = KProtocolInfo::T_FILESYSTEM; } else if (tmp == QLatin1String("stream")) { m_outputType = KProtocolInfo::T_STREAM; } else { m_outputType = KProtocolInfo::T_NONE; } m_docPath = config.readPathEntry("X-DocPath", QString()); if (m_docPath.isEmpty()) { m_docPath = config.readPathEntry("DocPath", QString()); } m_protClass = config.readEntry("Class").toLower(); if (!m_protClass.startsWith(QLatin1Char(':'))) { m_protClass.prepend(QLatin1Char(':')); } const QStringList extraNames = config.readEntry("ExtraNames", QStringList()); const QStringList extraTypes = config.readEntry("ExtraTypes", QStringList()); QStringList::const_iterator it = extraNames.begin(); QStringList::const_iterator typeit = extraTypes.begin(); for (; it != extraNames.end() && typeit != extraTypes.end(); ++it, ++typeit) { QVariant::Type type = QVariant::nameToType((*typeit).toLatin1().constData()); // currently QVariant::Type and ExtraField::Type use the same subset of values, so we can just cast. m_extraFields.append(KProtocolInfo::ExtraField(*it, static_cast(type))); } m_showPreviews = config.readEntry("ShowPreviews", m_protClass == QLatin1String(":local")); m_capabilities = config.readEntry("Capabilities", QStringList()); m_slaveHandlesNotify = config.readEntry("slaveHandlesNotify", QStringList()); m_proxyProtocol = config.readEntry("ProxiedBy"); } KProtocolInfoPrivate::KProtocolInfoPrivate(const QString &name, const QString &exec, const QJsonObject &json) : m_name(name) , m_exec(exec) { // source has fallback true if not set m_isSourceProtocol = json.value(QStringLiteral("source")).toBool(true); // other bools are fine with default false by toBool m_isHelperProtocol = json.value(QStringLiteral("helper")).toBool(); m_supportsReading = json.value(QStringLiteral("reading")).toBool(); m_supportsWriting = json.value(QStringLiteral("writing")).toBool(); m_supportsMakeDir = json.value(QStringLiteral("makedir")).toBool(); m_supportsDeleting = json.value(QStringLiteral("deleting")).toBool(); m_supportsLinking = json.value(QStringLiteral("linking")).toBool(); m_supportsMoving = json.value(QStringLiteral("moving")).toBool(); m_supportsOpening = json.value(QStringLiteral("opening")).toBool(); + m_supportsTruncating = json.value(QStringLiteral("truncating")).toBool(); m_canCopyFromFile = json.value(QStringLiteral("copyFromFile")).toBool(); m_canCopyToFile = json.value(QStringLiteral("copyToFile")).toBool(); m_canRenameFromFile = json.value(QStringLiteral("renameFromFile")).toBool(); m_canRenameToFile = json.value(QStringLiteral("renameToFile")).toBool(); m_canDeleteRecursive = json.value(QStringLiteral("deleteRecursive")).toBool(); // default is "FromURL" const QString fnu = json.value(QStringLiteral("fileNameUsedForCopying")).toString(); m_fileNameUsedForCopying = KProtocolInfo::FromUrl; if (fnu == QLatin1String("Name")) { m_fileNameUsedForCopying = KProtocolInfo::Name; } else if (fnu == QLatin1String("DisplayName")) { m_fileNameUsedForCopying = KProtocolInfo::DisplayName; } m_listing = json.value(QStringLiteral("listing")).toVariant().toStringList(); // Many .protocol files say "Listing=false" when they really mean "Listing=" (i.e. unsupported) if (m_listing.count() == 1 && m_listing.first() == QLatin1String("false")) { m_listing.clear(); } m_supportsListing = (m_listing.count() > 0); m_defaultMimetype = json.value(QStringLiteral("defaultMimetype")).toString(); // determineMimetypeFromExtension has fallback true if not set m_determineMimetypeFromExtension = json.value(QStringLiteral("determineMimetypeFromExtension")).toBool(true); m_archiveMimeTypes = json.value(QStringLiteral("archiveMimetype")).toVariant().toStringList(); m_icon = json.value(QStringLiteral("Icon")).toString(); // config has fallback to name if not set m_config = json.value(QStringLiteral("config")).toString(m_name); // max slaves has fallback to 1 if not set m_maxSlaves = json.value(QStringLiteral("maxInstances")).toInt(1); m_maxSlavesPerHost = json.value(QStringLiteral("maxInstancesPerHost")).toInt(); QString tmp = json.value(QStringLiteral("input")).toString(); if (tmp == QLatin1String("filesystem")) { m_inputType = KProtocolInfo::T_FILESYSTEM; } else if (tmp == QLatin1String("stream")) { m_inputType = KProtocolInfo::T_STREAM; } else { m_inputType = KProtocolInfo::T_NONE; } tmp = json.value(QStringLiteral("output")).toString(); if (tmp == QLatin1String("filesystem")) { m_outputType = KProtocolInfo::T_FILESYSTEM; } else if (tmp == QLatin1String("stream")) { m_outputType = KProtocolInfo::T_STREAM; } else { m_outputType = KProtocolInfo::T_NONE; } m_docPath = json.value(QStringLiteral("X-DocPath")).toString(); if (m_docPath.isEmpty()) { m_docPath = json.value(QStringLiteral("DocPath")).toString(); } m_protClass = json.value(QStringLiteral("Class")).toString().toLower(); if (m_protClass[0] != QLatin1Char(':')) { m_protClass.prepend(QLatin1Char(':')); } // ExtraNames is a translated value, use the KCoreAddons helper to read it const QStringList extraNames = KPluginMetaData::readTranslatedValue(json, QStringLiteral("ExtraNames")).toVariant().toStringList(); const QStringList extraTypes = json.value(QStringLiteral("ExtraTypes")).toVariant().toStringList(); QStringList::const_iterator it = extraNames.begin(); QStringList::const_iterator typeit = extraTypes.begin(); for (; it != extraNames.end() && typeit != extraTypes.end(); ++it, ++typeit) { QVariant::Type type = QVariant::nameToType((*typeit).toLatin1().constData()); // currently QVariant::Type and ExtraField::Type use the same subset of values, so we can just cast. m_extraFields.append(KProtocolInfo::ExtraField(*it, static_cast(type))); } // fallback based on class m_showPreviews = json.value(QStringLiteral("ShowPreviews")).toBool(m_protClass == QLatin1String(":local")); m_capabilities = json.value(QStringLiteral("Capabilities")).toVariant().toStringList(); m_slaveHandlesNotify = json.value(QStringLiteral("slaveHandlesNotify")).toVariant().toStringList(); m_proxyProtocol = json.value(QStringLiteral("ProxiedBy")).toString(); } // // Static functions: // QStringList KProtocolInfo::protocols() { return KProtocolInfoFactory::self()->protocols(); } bool KProtocolInfo::isFilterProtocol(const QString &_protocol) { // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings. KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return false; } return !prot->m_isSourceProtocol; } QString KProtocolInfo::icon(const QString &_protocol) { // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings. KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return QString(); } return prot->m_icon; } QString KProtocolInfo::config(const QString &_protocol) { // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings. KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return QString(); } return QStringLiteral("kio_%1rc").arg(prot->m_config); } int KProtocolInfo::maxSlaves(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return 1; } return prot->m_maxSlaves; } int KProtocolInfo::maxSlavesPerHost(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return 0; } return prot->m_maxSlavesPerHost; } bool KProtocolInfo::determineMimetypeFromExtension(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return true; } return prot->m_determineMimetypeFromExtension; } QString KProtocolInfo::exec(const QString &protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(protocol); if (!prot) { return QString(); } return prot->m_exec; } KProtocolInfo::ExtraFieldList KProtocolInfo::extraFields(const QUrl &url) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(url.scheme()); if (!prot) { return ExtraFieldList(); } return prot->m_extraFields; } QString KProtocolInfo::defaultMimetype(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return QString(); } return prot->m_defaultMimetype; } QString KProtocolInfo::docPath(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return QString(); } return prot->m_docPath; } QString KProtocolInfo::protocolClass(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return QString(); } return prot->m_protClass; } bool KProtocolInfo::showFilePreview(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); const bool defaultSetting = prot ? prot->m_showPreviews : false; KConfigGroup group(KSharedConfig::openConfig(), "PreviewSettings"); return group.readEntry(_protocol, defaultSetting); } QStringList KProtocolInfo::capabilities(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return QStringList(); } return prot->m_capabilities; } QStringList KProtocolInfo::archiveMimetypes(const QString &protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(protocol); if (!prot) { return QStringList(); } return prot->m_archiveMimeTypes; } QStringList KProtocolInfo::slaveHandlesNotify(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return QStringList(); } return prot->m_slaveHandlesNotify; } QString KProtocolInfo::proxiedBy(const QString &_protocol) { KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol); if (!prot) { return QString(); } return prot->m_proxyProtocol; } bool KProtocolInfo::isFilterProtocol(const QUrl &url) { return isFilterProtocol(url.scheme()); } bool KProtocolInfo::isHelperProtocol(const QUrl &url) { return isHelperProtocol(url.scheme()); } bool KProtocolInfo::isHelperProtocol(const QString &protocol) { // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings. KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(protocol); if (prot) { return prot->m_isHelperProtocol; } return false; } bool KProtocolInfo::isKnownProtocol(const QUrl &url) { return isKnownProtocol(url.scheme()); } bool KProtocolInfo::isKnownProtocol(const QString &protocol) { // We call the findProtocol (const QString&) to bypass any proxy settings. KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(protocol); return prot; } diff --git a/src/core/kprotocolinfo_p.h b/src/core/kprotocolinfo_p.h index c9923136..a9b15d71 100644 --- a/src/core/kprotocolinfo_p.h +++ b/src/core/kprotocolinfo_p.h @@ -1,74 +1,75 @@ /* This file is part of the KDE libraries Copyright (C) 1999 Torben Weis Copyright (C) 2000-2001 Waldo Bastian Copyright 2012 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KPROTOCOLINFOPRIVATE_H #define KPROTOCOLINFOPRIVATE_H #include "kprotocolinfo.h" #include /** * @internal */ class KProtocolInfoPrivate { public: explicit KProtocolInfoPrivate(const QString &path); KProtocolInfoPrivate(const QString &name, const QString &exec, const QJsonObject &json); QString m_name; QString m_exec; KProtocolInfo::Type m_inputType; KProtocolInfo::Type m_outputType; QStringList m_listing; bool m_isSourceProtocol : 1; bool m_isHelperProtocol : 1; bool m_supportsListing : 1; bool m_supportsReading : 1; bool m_supportsWriting : 1; bool m_supportsMakeDir : 1; bool m_supportsDeleting : 1; bool m_supportsLinking : 1; bool m_supportsMoving : 1; bool m_supportsOpening : 1; + bool m_supportsTruncating : 1; bool m_determineMimetypeFromExtension : 1; bool m_canCopyFromFile : 1; bool m_canCopyToFile : 1; bool m_showPreviews : 1; bool m_canRenameFromFile : 1; bool m_canRenameToFile : 1; bool m_canDeleteRecursive : 1; QString m_defaultMimetype; QString m_icon; QString m_config; int m_maxSlaves; QString m_docPath; QString m_protClass; QStringList m_archiveMimeTypes; KProtocolInfo::ExtraFieldList m_extraFields; KProtocolInfo::FileNameUsedForCopying m_fileNameUsedForCopying; QStringList m_capabilities; QStringList m_slaveHandlesNotify; QString m_proxyProtocol; int m_maxSlavesPerHost; }; #endif diff --git a/src/core/kprotocolmanager.cpp b/src/core/kprotocolmanager.cpp index 606d3607..a127d876 100644 --- a/src/core/kprotocolmanager.cpp +++ b/src/core/kprotocolmanager.cpp @@ -1,1320 +1,1330 @@ /* This file is part of the KDE libraries Copyright (C) 1999 Torben Weis Copyright (C) 2000- Waldo Bastain Copyright (C) 2000- Dawit Alemayehu Copyright (C) 2008 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kprotocolmanager.h" #include "kprotocolinfo_p.h" #include "hostinfo.h" #include #include #include #ifdef Q_OS_WIN #include #undef interface //windows.h defines this, breaks QtDBus since it has parameters named interface #else #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #if !defined(QT_NO_NETWORKPROXY) && (defined (Q_OS_WIN32) || defined(Q_OS_MAC)) #include #include #endif #include #include #include #include #include #include "slaveconfig.h" #include "ioslave_defaults.h" #include "http_slave_defaults.h" #define QL1S(x) QLatin1String(x) #define QL1C(x) QLatin1Char(x) typedef QPair SubnetPair; /* Domain suffix match. E.g. return true if host is "cuzco.inka.de" and nplist is "inka.de,hadiko.de" or if host is "localhost" and nplist is "localhost". */ static bool revmatch(const char *host, const char *nplist) { if (host == nullptr) { return false; } const char *hptr = host + strlen(host) - 1; const char *nptr = nplist + strlen(nplist) - 1; const char *shptr = hptr; while (nptr >= nplist) { if (*hptr != *nptr) { hptr = shptr; // Try to find another domain or host in the list while (--nptr >= nplist && *nptr != ',' && *nptr != ' '); // Strip out multiple spaces and commas while (--nptr >= nplist && (*nptr == ',' || *nptr == ' ')); } else { if (nptr == nplist || nptr[-1] == ',' || nptr[-1] == ' ') { return true; } if (nptr[-1] == '/' && hptr == host) { // "bugs.kde.org" vs "http://bugs.kde.org", the config UI says URLs are ok return true; } if (hptr == host) { // e.g. revmatch("bugs.kde.org","mybugs.kde.org") return false; } hptr--; nptr--; } } return false; } class KProxyData : public QObject { Q_OBJECT public: KProxyData(const QString &slaveProtocol, const QStringList &proxyAddresses) : protocol(slaveProtocol) , proxyList(proxyAddresses) { } void removeAddress(const QString &address) { proxyList.removeAll(address); } QString protocol; QStringList proxyList; }; class KProtocolManagerPrivate { public: KProtocolManagerPrivate(); ~KProtocolManagerPrivate(); bool shouldIgnoreProxyFor(const QUrl &url); void sync(); KProtocolManager::ProxyType proxyType(); bool useReverseProxy(); QString readNoProxyFor(); QString proxyFor(const QString &protocol); QStringList getSystemProxyFor(const QUrl &url); QMutex mutex; // protects all member vars KSharedConfig::Ptr configPtr; KSharedConfig::Ptr http_config; QString modifiers; QString useragent; QString noProxyFor; QList noProxySubnets; QCache cachedProxyData; QMap protocolForArchiveMimetypes; }; Q_GLOBAL_STATIC(KProtocolManagerPrivate, kProtocolManagerPrivate) static void syncOnExit() { if (kProtocolManagerPrivate.exists()) kProtocolManagerPrivate()->sync(); } KProtocolManagerPrivate::KProtocolManagerPrivate() { // post routine since KConfig::sync() breaks if called too late qAddPostRoutine(syncOnExit); cachedProxyData.setMaxCost(200); // double the max cost. } KProtocolManagerPrivate::~KProtocolManagerPrivate() { } /* * Returns true if url is in the no proxy list. */ bool KProtocolManagerPrivate::shouldIgnoreProxyFor(const QUrl &url) { bool isMatch = false; const KProtocolManager::ProxyType type = proxyType(); const bool useRevProxy = ((type == KProtocolManager::ManualProxy) && useReverseProxy()); const bool useNoProxyList = (type == KProtocolManager::ManualProxy || type == KProtocolManager::EnvVarProxy); // No proxy only applies to ManualProxy and EnvVarProxy types... if (useNoProxyList && noProxyFor.isEmpty()) { QStringList noProxyForList(readNoProxyFor().split(QL1C(','))); QMutableStringListIterator it(noProxyForList); while (it.hasNext()) { SubnetPair subnet = QHostAddress::parseSubnet(it.next()); if (!subnet.first.isNull()) { noProxySubnets << subnet; it.remove(); } } noProxyFor = noProxyForList.join(QLatin1Char(',')); } if (!noProxyFor.isEmpty()) { QString qhost = url.host().toLower(); QByteArray host = qhost.toLatin1(); const QString qno_proxy = noProxyFor.trimmed().toLower(); const QByteArray no_proxy = qno_proxy.toLatin1(); isMatch = revmatch(host.constData(), no_proxy.constData()); // If no match is found and the request url has a port // number, try the combination of "host:port". This allows // users to enter host:port in the No-proxy-For list. if (!isMatch && url.port() > 0) { qhost += QL1C(':') + QString::number(url.port()); host = qhost.toLatin1(); isMatch = revmatch(host.constData(), no_proxy.constData()); } // If the hostname does not contain a dot, check if // is part of noProxy. if (!isMatch && !host.isEmpty() && (strchr(host.constData(), '.') == nullptr)) { isMatch = revmatch("", no_proxy.constData()); } } const QString host(url.host()); if (!noProxySubnets.isEmpty() && !host.isEmpty()) { QHostAddress address(host); // If request url is not IP address, do a DNS lookup of the hostname. // TODO: Perhaps we should make configurable ? if (address.isNull()) { //qDebug() << "Performing DNS lookup for" << host; QHostInfo info = KIO::HostInfo::lookupHost(host, 2000); const QList addresses = info.addresses(); if (!addresses.isEmpty()) { address = addresses.first(); } } if (!address.isNull()) { for (const SubnetPair &subnet : qAsConst(noProxySubnets)) { if (address.isInSubnet(subnet)) { isMatch = true; break; } } } } return (useRevProxy != isMatch); } void KProtocolManagerPrivate::sync() { QMutexLocker lock(&mutex); if (http_config) { http_config->sync(); } if (configPtr) { configPtr->sync(); } } #define PRIVATE_DATA \ KProtocolManagerPrivate *d = kProtocolManagerPrivate() void KProtocolManager::reparseConfiguration() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); if (d->http_config) { d->http_config->reparseConfiguration(); } if (d->configPtr) { d->configPtr->reparseConfiguration(); } d->cachedProxyData.clear(); d->noProxyFor.clear(); d->modifiers.clear(); d->useragent.clear(); lock.unlock(); // Force the slave config to re-read its config... KIO::SlaveConfig::self()->reset(); } static KSharedConfig::Ptr config() { PRIVATE_DATA; Q_ASSERT(!d->mutex.tryLock()); // the caller must have locked the mutex if (!d->configPtr) { d->configPtr = KSharedConfig::openConfig(QStringLiteral("kioslaverc"), KConfig::NoGlobals); } return d->configPtr; } KProtocolManager::ProxyType KProtocolManagerPrivate::proxyType() { KConfigGroup cg(config(), "Proxy Settings"); return static_cast(cg.readEntry("ProxyType", 0)); } bool KProtocolManagerPrivate::useReverseProxy() { KConfigGroup cg(config(), "Proxy Settings"); return cg.readEntry("ReversedException", false); } QString KProtocolManagerPrivate::readNoProxyFor() { QString noProxy = config()->group("Proxy Settings").readEntry("NoProxyFor"); if (proxyType() == KProtocolManager::EnvVarProxy) { noProxy = QString::fromLocal8Bit(qgetenv(noProxy.toLocal8Bit().constData())); } return noProxy; } QMap KProtocolManager::entryMap(const QString &group) { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return config()->entryMap(group); } static KConfigGroup http_config() { PRIVATE_DATA; Q_ASSERT(!d->mutex.tryLock()); // the caller must have locked the mutex if (!d->http_config) { d->http_config = KSharedConfig::openConfig(QStringLiteral("kio_httprc"), KConfig::NoGlobals); } return KConfigGroup(d->http_config, QString()); } /*=============================== TIMEOUT SETTINGS ==========================*/ int KProtocolManager::readTimeout() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); KConfigGroup cg(config(), QString()); int val = cg.readEntry("ReadTimeout", DEFAULT_READ_TIMEOUT); return qMax(MIN_TIMEOUT_VALUE, val); } int KProtocolManager::connectTimeout() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); KConfigGroup cg(config(), QString()); int val = cg.readEntry("ConnectTimeout", DEFAULT_CONNECT_TIMEOUT); return qMax(MIN_TIMEOUT_VALUE, val); } int KProtocolManager::proxyConnectTimeout() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); KConfigGroup cg(config(), QString()); int val = cg.readEntry("ProxyConnectTimeout", DEFAULT_PROXY_CONNECT_TIMEOUT); return qMax(MIN_TIMEOUT_VALUE, val); } int KProtocolManager::responseTimeout() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); KConfigGroup cg(config(), QString()); int val = cg.readEntry("ResponseTimeout", DEFAULT_RESPONSE_TIMEOUT); return qMax(MIN_TIMEOUT_VALUE, val); } /*========================== PROXY SETTINGS =================================*/ bool KProtocolManager::useProxy() { return proxyType() != NoProxy; } bool KProtocolManager::useReverseProxy() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return d->useReverseProxy(); } KProtocolManager::ProxyType KProtocolManager::proxyType() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return d->proxyType(); } KProtocolManager::ProxyAuthMode KProtocolManager::proxyAuthMode() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); KConfigGroup cg(config(), "Proxy Settings"); return static_cast(cg.readEntry("AuthMode", 0)); } /*========================== CACHING =====================================*/ bool KProtocolManager::useCache() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return http_config().readEntry("UseCache", true); } KIO::CacheControl KProtocolManager::cacheControl() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); QString tmp = http_config().readEntry("cache"); if (tmp.isEmpty()) { return DEFAULT_CACHE_CONTROL; } return KIO::parseCacheControl(tmp); } QString KProtocolManager::cacheDir() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return http_config().readPathEntry("CacheDir", QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/kio_http")); } int KProtocolManager::maxCacheAge() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return http_config().readEntry("MaxCacheAge", DEFAULT_MAX_CACHE_AGE); } int KProtocolManager::maxCacheSize() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return http_config().readEntry("MaxCacheSize", DEFAULT_MAX_CACHE_SIZE); } QString KProtocolManager::noProxyFor() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return d->readNoProxyFor(); } static QString adjustProtocol(const QString &scheme) { if (scheme.compare(QL1S("webdav"), Qt::CaseInsensitive) == 0) { return QStringLiteral("http"); } if (scheme.compare(QL1S("webdavs"), Qt::CaseInsensitive) == 0) { return QStringLiteral("https"); } return scheme.toLower(); } QString KProtocolManager::proxyFor(const QString &protocol) { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return d->proxyFor(protocol); } QString KProtocolManagerPrivate::proxyFor(const QString &protocol) { const QString key = adjustProtocol(protocol) + QL1S("Proxy"); QString proxyStr(config()->group("Proxy Settings").readEntry(key)); const int index = proxyStr.lastIndexOf(QL1C(' ')); if (index > -1) { bool ok = false; const QStringRef portStr(proxyStr.rightRef(proxyStr.length() - index - 1)); portStr.toInt(&ok); if (ok) { proxyStr = proxyStr.leftRef(index) + QL1C(':') + portStr; } else { proxyStr.clear(); } } return proxyStr; } QString KProtocolManager::proxyForUrl(const QUrl &url) { const QStringList proxies = proxiesForUrl(url); if (proxies.isEmpty()) { return QString(); } return proxies.first(); } QStringList KProtocolManagerPrivate::getSystemProxyFor(const QUrl &url) { QStringList proxies; #if !defined(QT_NO_NETWORKPROXY) && (defined(Q_OS_WIN32) || defined(Q_OS_MAC)) QNetworkProxyQuery query(url); const QList proxyList = QNetworkProxyFactory::systemProxyForQuery(query); proxies.reserve(proxyList.size()); for (const QNetworkProxy &proxy : proxyList) { QUrl url; const QNetworkProxy::ProxyType type = proxy.type(); if (type == QNetworkProxy::NoProxy || type == QNetworkProxy::DefaultProxy) { proxies << QL1S("DIRECT"); continue; } if (type == QNetworkProxy::HttpProxy || type == QNetworkProxy::HttpCachingProxy) { url.setScheme(QL1S("http")); } else if (type == QNetworkProxy::Socks5Proxy) { url.setScheme(QL1S("socks")); } else if (type == QNetworkProxy::FtpCachingProxy) { url.setScheme(QL1S("ftp")); } url.setHost(proxy.hostName()); url.setPort(proxy.port()); url.setUserName(proxy.user()); proxies << url.url(); } #else // On Unix/Linux use system environment variables if any are set. QString proxyVar(proxyFor(url.scheme())); // Check for SOCKS proxy, if not proxy is found for given url. if (!proxyVar.isEmpty()) { const QString proxy(QString::fromLocal8Bit(qgetenv(proxyVar.toLocal8Bit().constData())).trimmed()); if (!proxy.isEmpty()) { proxies << proxy; } } // Add the socks proxy as an alternate proxy if it exists, proxyVar = proxyFor(QStringLiteral("socks")); if (!proxyVar.isEmpty()) { QString proxy = QString::fromLocal8Bit(qgetenv(proxyVar.toLocal8Bit().constData())).trimmed(); // Make sure the scheme of SOCKS proxy is always set to "socks://". const int index = proxy.indexOf(QL1S("://")); const int offset = (index == -1) ? 0 : (index + 3); proxy = QL1S("socks://") + proxy.midRef(offset); if (!proxy.isEmpty()) { proxies << proxy; } } #endif return proxies; } QStringList KProtocolManager::proxiesForUrl(const QUrl &url) { QStringList proxyList; PRIVATE_DATA; QMutexLocker lock(&d->mutex); if (!d->shouldIgnoreProxyFor(url)) { switch (d->proxyType()) { case PACProxy: case WPADProxy: { QUrl u(url); const QString protocol = adjustProtocol(u.scheme()); u.setScheme(protocol); if (protocol.startsWith(QLatin1String("http")) || protocol.startsWith(QLatin1String("ftp"))) { QDBusReply reply = QDBusInterface(QStringLiteral("org.kde.kded5"), QStringLiteral("/modules/proxyscout"), QStringLiteral("org.kde.KPAC.ProxyScout")) .call(QStringLiteral("proxiesForUrl"), u.toString()); proxyList = reply; } break; } case EnvVarProxy: proxyList = d->getSystemProxyFor(url); break; case ManualProxy: { QString proxy(d->proxyFor(url.scheme())); if (!proxy.isEmpty()) { proxyList << proxy; } // Add the socks proxy as an alternate proxy if it exists, proxy = d->proxyFor(QStringLiteral("socks")); if (!proxy.isEmpty()) { // Make sure the scheme of SOCKS proxy is always set to "socks://". const int index = proxy.indexOf(QL1S("://")); const int offset = (index == -1) ? 0 : (index + 3); proxy = QL1S("socks://") + proxy.midRef(offset); proxyList << proxy; } } break; case NoProxy: break; } } if (proxyList.isEmpty()) { proxyList << QStringLiteral("DIRECT"); } return proxyList; } void KProtocolManager::badProxy(const QString &proxy) { QDBusInterface(QStringLiteral("org.kde.kded5"), QStringLiteral("/modules/proxyscout")) .asyncCall(QStringLiteral("blackListProxy"), proxy); PRIVATE_DATA; QMutexLocker lock(&d->mutex); const QStringList keys(d->cachedProxyData.keys()); for (const QString &key : keys) { d->cachedProxyData[key]->removeAddress(proxy); } } QString KProtocolManager::slaveProtocol(const QUrl &url, QString &proxy) { QStringList proxyList; const QString protocol = KProtocolManager::slaveProtocol(url, proxyList); if (!proxyList.isEmpty()) { proxy = proxyList.first(); } return protocol; } // Generates proxy cache key from request given url. static void extractProxyCacheKeyFromUrl(const QUrl &u, QString *key) { if (!key) { return; } *key = u.scheme(); *key += u.host(); if (u.port() > 0) { *key += QString::number(u.port()); } } QString KProtocolManager::slaveProtocol(const QUrl &url, QStringList &proxyList) { proxyList.clear(); // Do not perform a proxy lookup for any url classified as a ":local" url or // one that does not have a host component or if proxy is disabled. QString protocol(url.scheme()); if (url.host().isEmpty() || KProtocolInfo::protocolClass(protocol) == QL1S(":local") || KProtocolManager::proxyType() == KProtocolManager::NoProxy) { return protocol; } QString proxyCacheKey; extractProxyCacheKeyFromUrl(url, &proxyCacheKey); PRIVATE_DATA; QMutexLocker lock(&d->mutex); // Look for cached proxy information to avoid more work. if (d->cachedProxyData.contains(proxyCacheKey)) { KProxyData *data = d->cachedProxyData.object(proxyCacheKey); proxyList = data->proxyList; return data->protocol; } lock.unlock(); const QStringList proxies = proxiesForUrl(url); const int count = proxies.count(); if (count > 0 && !(count == 1 && proxies.first() == QL1S("DIRECT"))) { for (const QString &proxy : proxies) { if (proxy == QL1S("DIRECT")) { proxyList << proxy; } else { QUrl u(proxy); if (!u.isEmpty() && u.isValid() && !u.scheme().isEmpty()) { proxyList << proxy; } } } } // The idea behind slave protocols is not applicable to http // and webdav protocols as well as protocols unknown to KDE. if (!proxyList.isEmpty() && !protocol.startsWith(QLatin1String("http")) && !protocol.startsWith(QLatin1String("webdav")) && KProtocolInfo::isKnownProtocol(protocol)) { for (const QString &proxy : qAsConst(proxyList)) { QUrl u(proxy); if (u.isValid() && KProtocolInfo::isKnownProtocol(u.scheme())) { protocol = u.scheme(); break; } } } lock.relock(); // cache the proxy information... d->cachedProxyData.insert(proxyCacheKey, new KProxyData(protocol, proxyList)); return protocol; } /*================================= USER-AGENT SETTINGS =====================*/ QString KProtocolManager::userAgentForHost(const QString &hostname) { const QString sendUserAgent = KIO::SlaveConfig::self()->configData(QStringLiteral("http"), hostname.toLower(), QStringLiteral("SendUserAgent")).toLower(); if (sendUserAgent == QL1S("false")) { return QString(); } const QString useragent = KIO::SlaveConfig::self()->configData(QStringLiteral("http"), hostname.toLower(), QStringLiteral("UserAgent")); // Return the default user-agent if none is specified // for the requested host. if (useragent.isEmpty()) { return defaultUserAgent(); } return useragent; } QString KProtocolManager::defaultUserAgent() { const QString modifiers = KIO::SlaveConfig::self()->configData(QStringLiteral("http"), QString(), QStringLiteral("UserAgentKeys")); return defaultUserAgent(modifiers); } static QString defaultUserAgentFromPreferredService() { QString agentStr; // Check if the default COMPONENT contains a custom default UA string... KService::Ptr service = KMimeTypeTrader::self()->preferredService(QStringLiteral("text/html"), QStringLiteral("KParts/ReadOnlyPart")); if (service && service->showInCurrentDesktop()) agentStr = service->property(QStringLiteral("X-KDE-Default-UserAgent"), QVariant::String).toString(); return agentStr; } // This is not the OS, but the windowing system, e.g. X11 on Unix/Linux. static QString platform() { #if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) return QStringLiteral("X11"); #elif defined(Q_OS_MAC) return QStringLiteral("Macintosh"); #elif defined(Q_OS_WIN) return QStringLiteral("Windows"); #else return QStringLiteral("Unknown"); #endif } QString KProtocolManager::defaultUserAgent(const QString &_modifiers) { PRIVATE_DATA; QMutexLocker lock(&d->mutex); QString modifiers = _modifiers.toLower(); if (modifiers.isEmpty()) { modifiers = QStringLiteral(DEFAULT_USER_AGENT_KEYS); } if (d->modifiers == modifiers && !d->useragent.isEmpty()) { return d->useragent; } d->modifiers = modifiers; /* The following code attempts to determine the default user agent string from the 'X-KDE-UA-DEFAULT-STRING' property of the desktop file for the preferred service that was configured to handle the 'text/html' mime type. If the preferred service's desktop file does not specify this property, the long standing default user agent string will be used. The following keyword placeholders are automatically converted when the user agent string is read from the property: %SECURITY% Expands to"N" when SSL is not supported, otherwise it is ignored. %OSNAME% Expands to operating system name, e.g. Linux. %OSVERSION% Expands to operating system version, e.g. 2.6.32 %SYSTYPE% Expands to machine or system type, e.g. i386 %PLATFORM% Expands to windowing system, e.g. X11 on Unix/Linux. %LANGUAGE% Expands to default language in use, e.g. en-US. %APPVERSION% Expands to QCoreApplication applicationName()/applicationVerison(), e.g. Konqueror/4.5.0. If application name and/or application version number are not set, then "KDE" and the runtime KDE version numbers are used respectively. All of the keywords are handled case-insensitively. */ QString systemName, systemVersion, machine, supp; const bool sysInfoFound = getSystemNameVersionAndMachine(systemName, systemVersion, machine); QString agentStr = defaultUserAgentFromPreferredService(); if (agentStr.isEmpty()) { supp += platform(); if (sysInfoFound) { if (modifiers.contains(QL1C('o'))) { supp += QL1S("; ") + systemName; if (modifiers.contains(QL1C('v'))) { supp += QL1C(' ') + systemVersion; } if (modifiers.contains(QL1C('m'))) { supp += QL1C(' ') + machine; } } if (modifiers.contains(QL1C('l'))) { supp += QL1S("; ") + QLocale::languageToString(QLocale().language()); } } // Full format: Mozilla/5.0 (Linux d->useragent = QL1S("Mozilla/5.0 (") + supp + QL1S(") KHTML/") + QString::number(KIO_VERSION_MAJOR) + QL1C('.') + QString::number(KIO_VERSION_MINOR) + QL1C('.') + QString::number(KIO_VERSION_PATCH) + QL1S(" (like Gecko) Konqueror/") + QString::number(KIO_VERSION_MAJOR) + QL1S(" KIO/") + QString::number(KIO_VERSION_MAJOR) + QL1C('.') + QString::number(KIO_VERSION_MINOR); } else { QString appName = QCoreApplication::applicationName(); if (appName.isEmpty() || appName.startsWith(QLatin1String("kcmshell"), Qt::CaseInsensitive)) { appName = QStringLiteral("KDE"); } QString appVersion = QCoreApplication::applicationVersion(); if (appVersion.isEmpty()) { appVersion += QString::number(KIO_VERSION_MAJOR) + QL1C('.') + QString::number(KIO_VERSION_MINOR) + QL1C('.') + QString::number(KIO_VERSION_PATCH); } appName += QL1C('/') + appVersion; agentStr.replace(QL1S("%appversion%"), appName, Qt::CaseInsensitive); if (!QSslSocket::supportsSsl()) { agentStr.replace(QLatin1String("%security%"), QL1S("N"), Qt::CaseInsensitive); } else { agentStr.remove(QStringLiteral("%security%"), Qt::CaseInsensitive); } if (sysInfoFound) { // Platform (e.g. X11). It is no longer configurable from UI. agentStr.replace(QL1S("%platform%"), platform(), Qt::CaseInsensitive); // Operating system (e.g. Linux) if (modifiers.contains(QL1C('o'))) { agentStr.replace(QL1S("%osname%"), systemName, Qt::CaseInsensitive); // OS version (e.g. 2.6.36) if (modifiers.contains(QL1C('v'))) { agentStr.replace(QL1S("%osversion%"), systemVersion, Qt::CaseInsensitive); } else { agentStr.remove(QStringLiteral("%osversion%"), Qt::CaseInsensitive); } // Machine type (i686, x86-64, etc.) if (modifiers.contains(QL1C('m'))) { agentStr.replace(QL1S("%systype%"), machine, Qt::CaseInsensitive); } else { agentStr.remove(QStringLiteral("%systype%"), Qt::CaseInsensitive); } } else { agentStr.remove(QStringLiteral("%osname%"), Qt::CaseInsensitive); agentStr.remove(QStringLiteral("%osversion%"), Qt::CaseInsensitive); agentStr.remove(QStringLiteral("%systype%"), Qt::CaseInsensitive); } // Language (e.g. en_US) if (modifiers.contains(QL1C('l'))) { agentStr.replace(QL1S("%language%"), QLocale::languageToString(QLocale().language()), Qt::CaseInsensitive); } else { agentStr.remove(QStringLiteral("%language%"), Qt::CaseInsensitive); } // Clean up unnecessary separators that could be left over from the // possible keyword removal above... agentStr.replace(QRegularExpression(QStringLiteral("[(]\\s*[;]\\s*")), QStringLiteral("(")); agentStr.replace(QRegularExpression(QStringLiteral("[;]\\s*[;]\\s*")), QStringLiteral("; ")); agentStr.replace(QRegularExpression(QStringLiteral("\\s*[;]\\s*[)]")), QStringLiteral(")")); } else { agentStr.remove(QStringLiteral("%osname%")); agentStr.remove(QStringLiteral("%osversion%")); agentStr.remove(QStringLiteral("%platform%")); agentStr.remove(QStringLiteral("%systype%")); agentStr.remove(QStringLiteral("%language%")); } d->useragent = agentStr.simplified(); } //qDebug() << "USERAGENT STRING:" << d->useragent; return d->useragent; } QString KProtocolManager::userAgentForApplication(const QString &appName, const QString &appVersion, const QStringList &extraInfo) { QString systemName, systemVersion, machine, info; if (getSystemNameVersionAndMachine(systemName, systemVersion, machine)) { info += systemName + QL1C('/') + systemVersion + QL1S("; "); } info += QL1S("KDE/") + QString::number(KIO_VERSION_MAJOR) + QL1C('.') + QString::number(KIO_VERSION_MINOR) + QL1C('.') + QString::number(KIO_VERSION_PATCH); if (!machine.isEmpty()) { info += QL1S("; ") + machine; } info += QL1S("; ") + extraInfo.join(QLatin1String("; ")); return (appName + QL1C('/') + appVersion + QStringLiteral(" (") + info + QL1C(')')); } bool KProtocolManager::getSystemNameVersionAndMachine( QString &systemName, QString &systemVersion, QString &machine) { #if defined(Q_OS_WIN) && !defined(_WIN32_WCE) // we do not use unameBuf.sysname information constructed in kdewin32 // because we want to get separate name and version systemName = QStringLiteral("Windows"); OSVERSIONINFOEX versioninfo; ZeroMemory(&versioninfo, sizeof(OSVERSIONINFOEX)); // try calling GetVersionEx using the OSVERSIONINFOEX, if that fails, try using the OSVERSIONINFO versioninfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); bool ok = GetVersionEx((OSVERSIONINFO *) &versioninfo); if (!ok) { versioninfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); ok = GetVersionEx((OSVERSIONINFO *) &versioninfo); } if (ok) { systemVersion = QString::number(versioninfo.dwMajorVersion); systemVersion += QL1C('.'); systemVersion += QString::number(versioninfo.dwMinorVersion); } #else struct utsname unameBuf; if (0 != uname(&unameBuf)) { return false; } systemName = QString::fromUtf8(unameBuf.sysname); systemVersion = QString::fromUtf8(unameBuf.release); machine = QString::fromUtf8(unameBuf.machine); #endif return true; } QString KProtocolManager::acceptLanguagesHeader() { const QLatin1String english("en"); // User's desktop language preference. QStringList languageList = QLocale().uiLanguages(); // Replace possible "C" in the language list with "en", unless "en" is // already pressent. This is to keep user's priorities in order. // If afterwards "en" is still not present, append it. int idx = languageList.indexOf(QLatin1String("C")); if (idx != -1) { if (languageList.contains(english)) { languageList.removeAt(idx); } else { languageList[idx] = english; } } if (!languageList.contains(english)) { languageList += english; } // Some languages may have web codes different from locale codes, // read them from the config and insert in proper order. KConfig acclangConf(QStringLiteral("accept-languages.codes"), KConfig::NoGlobals); KConfigGroup replacementCodes(&acclangConf, "ReplacementCodes"); QStringList languageListFinal; for (const QString &lang : qAsConst(languageList)) { const QStringList langs = replacementCodes.readEntry(lang, QStringList()); if (langs.isEmpty()) { languageListFinal += lang; } else { languageListFinal += langs; } } // The header is composed of comma separated languages, with an optional // associated priority estimate (q=1..0) defaulting to 1. // As our language tags are already sorted by priority, we'll just decrease // the value evenly int prio = 10; QString header; for (const QString &lang : qAsConst(languageListFinal)) { header += lang; if (prio < 10) { header += QL1S(";q=0.") + QString::number(prio); } // do not add cosmetic whitespace in here : it is less compatible (#220677) header += QL1C(','); if (prio > 1) { --prio; } } header.chop(1); // Some of the languages may have country specifier delimited by // underscore, or modifier delimited by at-sign. // The header should use dashes instead. header.replace(QL1C('_'), QL1C('-')); header.replace(QL1C('@'), QL1C('-')); return header; } /*==================================== OTHERS ===============================*/ bool KProtocolManager::markPartial() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return config()->group(QByteArray()).readEntry("MarkPartial", true); } int KProtocolManager::minimumKeepSize() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return config()->group(QByteArray()).readEntry("MinimumKeepSize", DEFAULT_MINIMUM_KEEP_SIZE); // 5000 byte } bool KProtocolManager::autoResume() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return config()->group(QByteArray()).readEntry("AutoResume", false); } bool KProtocolManager::persistentConnections() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return config()->group(QByteArray()).readEntry("PersistentConnections", true); } bool KProtocolManager::persistentProxyConnection() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return config()->group(QByteArray()).readEntry("PersistentProxyConnection", false); } QString KProtocolManager::proxyConfigScript() { PRIVATE_DATA; QMutexLocker lock(&d->mutex); return config()->group("Proxy Settings").readEntry("Proxy Config Script"); } /* =========================== PROTOCOL CAPABILITIES ============== */ static KProtocolInfoPrivate *findProtocol(const QUrl &url) { if (!url.isValid()) { return nullptr; } QString protocol = url.scheme(); if (!KProtocolInfo::proxiedBy(protocol).isEmpty()) { QString dummy; protocol = KProtocolManager::slaveProtocol(url, dummy); } return KProtocolInfoFactory::self()->findProtocol(protocol); } KProtocolInfo::Type KProtocolManager::inputType(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return KProtocolInfo::T_NONE; } return prot->m_inputType; } KProtocolInfo::Type KProtocolManager::outputType(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return KProtocolInfo::T_NONE; } return prot->m_outputType; } bool KProtocolManager::isSourceProtocol(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_isSourceProtocol; } bool KProtocolManager::supportsListing(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_supportsListing; } QStringList KProtocolManager::listing(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return QStringList(); } return prot->m_listing; } bool KProtocolManager::supportsReading(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_supportsReading; } bool KProtocolManager::supportsWriting(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_supportsWriting; } bool KProtocolManager::supportsMakeDir(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_supportsMakeDir; } bool KProtocolManager::supportsDeleting(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_supportsDeleting; } bool KProtocolManager::supportsLinking(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_supportsLinking; } bool KProtocolManager::supportsMoving(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_supportsMoving; } bool KProtocolManager::supportsOpening(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_supportsOpening; } +bool KProtocolManager::supportsTruncating(const QUrl &url) +{ + KProtocolInfoPrivate *prot = findProtocol(url); + if (!prot) { + return false; + } + + return prot->m_supportsTruncating; +} + bool KProtocolManager::canCopyFromFile(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_canCopyFromFile; } bool KProtocolManager::canCopyToFile(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_canCopyToFile; } bool KProtocolManager::canRenameFromFile(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_canRenameFromFile; } bool KProtocolManager::canRenameToFile(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_canRenameToFile; } bool KProtocolManager::canDeleteRecursive(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return false; } return prot->m_canDeleteRecursive; } KProtocolInfo::FileNameUsedForCopying KProtocolManager::fileNameUsedForCopying(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return KProtocolInfo::FromUrl; } return prot->m_fileNameUsedForCopying; } QString KProtocolManager::defaultMimetype(const QUrl &url) { KProtocolInfoPrivate *prot = findProtocol(url); if (!prot) { return QString(); } return prot->m_defaultMimetype; } QString KProtocolManager::protocolForArchiveMimetype(const QString &mimeType) { PRIVATE_DATA; QMutexLocker lock(&d->mutex); if (d->protocolForArchiveMimetypes.isEmpty()) { const QList allProtocols = KProtocolInfoFactory::self()->allProtocols(); for (KProtocolInfoPrivate *allProtocol : allProtocols) { const QStringList archiveMimetypes = allProtocol->m_archiveMimeTypes; for (const QString &mime : archiveMimetypes) { d->protocolForArchiveMimetypes.insert(mime, allProtocol->m_name); } } } return d->protocolForArchiveMimetypes.value(mimeType); } QString KProtocolManager::charsetFor(const QUrl &url) { return KIO::SlaveConfig::self()->configData(url.scheme(), url.host(), QStringLiteral("Charset")); } #undef PRIVATE_DATA #include "kprotocolmanager.moc" diff --git a/src/core/kprotocolmanager.h b/src/core/kprotocolmanager.h index bab87868..640acb85 100644 --- a/src/core/kprotocolmanager.h +++ b/src/core/kprotocolmanager.h @@ -1,682 +1,694 @@ /* This file is part of the KDE libraries Copyright (C) 1999 Torben Weis Copyright (C) 2000- Waldo Bastain Copyright (C) 2000- Dawit Alemayehu Copyright (C) 2008 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KPROTOCOLMANAGER_H #define KPROTOCOLMANAGER_H #include #include "kio/global.h" // KIO::CacheControl #include "kiocore_export.h" #include "kprotocolinfo.h" class KSharedConfig; template class QExplicitlySharedDataPointer; typedef QExplicitlySharedDataPointer KSharedConfigPtr; namespace KIO { class SlaveConfigPrivate; } // namespace KIO /** * @class KProtocolManager kprotocolmanager.h * * Provides information about I/O (Internet, etc.) settings chosen/set * by the end user. * * KProtocolManager has a heap of static functions that allows only read * access to KDE's IO related settings. These include proxy, cache, file * transfer resumption, timeout and user-agent related settings. * * The information provided by this class is generic enough to be applicable * to any application that makes use of KDE's IO sub-system. Note that this * mean the proxy, timeout etc. settings are saved in a separate user-specific * config file and not in the config file of the application. * * Original author: * @author Torben Weis * * Revised by: * @author Waldo Bastain * @author Dawit Alemayehu * @see KPAC */ class KIOCORE_EXPORT KProtocolManager { public: /*=========================== USER-AGENT SETTINGS ===========================*/ /** * Returns the default user-agent string used for web browsing. * * @return the default user-agent string */ static QString defaultUserAgent(); /** * Returns the default user-agent value used for web browsing, for example * "Mozilla/5.0 (compatible; Konqueror/4.0; Linux; X11; i686; en_US) KHTML/4.0.1 (like Gecko)" * * @param keys can be any of the following: * @li 'o' Show OS * @li 'v' Show OS Version * @li 'p' Show platform (only for X11) * @li 'm' Show machine architecture * @li 'l' Show language * @return the default user-agent value with the given @p keys */ static QString defaultUserAgent(const QString &keys); /** * Returns the application's user-agent string. * Example string is "KMail/1.9.50 (Windows/6.0; KDE/3.97.1; i686; svn-762186; 2008-01-15)", * where "KMail" is the @p appName parameter, "1.9.50" is the @p appVersion parameter, * "Windows/6.0; KDE/3.97.1; i686" part is added automatically and "svn-762186; 2008-01-15" * is provided by @p extraInfo list. * * @param appName name of the application * @param appVersion name of the application * @param extraInfo a list of elements that will be appended to the string as extra information * @return the application's user-agent string * * @since 4.1 */ static QString userAgentForApplication(const QString &appName, const QString &appVersion, const QStringList &extraInfo = QStringList()); /** * Returns the user-agent string configured for the * specified host. * * If hostname is not found or is empty (i.e. "" or * QString()) this function will return the default * user agent. * * @param hostname name of the host * @return specified user-agent string */ static QString userAgentForHost(const QString &hostname); /** * Returns system name, version and machine type, for example "Windows", "5.1", "i686". * This information can be used for constructing custom user-agent strings. * * @param systemName system name * @param systemVersion system version * @param machine machine type * @return true if system name, version and machine type has been provided * * @since 4.1 */ static bool getSystemNameVersionAndMachine( QString &systemName, QString &systemVersion, QString &machine); /*=========================== TIMEOUT CONFIG ================================*/ /** * Returns the preferred timeout value for reading from * remote connections in seconds. * * @return timeout value for remote connection in secs. */ static int readTimeout(); /** * Returns the preferred timeout value for remote connections * in seconds. * * @return timeout value for remote connection in secs. */ static int connectTimeout(); /** * Returns the preferred timeout value for proxy connections * in seconds. * * @return timeout value for proxy connection in secs. */ static int proxyConnectTimeout(); /** * Returns the preferred response timeout value for * remote connecting in seconds. * * @return timeout value for remote connection in seconds. */ static int responseTimeout(); /*=============================== PROXY CONFIG ==============================*/ /** * Returns whether or not the user specified the * use of proxy server to make connections. * @return true to use a proxy */ static bool useProxy(); /** * Returns whether or not the proxy server * lookup should be reversed or not. * @return true to use a reversed proxy */ static bool useReverseProxy(); /** * Types of proxy configuration * @li NoProxy - No proxy is used * @li ManualProxy - Proxies are manually configured * @li PACProxy - A Proxy configuration URL has been given * @li WPADProxy - A proxy should be automatically discovered * @li EnvVarProxy - Use the proxy values set through environment variables. */ enum ProxyType { NoProxy, ManualProxy, PACProxy, WPADProxy, EnvVarProxy }; /** * Returns the type of proxy configuration that is used. * @return the proxy type */ static ProxyType proxyType(); /** * Proxy authorization modes. * * @li Prompt - Ask for authorization as needed * @li Automatic - Use auto login as defined in kionetrc files. */ enum ProxyAuthMode { Prompt, Automatic }; /** * Returns the way proxy authorization should be handled. * * @return the proxy authorization mode * @see ProxyAuthMode */ static ProxyAuthMode proxyAuthMode(); /** * Returns the strings for hosts that should contacted * DIRECTLY, bypassing any proxy settings. * @return a list of (comma-separated) hostnames or partial host * names */ static QString noProxyFor(); /** * Returns the proxy server address for a given * protocol. * * @param protocol the protocol whose proxy info is needed * @returns the proxy server address if one is available, * or QString() if not available */ static QString proxyFor(const QString &protocol); /** * Returns the Proxy server address for a given URL. * * If the selected proxy type is @ref PACProxy or @ref WPADProxy, then a * helper kded module, proxyscout, is used to determine the proxy information. * Otherwise, @ref proxyFor is used to find the proxy to use for the given url. * * If this function returns an empty string, then the request to a proxy server * must be denied. For a direct connection, without the use of a proxy, this * function will return "DIRECT". * * @param url the URL whose proxy info is needed * @returns the proxy server address if one is available, otherwise a QString(). */ static QString proxyForUrl(const QUrl &url); /** * Returns all the possible proxy server addresses for @p url. * * If the selected proxy type is @ref PACProxy or @ref WPADProxy, then a * helper kded module, proxyscout, is used to determine the proxy information. * Otherwise, @ref proxyFor is used to find the proxy to use for the given url. * * If this function returns empty list, then the request is to a proxy server * must be denied. For a direct connection, this function will return a single * entry of "DIRECT". * * @since 4.7 * * @param url the URL whose proxy info is needed * @returns the proxy server address if one is available, otherwise an empty list . */ static QStringList proxiesForUrl(const QUrl &url); /** * Marks this proxy as bad (down). It will not be used for the * next 30 minutes. (The script may supply an alternate proxy) * @param proxy the proxy to mark as bad (as URL) */ static void badProxy(const QString &proxy); /** * Returns the URL of the script for automatic proxy configuration. * @return the proxy configuration script */ static QString proxyConfigScript(); /*========================== CACHE CONFIG ===================================*/ /** * Returns true/false to indicate whether a cache * should be used * * @return true to use the cache, false otherwisea */ static bool useCache(); /** * Returns the maximum age in seconds cached files should be * kept before they are deleted as necessary. * * @return the maximum cache age in seconds */ static int maxCacheAge(); /** * Returns the maximum size that can be used for caching. * * By default this function returns the DEFAULT_MAX_CACHE_SIZE * value as defined in http_slave_defaults.h. Not that the * value returned is in bytes, hence a value of 5120 would mean * 5 Kb. * * @return the maximum cache size in bytes */ static int maxCacheSize(); // Maximum cache size in Kb. /** * The directory which contains the cache files. * @return the directory that contains the cache files */ static QString cacheDir(); /** * Returns the Cache control directive to be used. * @return the cache control value */ static KIO::CacheControl cacheControl(); /*============================ DOWNLOAD CONFIG ==============================*/ /** * Returns true if partial downloads should be * automatically resumed. * @return true to resume partial downloads */ static bool autoResume(); /** * Returns true if partial downloads should be marked * with a ".part" extension. * @return true if partial downloads should get an ".part" extension */ static bool markPartial(); /** * Returns the minimum file size for keeping aborted * downloads. * * Any data downloaded that does not meet this minimum * requirement will simply be discarded. The default size * is 5 KB. * * @return the minimum keep size for aborted downloads in bytes */ static int minimumKeepSize(); /*============================ NETWORK CONNECTIONS ==========================*/ /** * Returns true if proxy connections should be persistent. * @return true if proxy connections should be persistent */ static bool persistentProxyConnection(); /** * Returns true if connections should be persistent * @return true if the connections should be persistent */ static bool persistentConnections(); /*===================== PROTOCOL CAPABILITIES ===============================*/ /** * Returns whether the protocol can list files/objects. * If a protocol supports listing it can be browsed in e.g. file-dialogs * and konqueror. * * Whether a protocol supports listing is determined by the "listing=" * field in the protocol description file. * If the protocol support listing it should list the fields it provides in * this field. If the protocol does not support listing this field should * remain empty (default.) * * @param url the url to check * @return true if the protocol support listing * @see listing() */ static bool supportsListing(const QUrl &url); /** * Returns whether the protocol can retrieve data from URLs. * * This corresponds to the "reading=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if it is possible to read from the URL */ static bool supportsReading(const QUrl &url); /** * Returns whether the protocol can store data to URLs. * * This corresponds to the "writing=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol supports writing */ static bool supportsWriting(const QUrl &url); /** * Returns whether the protocol can create directories/folders. * * This corresponds to the "makedir=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol can create directories */ static bool supportsMakeDir(const QUrl &url); /** * Returns whether the protocol can delete files/objects. * * This corresponds to the "deleting=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol supports deleting */ static bool supportsDeleting(const QUrl &url); /** * Returns whether the protocol can create links between files/objects. * * This corresponds to the "linking=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol supports linking */ static bool supportsLinking(const QUrl &url); /** * Returns whether the protocol can move files/objects between different * locations. * * This corresponds to the "moving=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol supports moving */ static bool supportsMoving(const QUrl &url); /** * Returns whether the protocol can be opened using KIO::open(const QUrl&). * * This corresponds to the "opening=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol supports opening */ static bool supportsOpening(const QUrl &url); + /** + * Returns whether the protocol can be truncated with FileJob::truncate(KIO::filesize_t length). + * + * This corresponds to the "truncating=" field in the protocol description file. + * Valid values for this field are "true" or "false" (default). + * + * @param url the url to check + * @return true if the protocol supports truncating + * @since 5.66 + */ + static bool supportsTruncating(const QUrl &url); + /** * Returns whether the protocol can copy files/objects directly from the * filesystem itself. If not, the application will read files from the * filesystem using the file-protocol and pass the data on to the destination * protocol. * * This corresponds to the "copyFromFile=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol can copy files from the local file system */ static bool canCopyFromFile(const QUrl &url); /** * Returns whether the protocol can copy files/objects directly to the * filesystem itself. If not, the application will receive the data from * the source protocol and store it in the filesystem using the * file-protocol. * * This corresponds to the "copyToFile=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol can copy files to the local file system */ static bool canCopyToFile(const QUrl &url); /** * Returns whether the protocol can rename (i.e. move fast) files/objects * directly from the filesystem itself. If not, the application will read * files from the filesystem using the file-protocol and pass the data on * to the destination protocol. * * This corresponds to the "renameFromFile=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol can rename/move files from the local file system */ static bool canRenameFromFile(const QUrl &url); /** * Returns whether the protocol can rename (i.e. move fast) files/objects * directly to the filesystem itself. If not, the application will receive * the data from the source protocol and store it in the filesystem using the * file-protocol. * * This corresponds to the "renameToFile=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol can rename files to the local file system */ static bool canRenameToFile(const QUrl &url); /** * Returns whether the protocol can recursively delete directories by itself. * If not (the usual case) then KIO will list the directory and delete files * and empty directories one by one. * * This corresponds to the "deleteRecursive=" field in the protocol description file. * Valid values for this field are "true" or "false" (default). * * @param url the url to check * @return true if the protocol can delete non-empty directories by itself. */ static bool canDeleteRecursive(const QUrl &url); /** * This setting defines the strategy to use for generating a filename, when * copying a file or directory to another directory. By default the destination * filename is made out of the filename in the source URL. However if the * ioslave displays names that are different from the filename of the URL * (e.g. kio_fonts shows Arial for arial.ttf, or kio_trash shows foo.txt and * uses some internal URL), using Name means that the display name (UDS_NAME) * will be used to as the filename in the destination directory. * * This corresponds to the "fileNameUsedForCopying=" field in the protocol description file. * Valid values for this field are "Name" or "FromURL" (default). * * @param url the url to check * @return how to generate the filename in the destination directory when copying/moving */ static KProtocolInfo::FileNameUsedForCopying fileNameUsedForCopying(const QUrl &url); /** * Returns default mimetype for this URL based on the protocol. * * This corresponds to the "defaultMimetype=" field in the protocol description file. * * @param url the url to check * @return the default mime type of the protocol, or null if unknown */ static QString defaultMimetype(const QUrl &url); /** * Returns whether the protocol should be treated as a filesystem * or as a stream when reading from it. * * This corresponds to the "input=" field in the protocol description file. * Valid values for this field are "filesystem", "stream" or "none" (default). * * @param url the url to check * @return the input type of the given @p url */ static KProtocolInfo::Type inputType(const QUrl &url); /** * Returns whether the protocol should be treated as a filesystem * or as a stream when writing to it. * * This corresponds to the "output=" field in the protocol description file. * Valid values for this field are "filesystem", "stream" or "none" (default). * * @param url the url to check * @return the output type of the given @p url */ static KProtocolInfo::Type outputType(const QUrl &url); /** * Returns the list of fields this protocol returns when listing * The current possibilities are * Name, Type, Size, Date, AccessDate, Access, Owner, Group, Link, URL, MimeType * as well as Extra1, Extra2 etc. for extra fields (see extraFields). * * This corresponds to the "listing=" field in the protocol description file. * The supported fields should be separated with ',' in the protocol description file. * * @param url the url to check * @return a list of field names */ static QStringList listing(const QUrl &url); /** * Returns whether the protocol can act as a source protocol. * * A source protocol retrieves data from or stores data to the * location specified by a URL. * A source protocol is the opposite of a filter protocol. * * The "source=" field in the protocol description file determines * whether a protocol is a source protocol or a filter protocol. * @param url the url to check * @return true if the protocol is a source of data (e.g. http), false if the * protocol is a filter (e.g. gzip) */ static bool isSourceProtocol(const QUrl &url); /** * Returns which protocol handles this mimetype, if it's an archive mimetype. * For instance zip:/ handles application/x-zip. * * This is defined in the protocol description file using an entry like * "archiveMimetype=application/x-zip" * * @param mimeType the mimetype to check * @return the protocol that can handle this archive mimetype, for instance "zip". * @since 4.1 */ static QString protocolForArchiveMimetype(const QString &mimeType); /*=============================== OTHERS ====================================*/ /** * Force a reload of the general config file of * io-slaves ( kioslaverc). */ static void reparseConfiguration(); /** * Return the protocol to use in order to handle the given @p url * It's usually the same, except that FTP, when handled by a proxy, * needs an HTTP ioslave. * * When a proxy is to be used, proxy contains the URL for the proxy. * @param url the url to check * @param proxy the URL of the proxy to use * @return the slave protocol (e.g. 'http'), can be null if unknown */ static QString slaveProtocol(const QUrl &url, QString &proxy); /** * Overloaded function that returns a list of all available proxy servers. * * @since 4.7 */ static QString slaveProtocol(const QUrl &url, QStringList &proxy); /** * Return Accept-Languages header built up according to user's desktop * language settings. * @return Accept-Languages header string */ static QString acceptLanguagesHeader(); /** * Returns the charset to use for the specified @ref url. * * @since 4.10 */ static QString charsetFor(const QUrl &url); private: friend class KIO::SlaveConfigPrivate; /** * @internal * (Shared with SlaveConfig) */ KIOCORE_NO_EXPORT static QMap entryMap(const QString &group); }; #endif diff --git a/src/core/slavebase.cpp b/src/core/slavebase.cpp index 92040342..072c7a65 100644 --- a/src/core/slavebase.cpp +++ b/src/core/slavebase.cpp @@ -1,1562 +1,1578 @@ /* * This file is part of the KDE libraries * Copyright (c) 2000 Waldo Bastian * Copyright (c) 2000 David Faure * Copyright (c) 2000 Stephan Kulow * Copyright (c) 2007 Thiago Macieira * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License version 2 as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * **/ #include "slavebase.h" #include #include #include #include #ifdef Q_OS_WIN #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include "kremoteencoding.h" #include "kioglobal_p.h" #include "connection_p.h" #include "commands_p.h" #include "ioslave_defaults.h" #include "slaveinterface.h" #include "kpasswdserverclient.h" #include "kiocoredebug.h" #ifdef Q_OS_UNIX #include #endif #if KIO_ASSERT_SLAVE_STATES #define KIO_STATE_ASSERT(cond, where, what) Q_ASSERT_X(cond, where, what) #else #define KIO_STATE_ASSERT(cond, where, what) do { if (!(cond)) qCWarning(KIO_CORE) << what; } while (false) #endif extern "C" { static void sigpipe_handler(int sig); } using namespace KIO; typedef QList AuthKeysList; typedef QMap AuthKeysMap; #define KIO_DATA QByteArray data; QDataStream stream( &data, QIODevice::WriteOnly ); stream #define KIO_FILESIZE_T(x) quint64(x) static const int KIO_MAX_ENTRIES_PER_BATCH = 200; static const int KIO_MAX_SEND_BATCH_TIME = 300; namespace KIO { class SlaveBasePrivate { public: SlaveBase * const q; explicit SlaveBasePrivate(SlaveBase *owner) : q(owner) , nextTimeoutMsecs(0) , m_passwdServerClient(nullptr) , m_confirmationAsked(false) , m_privilegeOperationStatus(OperationNotAllowed) { if (!qEnvironmentVariableIsEmpty("KIOSLAVE_ENABLE_TESTMODE")) { QStandardPaths::setTestModeEnabled(true); } pendingListEntries.reserve(KIO_MAX_ENTRIES_PER_BATCH); appConnection.setReadMode(Connection::ReadMode::Polled); } ~SlaveBasePrivate() { delete m_passwdServerClient; } UDSEntryList pendingListEntries; QElapsedTimer m_timeSinceLastBatch; Connection appConnection; QString poolSocket; bool isConnectedToApp; QString slaveid; bool resume: 1; bool needSendCanResume: 1; bool onHold: 1; bool wasKilled: 1; bool inOpenLoop: 1; bool exit_loop: 1; MetaData configData; KConfig *config = nullptr; KConfigGroup *configGroup = nullptr; QMap mapConfig; QUrl onHoldUrl; QElapsedTimer lastTimeout; QElapsedTimer nextTimeout; qint64 nextTimeoutMsecs; KIO::filesize_t totalSize; KRemoteEncoding *remotefile = nullptr; enum { Idle, InsideMethod, FinishedCalled, ErrorCalled } m_state; bool m_finalityCommand = true; // whether finished() or error() may/must be called QByteArray timeoutData; KPasswdServerClient *m_passwdServerClient = nullptr; bool m_rootEntryListed = false; bool m_confirmationAsked; QSet m_tempAuths; QString m_warningCaption; QString m_warningMessage; int m_privilegeOperationStatus; void updateTempAuthStatus() { #ifdef Q_OS_UNIX QSet::iterator it = m_tempAuths.begin(); while (it != m_tempAuths.end()) { KAuth::Action action(*it); if (action.status() != KAuth::Action::AuthorizedStatus) { it = m_tempAuths.erase(it); } else { ++it; } } #endif } bool hasTempAuth() const { return !m_tempAuths.isEmpty(); } // Reconstructs configGroup from configData and mIncomingMetaData void rebuildConfig() { mapConfig.clear(); // mIncomingMetaData cascades over config, so we write config first, // to let it be overwritten MetaData::ConstIterator end = configData.constEnd(); for (MetaData::ConstIterator it = configData.constBegin(); it != end; ++it) { mapConfig.insert(it.key(), it->toUtf8()); } end = q->mIncomingMetaData.constEnd(); for (MetaData::ConstIterator it = q->mIncomingMetaData.constBegin(); it != end; ++it) { mapConfig.insert(it.key(), it->toUtf8()); } delete configGroup; configGroup = nullptr; delete config; config = nullptr; } bool finalState() const { return ((m_state == FinishedCalled) || (m_state == ErrorCalled)); } void verifyState(const char *cmdName) { KIO_STATE_ASSERT(finalState(), Q_FUNC_INFO, qUtf8Printable(QStringLiteral("%1 did not call finished() or error()! Please fix the %2 KIO slave") .arg(QLatin1String(cmdName)) .arg(QCoreApplication::applicationName()))); // Force the command into finished state. We'll not reach this for Debug builds // that fail the assertion. For Release builds we'll have made sure that the // command is actually finished after the verification regardless of what // the slave did. if (!finalState()) { q->finished(); } } void verifyErrorFinishedNotCalled(const char *cmdName) { KIO_STATE_ASSERT(!finalState(), Q_FUNC_INFO, qUtf8Printable(QStringLiteral("%1 called finished() or error(), but it's not supposed to! Please fix the %2 KIO slave") .arg(QLatin1String(cmdName)) .arg(QCoreApplication::applicationName()))); } KPasswdServerClient *passwdServerClient() { if (!m_passwdServerClient) { m_passwdServerClient = new KPasswdServerClient; } return m_passwdServerClient; } }; } static SlaveBase *globalSlave; static volatile bool slaveWriteError = false; static const char *s_protocol; #ifdef Q_OS_UNIX extern "C" { static void genericsig_handler(int sigNumber) { ::signal(sigNumber, SIG_IGN); //WABA: Don't do anything that requires malloc, we can deadlock on it since //a SIGTERM signal can come in while we are in malloc/free. //qDebug()<<"kioslave : exiting due to signal "<setKillFlag(); } ::signal(SIGALRM, SIG_DFL); alarm(5); //generate an alarm signal in 5 seconds, in this time the slave has to exit } } #endif ////////////// SlaveBase::SlaveBase(const QByteArray &protocol, const QByteArray &pool_socket, const QByteArray &app_socket) : mProtocol(protocol), d(new SlaveBasePrivate(this)) { Q_ASSERT(!app_socket.isEmpty()); d->poolSocket = QFile::decodeName(pool_socket); s_protocol = protocol.data(); KCrash::initialize(); #ifdef Q_OS_UNIX struct sigaction act; act.sa_handler = sigpipe_handler; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGPIPE, &act, nullptr); ::signal(SIGINT, &genericsig_handler); ::signal(SIGQUIT, &genericsig_handler); ::signal(SIGTERM, &genericsig_handler); #endif globalSlave = this; d->isConnectedToApp = true; // by kahl for netmgr (need a way to identify slaves) d->slaveid = QString::fromUtf8(protocol) + QString::number(getpid()); d->resume = false; d->needSendCanResume = false; d->mapConfig = QMap(); d->onHold = false; d->wasKilled = false; // d->processed_size = 0; d->totalSize = 0; connectSlave(QFile::decodeName(app_socket)); d->remotefile = nullptr; d->inOpenLoop = false; d->exit_loop = false; } SlaveBase::~SlaveBase() { delete d->configGroup; delete d->config; delete d->remotefile; delete d; s_protocol = ""; } void SlaveBase::dispatchLoop() { while (!d->exit_loop) { if (d->nextTimeout.isValid() && (d->nextTimeout.hasExpired(d->nextTimeoutMsecs))) { QByteArray data = d->timeoutData; d->nextTimeout.invalidate(); d->timeoutData = QByteArray(); special(data); } Q_ASSERT(d->appConnection.inited()); int ms = -1; if (d->nextTimeout.isValid()) { ms = qMax(d->nextTimeout.elapsed() - d->nextTimeoutMsecs, 1); } int ret = -1; if (d->appConnection.hasTaskAvailable() || d->appConnection.waitForIncomingTask(ms)) { // dispatch application messages int cmd; QByteArray data; ret = d->appConnection.read(&cmd, data); if (ret != -1) { if (d->inOpenLoop) { dispatchOpenCommand(cmd, data); } else { dispatch(cmd, data); } } } else { ret = d->appConnection.isConnected() ? 0 : -1; } if (ret == -1) { // some error occurred, perhaps no more application // When the app exits, should the slave be put back in the pool ? if (!d->exit_loop && d->isConnectedToApp && !d->poolSocket.isEmpty()) { disconnectSlave(); d->isConnectedToApp = false; closeConnection(); d->updateTempAuthStatus(); connectSlave(d->poolSocket); } else { break; } } //I think we get here when we were killed in dispatch() and not in select() if (wasKilled()) { //qDebug() << "slave was killed, returning"; break; } // execute deferred deletes QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); } // execute deferred deletes QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); } void SlaveBase::connectSlave(const QString &address) { d->appConnection.connectToRemote(QUrl(address)); if (!d->appConnection.inited()) { /*qDebug() << "failed to connect to" << address << endl << "Reason:" << d->appConnection.errorString();*/ exit(); } d->inOpenLoop = false; } void SlaveBase::disconnectSlave() { d->appConnection.close(); } void SlaveBase::setMetaData(const QString &key, const QString &value) { mOutgoingMetaData.insert(key, value); // replaces existing key if already there } QString SlaveBase::metaData(const QString &key) const { auto it = mIncomingMetaData.find(key); if (it != mIncomingMetaData.end()) { return *it; } return d->configData.value(key); } MetaData SlaveBase::allMetaData() const { return mIncomingMetaData; } bool SlaveBase::hasMetaData(const QString &key) const { if (mIncomingMetaData.contains(key)) { return true; } if (d->configData.contains(key)) { return true; } return false; } QMap SlaveBase::mapConfig() const { return d->mapConfig; } bool SlaveBase::configValue(const QString &key, bool defaultValue) const { return d->mapConfig.value(key, defaultValue).toBool(); } int SlaveBase::configValue(const QString &key, int defaultValue) const { return d->mapConfig.value(key, defaultValue).toInt(); } QString SlaveBase::configValue(const QString &key, const QString &defaultValue) const { return d->mapConfig.value(key, defaultValue).toString(); } KConfigGroup *SlaveBase::config() { if (!d->config) { d->config = new KConfig(QString(), KConfig::SimpleConfig); d->configGroup = new KConfigGroup(d->config, QString()); auto end = d->mapConfig.cend(); for (auto it = d->mapConfig.cbegin(); it != end; ++it) { d->configGroup->writeEntry(it.key(), it->toString().toUtf8(), KConfigGroup::WriteConfigFlags()); } } return d->configGroup; } void SlaveBase::sendMetaData() { sendAndKeepMetaData(); mOutgoingMetaData.clear(); } void SlaveBase::sendAndKeepMetaData() { if (!mOutgoingMetaData.isEmpty()) { KIO_DATA << mOutgoingMetaData; send(INF_META_DATA, data); } } KRemoteEncoding *SlaveBase::remoteEncoding() { if (d->remotefile) { return d->remotefile; } const QByteArray charset(metaData(QStringLiteral("Charset")).toLatin1()); return (d->remotefile = new KRemoteEncoding(charset.constData())); } void SlaveBase::data(const QByteArray &data) { sendMetaData(); send(MSG_DATA, data); } void SlaveBase::dataReq() { //sendMetaData(); if (d->needSendCanResume) { canResume(0); } send(MSG_DATA_REQ); } void SlaveBase::opened() { sendMetaData(); send(MSG_OPENED); d->inOpenLoop = true; } void SlaveBase::error(int _errid, const QString &_text) { KIO_STATE_ASSERT(d->m_finalityCommand, Q_FUNC_INFO, qUtf8Printable(QStringLiteral("error() was called, but it's not supposed to! Please fix the %1 KIO slave") .arg(QCoreApplication::applicationName()))); if (d->m_state == d->ErrorCalled) { KIO_STATE_ASSERT(false, Q_FUNC_INFO, qUtf8Printable(QStringLiteral("error() called twice! Please fix the %1 KIO slave") .arg(QCoreApplication::applicationName()))); return; } else if (d->m_state == d->FinishedCalled) { KIO_STATE_ASSERT(false, Q_FUNC_INFO, qUtf8Printable(QStringLiteral("error() called after finished()! Please fix the %1 KIO slave") .arg(QCoreApplication::applicationName()))); return; } d->m_state = d->ErrorCalled; mIncomingMetaData.clear(); // Clear meta data d->rebuildConfig(); mOutgoingMetaData.clear(); KIO_DATA << static_cast(_errid) << _text; send(MSG_ERROR, data); //reset d->totalSize = 0; d->inOpenLoop = false; d->m_confirmationAsked = false; d->m_privilegeOperationStatus = OperationNotAllowed; } void SlaveBase::connected() { send(MSG_CONNECTED); } void SlaveBase::finished() { if (!d->pendingListEntries.isEmpty()) { if (!d->m_rootEntryListed) { qCWarning(KIO_CORE) << "UDSEntry for '.' not found, creating a default one. Please fix the" << QCoreApplication::applicationName() << "KIO slave"; KIO::UDSEntry entry; entry.fastInsert(KIO::UDSEntry::UDS_NAME, QStringLiteral(".")); entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); entry.fastInsert(KIO::UDSEntry::UDS_SIZE, 0); entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH); d->pendingListEntries.append(entry); } listEntries(d->pendingListEntries); d->pendingListEntries.clear(); } KIO_STATE_ASSERT(d->m_finalityCommand, Q_FUNC_INFO, qUtf8Printable(QStringLiteral("finished() was called, but it's not supposed to! Please fix the %2 KIO slave") .arg(QCoreApplication::applicationName()))); if (d->m_state == d->FinishedCalled) { KIO_STATE_ASSERT(false, Q_FUNC_INFO, qUtf8Printable(QStringLiteral("finished() called twice! Please fix the %1 KIO slave") .arg(QCoreApplication::applicationName()))); return; } else if (d->m_state == d->ErrorCalled) { KIO_STATE_ASSERT(false, Q_FUNC_INFO, qUtf8Printable(QStringLiteral("finished() called after error()! Please fix the %1 KIO slave") .arg(QCoreApplication::applicationName()))); return; } d->m_state = d->FinishedCalled; mIncomingMetaData.clear(); // Clear meta data d->rebuildConfig(); sendMetaData(); send(MSG_FINISHED); // reset d->totalSize = 0; d->inOpenLoop = false; d->m_rootEntryListed = false; d->m_confirmationAsked = false; d->m_privilegeOperationStatus = OperationNotAllowed; } void SlaveBase::needSubUrlData() { send(MSG_NEED_SUBURL_DATA); } void SlaveBase::slaveStatus(const QString &host, bool connected) { qint64 pid = getpid(); qint8 b = connected ? 1 : 0; KIO_DATA << pid << mProtocol << host << b << d->onHold << d->onHoldUrl << d->hasTempAuth(); send(MSG_SLAVE_STATUS_V2, data); } void SlaveBase::canResume() { send(MSG_CANRESUME); } void SlaveBase::totalSize(KIO::filesize_t _bytes) { KIO_DATA << KIO_FILESIZE_T(_bytes); send(INF_TOTAL_SIZE, data); //this one is usually called before the first item is listed in listDir() d->totalSize = _bytes; } void SlaveBase::processedSize(KIO::filesize_t _bytes) { bool emitSignal = false; if (_bytes == d->totalSize) { emitSignal = true; } else { if (d->lastTimeout.isValid()) { emitSignal = d->lastTimeout.hasExpired(100); // emit size 10 times a second } else { emitSignal = true; } } if (emitSignal) { KIO_DATA << KIO_FILESIZE_T(_bytes); send(INF_PROCESSED_SIZE, data); d->lastTimeout.start(); } // d->processed_size = _bytes; } void SlaveBase::written(KIO::filesize_t _bytes) { KIO_DATA << KIO_FILESIZE_T(_bytes); send(MSG_WRITTEN, data); } void SlaveBase::position(KIO::filesize_t _pos) { KIO_DATA << KIO_FILESIZE_T(_pos); send(INF_POSITION, data); } +void SlaveBase::truncated(KIO::filesize_t _length) +{ + KIO_DATA << KIO_FILESIZE_T(_length); + send(INF_TRUNCATED, data); +} + void SlaveBase::processedPercent(float /* percent */) { //qDebug() << "STUB"; } void SlaveBase::speed(unsigned long _bytes_per_second) { KIO_DATA << static_cast(_bytes_per_second); send(INF_SPEED, data); } void SlaveBase::redirection(const QUrl &_url) { KIO_DATA << _url; send(INF_REDIRECTION, data); } void SlaveBase::errorPage() { send(INF_ERROR_PAGE); } static bool isSubCommand(int cmd) { return ((cmd == CMD_REPARSECONFIGURATION) || (cmd == CMD_META_DATA) || (cmd == CMD_CONFIG) || (cmd == CMD_SUBURL) || (cmd == CMD_SLAVE_STATUS) || (cmd == CMD_SLAVE_CONNECT) || (cmd == CMD_SLAVE_HOLD) || (cmd == CMD_MULTI_GET)); } void SlaveBase::mimeType(const QString &_type) { //qDebug() << _type; int cmd; do { // Send the meta-data each time we send the mime-type. if (!mOutgoingMetaData.isEmpty()) { //qDebug() << "emitting meta data"; KIO_DATA << mOutgoingMetaData; send(INF_META_DATA, data); } KIO_DATA << _type; send(INF_MIME_TYPE, data); while (true) { cmd = 0; int ret = -1; if (d->appConnection.hasTaskAvailable() || d->appConnection.waitForIncomingTask(-1)) { ret = d->appConnection.read(&cmd, data); } if (ret == -1) { //qDebug() << "read error"; exit(); } //qDebug() << "got" << cmd; if (cmd == CMD_HOST) { // Ignore. continue; } if (!isSubCommand(cmd)) { break; } dispatch(cmd, data); } } while (cmd != CMD_NONE); mOutgoingMetaData.clear(); } void SlaveBase::exit() { d->exit_loop = true; // Using ::exit() here is too much (crashes in qdbus's qglobalstatic object), // so let's cleanly exit dispatchLoop() instead. // Update: we do need to call exit(), otherwise a long download (get()) would // keep going until it ends, even though the application exited. ::exit(255); } void SlaveBase::warning(const QString &_msg) { KIO_DATA << _msg; send(INF_WARNING, data); } void SlaveBase::infoMessage(const QString &_msg) { KIO_DATA << _msg; send(INF_INFOMESSAGE, data); } bool SlaveBase::requestNetwork(const QString &host) { KIO_DATA << host << d->slaveid; send(MSG_NET_REQUEST, data); if (waitForAnswer(INF_NETWORK_STATUS, 0, data) != -1) { bool status; QDataStream stream(data); stream >> status; return status; } else { return false; } } void SlaveBase::dropNetwork(const QString &host) { KIO_DATA << host << d->slaveid; send(MSG_NET_DROP, data); } void SlaveBase::statEntry(const UDSEntry &entry) { KIO_DATA << entry; send(MSG_STAT_ENTRY, data); } void SlaveBase::listEntry(const UDSEntry &entry, bool _ready) { if (_ready) { // #366795: many slaves don't create an entry for ".", so we keep track if they do // and we provide a fallback in finished() otherwise. if (entry.stringValue(KIO::UDSEntry::UDS_NAME) == QLatin1Char('.')) { d->m_rootEntryListed = true; } listEntries(d->pendingListEntries); d->pendingListEntries.clear(); } else { listEntry(entry); } } void SlaveBase::listEntry(const UDSEntry &entry) { // #366795: many slaves don't create an entry for ".", so we keep track if they do // and we provide a fallback in finished() otherwise. if (entry.stringValue(KIO::UDSEntry::UDS_NAME) == QLatin1Char('.')) { d->m_rootEntryListed = true; } // We start measuring the time from the point we start filling the list if (d->pendingListEntries.isEmpty()) { d->m_timeSinceLastBatch.restart(); } d->pendingListEntries.append(entry); // If more then KIO_MAX_SEND_BATCH_TIME time is passed, emit the current batch // Also emit if we have piled up a large number of entries already, to save memory (and time) if (d->m_timeSinceLastBatch.elapsed() > KIO_MAX_SEND_BATCH_TIME || d->pendingListEntries.size() > KIO_MAX_ENTRIES_PER_BATCH) { listEntries(d->pendingListEntries); d->pendingListEntries.clear(); // Restart time d->m_timeSinceLastBatch.restart(); } } void SlaveBase::listEntries(const UDSEntryList &list) { QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); for (const UDSEntry &entry : list) { stream << entry; } send(MSG_LIST_ENTRIES, data); } static void sigpipe_handler(int) { // We ignore a SIGPIPE in slaves. // A SIGPIPE can happen in two cases: // 1) Communication error with application. // 2) Communication error with network. slaveWriteError = true; // Don't add anything else here, especially no debug output } void SlaveBase::setHost(QString const &, quint16, QString const &, QString const &) { } KIOCORE_EXPORT QString KIO::unsupportedActionErrorString(const QString &protocol, int cmd) { switch (cmd) { case CMD_CONNECT: return i18n("Opening connections is not supported with the protocol %1.", protocol); case CMD_DISCONNECT: return i18n("Closing connections is not supported with the protocol %1.", protocol); case CMD_STAT: return i18n("Accessing files is not supported with the protocol %1.", protocol); case CMD_PUT: return i18n("Writing to %1 is not supported.", protocol); case CMD_SPECIAL: return i18n("There are no special actions available for protocol %1.", protocol); case CMD_LISTDIR: return i18n("Listing folders is not supported for protocol %1.", protocol); case CMD_GET: return i18n("Retrieving data from %1 is not supported.", protocol); case CMD_MIMETYPE: return i18n("Retrieving mime type information from %1 is not supported.", protocol); case CMD_RENAME: return i18n("Renaming or moving files within %1 is not supported.", protocol); case CMD_SYMLINK: return i18n("Creating symlinks is not supported with protocol %1.", protocol); case CMD_COPY: return i18n("Copying files within %1 is not supported.", protocol); case CMD_DEL: return i18n("Deleting files from %1 is not supported.", protocol); case CMD_MKDIR: return i18n("Creating folders is not supported with protocol %1.", protocol); case CMD_CHMOD: return i18n("Changing the attributes of files is not supported with protocol %1.", protocol); case CMD_CHOWN: return i18n("Changing the ownership of files is not supported with protocol %1.", protocol); case CMD_SUBURL: return i18n("Using sub-URLs with %1 is not supported.", protocol); case CMD_MULTI_GET: return i18n("Multiple get is not supported with protocol %1.", protocol); case CMD_OPEN: return i18n("Opening files is not supported with protocol %1.", protocol); default: return i18n("Protocol %1 does not support action %2.", protocol, cmd); }/*end switch*/ } void SlaveBase::openConnection() { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_CONNECT)); } void SlaveBase::closeConnection() { } // No response! void SlaveBase::stat(QUrl const &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_STAT)); } void SlaveBase::put(QUrl const &, int, JobFlags) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_PUT)); } void SlaveBase::special(const QByteArray &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_SPECIAL)); } void SlaveBase::listDir(QUrl const &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_LISTDIR)); } void SlaveBase::get(QUrl const &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_GET)); } void SlaveBase::open(QUrl const &, QIODevice::OpenMode) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_OPEN)); } void SlaveBase::read(KIO::filesize_t) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_READ)); } void SlaveBase::write(const QByteArray &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_WRITE)); } void SlaveBase::seek(KIO::filesize_t) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_SEEK)); } void SlaveBase::close() { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_CLOSE)); } void SlaveBase::mimetype(QUrl const &url) { get(url); } void SlaveBase::rename(QUrl const &, QUrl const &, JobFlags) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_RENAME)); } void SlaveBase::symlink(QString const &, QUrl const &, JobFlags) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_SYMLINK)); } void SlaveBase::copy(QUrl const &, QUrl const &, int, JobFlags) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_COPY)); } void SlaveBase::del(QUrl const &, bool) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_DEL)); } void SlaveBase::setLinkDest(const QUrl &, const QString &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_SETLINKDEST)); } void SlaveBase::mkdir(QUrl const &, int) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_MKDIR)); } void SlaveBase::chmod(QUrl const &, int) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_CHMOD)); } void SlaveBase::setModificationTime(QUrl const &, const QDateTime &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_SETMODIFICATIONTIME)); } void SlaveBase::chown(QUrl const &, const QString &, const QString &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_CHOWN)); } void SlaveBase::setSubUrl(QUrl const &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_SUBURL)); } void SlaveBase::multiGet(const QByteArray &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_MULTI_GET)); } void SlaveBase::slave_status() { slaveStatus(QString(), false); } void SlaveBase::reparseConfiguration() { delete d->remotefile; d->remotefile = nullptr; } bool SlaveBase::openPasswordDialog(AuthInfo &info, const QString &errorMsg) { const int errorCode = openPasswordDialogV2(info, errorMsg); return errorCode == KJob::NoError; } int SlaveBase::openPasswordDialogV2(AuthInfo &info, const QString &errorMsg) { const long windowId = metaData(QStringLiteral("window-id")).toLong(); const unsigned long userTimestamp = metaData(QStringLiteral("user-timestamp")).toULong(); QString errorMessage; if (metaData(QStringLiteral("no-auth-prompt")).compare(QLatin1String("true"), Qt::CaseInsensitive) == 0) { errorMessage = QStringLiteral(""); } else { errorMessage = errorMsg; } AuthInfo dlgInfo(info); // Make sure the modified flag is not set. dlgInfo.setModified(false); // Prevent queryAuthInfo from caching the user supplied password since // we need the ioslaves to first authenticate against the server with // it to ensure it is valid. dlgInfo.setExtraField(QStringLiteral("skip-caching-on-query"), true); KPasswdServerClient *passwdServerClient = d->passwdServerClient(); const int errCode = passwdServerClient->queryAuthInfo(&dlgInfo, errorMessage, windowId, userTimestamp); if (errCode == KJob::NoError) { info = dlgInfo; } return errCode; } int SlaveBase::messageBox(MessageBoxType type, const QString &text, const QString &caption, const QString &buttonYes, const QString &buttonNo) { return messageBox(text, type, caption, buttonYes, buttonNo, QString()); } int SlaveBase::messageBox(const QString &text, MessageBoxType type, const QString &caption, const QString &_buttonYes, const QString &_buttonNo, const QString &dontAskAgainName) { QString buttonYes = _buttonYes.isNull() ? i18n("&Yes") : _buttonYes; QString buttonNo = _buttonNo.isNull() ? i18n("&No") : _buttonNo; //qDebug() << "messageBox " << type << " " << text << " - " << caption << buttonYes << buttonNo; KIO_DATA << static_cast(type) << text << caption << buttonYes << buttonNo << dontAskAgainName; send(INF_MESSAGEBOX, data); if (waitForAnswer(CMD_MESSAGEBOXANSWER, 0, data) != -1) { QDataStream stream(data); int answer; stream >> answer; //qDebug() << "got messagebox answer" << answer; return answer; } else { return 0; // communication failure } } bool SlaveBase::canResume(KIO::filesize_t offset) { //qDebug() << "offset=" << KIO::number(offset); d->needSendCanResume = false; KIO_DATA << KIO_FILESIZE_T(offset); send(MSG_RESUME, data); if (offset) { int cmd; if (waitForAnswer(CMD_RESUMEANSWER, CMD_NONE, data, &cmd) != -1) { //qDebug() << "returning" << (cmd == CMD_RESUMEANSWER); return cmd == CMD_RESUMEANSWER; } else { return false; } } else { // No resuming possible -> no answer to wait for return true; } } int SlaveBase::waitForAnswer(int expected1, int expected2, QByteArray &data, int *pCmd) { int cmd = 0; int result = -1; for (;;) { if (d->appConnection.hasTaskAvailable() || d->appConnection.waitForIncomingTask(-1)) { result = d->appConnection.read(&cmd, data); } if (result == -1) { //qDebug() << "read error."; return -1; } if (cmd == expected1 || cmd == expected2) { if (pCmd) { *pCmd = cmd; } return result; } if (isSubCommand(cmd)) { dispatch(cmd, data); } else { qFatal("Fatal Error: Got cmd %d, while waiting for an answer!", cmd); } } } int SlaveBase::readData(QByteArray &buffer) { int result = waitForAnswer(MSG_DATA, 0, buffer); //qDebug() << "readData: length = " << result << " "; return result; } void SlaveBase::setTimeoutSpecialCommand(int timeout, const QByteArray &data) { if (timeout > 0) { d->nextTimeoutMsecs = timeout*1000; // from seconds to milliseconds d->nextTimeout.start(); } else if (timeout == 0) { d->nextTimeoutMsecs = 1000; // Immediate timeout d->nextTimeout.start(); } else { d->nextTimeout.invalidate(); // Canceled } d->timeoutData = data; } void SlaveBase::dispatch(int command, const QByteArray &data) { QDataStream stream(data); QUrl url; int i; d->m_finalityCommand = true; // default switch (command) { case CMD_HOST: { QString passwd; QString host, user; quint16 port; stream >> host >> port >> user >> passwd; d->m_state = d->InsideMethod; d->m_finalityCommand = false; setHost(host, port, user, passwd); d->m_state = d->Idle; } break; case CMD_CONNECT: { openConnection(); } break; case CMD_DISCONNECT: { closeConnection(); } break; case CMD_SLAVE_STATUS: { d->m_state = d->InsideMethod; d->m_finalityCommand = false; slave_status(); // TODO verify that the slave has called slaveStatus()? d->m_state = d->Idle; } break; case CMD_SLAVE_CONNECT: { d->onHold = false; QString app_socket; QDataStream stream(data); stream >> app_socket; d->appConnection.send(MSG_SLAVE_ACK); disconnectSlave(); d->isConnectedToApp = true; connectSlave(app_socket); virtual_hook(AppConnectionMade, nullptr); } break; case CMD_SLAVE_HOLD: { QUrl url; QDataStream stream(data); stream >> url; d->onHoldUrl = url; d->onHold = true; disconnectSlave(); d->isConnectedToApp = false; // Do not close connection! connectSlave(d->poolSocket); } break; case CMD_REPARSECONFIGURATION: { d->m_state = d->InsideMethod; d->m_finalityCommand = false; reparseConfiguration(); d->m_state = d->Idle; } break; case CMD_CONFIG: { stream >> d->configData; d->rebuildConfig(); delete d->remotefile; d->remotefile = nullptr; } break; case CMD_GET: { stream >> url; d->m_state = d->InsideMethod; get(url); d->verifyState("get()"); d->m_state = d->Idle; } break; case CMD_OPEN: { stream >> url >> i; QIODevice::OpenMode mode = QFlag(i); d->m_state = d->InsideMethod; open(url, mode); //krazy:exclude=syscalls d->m_state = d->Idle; } break; case CMD_PUT: { int permissions; qint8 iOverwrite, iResume; stream >> url >> iOverwrite >> iResume >> permissions; JobFlags flags; if (iOverwrite != 0) { flags |= Overwrite; } if (iResume != 0) { flags |= Resume; } // Remember that we need to send canResume(), TransferJob is expecting // it. Well, in theory this shouldn't be done if resume is true. // (the resume bool is currently unused) d->needSendCanResume = true /* !resume */; d->m_state = d->InsideMethod; put(url, permissions, flags); d->verifyState("put()"); d->m_state = d->Idle; } break; case CMD_STAT: { stream >> url; d->m_state = d->InsideMethod; stat(url); //krazy:exclude=syscalls d->verifyState("stat()"); d->m_state = d->Idle; } break; case CMD_MIMETYPE: { stream >> url; d->m_state = d->InsideMethod; mimetype(url); d->verifyState("mimetype()"); d->m_state = d->Idle; } break; case CMD_LISTDIR: { stream >> url; d->m_state = d->InsideMethod; listDir(url); d->verifyState("listDir()"); d->m_state = d->Idle; } break; case CMD_MKDIR: { stream >> url >> i; d->m_state = d->InsideMethod; mkdir(url, i); //krazy:exclude=syscalls d->verifyState("mkdir()"); d->m_state = d->Idle; } break; case CMD_RENAME: { qint8 iOverwrite; QUrl url2; stream >> url >> url2 >> iOverwrite; JobFlags flags; if (iOverwrite != 0) { flags |= Overwrite; } d->m_state = d->InsideMethod; rename(url, url2, flags); //krazy:exclude=syscalls d->verifyState("rename()"); d->m_state = d->Idle; } break; case CMD_SYMLINK: { qint8 iOverwrite; QString target; stream >> target >> url >> iOverwrite; JobFlags flags; if (iOverwrite != 0) { flags |= Overwrite; } d->m_state = d->InsideMethod; symlink(target, url, flags); d->verifyState("symlink()"); d->m_state = d->Idle; } break; case CMD_COPY: { int permissions; qint8 iOverwrite; QUrl url2; stream >> url >> url2 >> permissions >> iOverwrite; JobFlags flags; if (iOverwrite != 0) { flags |= Overwrite; } d->m_state = d->InsideMethod; copy(url, url2, permissions, flags); d->verifyState("copy()"); d->m_state = d->Idle; } break; case CMD_DEL: { qint8 isFile; stream >> url >> isFile; d->m_state = d->InsideMethod; del(url, isFile != 0); d->verifyState("del()"); d->m_state = d->Idle; } break; case CMD_CHMOD: { stream >> url >> i; d->m_state = d->InsideMethod; chmod(url, i); d->verifyState("chmod()"); d->m_state = d->Idle; } break; case CMD_CHOWN: { QString owner, group; stream >> url >> owner >> group; d->m_state = d->InsideMethod; chown(url, owner, group); d->verifyState("chown()"); d->m_state = d->Idle; } break; case CMD_SETMODIFICATIONTIME: { QDateTime dt; stream >> url >> dt; d->m_state = d->InsideMethod; setModificationTime(url, dt); d->verifyState("setModificationTime()"); d->m_state = d->Idle; } break; case CMD_SPECIAL: { d->m_state = d->InsideMethod; special(data); d->verifyState("special()"); d->m_state = d->Idle; } break; case CMD_META_DATA: { //qDebug() << "(" << getpid() << ") Incoming meta-data..."; stream >> mIncomingMetaData; d->rebuildConfig(); } break; case CMD_SUBURL: { stream >> url; d->m_state = d->InsideMethod; setSubUrl(url); d->verifyErrorFinishedNotCalled("setSubUrl()"); d->m_state = d->Idle; } break; case CMD_NONE: { qCWarning(KIO_CORE) << "Got unexpected CMD_NONE!"; } break; case CMD_MULTI_GET: { d->m_state = d->InsideMethod; multiGet(data); d->verifyState("multiGet()"); d->m_state = d->Idle; } break; case CMD_FILESYSTEMFREESPACE: { stream >> url; void *data = static_cast(&url); d->m_state = d->InsideMethod; virtual_hook(GetFileSystemFreeSpace, data); d->verifyState("fileSystemFreeSpace()"); d->m_state = d->Idle; } break; default: { // Some command we don't understand. // Just ignore it, it may come from some future version of KIO. } break; } } bool SlaveBase::checkCachedAuthentication(AuthInfo &info) { KPasswdServerClient *passwdServerClient = d->passwdServerClient(); return (passwdServerClient->checkAuthInfo(&info, metaData(QStringLiteral("window-id")).toLong(), metaData(QStringLiteral("user-timestamp")).toULong())); } void SlaveBase::dispatchOpenCommand(int command, const QByteArray &data) { QDataStream stream(data); switch (command) { case CMD_READ: { KIO::filesize_t bytes; stream >> bytes; read(bytes); break; } case CMD_WRITE: { write(data); break; } case CMD_SEEK: { KIO::filesize_t offset; stream >> offset; seek(offset); break; } + case CMD_TRUNCATE: { + KIO::filesize_t length; + stream >> length; + void *data = static_cast(&length); + virtual_hook(Truncate, data); + break; + } case CMD_NONE: break; case CMD_CLOSE: close(); // must call finish(), which will set d->inOpenLoop=false break; default: // Some command we don't understand. // Just ignore it, it may come from some future version of KIO. break; } } bool SlaveBase::cacheAuthentication(const AuthInfo &info) { KPasswdServerClient *passwdServerClient = d->passwdServerClient(); passwdServerClient->addAuthInfo(info, metaData(QStringLiteral("window-id")).toLongLong()); return true; } int SlaveBase::connectTimeout() { bool ok; QString tmp = metaData(QStringLiteral("ConnectTimeout")); int result = tmp.toInt(&ok); if (ok) { return result; } return DEFAULT_CONNECT_TIMEOUT; } int SlaveBase::proxyConnectTimeout() { bool ok; QString tmp = metaData(QStringLiteral("ProxyConnectTimeout")); int result = tmp.toInt(&ok); if (ok) { return result; } return DEFAULT_PROXY_CONNECT_TIMEOUT; } int SlaveBase::responseTimeout() { bool ok; QString tmp = metaData(QStringLiteral("ResponseTimeout")); int result = tmp.toInt(&ok); if (ok) { return result; } return DEFAULT_RESPONSE_TIMEOUT; } int SlaveBase::readTimeout() { bool ok; QString tmp = metaData(QStringLiteral("ReadTimeout")); int result = tmp.toInt(&ok); if (ok) { return result; } return DEFAULT_READ_TIMEOUT; } bool SlaveBase::wasKilled() const { return d->wasKilled; } void SlaveBase::setKillFlag() { d->wasKilled = true; } void SlaveBase::send(int cmd, const QByteArray &arr) { slaveWriteError = false; if (!d->appConnection.send(cmd, arr)) // Note that slaveWriteError can also be set by sigpipe_handler { slaveWriteError = true; } if (slaveWriteError) { exit(); } } void SlaveBase::virtual_hook(int id, void *data) { Q_UNUSED(data); switch(id) { case GetFileSystemFreeSpace: { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_FILESYSTEMFREESPACE)); } break; + case Truncate: { + error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(protocolName(), CMD_TRUNCATE)); + } break; } } void SlaveBase::lookupHost(const QString &host) { KIO_DATA << host; send(MSG_HOST_INFO_REQ, data); } int SlaveBase::waitForHostInfo(QHostInfo &info) { QByteArray data; int result = waitForAnswer(CMD_HOST_INFO, 0, data); if (result == -1) { info.setError(QHostInfo::UnknownError); info.setErrorString(i18n("Unknown Error")); return result; } QDataStream stream(data); QString hostName; QList addresses; int error; QString errorString; stream >> hostName >> addresses >> error >> errorString; info.setHostName(hostName); info.setAddresses(addresses); info.setError(QHostInfo::HostInfoError(error)); info.setErrorString(errorString); return result; } PrivilegeOperationStatus SlaveBase::requestPrivilegeOperation(const QString &operationDetails) { if (d->m_privilegeOperationStatus == OperationNotAllowed) { QByteArray buffer; send(MSG_PRIVILEGE_EXEC); waitForAnswer(MSG_PRIVILEGE_EXEC, 0, buffer); QDataStream ds(buffer); ds >> d->m_privilegeOperationStatus >> d->m_warningCaption >> d-> m_warningMessage; } if (metaData(QStringLiteral("UnitTesting")) != QLatin1String("true") && d->m_privilegeOperationStatus == OperationAllowed && !d->m_confirmationAsked) { //KF6 TODO Remove. We don't want to pass details as meta-data. Pass it as a parameter in messageBox(). setMetaData(QStringLiteral("privilege_conf_details"), operationDetails); sendMetaData(); int result = messageBox(d->m_warningMessage, WarningContinueCancelDetailed, d->m_warningCaption, QString(), QString(), QString()); d->m_privilegeOperationStatus = result == Continue ? OperationAllowed : OperationCanceled; d->m_confirmationAsked = true; } return KIO::PrivilegeOperationStatus(d->m_privilegeOperationStatus); } void SlaveBase::addTemporaryAuthorization(const QString &action) { d->m_tempAuths.insert(action); } #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 66) PrivilegeOperationStatus SlaveBase::requestPrivilegeOperation() { return KIO::OperationNotAllowed; } #endif diff --git a/src/core/slavebase.h b/src/core/slavebase.h index 9c7a919c..d7033fce 100644 --- a/src/core/slavebase.h +++ b/src/core/slavebase.h @@ -1,1072 +1,1078 @@ /* Copyright (C) 2000 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef SLAVEBASE_H #define SLAVEBASE_H #include #include #include #include "job_base.h" // for KIO::JobFlags #include #include class KConfigGroup; class KRemoteEncoding; class QUrl; namespace KIO { class Connection; class SlaveBasePrivate; /** * @class KIO::SlaveBase slavebase.h * * There are two classes that specifies the protocol between application (job) * and kioslave. SlaveInterface is the class to use on the application end, * SlaveBase is the one to use on the slave end. * * Slave implementations should simply inherit SlaveBase * * A call to foo() results in a call to slotFoo() on the other end. * * Note that a kioslave doesn't have a Qt event loop. When idle, it's waiting for a command * on the socket that connects it to the application. So don't expect a kioslave to react * to D-Bus signals for instance. KIOSlaves are short-lived anyway, so any kind of watching * or listening for notifications should be done elsewhere, for instance in a kded module * (see kio_desktop's desktopnotifier.cpp for an example). * * If a kioslave needs a Qt event loop within the implementation of one method, e.g. to * wait for an asynchronous operation to finish, that is possible, using QEventLoop. */ class KIOCORE_EXPORT SlaveBase { public: SlaveBase(const QByteArray &protocol, const QByteArray &pool_socket, const QByteArray &app_socket); virtual ~SlaveBase(); /** * @internal * Terminate the slave by calling the destructor and then ::exit() */ Q_NORETURN void exit(); /** * @internal */ void dispatchLoop(); /////////// // Message Signals to send to the job /////////// /** * Sends data in the slave to the job (i.e. in get). * * To signal end of data, simply send an empty * QByteArray(). * * @param data the data read by the slave */ void data(const QByteArray &data); /** * Asks for data from the job. * @see readData */ void dataReq(); /** * open succeeds * @see open() */ void opened(); /** * Call to signal an error. * This also finishes the job, so you must not call * finished() after calling this. * * If the error code is KIO::ERR_SLAVE_DEFINED then the * _text should contain the complete translated text of * of the error message. * * For all other error codes, _text should match the corresponding * error code. Usually, _text is a file or host name, or the error which * was passed from the server.
* For example, for KIO::ERR_DOES_NOT_EXIST, _text may only * be the file or folder which does not exist, nothing else. Otherwise, * this would break error strings generated by KIO::buildErrorString().
* If you have to add more details than what the standard error codes * provide, you'll need to use KIO::ERR_SLAVE_DEFINED. * For a complete list of what _text should contain for each error code, * look at the source of KIO::buildErrorString(). * * You can add rich text markup to the message, the places where the * error message will be displayed are rich text aware. * * @see KIO::Error * @see KIO::buildErrorString * @param _errid the error code from KIO::Error * @param _text the rich text error message */ void error(int _errid, const QString &_text); /** * Call in openConnection, if you reimplement it, when you're done. */ void connected(); /** * Call to signal successful completion of any command * besides openConnection and closeConnection. Do not * call this after calling error(). */ void finished(); /** * Call to signal that data from the sub-URL is needed */ void needSubUrlData(); /** * Used to report the status of the slave. * @param host the slave is currently connected to. (Should be * empty if not connected) * @param connected Whether an actual network connection exists. **/ void slaveStatus(const QString &host, bool connected); /** * Call this from stat() to express details about an object, the * UDSEntry customarily contains the atoms describing file name, size, * mimetype, etc. * @param _entry The UDSEntry containing all of the object attributes. */ void statEntry(const UDSEntry &_entry); /** * Call this in listDir, each time you have a bunch of entries * to report. * @param _entry The UDSEntry containing all of the object attributes. */ void listEntries(const UDSEntryList &_entry); /** * Call this at the beginning of put(), to give the size of the existing * partial file, if there is one. The @p offset argument notifies the * other job (the one that gets the data) about the offset to use. * In this case, the boolean returns whether we can indeed resume or not * (we can't if the protocol doing the get() doesn't support setting an offset) */ bool canResume(KIO::filesize_t offset); /** * Call this at the beginning of get(), if the "range-start" metadata was set * and returning byte ranges is implemented by this protocol. */ void canResume(); /////////// // Info Signals to send to the job /////////// /** * Call this in get and copy, to give the total size * of the file. */ void totalSize(KIO::filesize_t _bytes); /** * Call this during get and copy, once in a while, * to give some info about the current state. * Don't emit it in listDir, listEntries speaks for itself. */ void processedSize(KIO::filesize_t _bytes); void position(KIO::filesize_t _pos); void written(KIO::filesize_t _bytes); + /** + * @since 5.66 + */ + void truncated(KIO::filesize_t _length); + /** * Only use this if you can't know in advance the size of the * copied data. For example, if you're doing variable bitrate * compression of the source. * * STUB ! Currently unimplemented. Here now for binary compatibility. * * Call this during get and copy, once in a while, * to give some info about the current state. * Don't emit it in listDir, listEntries speaks for itself. */ void processedPercent(float percent); /** * Call this in get and copy, to give the current transfer * speed, but only if it can't be calculated out of the size you * passed to processedSize (in most cases you don't want to call it) */ void speed(unsigned long _bytes_per_second); /** * Call this to signal a redirection * The job will take care of going to that url. */ void redirection(const QUrl &_url); /** * Tell that we will only get an error page here. * This means: the data you'll get isn't the data you requested, * but an error page (usually HTML) that describes an error. */ void errorPage(); /** * Call this in mimetype() and in get(), when you know the mimetype. * See mimetype about other ways to implement it. */ void mimeType(const QString &_type); /** * Call to signal a warning, to be displayed in a dialog box. */ void warning(const QString &msg); /** * Call to signal a message, to be displayed if the application wants to, * for instance in a status bar. Usual examples are "connecting to host xyz", etc. */ void infoMessage(const QString &msg); /** * Type of message box. Should be kept in sync with KMessageBox::DialogType. */ enum MessageBoxType { QuestionYesNo = 1, WarningYesNo = 2, WarningContinueCancel = 3, WarningYesNoCancel = 4, Information = 5, SSLMessageBox = 6, //In KMessageBox::DialogType; Sorry = 7, Error = 8, QuestionYesNoCancel = 9 WarningContinueCancelDetailed = 10, }; /** * Button codes. Should be kept in sync with KMessageBox::ButtonCode */ enum ButtonCode { Ok = 1, Cancel = 2, Yes = 3, No = 4, Continue = 5 }; /** * Call this to show a message box from the slave * @param type type of message box: QuestionYesNo, WarningYesNo, WarningContinueCancel... * @param text Message string. May contain newlines. * @param caption Message box title. * @param buttonYes The text for the first button. * The default is i18n("&Yes"). * @param buttonNo The text for the second button. * The default is i18n("&No"). * Note: for ContinueCancel, buttonYes is the continue button and buttonNo is unused. * and for Information, none is used. * @return a button code, as defined in ButtonCode, or 0 on communication error. */ int messageBox(MessageBoxType type, const QString &text, const QString &caption = QString(), const QString &buttonYes = QString(), const QString &buttonNo = QString()); /** * Call this to show a message box from the slave * @param text Message string. May contain newlines. * @param type type of message box: QuestionYesNo, WarningYesNo, WarningContinueCancel... * @param caption Message box title. * @param buttonYes The text for the first button. * The default is i18n("&Yes"). * @param buttonNo The text for the second button. * The default is i18n("&No"). * Note: for ContinueCancel, buttonYes is the continue button and buttonNo is unused. * and for Information, none is used. * @param dontAskAgainName the name used to store result from 'Do not ask again' checkbox. * @return a button code, as defined in ButtonCode, or 0 on communication error. */ int messageBox(const QString &text, MessageBoxType type, const QString &caption = QString(), const QString &buttonYes = QString(), const QString &buttonNo = QString(), const QString &dontAskAgainName = QString()); /** * Sets meta-data to be send to the application before the first * data() or finished() signal. */ void setMetaData(const QString &key, const QString &value); /** * Queries for the existence of a certain config/meta-data entry * send by the application to the slave. */ bool hasMetaData(const QString &key) const; /** * Queries for config/meta-data send by the application to the slave. */ QString metaData(const QString &key) const; /** * @internal for ForwardingSlaveBase * Contains all metadata (but no config) sent by the application to the slave. */ MetaData allMetaData() const; /** * Returns a map to query config/meta-data information from. * * The application provides the slave with all configuration information * relevant for the current protocol and host. * * Use configValue() as shortcut. * @since 5.64 */ QMap mapConfig() const; /** * Returns a bool from the config/meta-data information. * @since 5.64 */ bool configValue(const QString &key, bool defaultValue) const; /** * Returns an int from the config/meta-data information. * @since 5.64 */ int configValue(const QString &key, int defaultValue) const; /** * Returns a QString from the config/meta-data information. * @since 5.64 */ QString configValue(const QString &key, const QString &defaultValue = QString()) const; #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 64) /** * Returns a configuration object to query config/meta-data information * from. * * The application provides the slave with all configuration information * relevant for the current protocol and host. * * @deprecated since 5.64 use mapConfig instead */ KIOCORE_DEPRECATED_VERSION(5, 64, "Use SlaveBase::mapConfig()") KConfigGroup *config(); // KF6: perhaps rename mapConfig() to config() when removing this #endif /** * Returns an object that can translate remote filenames into proper * Unicode forms. This encoding can be set by the user. */ KRemoteEncoding *remoteEncoding(); /////////// // Commands sent by the job, the slave has to // override what it wants to implement /////////// /** * Set the host * * Called directly by createSlave, this is why there is no equivalent in * SlaveInterface, unlike the other methods. * * This method is called whenever a change in host, port or user occurs. */ virtual void setHost(const QString &host, quint16 port, const QString &user, const QString &pass); /** * Prepare slave for streaming operation */ virtual void setSubUrl(const QUrl &url); /** * Opens the connection (forced) * When this function gets called the slave is operating in * connection-oriented mode. * When a connection gets lost while the slave operates in * connection oriented mode, the slave should report * ERR_CONNECTION_BROKEN instead of reconnecting. The user is * expected to disconnect the slave in the error handler. */ virtual void openConnection(); /** * Closes the connection (forced) * Called when the application disconnects the slave to close * any open network connections. * * When the slave was operating in connection-oriented mode, * it should reset itself to connectionless (default) mode. */ virtual void closeConnection(); /** * get, aka read. * @param url the full url for this request. Host, port and user of the URL * can be assumed to be the same as in the last setHost() call. * * The slave should first "emit" the mimetype by calling mimeType(), * and then "emit" the data using the data() method. * * The reason why we need get() to emit the mimetype is: * when pasting a URL in krunner, or konqueror's location bar, * we have to find out what is the mimetype of that URL. * Rather than doing it with a call to mimetype(), then the app or part * would have to do a second request to the same server, this is done * like this: get() is called, and when it emits the mimetype, the job * is put on hold and the right app or part is launched. When that app * or part calls get(), the slave is magically reused, and the download * can now happen. All with a single call to get() in the slave. * This mechanism is also described in KIO::get(). */ virtual void get(const QUrl &url); /** * open. * @param url the full url for this request. Host, port and user of the URL * can be assumed to be the same as in the last setHost() call. * @param mode see \ref QIODevice::OpenMode */ virtual void open(const QUrl &url, QIODevice::OpenMode mode); /** * read. * @param size the requested amount of data to read * @see KIO::FileJob::read() */ virtual void read(KIO::filesize_t size); /** * write. * @param data the data to write * @see KIO::FileJob::write() */ virtual void write(const QByteArray &data); /** * seek. * @param offset the requested amount of data to read * @see KIO::FileJob::read() */ virtual void seek(KIO::filesize_t offset); /** * close. * @see KIO::FileJob::close() */ virtual void close(); /** * put, i.e. write data into a file. * * @param url where to write the file * @param permissions may be -1. In this case no special permission mode is set. * @param flags We support Overwrite here. Hopefully, we're going to * support Resume in the future, too. * If the file indeed already exists, the slave should NOT apply the * permissions change to it. * The support for resuming using .part files is done by calling canResume(). * * IMPORTANT: Use the "modified" metadata in order to set the modification time of the file. * * @see canResume() */ virtual void put(const QUrl &url, int permissions, JobFlags flags); /** * Finds all details for one file or directory. * The information returned is the same as what listDir returns, * but only for one file or directory. * Call statEntry() after creating the appropriate UDSEntry for this * url. * * You can use the "details" metadata to optimize this method to only * do as much work as needed by the application. * By default details is 2 (all details wanted, including modification time, size, etc.), * details==1 is used when deleting: we don't need all the information if it takes * too much time, no need to follow symlinks etc. * details==0 is used for very simple probing: we'll only get the answer * "it's a file or a directory (or a symlink), or it doesn't exist". */ virtual void stat(const QUrl &url); /** * Finds mimetype for one file or directory. * * This method should either emit 'mimeType' or it * should send a block of data big enough to be able * to determine the mimetype. * * If the slave doesn't reimplement it, a get will * be issued, i.e. the whole file will be downloaded before * determining the mimetype on it - this is obviously not a * good thing in most cases. */ virtual void mimetype(const QUrl &url); /** * Lists the contents of @p url. * The slave should emit ERR_CANNOT_ENTER_DIRECTORY if it doesn't exist, * if we don't have enough permissions. * It should also emit totalFiles as soon as it knows how many * files it will list. * You should not list files if the path in @p url is empty, but redirect * to a non-empty path instead. */ virtual void listDir(const QUrl &url); /** * Create a directory * @param url path to the directory to create * @param permissions the permissions to set after creating the directory * (-1 if no permissions to be set) * The slave emits ERR_CANNOT_MKDIR if failure. */ virtual void mkdir(const QUrl &url, int permissions); /** * Rename @p oldname into @p newname. * If the slave returns an error ERR_UNSUPPORTED_ACTION, the job will * ask for copy + del instead. * * Important: the slave must implement the logic "if the destination already * exists, error ERR_DIR_ALREADY_EXIST or ERR_FILE_ALREADY_EXIST". * For performance reasons no stat is done in the destination before hand, * the slave must do it. * * By default, rename() is only called when renaming (moving) from * yourproto://host/path to yourproto://host/otherpath. * * If you set renameFromFile=true then rename() will also be called when * moving a file from file:///path to yourproto://host/otherpath. * Otherwise such a move would have to be done the slow way (copy+delete). * See KProtocolManager::canRenameFromFile() for more details. * * If you set renameToFile=true then rename() will also be called when * moving a file from yourproto: to file:. * See KProtocolManager::canRenameToFile() for more details. * * @param src where to move the file from * @param dest where to move the file to * @param flags We support Overwrite here */ virtual void rename(const QUrl &src, const QUrl &dest, JobFlags flags); /** * Creates a symbolic link named @p dest, pointing to @p target, which * may be a relative or an absolute path. * @param target The string that will become the "target" of the link (can be relative) * @param dest The symlink to create. * @param flags We support Overwrite here */ virtual void symlink(const QString &target, const QUrl &dest, JobFlags flags); /** * Change permissions on @p url * The slave emits ERR_DOES_NOT_EXIST or ERR_CANNOT_CHMOD */ virtual void chmod(const QUrl &url, int permissions); /** * Change ownership of @p url * The slave emits ERR_DOES_NOT_EXIST or ERR_CANNOT_CHOWN */ virtual void chown(const QUrl &url, const QString &owner, const QString &group); /** * Sets the modification time for @url * For instance this is what CopyJob uses to set mtime on dirs at the end of a copy. * It could also be used to set the mtime on any file, in theory. * The usual implementation on unix is to call utime(path, &myutimbuf). * The slave emits ERR_DOES_NOT_EXIST or ERR_CANNOT_SETTIME */ virtual void setModificationTime(const QUrl &url, const QDateTime &mtime); /** * Copy @p src into @p dest. * * By default, copy() is only called when copying a file from * yourproto://host/path to yourproto://host/otherpath. * * If you set copyFromFile=true then copy() will also be called when * moving a file from file:///path to yourproto://host/otherpath. * Otherwise such a copy would have to be done the slow way (get+put). * See also KProtocolManager::canCopyFromFile(). * * If you set copyToFile=true then copy() will also be called when * moving a file from yourproto: to file:. * See also KProtocolManager::canCopyToFile(). * * If the slave returns an error ERR_UNSUPPORTED_ACTION, the job will * ask for get + put instead. * @param src where to copy the file from (decoded) * @param dest where to copy the file to (decoded) * @param permissions may be -1. In this case no special permission mode is set. * @param flags We support Overwrite here * * Don't forget to set the modification time of @p dest to be the modification time of @p src. */ virtual void copy(const QUrl &src, const QUrl &dest, int permissions, JobFlags flags); /** * Delete a file or directory. * @param url file/directory to delete * @param isfile if true, a file should be deleted. * if false, a directory should be deleted. * * By default, del() on a directory should FAIL if the directory is not empty. * However, if metadata("recurse") == "true", then the slave can do a recursive deletion. * This behavior is only invoked if the slave specifies deleteRecursive=true in its protocol file. */ virtual void del(const QUrl &url, bool isfile); /** * Change the destination of a symlink * @param url the url of the symlink to modify * @param target the new destination (target) of the symlink */ virtual void setLinkDest(const QUrl &url, const QString &target); /** * Used for any command that is specific to this slave (protocol) * Examples are : HTTP POST, mount and unmount (kio_file) * * @param data packed data; the meaning is completely dependent on the * slave, but usually starts with an int for the command number. * Document your slave's commands, at least in its header file. */ virtual void special(const QByteArray &data); /** * Used for multiple get. Currently only used for HTTP pipelining * support. * * @param data packed data; Contains number of URLs to fetch, and for * each URL the URL itself and its associated MetaData. */ virtual void multiGet(const QByteArray &data); /** * Called to get the status of the slave. Slave should respond * by calling slaveStatus(...) */ virtual void slave_status(); /** * Called by the scheduler to tell the slave that the configuration * changed (i.e. proxy settings) . */ virtual void reparseConfiguration(); /** * @return timeout value for connecting to remote host. */ int connectTimeout(); /** * @return timeout value for connecting to proxy in secs. */ int proxyConnectTimeout(); /** * @return timeout value for read from first data from * remote host in seconds. */ int responseTimeout(); /** * @return timeout value for read from subsequent data from * remote host in secs. */ int readTimeout(); /** * This function sets a timeout of @p timeout seconds and calls * special(data) when the timeout occurs as if it was called by the * application. * * A timeout can only occur when the slave is waiting for a command * from the application. * * Specifying a negative timeout cancels a pending timeout. * * Only one timeout at a time is supported, setting a timeout * cancels any pending timeout. */ void setTimeoutSpecialCommand(int timeout, const QByteArray &data = QByteArray()); ///////////////// // Dispatching (internal) //////////////// /** * @internal */ virtual void dispatch(int command, const QByteArray &data); /** * @internal */ virtual void dispatchOpenCommand(int command, const QByteArray &data); /** * Read data sent by the job, after a dataReq * * @param buffer buffer where data is stored * @return 0 on end of data, * > 0 bytes read * < 0 error **/ int readData(QByteArray &buffer); #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) /** * It collects entries and emits them via listEntries * when enough of them are there or a certain time * frame exceeded (to make sure the app gets some * items in time but not too many items one by one * as this will cause a drastic performance penalty). * @param _entry The UDSEntry containing all of the object attributes. * @param ready set to true after emitting all items. @p _entry is not * used in this case * @deprecated since 5.0. the listEntry(entry, true) indicated * that the entry listing was completed. However, each slave should * already call finished() to also tell us that we're done listing. * You should make sure that finished() is called when the entry * listing is completed and simply remove the call to listEntry(entry, true). */ KIOCORE_DEPRECATED_VERSION(5, 0, "See API docs") void listEntry(const UDSEntry &_entry, bool ready); #endif /** * It collects entries and emits them via listEntries * when enough of them are there or a certain time * frame exceeded (to make sure the app gets some * items in time but not too many items one by one * as this will cause a drastic performance penalty). * @param entry The UDSEntry containing all of the object attributes. * @since 5.0 */ void listEntry(const UDSEntry &entry); /** * internal function to connect a slave to/ disconnect from * either the slave pool or the application */ void connectSlave(const QString &path); void disconnectSlave(); /** * Prompt the user for Authorization info (login & password). * * Use this function to request authorization information from * the end user. You can also pass an error message which explains * why a previous authorization attempt failed. Here is a very * simple example: * * \code * KIO::AuthInfo authInfo; * int errorCode = openPasswordDialogV2(authInfo); * if (!errorCode) { * qDebug() << QLatin1String("User: ") << authInfo.username; * qDebug() << QLatin1String("Password: not displayed here!"); * } else { * error(errorCode, QString()); * } * \endcode * * You can also preset some values like the username, caption or * comment as follows: * * \code * KIO::AuthInfo authInfo; * authInfo.caption = i18n("Acme Password Dialog"); * authInfo.username = "Wile E. Coyote"; * QString errorMsg = i18n("You entered an incorrect password."); * int errorCode = openPasswordDialogV2(authInfo, errorMsg); * [...] * \endcode * * \note You should consider using checkCachedAuthentication() to * see if the password is available in kpasswdserver before calling * this function. * * \note A call to this function can fail and return @p false, * if the password server could not be started for whatever reason. * * \note This function does not store the password information * automatically (and has not since kdelibs 4.7). If you want to * store the password information in a persistent storage like * KWallet, then you MUST call @ref cacheAuthentication. * * @see checkCachedAuthentication * @param info See AuthInfo. * @param errorMsg Error message to show * @return a KIO error code: NoError (0), KIO::USER_CANCELED, or other error codes. */ int openPasswordDialogV2(KIO::AuthInfo &info, const QString &errorMsg = QString()); #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 24) /** * @deprecated since KF 5.24, use openPasswordDialogV2. * The return value works differently: * instead of * if (!openPasswordDialog()) { error(USER_CANCELED); } * store and pass the return value to error(), when NOT zero, * as shown documentation for openPasswordDialogV2(). */ KIOCORE_DEPRECATED_VERSION(5, 24, "Use SlaveBase::openPasswordDialogV2(...)") bool openPasswordDialog(KIO::AuthInfo &info, const QString &errorMsg = QString()); #endif /** * Checks for cached authentication based on parameters * given by @p info. * * Use this function to check if any cached password exists * for the URL given by @p info. If @p AuthInfo::realmValue * and/or @p AuthInfo::verifyPath flag is specified, then * they will also be factored in determining the presence * of a cached password. Note that @p Auth::url is a required * parameter when attempting to check for cached authorization * info. Here is a simple example: * * \code * AuthInfo info; * info.url = QUrl("http://www.foobar.org/foo/bar"); * info.username = "somename"; * info.verifyPath = true; * if ( !checkCachedAuthentication( info ) ) * { * int errorCode = openPasswordDialogV2(info); * .... * } * \endcode * * @param info See AuthInfo. * @return @p true if cached Authorization is found, false otherwise. */ bool checkCachedAuthentication(AuthInfo &info); /** * Caches @p info in a persistent storage like KWallet. * * Note that calling openPasswordDialogV2 does not store passwords * automatically for you (and has not since kdelibs 4.7). * * Here is a simple example of how to use cacheAuthentication: * * \code * AuthInfo info; * info.url = QUrl("http://www.foobar.org/foo/bar"); * info.username = "somename"; * info.verifyPath = true; * if ( !checkCachedAuthentication( info ) ) { * int errorCode = openPasswordDialogV2(info); * if (!errorCode) { * if (info.keepPassword) { // user asked password be save/remembered * cacheAuthentication(info); * } * } * } * \endcode * * @param info See AuthInfo. * @return @p true if @p info was successfully cached. */ bool cacheAuthentication(const AuthInfo &info); #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) /** * Used by the slave to check if it can connect * to a given host. This should be called where the slave is ready * to do a ::connect() on a socket. For each call to * requestNetwork must exist a matching call to * dropNetwork, or the system will stay online until * KNetMgr gets closed (or the SlaveBase gets destructed)! * * If KNetMgr is not running, then this is a no-op and returns true * * @param host tells the netmgr the host the slave wants to connect * to. As this could also be a proxy, we can't just take * the host currently connected to (but that's the default * value) * * @return true in theory, the host is reachable * false the system is offline and the host is in a remote network. * * @deprecated Since 5.0, for a very very long time, not implemented anymore * Probably dates back to model dialup times. */ KIOCORE_DEPRECATED_VERSION(5, 0, "Not implemented & used") bool requestNetwork(const QString &host = QString()); #endif #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) /** * * Used by the slave to withdraw a connection requested by * requestNetwork. This function cancels the last call to * requestNetwork. If a client uses more than one internet * connection, it must use dropNetwork(host) to * stop each request. * * If KNetMgr is not running, then this is a no-op. * * @param host the host passed to requestNetwork * * A slave should call this function every time it disconnect from a host. * * * @deprecated Since 5.0, for a very very long time, not implemented anymore * Probably dates back to model dialup times. * */ KIOCORE_DEPRECATED_VERSION(5, 0, "Not implemented & used") void dropNetwork(const QString &host = QString()); #endif /** * Wait for an answer to our request, until we get @p expected1 or @p expected2 * @return the result from readData, as well as the cmd in *pCmd if set, and the data in @p data */ int waitForAnswer(int expected1, int expected2, QByteArray &data, int *pCmd = nullptr); /** * Internal function to transmit meta data to the application. * m_outgoingMetaData will be cleared; this means that if the slave is for * example put on hold and picked up by a different KIO::Job later the new * job will not see the metadata sent before. * See kio/DESIGN.krun for an overview of the state * progression of a job/slave. * @warning calling this method may seriously interfere with the operation * of KIO which relies on the presence of some metadata at some points in time. * You should not use it if you are not familiar with KIO and not before * the slave is connected to the last job before returning to idle state. */ void sendMetaData(); /** * Internal function to transmit meta data to the application. * Like sendMetaData() but m_outgoingMetaData will not be cleared. * This method is mainly useful in code that runs before the slave is connected * to its final job. */ void sendAndKeepMetaData(); /** If your ioslave was killed by a signal, wasKilled() returns true. Check it regularly in lengthy functions (e.g. in get();) and return as fast as possible from this function if wasKilled() returns true. This will ensure that your slave destructor will be called correctly. */ bool wasKilled() const; /** Internally used. * @internal */ void setKillFlag(); /** Internally used * @internal */ void lookupHost(const QString &host); /** Internally used * @internal */ int waitForHostInfo(QHostInfo &info); /** * Checks with job if privilege operation is allowed. * @return privilege operation status. * @see PrivilegeOperationStatus * @since 5.66 */ PrivilegeOperationStatus requestPrivilegeOperation(const QString &operationDetails); /** * Adds @p action to the list of PolicyKit actions which the * slave is authorized to perform. * * @param action the PolicyKit action * @since 5.45 */ void addTemporaryAuthorization(const QString &action); /** * @deprecated since 5.66, use requestPrivilegeOperation(QString) */ #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 66) KIOCORE_DEPRECATED_VERSION(5, 66, "Pass QString action to requestPrivilegeOperation") PrivilegeOperationStatus requestPrivilegeOperation(); #endif protected: /** * Name of the protocol supported by this slave */ QByteArray mProtocol; //Often used by TcpSlaveBase and unlikely to change MetaData mOutgoingMetaData; MetaData mIncomingMetaData; enum VirtualFunctionId { AppConnectionMade = 0, - GetFileSystemFreeSpace = 1 // KF6 TODO: Turn into a virtual method + GetFileSystemFreeSpace = 1, // KF6 TODO: Turn into a virtual method + Truncate = 2, // KF6 TODO: Turn into a virtual method }; virtual void virtual_hook(int id, void *data); private: // Convenience function converting mProtocol to QString as unsupportedActionErrorString(), which // is used in many places in the code, takes a QString parameter inline const QString protocolName() const { return QString::fromLatin1(mProtocol); } // This helps catching missing tr()/i18n() calls in error(). void error(int _errid, const QByteArray &_text); void send(int cmd, const QByteArray &arr = QByteArray()); SlaveBasePrivate *const d; friend class SlaveBasePrivate; }; /** * Returns an appropriate error message if the given command @p cmd * is an unsupported action (ERR_UNSUPPORTED_ACTION). * @param protocol name of the protocol * @param cmd given command * @see enum Command */ KIOCORE_EXPORT QString unsupportedActionErrorString(const QString &protocol, int cmd); } #endif diff --git a/src/core/slaveinterface.cpp b/src/core/slaveinterface.cpp index c5ae817b..f56f8060 100644 --- a/src/core/slaveinterface.cpp +++ b/src/core/slaveinterface.cpp @@ -1,441 +1,446 @@ /* This file is part of the KDE libraries Copyright (C) 2000 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "slaveinterface.h" #include "slaveinterface_p.h" #include "usernotificationhandler_p.h" #include "slavebase.h" #include "connection_p.h" #include "commands_p.h" #include "hostinfo.h" #include #include #include #include #include #include #include using namespace KIO; Q_GLOBAL_STATIC(UserNotificationHandler, globalUserNotificationHandler) SlaveInterface::SlaveInterface(SlaveInterfacePrivate &dd, QObject *parent) : QObject(parent), d_ptr(&dd) { connect(&d_ptr->speed_timer, &QTimer::timeout, this, &SlaveInterface::calcSpeed); } SlaveInterface::~SlaveInterface() { // Note: no Debug() here (scheduler is deleted very late) delete d_ptr; } void SlaveInterface::setConnection(Connection *connection) { Q_D(SlaveInterface); d->connection = connection; } Connection *SlaveInterface::connection() const { const Q_D(SlaveInterface); return d->connection; } static KIO::filesize_t readFilesize_t(QDataStream &stream) { KIO::filesize_t result; stream >> result; return result; } bool SlaveInterface::dispatch() { Q_D(SlaveInterface); Q_ASSERT(d->connection); int cmd; QByteArray data; int ret = d->connection->read(&cmd, data); if (ret == -1) { return false; } return dispatch(cmd, data); } void SlaveInterface::calcSpeed() { Q_D(SlaveInterface); if (d->slave_calcs_speed || !d->connection->isConnected()) { // killing a job results in disconnection but the timer never stops d->speed_timer.stop(); return; } const qint64 currentTime = QDateTime::currentMSecsSinceEpoch(); const qint64 diff = currentTime - d->start_time; if (diff - d->last_time >= 900) { d->last_time = diff; if (d->nums == max_nums) { // let's hope gcc can optimize that well enough // otherwise I'd try memcpy :) for (unsigned int i = 1; i < max_nums; ++i) { d->times[i - 1] = d->times[i]; d->sizes[i - 1] = d->sizes[i]; } d->nums--; } d->times[d->nums] = diff; d->sizes[d->nums++] = d->filesize - d->offset; KIO::filesize_t lspeed = 1000 * (d->sizes[d->nums - 1] - d->sizes[0]) / (d->times[d->nums - 1] - d->times[0]); //qDebug() << (long)d->filesize << diff // << long(d->sizes[d->nums-1] - d->sizes[0]) // << d->times[d->nums-1] - d->times[0] // << long(lspeed) << double(d->filesize) / diff // << convertSize(lspeed) // << convertSize(long(double(d->filesize) / diff) * 1000); if (!lspeed) { d->nums = 1; d->times[0] = diff; d->sizes[0] = d->filesize - d->offset; } emit speed(lspeed); } } bool SlaveInterface::dispatch(int _cmd, const QByteArray &rawdata) { Q_D(SlaveInterface); //qDebug() << "dispatch " << _cmd; QDataStream stream(rawdata); QString str1; qint32 i; qint8 b; quint32 ul; switch (_cmd) { case MSG_DATA: emit data(rawdata); break; case MSG_DATA_REQ: emit dataReq(); break; case MSG_OPENED: emit open(); break; case MSG_FINISHED: //qDebug() << "Finished [this = " << this << "]"; d->offset = 0; d->speed_timer.stop(); emit finished(); break; case MSG_STAT_ENTRY: { UDSEntry entry; stream >> entry; emit statEntry(entry); break; } case MSG_LIST_ENTRIES: { UDSEntryList list; UDSEntry entry; while (!stream.atEnd()) { stream >> entry; list.append(entry); } emit listEntries(list); break; } case MSG_RESUME: { // From the put job d->offset = readFilesize_t(stream); emit canResume(d->offset); break; } case MSG_CANRESUME: // From the get job d->filesize = d->offset; emit canResume(0); // the arg doesn't matter break; case MSG_ERROR: stream >> i >> str1; //qDebug() << "error " << i << " " << str1; emit error(i, str1); break; case MSG_SLAVE_STATUS: case MSG_SLAVE_STATUS_V2: { qint64 pid; QByteArray protocol; stream >> pid >> protocol >> str1 >> b; emit slaveStatus(pid, protocol, str1, (b != 0)); break; } case MSG_CONNECTED: emit connected(); break; case MSG_WRITTEN: { KIO::filesize_t size = readFilesize_t(stream); emit written(size); break; } case INF_TOTAL_SIZE: { KIO::filesize_t size = readFilesize_t(stream); d->start_time = QDateTime::currentMSecsSinceEpoch(); d->last_time = 0; d->filesize = d->offset; d->sizes[0] = d->filesize - d->offset; d->times[0] = 0; d->nums = 1; d->speed_timer.start(1000); d->slave_calcs_speed = false; emit totalSize(size); break; } case INF_PROCESSED_SIZE: { KIO::filesize_t size = readFilesize_t(stream); emit processedSize(size); d->filesize = size; break; } case INF_POSITION: { KIO::filesize_t pos = readFilesize_t(stream); emit position(pos); break; } + case INF_TRUNCATED: { + KIO::filesize_t length = readFilesize_t(stream); + emit truncated(length); + break; + } case INF_SPEED: stream >> ul; d->slave_calcs_speed = true; d->speed_timer.stop(); emit speed(ul); break; case INF_GETTING_FILE: break; case INF_ERROR_PAGE: emit errorPage(); break; case INF_REDIRECTION: { QUrl url; stream >> url; emit redirection(url); break; } case INF_MIME_TYPE: stream >> str1; emit mimeType(str1); if (!d->connection->suspended()) { d->connection->sendnow(CMD_NONE, QByteArray()); } break; case INF_WARNING: stream >> str1; emit warning(str1); break; case INF_MESSAGEBOX: { //qDebug() << "needs a msg box"; QString text, caption, buttonYes, buttonNo, dontAskAgainName; int type; stream >> type >> text >> caption >> buttonYes >> buttonNo; if (stream.atEnd()) { messageBox(type, text, caption, buttonYes, buttonNo); } else { stream >> dontAskAgainName; messageBox(type, text, caption, buttonYes, buttonNo, dontAskAgainName); } break; } case INF_INFOMESSAGE: { QString msg; stream >> msg; emit infoMessage(msg); break; } case INF_META_DATA: { MetaData m; stream >> m; if (m.contains(QLatin1String("ssl_in_use"))) { const QLatin1String ssl_("ssl_"); const MetaData &constM = m; for (MetaData::ConstIterator it = constM.lowerBound(ssl_); it != constM.constEnd(); ++it) { if (it.key().startsWith(ssl_)) { d->sslMetaData.insert(it.key(), it.value()); } else { // we're past the ssl_* entries; remember that QMap is ordered. break; } } } else if (m.contains(QStringLiteral("privilege_conf_details"))) { // KF6 TODO Remove this conditional. d->privilegeConfMetaData = m; } emit metaData(m); break; } case MSG_NET_REQUEST: { QString host; QString slaveid; stream >> host >> slaveid; requestNetwork(host, slaveid); break; } case MSG_NET_DROP: { QString host; QString slaveid; stream >> host >> slaveid; dropNetwork(host, slaveid); break; } case MSG_NEED_SUBURL_DATA: { emit needSubUrlData(); break; } case MSG_HOST_INFO_REQ: { QString hostName; stream >> hostName; HostInfo::lookupHost(hostName, this, SLOT(slotHostInfo(QHostInfo))); break; } case MSG_PRIVILEGE_EXEC: emit privilegeOperationRequested(); break; default: qCWarning(KIO_CORE) << "Slave sends unknown command (" << _cmd << "), dropping slave"; return false; } return true; } void SlaveInterface::setOffset(KIO::filesize_t o) { Q_D(SlaveInterface); d->offset = o; } KIO::filesize_t SlaveInterface::offset() const { const Q_D(SlaveInterface); return d->offset; } void SlaveInterface::requestNetwork(const QString &host, const QString &slaveid) { Q_D(SlaveInterface); Q_UNUSED(host); Q_UNUSED(slaveid); //qDebug() << "requestNetwork " << host << slaveid; // This is old stuff. We just always return true... QByteArray packedArgs; QDataStream stream(&packedArgs, QIODevice::WriteOnly); stream << true; d->connection->sendnow(INF_NETWORK_STATUS, packedArgs); } void SlaveInterface::dropNetwork(const QString &host, const QString &slaveid) { Q_UNUSED(host); Q_UNUSED(slaveid); //qDebug() << "dropNetwork " << host << slaveid; } void SlaveInterface::sendResumeAnswer(bool resume) { Q_D(SlaveInterface); //qDebug() << "ok for resuming:" << resume; d->connection->sendnow(resume ? CMD_RESUMEANSWER : CMD_NONE, QByteArray()); } void SlaveInterface::sendMessageBoxAnswer(int result) { Q_D(SlaveInterface); if (!d->connection) { return; } if (d->connection->suspended()) { d->connection->resume(); } QByteArray packedArgs; QDataStream stream(&packedArgs, QIODevice::WriteOnly); stream << result; d->connection->sendnow(CMD_MESSAGEBOXANSWER, packedArgs); // qDebug() << "message box answer" << result; } void SlaveInterface::messageBox(int type, const QString &text, const QString &_caption, const QString &buttonYes, const QString &buttonNo) { messageBox(type, text, _caption, buttonYes, buttonNo, QString()); } void SlaveInterface::messageBox(int type, const QString &text, const QString &caption, const QString &buttonYes, const QString &buttonNo, const QString &dontAskAgainName) { Q_D(SlaveInterface); if (d->connection) { d->connection->suspend(); } QHash data; data.insert(UserNotificationHandler::MSG_TEXT, text); data.insert(UserNotificationHandler::MSG_CAPTION, caption); data.insert(UserNotificationHandler::MSG_YES_BUTTON_TEXT, buttonYes); data.insert(UserNotificationHandler::MSG_NO_BUTTON_TEXT, buttonNo); data.insert(UserNotificationHandler::MSG_DONT_ASK_AGAIN, dontAskAgainName); // SMELL: the braindead way to support button icons // TODO: Fix this in KIO::SlaveBase. if (buttonYes == i18n("&Details")) { data.insert(UserNotificationHandler::MSG_YES_BUTTON_ICON, QLatin1String("help-about")); } else if (buttonYes == i18n("&Forever")) { data.insert(UserNotificationHandler::MSG_YES_BUTTON_ICON, QLatin1String("flag-green")); } if (buttonNo == i18n("Co&ntinue")) { data.insert(UserNotificationHandler::MSG_NO_BUTTON_ICON, QLatin1String("arrow-right")); } else if (buttonNo == i18n("&Current Session only")) { data.insert(UserNotificationHandler::MSG_NO_BUTTON_ICON, QLatin1String("chronometer")); } if (type == KIO::SlaveBase::SSLMessageBox) { data.insert(UserNotificationHandler::MSG_META_DATA, d->sslMetaData.toVariant()); } else if (type == KIO::SlaveBase::WarningContinueCancelDetailed) { // KF6 TODO Remove data.insert(UserNotificationHandler::MSG_META_DATA, d->privilegeConfMetaData.toVariant()); } globalUserNotificationHandler()->requestMessageBox(this, type, data); } void SlaveInterfacePrivate::slotHostInfo(const QHostInfo &info) { QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); stream << info.hostName() << info.addresses() << info.error() << info.errorString(); connection->send(CMD_HOST_INFO, data); } #include "moc_slaveinterface.cpp" diff --git a/src/core/slaveinterface.h b/src/core/slaveinterface.h index 327f44b3..4a7ec688 100644 --- a/src/core/slaveinterface.h +++ b/src/core/slaveinterface.h @@ -1,216 +1,218 @@ /* This file is part of the KDE project Copyright (C) 2000 David Faure This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef __kio_slaveinterface_h #define __kio_slaveinterface_h #include #include #include #include #include #include class QUrl; namespace KIO { class Connection; // better there is one ... class SlaveInterfacePrivate; // Definition of enum Command has been moved to global.h /** * Identifiers for KIO informational messages. */ enum Info { INF_TOTAL_SIZE = 10, INF_PROCESSED_SIZE = 11, INF_SPEED, INF_REDIRECTION = 20, INF_MIME_TYPE = 21, INF_ERROR_PAGE = 22, INF_WARNING = 23, #if KIOCORE_ENABLE_DEPRECATED_SINCE(3, 0) INF_GETTING_FILE, ///< @deprecated Since 3.0 #else INF_GETTING_FILE_DEPRECATED_DO_NOT_USE, #endif INF_UNUSED = 25, ///< now unused INF_INFOMESSAGE, INF_META_DATA, INF_NETWORK_STATUS, INF_MESSAGEBOX, - INF_POSITION + INF_POSITION, + INF_TRUNCATED // add new ones here once a release is done, to avoid breaking binary compatibility }; /** * Identifiers for KIO data messages. */ enum Message { MSG_DATA = 100, MSG_DATA_REQ, MSG_ERROR, MSG_CONNECTED, MSG_FINISHED, MSG_STAT_ENTRY, // 105 MSG_LIST_ENTRIES, MSG_RENAMED, ///< unused MSG_RESUME, #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 45) MSG_SLAVE_STATUS, ///< @deprecated Since 5.45, use MSG_SLAVE_STATUS_V2 #else MSG_SLAVE_STATUS_DEPRECATED_DO_NOT_USE, #endif MSG_SLAVE_ACK, // 110 MSG_NET_REQUEST, MSG_NET_DROP, MSG_NEED_SUBURL_DATA, MSG_CANRESUME, #if KIOCORE_ENABLE_DEPRECATED_SINCE(3, 1) MSG_AUTH_KEY, ///< @deprecated Since 3.1 MSG_DEL_AUTH_KEY, ///< @deprecated Since 3.1 #else MSG_AUTH_KEY_DEPRECATED_DO_NOT_USE, MSG_DEL_AUTH_KEY_DEPRECATED_DO_NOT_USE, #endif MSG_OPENED, MSG_WRITTEN, MSG_HOST_INFO_REQ, MSG_PRIVILEGE_EXEC, MSG_SLAVE_STATUS_V2 // add new ones here once a release is done, to avoid breaking binary compatibility }; /** * @class KIO::SlaveInterface slaveinterface.h * * There are two classes that specifies the protocol between application * ( KIO::Job) and kioslave. SlaveInterface is the class to use on the application * end, SlaveBase is the one to use on the slave end. * * A call to foo() results in a call to slotFoo() on the other end. */ class KIOCORE_EXPORT SlaveInterface : public QObject { Q_OBJECT protected: SlaveInterface(SlaveInterfacePrivate &dd, QObject *parent = nullptr); public: virtual ~SlaveInterface(); #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) // TODO KF6: remove these methods, Connection isn't an exported class KIOCORE_DEPRECATED_VERSION(5, 0, "Do not use") void setConnection(Connection *connection); KIOCORE_DEPRECATED_VERSION(5, 0, "Do not use") Connection *connection() const; #endif // Send our answer to the MSG_RESUME (canResume) request // (to tell the "put" job whether to resume or not) void sendResumeAnswer(bool resume); /** * Sends our answer for the INF_MESSAGEBOX request. * * @since 4.11 */ void sendMessageBoxAnswer(int result); void setOffset(KIO::filesize_t offset); KIO::filesize_t offset() const; Q_SIGNALS: /////////// // Messages sent by the slave /////////// void data(const QByteArray &); void dataReq(); void error(int, const QString &); void connected(); void finished(); void slaveStatus(qint64, const QByteArray &, const QString &, bool); void listEntries(const KIO::UDSEntryList &); void statEntry(const KIO::UDSEntry &); void needSubUrlData(); void canResume(KIO::filesize_t); void open(); void written(KIO::filesize_t); void close(); void privilegeOperationRequested(); /////////// // Info sent by the slave ////////// void metaData(const KIO::MetaData &); void totalSize(KIO::filesize_t); void processedSize(KIO::filesize_t); void redirection(const QUrl &); void position(KIO::filesize_t); + void truncated(KIO::filesize_t); void speed(unsigned long); void errorPage(); void mimeType(const QString &); void warning(const QString &); void infoMessage(const QString &); //void connectFinished(); //it does not get emitted anywhere protected: ///////////////// // Dispatching //////////////// virtual bool dispatch(); virtual bool dispatch(int _cmd, const QByteArray &data); void messageBox(int type, const QString &text, const QString &caption, const QString &buttonYes, const QString &buttonNo); void messageBox(int type, const QString &text, const QString &caption, const QString &buttonYes, const QString &buttonNo, const QString &dontAskAgainName); // I need to identify the slaves void requestNetwork(const QString &, const QString &); void dropNetwork(const QString &, const QString &); protected Q_SLOTS: void calcSpeed(); protected: SlaveInterfacePrivate *const d_ptr; Q_DECLARE_PRIVATE(SlaveInterface) private: Q_PRIVATE_SLOT(d_func(), void slotHostInfo(QHostInfo)) }; } #endif diff --git a/src/ioslaves/file/file.cpp b/src/ioslaves/file/file.cpp index eae7dc01..55949ab2 100644 --- a/src/ioslaves/file/file.cpp +++ b/src/ioslaves/file/file.cpp @@ -1,1557 +1,1573 @@ /* Copyright (C) 2000-2002 Stephan Kulow Copyright (C) 2000-2002 David Faure Copyright (C) 2000-2002 Waldo Bastian Copyright (C) 2006 Allan Sandfeld Jensen Copyright (C) 2007 Thiago Macieira This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License (LGPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "file.h" #include #include #include #include #include "kioglobal_p.h" #ifdef Q_OS_UNIX #include "legacycodec.h" #endif #include #include #ifdef Q_OS_WIN #include #include #include //struct timeval #else #include #endif #include #include #include #include #include #ifdef Q_OS_WIN #include #include #endif #include #include #include #include #include #include #include #include #if HAVE_STATX #include #endif #if HAVE_VOLMGT #include #include #endif #include #include Q_LOGGING_CATEGORY(KIO_FILE, "kf5.kio.kio_file") // Pseudo plugin class to embed meta data class KIOPluginForMetaData : public QObject { Q_OBJECT Q_PLUGIN_METADATA(IID "org.kde.kio.slave.file" FILE "file.json") }; using namespace KIO; #define MAX_IPC_SIZE (1024*32) static QString readLogFile(const QByteArray &_filename); #if HAVE_POSIX_ACL static void appendACLAtoms(const QByteArray &path, UDSEntry &entry, mode_t type); #endif extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv) { QCoreApplication app(argc, argv); // needed for QSocketNotifier app.setApplicationName(QStringLiteral("kio_file")); if (argc != 4) { fprintf(stderr, "Usage: kio_file protocol domain-socket1 domain-socket2\n"); exit(-1); } #ifdef Q_OS_UNIX // From Qt doc : "Note that you should not delete codecs yourself: once created they become Qt's responsibility" (void)new LegacyCodec; #endif FileProtocol slave(argv[2], argv[3]); // Make sure the first kDebug is after the slave ctor (which sets a SIGPIPE handler) // This is useful in case kdeinit was autostarted by another app, which then exited and closed fd2 // (e.g. ctest does that, or closing the terminal window would do that) //qDebug() << "Starting" << getpid(); slave.dispatchLoop(); //qDebug() << "Done"; return 0; } static QFile::Permissions modeToQFilePermissions(int mode) { QFile::Permissions perms; if (mode & S_IRUSR) { perms |= QFile::ReadOwner; } if (mode & S_IWUSR) { perms |= QFile::WriteOwner; } if (mode & S_IXUSR) { perms |= QFile::ExeOwner; } if (mode & S_IRGRP) { perms |= QFile::ReadGroup; } if (mode & S_IWGRP) { perms |= QFile::WriteGroup; } if (mode & S_IXGRP) { perms |= QFile::ExeGroup; } if (mode & S_IROTH) { perms |= QFile::ReadOther; } if (mode & S_IWOTH) { perms |= QFile::WriteOther; } if (mode & S_IXOTH) { perms |= QFile::ExeOther; } return perms; } FileProtocol::FileProtocol(const QByteArray &pool, const QByteArray &app) : SlaveBase(QByteArrayLiteral("file"), pool, app), mFile(nullptr) { } FileProtocol::~FileProtocol() { } #if HAVE_POSIX_ACL static QString aclToText(acl_t acl) { ssize_t size = 0; char *txt = acl_to_text(acl, &size); const QString ret = QString::fromLatin1(txt, size); acl_free(txt); return ret; } #endif int FileProtocol::setACL(const char *path, mode_t perm, bool directoryDefault) { int ret = 0; #if HAVE_POSIX_ACL const QString ACLString = metaData(QStringLiteral("ACL_STRING")); const QString defaultACLString = metaData(QStringLiteral("DEFAULT_ACL_STRING")); // Empty strings mean leave as is if (!ACLString.isEmpty()) { acl_t acl = nullptr; if (ACLString == QLatin1String("ACL_DELETE")) { // user told us to delete the extended ACL, so let's write only // the minimal (UNIX permission bits) part acl = acl_from_mode(perm); } acl = acl_from_text(ACLString.toLatin1().constData()); if (acl_valid(acl) == 0) { // let's be safe ret = acl_set_file(path, ACL_TYPE_ACCESS, acl); // qDebug() << "Set ACL on:" << path << "to:" << aclToText(acl); } acl_free(acl); if (ret != 0) { return ret; // better stop trying right away } } if (directoryDefault && !defaultACLString.isEmpty()) { if (defaultACLString == QLatin1String("ACL_DELETE")) { // user told us to delete the default ACL, do so ret += acl_delete_def_file(path); } else { acl_t acl = acl_from_text(defaultACLString.toLatin1().constData()); if (acl_valid(acl) == 0) { // let's be safe ret += acl_set_file(path, ACL_TYPE_DEFAULT, acl); // qDebug() << "Set Default ACL on:" << path << "to:" << aclToText(acl); } acl_free(acl); } } #else Q_UNUSED(path); Q_UNUSED(perm); Q_UNUSED(directoryDefault); #endif return ret; } void FileProtocol::chmod(const QUrl &url, int permissions) { const QString path(url.toLocalFile()); const QByteArray _path(QFile::encodeName(path)); /* FIXME: Should be atomic */ #ifdef Q_OS_UNIX // QFile::Permissions does not support special attributes like sticky if (::chmod(_path.constData(), permissions) == -1 || #else if (!QFile::setPermissions(path, modeToQFilePermissions(permissions)) || #endif (setACL(_path.data(), permissions, false) == -1) || /* if not a directory, cannot set default ACLs */ (setACL(_path.data(), permissions, true) == -1 && errno != ENOTDIR)) { if (auto err = execWithElevatedPrivilege(CHMOD, {_path, permissions}, errno)) { if (!err.wasCanceled()) { switch (err) { case EPERM: case EACCES: error(KIO::ERR_ACCESS_DENIED, path); break; #if defined(ENOTSUP) case ENOTSUP: // from setACL since chmod can't return ENOTSUP error(KIO::ERR_UNSUPPORTED_ACTION, i18n("Setting ACL for %1", path)); break; #endif case ENOSPC: error(KIO::ERR_DISK_FULL, path); break; default: error(KIO::ERR_CANNOT_CHMOD, path); } return; } } } finished(); } void FileProtocol::setModificationTime(const QUrl &url, const QDateTime &mtime) { const QString path(url.toLocalFile()); QT_STATBUF statbuf; if (QT_LSTAT(QFile::encodeName(path).constData(), &statbuf) == 0) { struct utimbuf utbuf; utbuf.actime = statbuf.st_atime; // access time, unchanged utbuf.modtime = mtime.toSecsSinceEpoch(); // modification time if (::utime(QFile::encodeName(path).constData(), &utbuf) != 0) { if (auto err = execWithElevatedPrivilege(UTIME, {path, qint64(utbuf.actime), qint64(utbuf.modtime)}, errno)) { if (!err.wasCanceled()) { // TODO: errno could be EACCES, EPERM, EROFS error(KIO::ERR_CANNOT_SETTIME, path); } } } else { finished(); } } else { error(KIO::ERR_DOES_NOT_EXIST, path); } } void FileProtocol::mkdir(const QUrl &url, int permissions) { const QString path(url.toLocalFile()); // qDebug() << path << "permission=" << permissions; // Remove existing file or symlink, if requested (#151851) if (metaData(QStringLiteral("overwrite")) == QLatin1String("true")) { if (!QFile::remove(path)) { execWithElevatedPrivilege(DEL, {path}, errno); } } QT_STATBUF buff; if (QT_LSTAT(QFile::encodeName(path).constData(), &buff) == -1) { bool dirCreated = QDir().mkdir(path); if (!dirCreated) { if (auto err = execWithElevatedPrivilege(MKDIR, {path}, errno)) { if (!err.wasCanceled()) { //TODO: add access denied & disk full (or another reasons) handling (into Qt, possibly) error(KIO::ERR_CANNOT_MKDIR, path); } return; } dirCreated = true; } if (dirCreated) { if (permissions != -1) { chmod(url, permissions); } else { finished(); } return; } } if ((buff.st_mode & QT_STAT_MASK) == QT_STAT_DIR) { // qDebug() << "ERR_DIR_ALREADY_EXIST"; error(KIO::ERR_DIR_ALREADY_EXIST, path); return; } error(KIO::ERR_FILE_ALREADY_EXIST, path); return; } void FileProtocol::redirect(const QUrl &url) { QUrl redir(url); redir.setScheme(configValue(QStringLiteral("DefaultRemoteProtocol"), QStringLiteral("smb"))); // if we would redirect into the Windows world, let's also check for the // DavWWWRoot "token" which in the Windows world tells win explorer to access // a webdav url // https://www.webdavsystem.com/server/access/windows if ((redir.scheme() == QLatin1String("smb")) && redir.path().startsWith(QLatin1String("/DavWWWRoot/"))) { redir.setPath(redir.path().mid(11)); // remove /DavWWWRoot redir.setScheme(QStringLiteral("webdav")); } redirection(redir); finished(); } void FileProtocol::get(const QUrl &url) { if (!url.isLocalFile()) { redirect(url); return; } const QString path(url.toLocalFile()); QT_STATBUF buff; if (QT_STAT(QFile::encodeName(path).constData(), &buff) == -1) { if (errno == EACCES) { error(KIO::ERR_ACCESS_DENIED, path); } else { error(KIO::ERR_DOES_NOT_EXIST, path); } return; } if ((buff.st_mode & QT_STAT_MASK) == QT_STAT_DIR) { error(KIO::ERR_IS_DIRECTORY, path); return; } if ((buff.st_mode & QT_STAT_MASK) != QT_STAT_REG) { error(KIO::ERR_CANNOT_OPEN_FOR_READING, path); return; } QFile f(path); if (!f.open(QIODevice::ReadOnly)) { if (auto err = tryOpen(f, QFile::encodeName(path), O_RDONLY, S_IRUSR, errno)) { if (!err.wasCanceled()) { error(KIO::ERR_CANNOT_OPEN_FOR_READING, path); } return; } } #if HAVE_FADVISE //TODO check return code posix_fadvise(f.handle(), 0, 0, POSIX_FADV_SEQUENTIAL); #endif // Determine the mimetype of the file to be retrieved, and emit it. // This is mandatory in all slaves (for KRun/BrowserRun to work) // In real "remote" slaves, this is usually done using mimeTypeForFileNameAndData // after receiving some data. But we don't know how much data the mimemagic rules // need, so for local files, better use mimeTypeForFile. QMimeDatabase db; QMimeType mt = db.mimeTypeForFile(url.toLocalFile()); mimeType(mt.name()); // Emit total size AFTER mimetype totalSize(buff.st_size); KIO::filesize_t processed_size = 0; QString resumeOffset = metaData(QStringLiteral("range-start")); if (resumeOffset.isEmpty()) { resumeOffset = metaData(QStringLiteral("resume")); // old name } if (!resumeOffset.isEmpty()) { bool ok; KIO::fileoffset_t offset = resumeOffset.toLongLong(&ok); if (ok && (offset > 0) && (offset < buff.st_size)) { if (f.seek(offset)) { canResume(); processed_size = offset; // qDebug() << "Resume offset:" << KIO::number(offset); } } } char buffer[ MAX_IPC_SIZE ]; QByteArray array; while (1) { int n = f.read(buffer, MAX_IPC_SIZE); if (n == -1) { if (errno == EINTR) { continue; } error(KIO::ERR_CANNOT_READ, path); f.close(); return; } if (n == 0) { break; // Finished } array = QByteArray::fromRawData(buffer, n); data(array); array.clear(); processed_size += n; processedSize(processed_size); //qDebug() << "Processed: " << KIO::number (processed_size); } data(QByteArray()); f.close(); processedSize(buff.st_size); finished(); } void FileProtocol::open(const QUrl &url, QIODevice::OpenMode mode) { // qDebug() << url; QString openPath = url.toLocalFile(); QT_STATBUF buff; if (QT_STAT(QFile::encodeName(openPath).constData(), &buff) == -1) { if (errno == EACCES) { error(KIO::ERR_ACCESS_DENIED, openPath); } else { error(KIO::ERR_DOES_NOT_EXIST, openPath); } return; } if ((buff.st_mode & QT_STAT_MASK) == QT_STAT_DIR) { error(KIO::ERR_IS_DIRECTORY, openPath); return; } if ((buff.st_mode & QT_STAT_MASK) != QT_STAT_REG) { error(KIO::ERR_CANNOT_OPEN_FOR_READING, openPath); return; } mFile = new QFile(openPath); if (!mFile->open(mode)) { if (mode & QIODevice::ReadOnly) { error(KIO::ERR_CANNOT_OPEN_FOR_READING, openPath); } else { error(KIO::ERR_CANNOT_OPEN_FOR_WRITING, openPath); } return; } // Determine the mimetype of the file to be retrieved, and emit it. // This is mandatory in all slaves (for KRun/BrowserRun to work). // If we're not opening the file ReadOnly or ReadWrite, don't attempt to // read the file and send the mimetype. if (mode & QIODevice::ReadOnly) { QMimeDatabase db; QMimeType mt = db.mimeTypeForFile(url.toLocalFile()); mimeType(mt.name()); } totalSize(buff.st_size); position(0); opened(); } void FileProtocol::read(KIO::filesize_t bytes) { // qDebug() << "File::open -- read"; Q_ASSERT(mFile && mFile->isOpen()); QVarLengthArray buffer(bytes); qint64 bytesRead = mFile->read(buffer.data(), bytes); if (bytesRead == -1) { qCWarning(KIO_FILE) << "Couldn't read. Error:" << mFile->errorString(); error(KIO::ERR_CANNOT_READ, mFile->fileName()); closeWithoutFinish(); } else { const QByteArray fileData = QByteArray::fromRawData(buffer.data(), bytesRead); data(fileData); } } void FileProtocol::write(const QByteArray &data) { // qDebug() << "File::open -- write"; Q_ASSERT(mFile && mFile->isWritable()); qint64 bytesWritten = mFile->write(data); if (bytesWritten == -1) { if (mFile->error() == QFileDevice::ResourceError) { // disk full error(KIO::ERR_DISK_FULL, mFile->fileName()); closeWithoutFinish(); } else { qCWarning(KIO_FILE) << "Couldn't write. Error:" << mFile->errorString(); error(KIO::ERR_CANNOT_WRITE, mFile->fileName()); closeWithoutFinish(); } } else { written(bytesWritten); } } void FileProtocol::seek(KIO::filesize_t offset) { // qDebug() << "File::open -- seek"; Q_ASSERT(mFile && mFile->isOpen()); if (mFile->seek(offset)) { position(offset); } else { error(KIO::ERR_CANNOT_SEEK, mFile->fileName()); closeWithoutFinish(); } } +void FileProtocol::truncate(KIO::filesize_t length) +{ + Q_ASSERT(mFile && mFile->isOpen()); + + if (mFile->resize(length)) { + truncated(length); + } else { + error(KIO::ERR_CANNOT_TRUNCATE, mFile->fileName()); + closeWithoutFinish(); + } +} + void FileProtocol::closeWithoutFinish() { Q_ASSERT(mFile); delete mFile; mFile = nullptr; } void FileProtocol::close() { // qDebug() << "File::open -- close "; closeWithoutFinish(); finished(); } void FileProtocol::put(const QUrl &url, int _mode, KIO::JobFlags _flags) { if (privilegeOperationUnitTestMode()) { finished(); return; } const QString dest_orig = url.toLocalFile(); // qDebug() << dest_orig << "mode=" << _mode; QString dest_part(dest_orig + QLatin1String(".part")); QT_STATBUF buff_orig; const bool bOrigExists = (QT_LSTAT(QFile::encodeName(dest_orig).constData(), &buff_orig) != -1); bool bPartExists = false; const bool bMarkPartial = configValue(QStringLiteral("MarkPartial"), true); if (bMarkPartial) { QT_STATBUF buff_part; bPartExists = (QT_LSTAT(QFile::encodeName(dest_part).constData(), &buff_part) != -1); if (bPartExists && !(_flags & KIO::Resume) && !(_flags & KIO::Overwrite) && buff_part.st_size > 0 && ((buff_part.st_mode & QT_STAT_MASK) == QT_STAT_REG)) { // qDebug() << "calling canResume with" << KIO::number(buff_part.st_size); // Maybe we can use this partial file for resuming // Tell about the size we have, and the app will tell us // if it's ok to resume or not. _flags |= canResume(buff_part.st_size) ? KIO::Resume : KIO::DefaultFlags; // qDebug() << "got answer" << (_flags & KIO::Resume); } } if (bOrigExists && !(_flags & KIO::Overwrite) && !(_flags & KIO::Resume)) { if ((buff_orig.st_mode & QT_STAT_MASK) == QT_STAT_DIR) { error(KIO::ERR_DIR_ALREADY_EXIST, dest_orig); } else { error(KIO::ERR_FILE_ALREADY_EXIST, dest_orig); } return; } int result; QString dest; QFile f; // Loop until we got 0 (end of data) do { QByteArray buffer; dataReq(); // Request for data result = readData(buffer); if (result >= 0) { if (dest.isEmpty()) { if (bMarkPartial) { // qDebug() << "Appending .part extension to" << dest_orig; dest = dest_part; if (bPartExists && !(_flags & KIO::Resume)) { // qDebug() << "Deleting partial file" << dest_part; QFile::remove(dest_part); // Catch errors when we try to open the file. } } else { dest = dest_orig; if (bOrigExists && !(_flags & KIO::Resume)) { // qDebug() << "Deleting destination file" << dest_orig; QFile::remove(dest_orig); // Catch errors when we try to open the file. } } f.setFileName(dest); if ((_flags & KIO::Resume)) { f.open(QIODevice::ReadWrite | QIODevice::Append); } else { f.open(QIODevice::Truncate | QIODevice::WriteOnly); if (_mode != -1) { // WABA: Make sure that we keep writing permissions ourselves, // otherwise we can be in for a surprise on NFS. mode_t initialMode = _mode | S_IWUSR | S_IRUSR; f.setPermissions(modeToQFilePermissions(initialMode)); } } if (!f.isOpen()) { int oflags = 0; int filemode = _mode; if ((_flags & KIO::Resume)) { oflags = O_RDWR | O_APPEND; } else { oflags = O_WRONLY | O_TRUNC | O_CREAT; if (_mode != -1) { filemode = _mode | S_IWUSR | S_IRUSR; } } if (auto err = tryOpen(f, QFile::encodeName(dest), oflags, filemode, errno)) { if (!err.wasCanceled()) { // qDebug() << "####################### COULD NOT WRITE" << dest << "_mode=" << _mode; // qDebug() << "QFile error==" << f.error() << "(" << f.errorString() << ")"; if (f.error() == QFileDevice::PermissionsError) { error(KIO::ERR_WRITE_ACCESS_DENIED, dest); } else { error(KIO::ERR_CANNOT_OPEN_FOR_WRITING, dest); } } return; } else { #ifndef Q_OS_WIN if ((_flags & KIO::Resume)) { execWithElevatedPrivilege(CHOWN, {dest, getuid(), getgid()}, errno); QFile::setPermissions(dest, modeToQFilePermissions(filemode)); } #endif } } } if (f.write(buffer) == -1) { if (f.error() == QFile::ResourceError) { // disk full error(KIO::ERR_DISK_FULL, dest_orig); result = -2; // means: remove dest file } else { qCWarning(KIO_FILE) << "Couldn't write. Error:" << f.errorString(); error(KIO::ERR_CANNOT_WRITE, dest_orig); result = -1; } } } else { qCWarning(KIO_FILE) << "readData() returned" << result; error(KIO::ERR_CANNOT_WRITE, dest_orig); } } while (result > 0); // An error occurred deal with it. if (result < 0) { // qDebug() << "Error during 'put'. Aborting."; if (f.isOpen()) { f.close(); QT_STATBUF buff; if (QT_STAT(QFile::encodeName(dest).constData(), &buff) == 0) { int size = configValue(QStringLiteral("MinimumKeepSize"), DEFAULT_MINIMUM_KEEP_SIZE); if (buff.st_size < size) { QFile::remove(dest); } } } return; } if (!f.isOpen()) { // we got nothing to write out, so we never opened the file finished(); return; } f.close(); if (f.error() != QFile::NoError) { qCWarning(KIO_FILE) << "Error when closing file descriptor:" << f.errorString(); error(KIO::ERR_CANNOT_WRITE, dest_orig); return; } // after full download rename the file back to original name if (bMarkPartial) { //QFile::rename() never overwrites the destination file unlike ::remove, //so we must remove it manually first if (_flags & KIO::Overwrite) { if (!QFile::remove(dest_orig)) { execWithElevatedPrivilege(DEL, {dest_orig}, errno); } } if (!QFile::rename(dest, dest_orig)) { if (auto err = execWithElevatedPrivilege(RENAME, {dest, dest_orig}, errno)) { if (!err.wasCanceled()) { qCWarning(KIO_FILE) << " Couldn't rename " << dest << " to " << dest_orig; error(KIO::ERR_CANNOT_RENAME_PARTIAL, dest_orig); } return; } } org::kde::KDirNotify::emitFileRenamed(QUrl::fromLocalFile(dest), QUrl::fromLocalFile(dest_orig)); } // set final permissions if (_mode != -1 && !(_flags & KIO::Resume)) { if (!QFile::setPermissions(dest_orig, modeToQFilePermissions(_mode))) { // couldn't chmod. Eat the error if the filesystem apparently doesn't support it. KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(dest_orig); if (mp && mp->testFileSystemFlag(KMountPoint::SupportsChmod)) { if (tryChangeFileAttr(CHMOD, {dest_orig, _mode}, errno)) { warning(i18n("Could not change permissions for\n%1", dest_orig)); } } } } // set modification time const QString mtimeStr = metaData(QStringLiteral("modified")); if (!mtimeStr.isEmpty()) { QDateTime dt = QDateTime::fromString(mtimeStr, Qt::ISODate); if (dt.isValid()) { QT_STATBUF dest_statbuf; if (QT_STAT(QFile::encodeName(dest_orig).constData(), &dest_statbuf) == 0) { #ifndef Q_OS_WIN struct timeval utbuf[2]; // access time utbuf[0].tv_sec = dest_statbuf.st_atime; // access time, unchanged ## TODO preserve msec utbuf[0].tv_usec = 0; // modification time utbuf[1].tv_sec = dt.toSecsSinceEpoch(); utbuf[1].tv_usec = dt.time().msec() * 1000; utimes(QFile::encodeName(dest_orig).constData(), utbuf); #else struct utimbuf utbuf; utbuf.actime = dest_statbuf.st_atime; utbuf.modtime = dt.toSecsSinceEpoch(); if (utime(QFile::encodeName(dest_orig).constData(), &utbuf) != 0) { tryChangeFileAttr(UTIME, {dest_orig, qint64(utbuf.actime), qint64(utbuf.modtime)}, errno); } #endif } } } // We have done our job => finish finished(); } QString FileProtocol::getUserName(KUserId uid) const { if (Q_UNLIKELY(!uid.isValid())) { return QString(); } auto it = mUsercache.find(uid); if (it == mUsercache.end()) { KUser user(uid); QString name = user.loginName(); if (name.isEmpty()) { name = uid.toString(); } it = mUsercache.insert(uid, name); } return *it; } QString FileProtocol::getGroupName(KGroupId gid) const { if (Q_UNLIKELY(!gid.isValid())) { return QString(); } auto it = mGroupcache.find(gid); if (it == mGroupcache.end()) { KUserGroup group(gid); QString name = group.name(); if (name.isEmpty()) { name = gid.toString(); } it = mGroupcache.insert(gid, name); } return *it; } #if HAVE_STATX // statx syscall is available inline int LSTAT(const char* path, struct statx * buff) { return statx(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS | STATX_BTIME, buff); } inline int STAT(const char* path, struct statx * buff) { return statx(AT_FDCWD, path, AT_STATX_SYNC_AS_STAT, STATX_BASIC_STATS | STATX_BTIME, buff); } inline static uint16_t stat_mode(struct statx &buf) { return buf.stx_mode; } inline static uint32_t stat_dev(struct statx &buf) { return buf.stx_dev_major; } inline static uint64_t stat_ino(struct statx &buf) { return buf.stx_ino; } inline static uint64_t stat_size(struct statx &buf) { return buf.stx_size; } inline static uint32_t stat_uid(struct statx &buf) { return buf.stx_uid; } inline static uint32_t stat_gid(struct statx &buf) { return buf.stx_gid; } inline static int64_t stat_atime(struct statx &buf) { return buf.stx_atime.tv_sec; } inline static int64_t stat_mtime(struct statx &buf) { return buf.stx_mtime.tv_sec; } #else // regular stat struct inline int LSTAT(const char* path, QT_STATBUF * buff) { return QT_LSTAT(path, buff); } inline int STAT(const char* path, QT_STATBUF * buff) { return QT_STAT(path, buff); } inline static mode_t stat_mode(QT_STATBUF &buf) { return buf.st_mode; } inline static dev_t stat_dev(QT_STATBUF &buf) { return buf.st_dev; } inline static ino_t stat_ino(QT_STATBUF &buf) { return buf.st_ino; } inline static off_t stat_size(QT_STATBUF &buf) { return buf.st_size; } #ifndef Q_OS_WIN inline static uid_t stat_uid(QT_STATBUF &buf) { return buf.st_uid; } inline static gid_t stat_gid(QT_STATBUF &buf) { return buf.st_gid; } #endif inline static time_t stat_atime(QT_STATBUF &buf) { return buf.st_atime; } inline static time_t stat_mtime(QT_STATBUF &buf) { return buf.st_mtime; } #endif bool FileProtocol::createUDSEntry(const QString &filename, const QByteArray &path, UDSEntry &entry, short int details) { assert(entry.count() == 0); // by contract :-) switch (details) { case 0: // filename, access, type, size, linkdest entry.reserve(5); break; case 1: // uid, gid, atime, mtime, btime entry.reserve(10); break; case 2: // acl data entry.reserve(13); break; default: // case details > 2 // dev, inode entry.reserve(15); break; } entry.fastInsert(KIO::UDSEntry::UDS_NAME, filename); mode_t type; mode_t access; bool isBrokenSymLink = false; signed long long size = 0LL; #if HAVE_POSIX_ACL QByteArray targetPath = path; #endif #if HAVE_STATX // statx syscall is available struct statx buff; #else QT_STATBUF buff; #endif if (LSTAT(path.data(), &buff) == 0) { if (details > 2) { entry.fastInsert(KIO::UDSEntry::UDS_DEVICE_ID, stat_dev(buff)); entry.fastInsert(KIO::UDSEntry::UDS_INODE, stat_ino(buff)); } if ((stat_mode(buff) & QT_STAT_MASK) == QT_STAT_LNK) { #ifdef Q_OS_WIN const QString linkTarget = QFile::symLinkTarget(QFile::decodeName(path)); #else // Use readlink on Unix because symLinkTarget turns relative targets into absolute (#352927) #if HAVE_STATX size_t lowerBound = 256; size_t higherBound = 1024; uint64_t s = stat_size(buff); if (s > SIZE_MAX) { qCWarning(KIO_FILE) << "file size bigger than SIZE_MAX, too big for readlink use!" << path; return false; } size_t size = static_cast(s); using SizeType = size_t; #else off_t lowerBound = 256; off_t higherBound = 1024; off_t size = stat_size(buff); using SizeType = off_t; #endif SizeType bufferSize = qBound(lowerBound, size +1, higherBound); QByteArray linkTargetBuffer; linkTargetBuffer.resize(bufferSize); while (true) { ssize_t n = readlink(path.constData(), linkTargetBuffer.data(), bufferSize); if (n < 0 && errno != ERANGE) { qCWarning(KIO_FILE) << "readlink failed!" << path; return false; } else if (n > 0 && static_cast(n) != bufferSize) { // the buffer was not filled in the last iteration // we are finished reading, break the loop linkTargetBuffer.truncate(n); break; } bufferSize *= 2; linkTargetBuffer.resize(bufferSize); } const QString linkTarget = QFile::decodeName(linkTargetBuffer); #endif entry.fastInsert(KIO::UDSEntry::UDS_LINK_DEST, linkTarget); // A symlink -> follow it only if details>1 if (details > 1) { if (STAT(path.constData(), &buff) == -1) { isBrokenSymLink = true; } else { #if HAVE_POSIX_ACL // valid symlink, will get the ACLs of the destination targetPath = linkTargetBuffer; #endif } } } } else { // qCWarning(KIO_FILE) << "lstat didn't work on " << path.data(); return false; } if (isBrokenSymLink) { // It is a link pointing to nowhere type = S_IFMT - 1; access = S_IRWXU | S_IRWXG | S_IRWXO; size = 0LL; } else { type = stat_mode(buff) & S_IFMT; // extract file type access = stat_mode(buff) & 07777; // extract permissions size = stat_size(buff); } entry.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, type); entry.fastInsert(KIO::UDSEntry::UDS_ACCESS, access); entry.fastInsert(KIO::UDSEntry::UDS_SIZE, size); #if HAVE_POSIX_ACL if (details > 1) { /* Append an atom indicating whether the file has extended acl information * and if withACL is specified also one with the acl itself. If it's a directory * and it has a default ACL, also append that. */ appendACLAtoms(targetPath, entry, type); } #endif if (details > 0) { entry.fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, stat_mtime(buff)); entry.fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME, stat_atime(buff)); #ifndef Q_OS_WIN entry.fastInsert(KIO::UDSEntry::UDS_USER, getUserName(KUserId(stat_uid(buff)))); entry.fastInsert(KIO::UDSEntry::UDS_GROUP, getGroupName(KGroupId(stat_gid(buff)))); #else #pragma message("TODO: st_uid and st_gid are always zero, use GetSecurityInfo to find the owner") #endif #ifdef st_birthtime /* For example FreeBSD's and NetBSD's stat contains a field for * the inode birth time: st_birthtime * This however only works on UFS and ZFS, and not, on say, NFS. * Instead of setting a bogus fallback like st_mtime, only use * it if it is greater than 0. */ if (buff.st_birthtime > 0) { entry.fastInsert(KIO::UDSEntry::UDS_CREATION_TIME, buff.st_birthtime); } #elif defined __st_birthtime /* As above, but OpenBSD calls it slightly differently. */ if (buff.__st_birthtime > 0) { entry.fastInsert(KIO::UDSEntry::UDS_CREATION_TIME, buff.__st_birthtime); } #elif HAVE_STATX /* And linux version using statx syscall */ if (buff.stx_mask & STATX_BTIME) { entry.fastInsert(KIO::UDSEntry::UDS_CREATION_TIME, buff.stx_btime.tv_sec); } #endif } return true; } void FileProtocol::special(const QByteArray &data) { int tmp; QDataStream stream(data); stream >> tmp; switch (tmp) { case 1: { QString fstype, dev, point; qint8 iRo; stream >> iRo >> fstype >> dev >> point; bool ro = (iRo != 0); // qDebug() << "MOUNTING fstype=" << fstype << " dev=" << dev << " point=" << point << " ro=" << ro; bool ok = pmount(dev); if (ok) { finished(); } else { mount(ro, fstype.toLatin1().constData(), dev, point); } } break; case 2: { QString point; stream >> point; bool ok = pumount(point); if (ok) { finished(); } else { unmount(point); } } break; default: break; } } static QStringList fallbackSystemPath() { return QStringList{ QStringLiteral("/sbin"), QStringLiteral("/bin"), }; } void FileProtocol::mount(bool _ro, const char *_fstype, const QString &_dev, const QString &_point) { // qDebug() << "fstype=" << _fstype; #ifndef _WIN32_WCE #if HAVE_VOLMGT /* * support for Solaris volume management */ QString err; QByteArray devname = QFile::encodeName(_dev); if (volmgt_running()) { // qDebug() << "VOLMGT: vold ok."; if (volmgt_check(devname.data()) == 0) { // qDebug() << "VOLMGT: no media in " << devname.data(); err = i18n("No Media inserted or Media not recognized."); error(KIO::ERR_CANNOT_MOUNT, err); return; } else { // qDebug() << "VOLMGT: " << devname.data() << ": media ok"; finished(); return; } } else { err = i18n("\"vold\" is not running."); // qDebug() << "VOLMGT: " << err; error(KIO::ERR_CANNOT_MOUNT, err); return; } #else QTemporaryFile tmpFile; tmpFile.setAutoRemove(false); tmpFile.open(); QByteArray tmpFileName = QFile::encodeName(tmpFile.fileName()); QByteArray dev; if (_dev.startsWith(QLatin1String("LABEL="))) { // turn LABEL=foo into -L foo (#71430) QString labelName = _dev.mid(6); dev = "-L " + QFile::encodeName(KShell::quoteArg(labelName)); // is it correct to assume same encoding as filesystem? } else if (_dev.startsWith(QLatin1String("UUID="))) { // and UUID=bar into -U bar QString uuidName = _dev.mid(5); dev = "-U " + QFile::encodeName(KShell::quoteArg(uuidName)); } else { dev = QFile::encodeName(KShell::quoteArg(_dev)); // get those ready to be given to a shell } QByteArray point = QFile::encodeName(KShell::quoteArg(_point)); bool fstype_empty = !_fstype || !*_fstype; QByteArray fstype = KShell::quoteArg(QString::fromLatin1(_fstype)).toLatin1(); // good guess QByteArray readonly = _ro ? "-r" : ""; QByteArray mountProg = QStandardPaths::findExecutable(QStringLiteral("mount")).toLocal8Bit(); if (mountProg.isEmpty()) { mountProg = QStandardPaths::findExecutable(QStringLiteral("mount"), fallbackSystemPath()).toLocal8Bit(); } if (mountProg.isEmpty()) { error(KIO::ERR_CANNOT_MOUNT, i18n("Could not find program \"mount\"")); return; } // Two steps, in case mount doesn't like it when we pass all options for (int step = 0; step <= 1; step++) { QByteArray buffer = mountProg + ' '; // Mount using device only if no fstype nor mountpoint (KDE-1.x like) if (!dev.isEmpty() && _point.isEmpty() && fstype_empty) { buffer += dev; } else // Mount using the mountpoint, if no fstype nor device (impossible in first step) if (!_point.isEmpty() && dev.isEmpty() && fstype_empty) { buffer += point; } else // mount giving device + mountpoint but no fstype if (!_point.isEmpty() && !dev.isEmpty() && fstype_empty) { buffer += readonly + ' ' + dev + ' ' + point; } else // mount giving device + mountpoint + fstype #if defined(__svr4__) && defined(Q_OS_SOLARIS) // MARCO for Solaris 8 and I // believe this is true for SVR4 in general buffer += "-F " + fstype + ' ' + (_ro ? "-oro" : "") + ' ' + dev + ' ' + point; #else buffer += readonly + " -t " + fstype + ' ' + dev + ' ' + point; #endif buffer += " 2>" + tmpFileName; // qDebug() << buffer; int mount_ret = system(buffer.constData()); QString err = readLogFile(tmpFileName); if (err.isEmpty() && mount_ret == 0) { finished(); return; } else { // Didn't work - or maybe we just got a warning KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByDevice(_dev); // Is the device mounted ? if (mp && mount_ret == 0) { // qDebug() << "mount got a warning:" << err; warning(err); finished(); return; } else { if ((step == 0) && !_point.isEmpty()) { // qDebug() << err; // qDebug() << "Mounting with those options didn't work, trying with only mountpoint"; fstype = ""; fstype_empty = true; dev = ""; // The reason for trying with only mountpoint (instead of // only device) is that some people (hi Malte!) have the // same device associated with two mountpoints // for different fstypes, like /dev/fd0 /mnt/e2floppy and // /dev/fd0 /mnt/dosfloppy. // If the user has the same mountpoint associated with two // different devices, well they shouldn't specify the // mountpoint but just the device. } else { error(KIO::ERR_CANNOT_MOUNT, err); return; } } } } #endif /* ! HAVE_VOLMGT */ #else QString err; err = i18n("mounting is not supported by wince."); error(KIO::ERR_CANNOT_MOUNT, err); #endif } void FileProtocol::unmount(const QString &_point) { #ifndef _WIN32_WCE QByteArray buffer; QTemporaryFile tmpFile; tmpFile.setAutoRemove(false); tmpFile.open(); QByteArray tmpFileName = QFile::encodeName(tmpFile.fileName()); QString err; #if HAVE_VOLMGT /* * support for Solaris volume management */ char *devname; char *ptr; FILE *mnttab; struct mnttab mnt; if (volmgt_running()) { // qDebug() << "VOLMGT: looking for " << _point.toLocal8Bit(); if ((mnttab = QT_FOPEN(MNTTAB, "r")) == nullptr) { err = QLatin1String("could not open mnttab"); // qDebug() << "VOLMGT: " << err; error(KIO::ERR_CANNOT_UNMOUNT, err); return; } /* * since there's no way to derive the device name from * the mount point through the volmgt library (and * media_findname() won't work in this case), we have to * look ourselves... */ devname = nullptr; rewind(mnttab); while (getmntent(mnttab, &mnt) == nullptr) { if (strcmp(_point.toLocal8Bit(), mnt.mnt_mountp) == 0) { devname = mnt.mnt_special; break; } } fclose(mnttab); if (devname == nullptr) { err = QLatin1String("not in mnttab"); // qDebug() << "VOLMGT: " << QFile::encodeName(_point).data() << ": " << err; error(KIO::ERR_CANNOT_UNMOUNT, err); return; } /* * strip off the directory name (volume name) * the eject(1) command will handle unmounting and * physically eject the media (if possible) */ ptr = strrchr(devname, '/'); *ptr = '\0'; QByteArray qdevname(QFile::encodeName(KShell::quoteArg(QFile::decodeName(QByteArray(devname)))).data()); buffer = "/usr/bin/eject " + qdevname + " 2>" + tmpFileName; // qDebug() << "VOLMGT: eject " << qdevname; /* * from eject(1): exit status == 0 => need to manually eject * exit status == 4 => media was ejected */ if (WEXITSTATUS(system(buffer.constData())) == 4) { /* * this is not an error, so skip "readLogFile()" * to avoid wrong/confusing error popup. The * temporary file is removed by QTemporaryFile's * destructor, so don't do that manually. */ finished(); return; } } else { /* * eject(1) should do its job without vold(1M) running, * so we probably could call eject anyway, but since the * media is mounted now, vold must've died for some reason * during the user's session, so it should be restarted... */ err = i18n("\"vold\" is not running."); // qDebug() << "VOLMGT: " << err; error(KIO::ERR_CANNOT_UNMOUNT, err); return; } #else QByteArray umountProg = QStandardPaths::findExecutable(QStringLiteral("umount")).toLocal8Bit(); if (umountProg.isEmpty()) { umountProg = QStandardPaths::findExecutable(QStringLiteral("umount"), fallbackSystemPath()).toLocal8Bit(); } if (umountProg.isEmpty()) { error(KIO::ERR_CANNOT_UNMOUNT, i18n("Could not find program \"umount\"")); return; } buffer = umountProg + ' ' + QFile::encodeName(KShell::quoteArg(_point)) + " 2>" + tmpFileName; system(buffer.constData()); #endif /* HAVE_VOLMGT */ err = readLogFile(tmpFileName); if (err.isEmpty()) { finished(); } else { error(KIO::ERR_CANNOT_UNMOUNT, err); } #else QString err; err = i18n("unmounting is not supported by wince."); error(KIO::ERR_CANNOT_MOUNT, err); #endif } /************************************* * * pmount handling * *************************************/ bool FileProtocol::pmount(const QString &dev) { #ifndef _WIN32_WCE QString pmountProg = QStandardPaths::findExecutable(QStringLiteral("pmount")); if (pmountProg.isEmpty()) { pmountProg = QStandardPaths::findExecutable(QStringLiteral("pmount"), fallbackSystemPath()); } if (pmountProg.isEmpty()) { return false; } QByteArray buffer = QFile::encodeName(pmountProg) + ' ' + QFile::encodeName(KShell::quoteArg(dev)); int res = system(buffer.constData()); return res == 0; #else return false; #endif } bool FileProtocol::pumount(const QString &point) { #ifndef _WIN32_WCE KMountPoint::Ptr mp = KMountPoint::currentMountPoints(KMountPoint::NeedRealDeviceName).findByPath(point); if (!mp) { return false; } QString dev = mp->realDeviceName(); if (dev.isEmpty()) { return false; } QString pumountProg = QStandardPaths::findExecutable(QStringLiteral("pumount")); if (pumountProg.isEmpty()) { pumountProg = QStandardPaths::findExecutable(QStringLiteral("pumount"), fallbackSystemPath()); } if (pumountProg.isEmpty()) { return false; } const QByteArray buffer = QFile::encodeName(pumountProg) + ' ' + QFile::encodeName(KShell::quoteArg(dev)); int res = system(buffer.data()); return res == 0; #else return false; #endif } /************************************* * * Utilities * *************************************/ static QString readLogFile(const QByteArray &_filename) { QString result; QFile file(QFile::decodeName(_filename)); if (file.open(QIODevice::ReadOnly)) { result = QString::fromLocal8Bit(file.readAll()); } (void)file.remove(); return result; } /************************************* * * ACL handling helpers * *************************************/ #if HAVE_POSIX_ACL bool FileProtocol::isExtendedACL(acl_t acl) { return (acl_equiv_mode(acl, nullptr) != 0); } static void appendACLAtoms(const QByteArray &path, UDSEntry &entry, mode_t type) { // first check for a noop if (acl_extended_file(path.data()) == 0) { return; } acl_t acl = nullptr; acl_t defaultAcl = nullptr; bool isDir = (type & QT_STAT_MASK) == QT_STAT_DIR; // do we have an acl for the file, and/or a default acl for the dir, if it is one? acl = acl_get_file(path.data(), ACL_TYPE_ACCESS); /* Sadly libacl does not provided a means of checking for extended ACL and default * ACL separately. Since a directory can have both, we need to check again. */ if (isDir) { if (acl) { if (!FileProtocol::isExtendedACL(acl)) { acl_free(acl); acl = nullptr; } } defaultAcl = acl_get_file(path.data(), ACL_TYPE_DEFAULT); } if (acl || defaultAcl) { // qDebug() << path.constData() << "has extended ACL entries"; entry.fastInsert(KIO::UDSEntry::UDS_EXTENDED_ACL, 1); if (acl) { const QString str = aclToText(acl); entry.fastInsert(KIO::UDSEntry::UDS_ACL_STRING, str); // qDebug() << path.constData() << "ACL:" << str; acl_free(acl); } if (defaultAcl) { const QString str = aclToText(defaultAcl); entry.fastInsert(KIO::UDSEntry::UDS_DEFAULT_ACL_STRING, str); // qDebug() << path.constData() << "DEFAULT ACL:" << str; acl_free(defaultAcl); } } } #endif // We could port this to KTempDir::removeDir but then we wouldn't be able to tell the user // where exactly the deletion failed, in case of errors. bool FileProtocol::deleteRecursive(const QString &path) { //qDebug() << path; QDirIterator it(path, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden, QDirIterator::Subdirectories); QStringList dirsToDelete; while (it.hasNext()) { const QString itemPath = it.next(); //qDebug() << "itemPath=" << itemPath; const QFileInfo info = it.fileInfo(); if (info.isDir() && !info.isSymLink()) { dirsToDelete.prepend(itemPath); } else { //qDebug() << "QFile::remove" << itemPath; if (!QFile::remove(itemPath)) { if (auto err = execWithElevatedPrivilege(DEL, {itemPath}, errno)) { if (!err.wasCanceled()) { error(KIO::ERR_CANNOT_DELETE, itemPath); } return false; } } } } QDir dir; for (const QString &itemPath : qAsConst(dirsToDelete)) { //qDebug() << "QDir::rmdir" << itemPath; if (!dir.rmdir(itemPath)) { if (auto err = execWithElevatedPrivilege(RMDIR, {itemPath}, errno)) { if (!err.wasCanceled()) { error(KIO::ERR_CANNOT_DELETE, itemPath); } return false; } } } return true; } void FileProtocol::fileSystemFreeSpace(const QUrl &url) { if (url.isLocalFile()) { const KDiskFreeSpaceInfo spaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(url.toLocalFile()); if (spaceInfo.isValid()) { setMetaData(QStringLiteral("total"), QString::number(spaceInfo.size())); setMetaData(QStringLiteral("available"), QString::number(spaceInfo.available())); finished(); } else { error(KIO::ERR_CANNOT_STAT, url.url()); } } else { error(KIO::ERR_UNSUPPORTED_PROTOCOL, url.url()); } } void FileProtocol::virtual_hook(int id, void *data) { switch(id) { case SlaveBase::GetFileSystemFreeSpace: { QUrl *url = static_cast(data); fileSystemFreeSpace(*url); } break; + case SlaveBase::Truncate: { + auto length = static_cast(data); + truncate(*length); + } break; default: { SlaveBase::virtual_hook(id, data); } break; } } // needed for JSON file embedding #include "file.moc" diff --git a/src/ioslaves/file/file.h b/src/ioslaves/file/file.h index b8ae5c0e..1f819632 100644 --- a/src/ioslaves/file/file.h +++ b/src/ioslaves/file/file.h @@ -1,125 +1,126 @@ /* Copyright (C) 2000-2002 Stephan Kulow Copyright (C) 2000-2002 David Faure Copyright (C) 2000-2002 Waldo Bastian This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License (LGPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef __file_h__ #define __file_h__ #include #include #include #include #include #include #include // mode_t #include #if HAVE_POSIX_ACL #include #include #endif #include "file_p.h" #include Q_DECLARE_LOGGING_CATEGORY(KIO_FILE) class FileProtocol : public QObject, public KIO::SlaveBase { Q_OBJECT public: FileProtocol(const QByteArray &pool, const QByteArray &app); virtual ~FileProtocol(); void get(const QUrl &url) override; virtual void put(const QUrl &url, int _mode, KIO::JobFlags _flags) override; virtual void copy(const QUrl &src, const QUrl &dest, int mode, KIO::JobFlags flags) override; virtual void rename(const QUrl &src, const QUrl &dest, KIO::JobFlags flags) override; virtual void symlink(const QString &target, const QUrl &dest, KIO::JobFlags flags) override; void stat(const QUrl &url) override; void listDir(const QUrl &url) override; void mkdir(const QUrl &url, int permissions) override; void chmod(const QUrl &url, int permissions) override; void chown(const QUrl &url, const QString &owner, const QString &group) override; void setModificationTime(const QUrl &url, const QDateTime &mtime) override; void del(const QUrl &url, bool isfile) override; void open(const QUrl &url, QIODevice::OpenMode mode) override; void read(KIO::filesize_t size) override; void write(const QByteArray &data) override; void seek(KIO::filesize_t offset) override; + void truncate(KIO::filesize_t length); void close() override; /** * Special commands supported by this slave: * 1 - mount * 2 - unmount */ void special(const QByteArray &data) override; void unmount(const QString &point); void mount(bool _ro, const char *_fstype, const QString &dev, const QString &point); bool pumount(const QString &point); bool pmount(const QString &dev); #if HAVE_POSIX_ACL static bool isExtendedACL(acl_t acl); #endif protected: void virtual_hook(int id, void *data) override; private: bool createUDSEntry(const QString &filename, const QByteArray &path, KIO::UDSEntry &entry, short int details); int setACL(const char *path, mode_t perm, bool _directoryDefault); QString getUserName(KUserId uid) const; QString getGroupName(KGroupId gid) const; bool deleteRecursive(const QString &path); void fileSystemFreeSpace(const QUrl &url); // KF6 TODO: Turn into virtual method in SlaveBase bool privilegeOperationUnitTestMode(); PrivilegeOperationReturnValue execWithElevatedPrivilege(ActionType action, const QVariantList &args, int errcode); PrivilegeOperationReturnValue tryOpen(QFile &f, const QByteArray &path, int flags, int mode, int errcode); // We want to execute chmod/chown/utime with elevated privileges (in copy & put) // only during the brief period privileges are elevated. If it's not the case show // a warning and continue. PrivilegeOperationReturnValue tryChangeFileAttr(ActionType action, const QVariantList &args, int errcode); void redirect(const QUrl &url); // Close without calling finish(). Use this to close after error. void closeWithoutFinish(); private: mutable QHash mUsercache; mutable QHash mGroupcache; QFile *mFile; }; #endif diff --git a/src/ioslaves/file/file.json b/src/ioslaves/file/file.json index 514ee0a7..d762b5f2 100644 --- a/src/ioslaves/file/file.json +++ b/src/ioslaves/file/file.json @@ -1,34 +1,35 @@ { "KDE-KIO-Protocols": { "file": { "Class": ":local", "ExtraNames": [], "Icon": "folder", "X-DocPath": "kioslave5/file/index.html", "deleteRecursive": true, "deleting": true, "exec": "kf5/kio/file", "input": "none", "linking": true, "listing": [ "Name", "Type", "Size", "Date", "AccessDate", "Access", "Owner", "Group", "Link" ], "makedir": true, "maxInstances": 5, "moving": true, "opening": true, + "truncating": true, "output": "filesystem", "protocol": "file", "reading": true, "writing": true } } }