diff --git a/src/core/global.h b/src/core/global.h index 294c357b..d1088f10 100644 --- a/src/core/global.h +++ b/src/core/global.h @@ -1,332 +1,342 @@ /* 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); /** * Calculates remaining time from total size, processed size and speed. * Warning: As QTime is limited to 23:59:59, use calculateRemainingSeconds() instead * * @param totalSize total size in bytes * @param processedSize processed size in bytes * @param speed speed in bytes per second * @return calculated remaining time */ #ifndef KIOCORE_NO_DEPRECATED KIOCORE_DEPRECATED_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); /** * 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 */ KIOCORE_EXPORT QString suggestName(const QUrl &baseURL, const QString &oldName); /** * 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, ERR_COULD_NOT_CREATE_SOCKET = KJob::UserDefinedError + 22, ///< @deprecated ERR_CANNOT_CREATE_SOCKET = KJob::UserDefinedError + 22, ERR_COULD_NOT_CONNECT = KJob::UserDefinedError + 23, ///< @deprecated ERR_CANNOT_CONNECT = KJob::UserDefinedError + 23, ERR_CONNECTION_BROKEN = KJob::UserDefinedError + 24, ERR_NOT_FILTER_PROTOCOL = KJob::UserDefinedError + 25, ERR_COULD_NOT_MOUNT = KJob::UserDefinedError + 26, ///< @deprecated ERR_CANNOT_MOUNT = KJob::UserDefinedError + 26, ERR_COULD_NOT_UNMOUNT = KJob::UserDefinedError + 27, ///< @deprecated ERR_CANNOT_UNMOUNT = KJob::UserDefinedError + 27, ERR_COULD_NOT_READ = KJob::UserDefinedError + 28, ///< @deprecated ERR_CANNOT_READ = KJob::UserDefinedError + 28, ERR_COULD_NOT_WRITE = KJob::UserDefinedError + 29, ///< @deprecated ERR_CANNOT_WRITE = KJob::UserDefinedError + 29, ERR_COULD_NOT_BIND = KJob::UserDefinedError + 30, ///< @deprecated ERR_CANNOT_BIND = KJob::UserDefinedError + 30, ERR_COULD_NOT_LISTEN = KJob::UserDefinedError + 31, ///< @deprecated ERR_CANNOT_LISTEN = KJob::UserDefinedError + 31, ERR_COULD_NOT_ACCEPT = KJob::UserDefinedError + 32, ///< @deprecated ERR_CANNOT_ACCEPT = KJob::UserDefinedError + 32, ERR_COULD_NOT_LOGIN = KJob::UserDefinedError + 33, ///< @deprecated ERR_CANNOT_LOGIN = KJob::UserDefinedError + 33, ERR_COULD_NOT_STAT = KJob::UserDefinedError + 34, ///< @deprecated ERR_CANNOT_STAT = KJob::UserDefinedError + 34, ERR_COULD_NOT_CLOSEDIR = KJob::UserDefinedError + 35, ///< @deprecated ERR_CANNOT_CLOSEDIR = KJob::UserDefinedError + 35, ERR_COULD_NOT_MKDIR = KJob::UserDefinedError + 37, ///< @deprecated ERR_CANNOT_MKDIR = KJob::UserDefinedError + 37, ERR_COULD_NOT_RMDIR = KJob::UserDefinedError + 38, ///< @deprecated 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, ERR_COULD_NOT_AUTHENTICATE = KJob::UserDefinedError + 46, ///< @deprecated 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) 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 "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" #ifndef NDEBUG #if HAVE_BACKTRACE #include #endif #endif extern "C" { Q_NORETURN static void sigsegv_handler(int sig); 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 *q; SlaveBasePrivate(SlaveBase *owner): q(owner), m_passwdServerClient(nullptr) { if (!qEnvironmentVariableIsEmpty("KIOSLAVE_ENABLE_TESTMODE")) { QStandardPaths::enableTestMode(true); } pendingListEntries.reserve(KIO_MAX_ENTRIES_PER_BATCH); } ~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; KConfigGroup *configGroup; QUrl onHoldUrl; QDateTime lastTimeout; QDateTime nextTimeout; KIO::filesize_t totalSize; KRemoteEncoding *remotefile; enum { Idle, InsideMethod, FinishedCalled, ErrorCalled } m_state; QByteArray timeoutData; KPasswdServerClient *m_passwdServerClient; bool m_rootEntryListed = false; // Reconstructs configGroup from configData and mIncomingMetaData void rebuildConfig() { configGroup->deleteGroup(KConfigGroup::WriteConfigFlags()); // 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) { configGroup->writeEntry(it.key(), it->toUtf8(), KConfigGroup::WriteConfigFlags()); } end = q->mIncomingMetaData.constEnd(); for (MetaData::ConstIterator it = q->mIncomingMetaData.constBegin(); it != end; ++it) { configGroup->writeEntry(it.key(), it->toUtf8(), KConfigGroup::WriteConfigFlags()); } } void verifyState(const char *cmdName) { if ((m_state != FinishedCalled) && (m_state != ErrorCalled)) { qCWarning(KIO_CORE) << cmdName << "did not call finished() or error()! Please fix the" << QCoreApplication::applicationName() << "KIO slave"; } } void verifyErrorFinishedNotCalled(const char *cmdName) { if (m_state == FinishedCalled || m_state == ErrorCalled) { qCWarning(KIO_CORE) << cmdName << "called finished() or error(), but it's not supposed to! Please fix the" << QCoreApplication::applicationName() << "KIO slave"; } } 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(); #ifdef Q_OS_UNIX if (qEnvironmentVariableIsEmpty("KDE_DEBUG")) { ::signal(SIGSEGV, &sigsegv_handler); ::signal(SIGILL, &sigsegv_handler); ::signal(SIGTRAP, &sigsegv_handler); ::signal(SIGABRT, &sigsegv_handler); ::signal(SIGBUS, &sigsegv_handler); ::signal(SIGALRM, &sigsegv_handler); ::signal(SIGFPE, &sigsegv_handler); #ifdef SIGPOLL ::signal(SIGPOLL, &sigsegv_handler); #endif #ifdef SIGSYS ::signal(SIGSYS, &sigsegv_handler); #endif #ifdef SIGVTALRM ::signal(SIGVTALRM, &sigsegv_handler); #endif #ifdef SIGXCPU ::signal(SIGXCPU, &sigsegv_handler); #endif #ifdef SIGXFSZ ::signal(SIGXFSZ, &sigsegv_handler); #endif } 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 = protocol; d->slaveid += QString::number(getpid()); d->resume = false; d->needSendCanResume = false; d->config = new KConfig(QString(), KConfig::SimpleConfig); // The KConfigGroup needs the KConfig to exist during its whole lifetime. d->configGroup = new KConfigGroup(d->config, QString()); 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 < QDateTime::currentDateTime())) { QByteArray data = d->timeoutData; d->nextTimeout = QDateTime(); d->timeoutData = QByteArray(); special(data); } Q_ASSERT(d->appConnection.inited()); int ms = -1; if (d->nextTimeout.isValid()) { ms = qMax(QDateTime::currentDateTime().msecsTo(d->nextTimeout), 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(); 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; } KConfigGroup *SlaveBase::config() { 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)); } 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) { if (d->m_state == d->ErrorCalled) { qCWarning(KIO_CORE) << "error() called twice! Please fix the" << QCoreApplication::applicationName() << "KIO slave"; return; } else if (d->m_state == d->FinishedCalled) { qCWarning(KIO_CORE) << "error() called after finished()! Please fix the" << QCoreApplication::applicationName() << "KIO slave"; 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; } 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.insert(KIO::UDSEntry::UDS_NAME, QStringLiteral(".")); entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); entry.insert(KIO::UDSEntry::UDS_SIZE, 0); entry.insert(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(); } if (d->m_state == d->FinishedCalled) { qCWarning(KIO_CORE) << "finished() called twice! Please fix the" << QCoreApplication::applicationName() << "KIO slave"; return; } else if (d->m_state == d->ErrorCalled) { qCWarning(KIO_CORE) << "finished() called after error()! Please fix the" << QCoreApplication::applicationName() << "KIO slave"; 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; } 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; if (d->onHold) { stream << d->onHoldUrl; } send(MSG_SLAVE_STATUS, 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; QDateTime now = QDateTime::currentDateTime(); if (_bytes == d->totalSize) { emitSignal = true; } else { if (d->lastTimeout.isValid()) { emitSignal = d->lastTimeout.msecsTo(now) >= 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 = now; } // 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::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); } #ifndef KIOCORE_NO_DEPRECATED 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); } #endif void SlaveBase::statEntry(const UDSEntry &entry) { KIO_DATA << entry; send(MSG_STAT_ENTRY, data); } #ifndef KIOCORE_NO_DEPRECATED 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) == QLatin1String(".")) { d->m_rootEntryListed = true; } listEntries(d->pendingListEntries); d->pendingListEntries.clear(); } else { listEntry(entry); } } #endif 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) == QLatin1String(".")) { 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); foreach (const UDSEntry &entry, list) { stream << entry; } send(MSG_LIST_ENTRIES, data); } Q_NORETURN static void sigsegv_handler(int sig) { #ifdef Q_OS_UNIX ::signal(sig, SIG_DFL); // Next one kills //Kill us if we deadlock ::signal(SIGALRM, SIG_DFL); alarm(5); //generate an alarm signal in 5 seconds, in this time the slave has to exit // Debug and printf should be avoided because they might // call malloc.. and get in a nice recursive malloc loop char buffer[120]; qsnprintf(buffer, sizeof(buffer), "kioslave: ####### CRASH ###### protocol = %s pid = %d signal = %d\n", s_protocol, getpid(), sig); write(2, buffer, strlen(buffer)); #ifndef NDEBUG #if HAVE_BACKTRACE void *trace[256]; int n = backtrace(trace, 256); if (n) { backtrace_symbols_fd(trace, n, 2); } #endif #endif ::exit(1); #endif } 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(void) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_CONNECT)); } void SlaveBase::closeConnection(void) { } // No response! void SlaveBase::stat(QUrl const &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_STAT)); } void SlaveBase::put(QUrl const &, int, JobFlags) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_PUT)); } void SlaveBase::special(const QByteArray &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_SPECIAL)); } void SlaveBase::listDir(QUrl const &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_LISTDIR)); } void SlaveBase::get(QUrl const &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_GET)); } void SlaveBase::open(QUrl const &, QIODevice::OpenMode) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_OPEN)); } void SlaveBase::read(KIO::filesize_t) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_READ)); } void SlaveBase::write(const QByteArray &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_WRITE)); } void SlaveBase::seek(KIO::filesize_t) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_SEEK)); } void SlaveBase::close() { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_CLOSE)); } void SlaveBase::mimetype(QUrl const &url) { get(url); } void SlaveBase::rename(QUrl const &, QUrl const &, JobFlags) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_RENAME)); } void SlaveBase::symlink(QString const &, QUrl const &, JobFlags) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_SYMLINK)); } void SlaveBase::copy(QUrl const &, QUrl const &, int, JobFlags) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_COPY)); } void SlaveBase::del(QUrl const &, bool) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_DEL)); } void SlaveBase::setLinkDest(const QUrl &, const QString &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_SETLINKDEST)); } void SlaveBase::mkdir(QUrl const &, int) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_MKDIR)); } void SlaveBase::chmod(QUrl const &, int) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_CHMOD)); } void SlaveBase::setModificationTime(QUrl const &, const QDateTime &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_SETMODIFICATIONTIME)); } void SlaveBase::chown(QUrl const &, const QString &, const QString &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_CHOWN)); } void SlaveBase::setSubUrl(QUrl const &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_SUBURL)); } void SlaveBase::multiGet(const QByteArray &) { error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, 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->nextTimeout = QDateTime::currentDateTime().addSecs(timeout); } else if (timeout == 0) { d->nextTimeout = QDateTime::currentDateTime().addSecs(1); // Immediate timeout } else { d->nextTimeout = QDateTime(); // Canceled } d->timeoutData = data; } void SlaveBase::dispatch(int command, const QByteArray &data) { QDataStream stream(data); QUrl url; int i; switch (command) { case CMD_HOST: { QString passwd; QString host, user; quint16 port; stream >> host >> port >> user >> passwd; d->m_state = d->InsideMethod; setHost(host, port, user, passwd); d->verifyErrorFinishedNotCalled("setHost()"); 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; slave_status(); // TODO verify that the slave has called slaveStatus()? d->verifyErrorFinishedNotCalled("slave_status()"); 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; reparseConfiguration(); d->verifyErrorFinishedNotCalled("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 KDE. } 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_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 KDE. 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(mProtocol, CMD_FILESYSTEMFREESPACE)); } 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() +{ + QByteArray buffer; + send(MSG_PRIVILEGE_EXEC); + waitForAnswer(MSG_PRIVILEGE_EXEC, 0, buffer); + return KIO::PrivilegeOperationStatus(buffer.toInt()); +} diff --git a/src/core/slavebase.h b/src/core/slavebase.h index a05632a0..43b44791 100644 --- a/src/core/slavebase.h +++ b/src/core/slavebase.h @@ -1,971 +1,979 @@ /* 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 succedes * @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); /** * 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::ButtonCode. */ enum MessageBoxType { QuestionYesNo = 1, WarningYesNo = 2, WarningContinueCancel = 3, WarningYesNoCancel = 4, Information = 5, SSLMessageBox = 6 }; /** * 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 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. */ KConfigGroup *config(); /** * 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); virtual void read(KIO::filesize_t size); virtual void write(const QByteArray &data); virtual void seek(KIO::filesize_t offset); 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 foir HTTP pielining * 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); /** * 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). */ #ifndef KIOCORE_NO_DEPRECATED KIOCORE_DEPRECATED 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()); /** * @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 bool openPasswordDialog(KIO::AuthInfo &info, const QString &errorMsg = QString()); /** * 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); /** * @deprecated for a very very long time, not implemented anymore * Probably dates back to model dialup times. * * 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 currenctly 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. */ #ifndef KIOCORE_NO_DEPRECATED bool requestNetwork(const QString &host = QString()); #endif /** * @deprecated for a very very long time, not implemented anymore * Probably dates back to model dialup times. * * 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. * */ #ifndef KIOCORE_NO_DEPRECATED 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.40 + */ + PrivilegeOperationStatus requestPrivilegeOperation(); + 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 }; virtual void virtual_hook(int id, void *data); private: // 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 c863d33c..d1d52f39 100644 --- a/src/core/slaveinterface.cpp +++ b/src/core/slaveinterface.cpp @@ -1,437 +1,440 @@ /* 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 #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, SIGNAL(timeout()), SLOT(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->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: { 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_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(QStringLiteral("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; } } } 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()); } 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 0d08c1bd..4cd60c55 100644 --- a/src/core/slaveinterface.h +++ b/src/core/slaveinterface.h @@ -1,193 +1,196 @@ /* 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, INF_GETTING_FILE, ///< @deprecated INF_UNUSED = 25, ///< now unused INF_INFOMESSAGE, INF_META_DATA, INF_NETWORK_STATUS, INF_MESSAGEBOX, INF_POSITION // 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, MSG_SLAVE_STATUS, MSG_SLAVE_ACK, // 110 MSG_NET_REQUEST, MSG_NET_DROP, MSG_NEED_SUBURL_DATA, MSG_CANRESUME, MSG_AUTH_KEY, ///< @deprecated MSG_DEL_AUTH_KEY, ///< @deprecated MSG_OPENED, MSG_WRITTEN, - MSG_HOST_INFO_REQ + MSG_HOST_INFO_REQ, + MSG_PRIVILEGE_EXEC // 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(); void setConnection(Connection *connection); Connection *connection() const; // 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 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 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.h b/src/ioslaves/file/file.h index 109ea808..1422590e 100644 --- a/src/ioslaves/file/file.h +++ b/src/ioslaves/file/file.h @@ -1,109 +1,114 @@ /* 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) Q_DECL_OVERRIDE; virtual void put(const QUrl &url, int _mode, KIO::JobFlags _flags) Q_DECL_OVERRIDE; virtual void copy(const QUrl &src, const QUrl &dest, int mode, KIO::JobFlags flags) Q_DECL_OVERRIDE; virtual void rename(const QUrl &src, const QUrl &dest, KIO::JobFlags flags) Q_DECL_OVERRIDE; virtual void symlink(const QString &target, const QUrl &dest, KIO::JobFlags flags) Q_DECL_OVERRIDE; void stat(const QUrl &url) Q_DECL_OVERRIDE; void listDir(const QUrl &url) Q_DECL_OVERRIDE; void mkdir(const QUrl &url, int permissions) Q_DECL_OVERRIDE; void chmod(const QUrl &url, int permissions) Q_DECL_OVERRIDE; void chown(const QUrl &url, const QString &owner, const QString &group) Q_DECL_OVERRIDE; void setModificationTime(const QUrl &url, const QDateTime &mtime) Q_DECL_OVERRIDE; void del(const QUrl &url, bool isfile) Q_DECL_OVERRIDE; void open(const QUrl &url, QIODevice::OpenMode mode) Q_DECL_OVERRIDE; void read(KIO::filesize_t size) Q_DECL_OVERRIDE; void write(const QByteArray &data) Q_DECL_OVERRIDE; void seek(KIO::filesize_t offset) Q_DECL_OVERRIDE; void close() Q_DECL_OVERRIDE; /** * Special commands supported by this slave: * 1 - mount * 2 - unmount */ void special(const QByteArray &data) Q_DECL_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) Q_DECL_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 + PrivilegeOperationReturnValue execWithElevatedPrivilege(ActionType action, const QVariant &arg1, + const QVariant &arg2 = QVariant(), + const QVariant &arg3 = QVariant()); private: mutable QHash mUsercache; mutable QHash mGroupcache; QFile *mFile; }; #endif diff --git a/src/ioslaves/file/file_p.h b/src/ioslaves/file/file_p.h index 18ae2ceb..3ede48e1 100644 --- a/src/ioslaves/file/file_p.h +++ b/src/ioslaves/file/file_p.h @@ -1,37 +1,69 @@ /*** Copyright (C) 2017 by Chinmoy Ranjan Pradhan This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . ***/ #ifndef FILE_P_H #define FILE_P_H enum ActionType { CHMOD = 1, CHOWN, DEL, MKDIR, OPEN, OPENDIR, RENAME, RMDIR, SYMLINK, UTIME, }; +/** + * PrivilegeOperationReturnValue encapsulates the return value from execWithElevatedPrivilege() in a convenient way. + * Warning, this class will cast to a bool that is false on success and true on failure. This unusual solution allows + * to write kioslave code like this: + +if (!dir.rmdir(itemPath)) { + if (auto ret = execWithElevatedPrivilege(RMDIR, itemPath)) { + if (!ret.wasCanceled()) { + error(KIO::ERR_CANNOT_DELETE, itemPath); + } + return false; + } +} +// directory successfully removed, continue with the next operation +*/ +class PrivilegeOperationReturnValue +{ +public: + static PrivilegeOperationReturnValue success() { return PrivilegeOperationReturnValue{false, false}; } + static PrivilegeOperationReturnValue failure() { return PrivilegeOperationReturnValue{true, false}; } + static PrivilegeOperationReturnValue canceled() { return PrivilegeOperationReturnValue{true, true}; } + operator bool() const { return m_failed; } + bool wasCanceled() const { return m_canceled; } +private: + PrivilegeOperationReturnValue(bool failed, bool canceled) : m_failed(failed), m_canceled(canceled) {} + const bool m_failed; + const bool m_canceled; +}; + +class QString; +const QString socketPath(); + #endif diff --git a/src/ioslaves/file/file_unix.cpp b/src/ioslaves/file/file_unix.cpp index 3c1b9927..e369ac24 100644 --- a/src/ioslaves/file/file_unix.cpp +++ b/src/ioslaves/file/file_unix.cpp @@ -1,652 +1,716 @@ /* 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 Copyright (C) 2007 Christian Ehrlicher 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 #include #include #include #include #include +#include + //sendfile has different semantics in different platforms #if defined HAVE_SENDFILE && defined Q_OS_LINUX #define USE_SENDFILE 1 #endif #ifdef USE_SENDFILE #include #endif using namespace KIO; #define MAX_IPC_SIZE (1024*32) static bool same_inode(const QT_STATBUF &src, const QT_STATBUF &dest) { if (src.st_ino == dest.st_ino && src.st_dev == dest.st_dev) { return true; } return false; } +const QString socketPath() +{ + return QStringLiteral("org_kde_kio_file_helper_%1").arg(getpid()); +} + void FileProtocol::copy(const QUrl &srcUrl, const QUrl &destUrl, int _mode, JobFlags _flags) { // qDebug() << "copy(): " << srcUrl << " -> " << destUrl << ", mode=" << _mode; const QString src = srcUrl.toLocalFile(); const QString dest = destUrl.toLocalFile(); QByteArray _src(QFile::encodeName(src)); QByteArray _dest(QFile::encodeName(dest)); QT_STATBUF buff_src; #if HAVE_POSIX_ACL acl_t acl; #endif if (QT_STAT(_src.data(), &buff_src) == -1) { if (errno == EACCES) { error(KIO::ERR_ACCESS_DENIED, src); } else { error(KIO::ERR_DOES_NOT_EXIST, src); } return; } if ((buff_src.st_mode & QT_STAT_MASK) == QT_STAT_DIR) { error(KIO::ERR_IS_DIRECTORY, src); return; } if (S_ISFIFO(buff_src.st_mode) || S_ISSOCK(buff_src.st_mode)) { error(KIO::ERR_CANNOT_OPEN_FOR_READING, src); return; } QT_STATBUF buff_dest; bool dest_exists = (QT_LSTAT(_dest.data(), &buff_dest) != -1); if (dest_exists) { if (same_inode(buff_dest, buff_src)) { error(KIO::ERR_IDENTICAL_FILES, dest); return; } if ((buff_dest.st_mode & QT_STAT_MASK) == QT_STAT_DIR) { error(KIO::ERR_DIR_ALREADY_EXIST, dest); return; } if (!(_flags & KIO::Overwrite)) { error(KIO::ERR_FILE_ALREADY_EXIST, dest); return; } // If the destination is a symlink and overwrite is TRUE, // remove the symlink first to prevent the scenario where // the symlink actually points to current source! if ((_flags & KIO::Overwrite) && ((buff_dest.st_mode & QT_STAT_MASK) == QT_STAT_LNK)) { //qDebug() << "copy(): LINK DESTINATION"; QFile::remove(dest); } } QFile src_file(src); if (!src_file.open(QIODevice::ReadOnly)) { error(KIO::ERR_CANNOT_OPEN_FOR_READING, src); return; } #if HAVE_FADVISE posix_fadvise(src_file.handle(), 0, 0, POSIX_FADV_SEQUENTIAL); #endif QFile dest_file(dest); if (!dest_file.open(QIODevice::Truncate | QIODevice::WriteOnly)) { // qDebug() << "###### COULD NOT WRITE " << dest; if (errno == EACCES) { error(KIO::ERR_WRITE_ACCESS_DENIED, dest); } else { error(KIO::ERR_CANNOT_OPEN_FOR_WRITING, dest); } src_file.close(); return; } // nobody shall be allowed to peek into the file during creation dest_file.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner); #if HAVE_FADVISE posix_fadvise(dest_file.handle(), 0, 0, POSIX_FADV_SEQUENTIAL); #endif #if HAVE_POSIX_ACL acl = acl_get_fd(src_file.handle()); if (acl && !isExtendedACL(acl)) { // qDebug() << _dest.data() << " doesn't have extended ACL"; acl_free(acl); acl = nullptr; } #endif totalSize(buff_src.st_size); KIO::filesize_t processed_size = 0; char buffer[ MAX_IPC_SIZE ]; int n; #ifdef USE_SENDFILE bool use_sendfile = buff_src.st_size < 0x7FFFFFFF; #endif while (1) { #ifdef USE_SENDFILE if (use_sendfile) { off_t sf = processed_size; n = ::sendfile(dest_file.handle(), src_file.handle(), &sf, MAX_IPC_SIZE); processed_size = sf; if (n == -1 && (errno == EINVAL || errno == ENOSYS)) { //not all filesystems support sendfile() // qDebug() << "sendfile() not supported, falling back "; use_sendfile = false; } } if (!use_sendfile) #endif n = ::read(src_file.handle(), buffer, MAX_IPC_SIZE); if (n == -1) { if (errno == EINTR) { continue; } #ifdef USE_SENDFILE if (use_sendfile) { // qDebug() << "sendfile() error:" << strerror(errno); if (errno == ENOSPC) { // disk full error(KIO::ERR_DISK_FULL, dest); } else { error(KIO::ERR_SLAVE_DEFINED, i18n("Cannot copy file from %1 to %2. (Errno: %3)", src, dest, errno)); } } else #endif error(KIO::ERR_CANNOT_READ, src); src_file.close(); dest_file.close(); #if HAVE_POSIX_ACL if (acl) { acl_free(acl); } #endif dest_file.remove(); // don't keep partly copied file return; } if (n == 0) { break; // Finished } #ifdef USE_SENDFILE if (!use_sendfile) { #endif if (dest_file.write(buffer, n) != n) { if (dest_file.error() == QFileDevice::ResourceError) { // disk full error(KIO::ERR_DISK_FULL, dest); } else { qCWarning(KIO_FILE) << "Couldn't write[2]. Error:" << dest_file.errorString(); error(KIO::ERR_CANNOT_WRITE, dest); } #if HAVE_POSIX_ACL if (acl) { acl_free(acl); } #endif dest_file.remove(); // don't keep partly copied file return; } processed_size += n; #ifdef USE_SENDFILE } #endif processedSize(processed_size); } src_file.close(); dest_file.close(); if (dest_file.error() != QFile::NoError) { qCWarning(KIO_FILE) << "Error when closing file descriptor[2]:" << dest_file.errorString(); error(KIO::ERR_CANNOT_WRITE, dest); #if HAVE_POSIX_ACL if (acl) { acl_free(acl); } #endif dest_file.remove(); // don't keep partly copied file return; } // set final permissions // if no special mode given, preserve the mode from the sourcefile if (_mode == -1) { _mode = buff_src.st_mode; } if ((::chmod(_dest.data(), _mode) != 0) #if HAVE_POSIX_ACL || (acl && acl_set_file(_dest.data(), ACL_TYPE_ACCESS, acl) != 0) #endif ) { KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(dest); // Eat the error if the filesystem apparently doesn't support chmod. if (mp && mp->testFileSystemFlag(KMountPoint::SupportsChmod)) { warning(i18n("Could not change permissions for '%1'", dest)); } } #if HAVE_POSIX_ACL if (acl) { acl_free(acl); } #endif // preserve ownership if (::chown(_dest.data(), -1 /*keep user*/, buff_src.st_gid) == 0) { // as we are the owner of the new file, we can always change the group, but // we might not be allowed to change the owner (void)::chown(_dest.data(), buff_src.st_uid, -1 /*keep group*/); } else { qCWarning(KIO_FILE) << QStringLiteral("Couldn't preserve group for '%1'").arg(dest); } // copy access and modification time struct utimbuf ut; ut.actime = buff_src.st_atime; ut.modtime = buff_src.st_mtime; if (::utime(_dest.data(), &ut) != 0) { qCWarning(KIO_FILE) << QStringLiteral("Couldn't preserve access and modification time for '%1'").arg(dest); } processedSize(buff_src.st_size); finished(); } static bool isLocalFileSameHost(const QUrl &url) { if (!url.isLocalFile()) { return false; } if (url.host().isEmpty() || (url.host() == QLatin1String("localhost"))) { return true; } char hostname[ 256 ]; hostname[ 0 ] = '\0'; if (!gethostname(hostname, 255)) { hostname[sizeof(hostname) - 1] = '\0'; } return (QString::compare(url.host(), QLatin1String(hostname), Qt::CaseInsensitive) == 0); } void FileProtocol::listDir(const QUrl &url) { if (!isLocalFileSameHost(url)) { QUrl redir(url); redir.setScheme(config()->readEntry("DefaultRemoteProtocol", "smb")); redirection(redir); // qDebug() << "redirecting to " << redir; finished(); return; } const QString path(url.toLocalFile()); const QByteArray _path(QFile::encodeName(path)); DIR *dp = opendir(_path.data()); if (dp == nullptr) { switch (errno) { case ENOENT: error(KIO::ERR_DOES_NOT_EXIST, path); return; case ENOTDIR: error(KIO::ERR_IS_FILE, path); break; #ifdef ENOMEDIUM case ENOMEDIUM: error(ERR_SLAVE_DEFINED, i18n("No media in device for %1", path)); break; #endif default: error(KIO::ERR_CANNOT_ENTER_DIRECTORY, path); break; } return; } /* set the current dir to the path to speed up in not having to pass an absolute path. We restore the path later to get out of the path - the kernel wouldn't unmount or delete directories we keep as active directory. And as the slave runs in the background, it's hard to see for the user what the problem would be */ const QString pathBuffer(QDir::currentPath()); if (!QDir::setCurrent(path)) { closedir(dp); error(ERR_CANNOT_ENTER_DIRECTORY, path); return; } const QString sDetails = metaData(QStringLiteral("details")); const int details = sDetails.isEmpty() ? 2 : sDetails.toInt(); //qDebug() << "========= LIST " << url << "details=" << details << " ========="; UDSEntry entry; #ifndef HAVE_DIRENT_D_TYPE QT_STATBUF st; #endif QT_DIRENT *ep; while ((ep = QT_READDIR(dp)) != nullptr) { entry.clear(); const QString filename = QFile::decodeName(ep->d_name); /* * details == 0 (if statement) is the fast code path. * We only get the file name and type. After that we emit * the result. * * The else statement is the slow path that requests all * file information in file.cpp. It executes a stat call * for every entry thus becoming slower. * */ if (details == 0) { entry.insert(KIO::UDSEntry::UDS_NAME, filename); #ifdef HAVE_DIRENT_D_TYPE entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, (ep->d_type == DT_DIR) ? S_IFDIR : S_IFREG); const bool isSymLink = (ep->d_type == DT_LNK); #else // oops, no fast way, we need to stat (e.g. on Solaris) if (QT_LSTAT(ep->d_name, &st) == -1) { continue; // how can stat fail? } entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, ((st.st_mode & QT_STAT_MASK) == QT_STAT_DIR) ? S_IFDIR : S_IFREG); const bool isSymLink = ((st.st_mode & QT_STAT_MASK) == QT_STAT_LNK); #endif if (isSymLink) { // for symlinks obey the UDSEntry contract and provide UDS_LINK_DEST // even if we don't know the link dest (and DeleteJob doesn't care...) entry.insert(KIO::UDSEntry::UDS_LINK_DEST, QStringLiteral("Dummy Link Target")); } listEntry(entry); } else { if (createUDSEntry(filename, QByteArray(ep->d_name), entry, details)) { listEntry(entry); } } } closedir(dp); // Restore the path QDir::setCurrent(pathBuffer); finished(); } void FileProtocol::rename(const QUrl &srcUrl, const QUrl &destUrl, KIO::JobFlags _flags) { char off_t_should_be_64_bits[sizeof(off_t) >= 8 ? 1 : -1]; (void) off_t_should_be_64_bits; const QString src = srcUrl.toLocalFile(); const QString dest = destUrl.toLocalFile(); const QByteArray _src(QFile::encodeName(src)); const QByteArray _dest(QFile::encodeName(dest)); QT_STATBUF buff_src; if (QT_LSTAT(_src.data(), &buff_src) == -1) { if (errno == EACCES) { error(KIO::ERR_ACCESS_DENIED, src); } else { error(KIO::ERR_DOES_NOT_EXIST, src); } return; } QT_STATBUF buff_dest; // stat symlinks here (lstat, not stat), to avoid ERR_IDENTICAL_FILES when replacing symlink // with its target (#169547) bool dest_exists = (QT_LSTAT(_dest.data(), &buff_dest) != -1); if (dest_exists) { if (same_inode(buff_dest, buff_src)) { error(KIO::ERR_IDENTICAL_FILES, dest); return; } if ((buff_dest.st_mode & QT_STAT_MASK) == QT_STAT_DIR) { error(KIO::ERR_DIR_ALREADY_EXIST, dest); return; } if (!(_flags & KIO::Overwrite)) { error(KIO::ERR_FILE_ALREADY_EXIST, dest); return; } } if (::rename(_src.data(), _dest.data())) { if ((errno == EACCES) || (errno == EPERM)) { error(KIO::ERR_ACCESS_DENIED, dest); } else if (errno == EXDEV) { error(KIO::ERR_UNSUPPORTED_ACTION, QStringLiteral("rename")); } else if (errno == EROFS) { // The file is on a read-only filesystem error(KIO::ERR_CANNOT_DELETE, src); } else { error(KIO::ERR_CANNOT_RENAME, src); } return; } finished(); } void FileProtocol::symlink(const QString &target, const QUrl &destUrl, KIO::JobFlags flags) { const QString dest = destUrl.toLocalFile(); // Assume dest is local too (wouldn't be here otherwise) if (::symlink(QFile::encodeName(target).constData(), QFile::encodeName(dest).constData()) == -1) { // Does the destination already exist ? if (errno == EEXIST) { if ((flags & KIO::Overwrite)) { // Try to delete the destination if (unlink(QFile::encodeName(dest).constData()) != 0) { error(KIO::ERR_CANNOT_DELETE, dest); return; } // Try again - this won't loop forever since unlink succeeded symlink(target, destUrl, flags); return; } else { QT_STATBUF buff_dest; if (QT_LSTAT(QFile::encodeName(dest).constData(), &buff_dest) == 0 && ((buff_dest.st_mode & QT_STAT_MASK) == QT_STAT_DIR)) { error(KIO::ERR_DIR_ALREADY_EXIST, dest); } else { error(KIO::ERR_FILE_ALREADY_EXIST, dest); } return; } } else { // Some error occurred while we tried to symlink error(KIO::ERR_CANNOT_SYMLINK, dest); return; } } finished(); } void FileProtocol::del(const QUrl &url, bool isfile) { const QString path = url.toLocalFile(); const QByteArray _path(QFile::encodeName(path)); /***** * Delete files *****/ if (isfile) { // qDebug() << "Deleting file "<< url; if (unlink(_path.data()) == -1) { if ((errno == EACCES) || (errno == EPERM)) { error(KIO::ERR_ACCESS_DENIED, path); } else if (errno == EISDIR) { error(KIO::ERR_IS_DIRECTORY, path); } else { error(KIO::ERR_CANNOT_DELETE, path); } return; } } else { /***** * Delete empty directory *****/ // qDebug() << "Deleting directory " << url; if (metaData(QStringLiteral("recurse")) == QLatin1String("true")) { if (!deleteRecursive(path)) { return; } } if (QT_RMDIR(_path.data()) == -1) { if ((errno == EACCES) || (errno == EPERM)) { error(KIO::ERR_ACCESS_DENIED, path); } else { // qDebug() << "could not rmdir " << perror; error(KIO::ERR_CANNOT_RMDIR, path); return; } } } finished(); } void FileProtocol::chown(const QUrl &url, const QString &owner, const QString &group) { const QString path = url.toLocalFile(); const QByteArray _path(QFile::encodeName(path)); uid_t uid; gid_t gid; // get uid from given owner { struct passwd *p = ::getpwnam(owner.toLocal8Bit().constData()); if (! p) { error(KIO::ERR_SLAVE_DEFINED, i18n("Could not get user id for given user name %1", owner)); return; } uid = p->pw_uid; } // get gid from given group { struct group *p = ::getgrnam(group.toLocal8Bit().constData()); if (! p) { error(KIO::ERR_SLAVE_DEFINED, i18n("Could not get group id for given group name %1", group)); return; } gid = p->gr_gid; } if (::chown(_path.constData(), uid, gid) == -1) { switch (errno) { case EPERM: case EACCES: error(KIO::ERR_ACCESS_DENIED, path); break; case ENOSPC: error(KIO::ERR_DISK_FULL, path); break; default: error(KIO::ERR_CANNOT_CHOWN, path); } } else { finished(); } } void FileProtocol::stat(const QUrl &url) { if (!isLocalFileSameHost(url)) { QUrl redir(url); redir.setScheme(config()->readEntry("DefaultRemoteProtocol", "smb")); redirection(redir); // qDebug() << "redirecting to " << redir; finished(); return; } /* directories may not have a slash at the end if * we want to stat() them; it requires that we * change into it .. which may not be allowed * stat("/is/unaccessible") -> rwx------ * stat("/is/unaccessible/") -> EPERM H.Z. * This is the reason for the -1 */ const QString path(url.adjusted(QUrl::StripTrailingSlash).toLocalFile()); const QByteArray _path(QFile::encodeName(path)); const QString sDetails = metaData(QStringLiteral("details")); const int details = sDetails.isEmpty() ? 2 : sDetails.toInt(); UDSEntry entry; if (!createUDSEntry(url.fileName(), _path, entry, details)) { error(KIO::ERR_DOES_NOT_EXIST, path); return; } #if 0 ///////// debug code MetaData::iterator it1 = mOutgoingMetaData.begin(); for (; it1 != mOutgoingMetaData.end(); it1++) { // qDebug() << it1.key() << " = " << it1.data(); } ///////// #endif statEntry(entry); finished(); } + +PrivilegeOperationReturnValue FileProtocol::execWithElevatedPrivilege(ActionType action, const QVariant &arg1, + const QVariant &arg2, const QVariant &arg3) +{ + if (!(errno == EACCES || errno == EPERM)) { + return PrivilegeOperationReturnValue::failure(); + } + + KIO::PrivilegeOperationStatus opStatus = requestPrivilegeOperation(); + if (opStatus != KIO::OperationAllowed) { + if (opStatus == KIO::OperationCanceled) { + error(KIO::ERR_USER_CANCELED, QString()); + return PrivilegeOperationReturnValue::canceled(); + } + return PrivilegeOperationReturnValue::failure(); + } + + KAuth::Action execAction(QStringLiteral("org.kde.kio.file.exec")); + execAction.setHelperId(QStringLiteral("org.kde.kio.file")); + + // If we are unit testing let's pretend to execute the action. + if (metaData(QStringLiteral("UnitTesting")) == QLatin1String("true")) { + const QString metaData = execAction.name() + ',' + + (execAction.isValid() ? "true" : "false") + ',' + + execAction.helperId() + ',' + + (execAction.status() == KAuth::Action::AuthRequiredStatus ? "true" : "false"); + setMetaData(QStringLiteral("TestData"), metaData); + return PrivilegeOperationReturnValue::success(); + } + + if (action == CHMOD || action == CHOWN || action == UTIME) { + KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(arg1.toString()); + // Test for chmod and utime will return the same result as test for chown. + if (mp && !mp->testFileSystemFlag(KMountPoint::SupportsChown)) { + return PrivilegeOperationReturnValue::failure(); + } + } + + QByteArray helperArgs; + QDataStream out(&helperArgs, QIODevice::WriteOnly); + out << action << arg1 << arg2 << arg3; + + if (action == OPEN || action == OPENDIR) { + out << socketPath(); + } + + QVariantMap argv; + argv.insert(QStringLiteral("arguments"), helperArgs); + execAction.setArguments(argv); + + auto reply = execAction.execute(); + if (reply->exec()) { + return PrivilegeOperationReturnValue::success(); + } + + return PrivilegeOperationReturnValue::failure(); +} diff --git a/src/ioslaves/file/file_win.cpp b/src/ioslaves/file/file_win.cpp index bfba5c85..02eb5d38 100644 --- a/src/ioslaves/file/file_win.cpp +++ b/src/ioslaves/file/file_win.cpp @@ -1,383 +1,389 @@ /* 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 Copyright (C) 2007 Christian Ehrlicher 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 #include #include "kioglobal_p.h" using namespace KIO; static DWORD CALLBACK CopyProgressRoutine( LARGE_INTEGER TotalFileSize, LARGE_INTEGER TotalBytesTransferred, LARGE_INTEGER StreamSize, LARGE_INTEGER StreamBytesTransferred, DWORD dwStreamNumber, DWORD dwCallbackReason, HANDLE hSourceFile, HANDLE hDestinationFile, LPVOID lpData ) { FileProtocol *f = reinterpret_cast(lpData); f->processedSize(TotalBytesTransferred.QuadPart); return PROGRESS_CONTINUE; } static UDSEntry createUDSEntryWin(const QFileInfo &fileInfo) { UDSEntry entry; entry.insert(KIO::UDSEntry::UDS_NAME, fileInfo.fileName()); if (fileInfo.isSymLink()) { entry.insert(KIO::UDSEntry::UDS_TARGET_URL, fileInfo.symLinkTarget()); /* TODO - or not useful on windows? if ( details > 1 ) { // It is a link pointing to nowhere type = S_IFMT - 1; access = S_IRWXU | S_IRWXG | S_IRWXO; entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, type ); entry.insert( KIO::UDSEntry::UDS_ACCESS, access ); entry.insert( KIO::UDSEntry::UDS_SIZE, 0LL ); goto notype; } */ } int type = S_IFREG; int access = 0; if (fileInfo.isDir()) { type = S_IFDIR; } else if (fileInfo.isSymLink()) { type = QT_STAT_LNK; } if (fileInfo.isReadable()) { access |= S_IRUSR; } if (fileInfo.isWritable()) { access |= S_IWUSR; } if (fileInfo.isExecutable()) { access |= S_IXUSR; } entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, type); entry.insert(KIO::UDSEntry::UDS_ACCESS, access); entry.insert(KIO::UDSEntry::UDS_SIZE, fileInfo.size()); if (fileInfo.isHidden()) { entry.insert(KIO::UDSEntry::UDS_HIDDEN, true); } entry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, fileInfo.lastModified().toTime_t()); entry.insert(KIO::UDSEntry::UDS_USER, fileInfo.owner()); entry.insert(KIO::UDSEntry::UDS_GROUP, fileInfo.group()); entry.insert(KIO::UDSEntry::UDS_ACCESS_TIME, fileInfo.lastRead().toTime_t()); return entry; } void FileProtocol::copy(const QUrl &src, const QUrl &dest, int _mode, JobFlags _flags) { // qDebug() << "copy(): " << src << " -> " << dest << ", mode=" << _mode; QFileInfo _src(src.toLocalFile()); QFileInfo _dest(dest.toLocalFile()); DWORD dwFlags = COPY_FILE_FAIL_IF_EXISTS; if (_src == _dest) { error(KIO::ERR_IDENTICAL_FILES, _dest.filePath()); return; } if (!_src.exists()) { error(KIO::ERR_DOES_NOT_EXIST, _src.filePath()); return; } if (_src.isDir()) { error(KIO::ERR_IS_DIRECTORY, _src.filePath()); return; } if (_dest.exists()) { if (_dest.isDir()) { error(KIO::ERR_DIR_ALREADY_EXIST, _dest.filePath()); return; } if (!(_flags & KIO::Overwrite)) { error(KIO::ERR_FILE_ALREADY_EXIST, _dest.filePath()); return; } dwFlags = 0; } if (!QFileInfo(_dest.dir().absolutePath()).exists()) { _dest.dir().mkdir(_dest.dir().absolutePath()); } if (CopyFileExW((LPCWSTR) _src.filePath().utf16(), (LPCWSTR) _dest.filePath().utf16(), CopyProgressRoutine, (LPVOID) this, FALSE, dwFlags) == 0) { DWORD dwLastErr = GetLastError(); if (dwLastErr == ERROR_FILE_NOT_FOUND) { error(KIO::ERR_DOES_NOT_EXIST, _src.filePath()); } else if (dwLastErr == ERROR_ACCESS_DENIED) { error(KIO::ERR_ACCESS_DENIED, _dest.filePath()); } else { #if 0 LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwLastErr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL); OutputDebugString((WCHAR *)lpMsgBuf); #endif error(KIO::ERR_CANNOT_RENAME, _src.filePath()); // qDebug() << "Copying file " << _src.filePath() << " failed (" << dwLastErr << ")"; } return; } finished(); } void FileProtocol::listDir(const QUrl &url) { // qDebug() << "========= LIST " << url << " ========="; if (!url.isLocalFile()) { QUrl redir(url); redir.setScheme(config()->readEntry("DefaultRemoteProtocol", "smb")); redirection(redir); // qDebug() << "redirecting to " << redir; finished(); return; } const QString path = url.toLocalFile(); const QFileInfo info(path); if (info.isFile()) { error(KIO::ERR_IS_FILE, path); return; } QDir dir(path); dir.setFilter(QDir::AllEntries | QDir::Hidden); if (!dir.exists()) { // qDebug() << "========= ERR_DOES_NOT_EXIST ========="; error(KIO::ERR_DOES_NOT_EXIST, path); return; } if (!dir.isReadable()) { // qDebug() << "========= ERR_CANNOT_ENTER_DIRECTORY ========="; error(KIO::ERR_CANNOT_ENTER_DIRECTORY, path); return; } QDirIterator it(dir); UDSEntry entry; while (it.hasNext()) { it.next(); UDSEntry entry = createUDSEntryWin(it.fileInfo()); listEntry(entry); entry.clear(); } // qDebug() << "============= COMPLETED LIST ============"; finished(); } void FileProtocol::rename(const QUrl &src, const QUrl &dest, KIO::JobFlags _flags) { // qDebug() << "rename(): " << src << " -> " << dest; QFileInfo _src(src.toLocalFile()); QFileInfo _dest(dest.toLocalFile()); DWORD dwFlags = 0; if (_src == _dest) { error(KIO::ERR_IDENTICAL_FILES, _dest.filePath()); return; } if (!_src.exists()) { error(KIO::ERR_DOES_NOT_EXIST, _src.filePath()); return; } if (_dest.exists()) { if (_dest.isDir()) { error(KIO::ERR_DIR_ALREADY_EXIST, _dest.filePath()); return; } if (!(_flags & KIO::Overwrite)) { error(KIO::ERR_FILE_ALREADY_EXIST, _dest.filePath()); return; } #ifndef _WIN32_WCE dwFlags = MOVEFILE_REPLACE_EXISTING; #endif } // To avoid error 17 - The system cannot move the file to a different disk drive. #ifndef _WIN32_WCE dwFlags |= MOVEFILE_COPY_ALLOWED; if (MoveFileExW((LPCWSTR) _src.filePath().utf16(), (LPCWSTR) _dest.filePath().utf16(), dwFlags) == 0) #else if (MoveFileW((LPCWSTR) _src.filePath().utf16(), (LPCWSTR) _dest.filePath().utf16()) == 0) #endif { DWORD dwLastErr = GetLastError(); if (dwLastErr == ERROR_FILE_NOT_FOUND) { error(KIO::ERR_DOES_NOT_EXIST, _src.filePath()); } else if (dwLastErr == ERROR_ACCESS_DENIED) { error(KIO::ERR_ACCESS_DENIED, _dest.filePath()); } else { error(KIO::ERR_CANNOT_RENAME, _src.filePath()); qCDebug(KIO_FILE) << "Renaming file " << _src.filePath() << " failed (" << dwLastErr << ")"; } return; } finished(); } void FileProtocol::symlink(const QString &target, const QUrl &dest, KIO::JobFlags flags) { QString localDest = dest.toLocalFile(); //TODO handle overwrite, etc if (!KIOPrivate::createSymlink(target, localDest)) { error(KIO::ERR_UNKNOWN, localDest); } } void FileProtocol::del(const QUrl &url, bool isfile) { QString _path(url.toLocalFile()); /***** * Delete files *****/ if (isfile) { // qDebug() << "Deleting file " << _path; if (DeleteFileW((LPCWSTR) _path.utf16()) == 0) { DWORD dwLastErr = GetLastError(); if (dwLastErr == ERROR_PATH_NOT_FOUND) { error(KIO::ERR_DOES_NOT_EXIST, _path); } else if (dwLastErr == ERROR_ACCESS_DENIED) { error(KIO::ERR_ACCESS_DENIED, _path); } else { error(KIO::ERR_CANNOT_DELETE, _path); // qDebug() << "Deleting file " << _path << " failed (" << dwLastErr << ")"; } } } else { // qDebug() << "Deleting directory " << _path; if (!deleteRecursive(_path)) { return; } if (RemoveDirectoryW((LPCWSTR) _path.utf16()) == 0) { DWORD dwLastErr = GetLastError(); if (dwLastErr == ERROR_FILE_NOT_FOUND) { error(KIO::ERR_DOES_NOT_EXIST, _path); } else if (dwLastErr == ERROR_ACCESS_DENIED) { error(KIO::ERR_ACCESS_DENIED, _path); } else { error(KIO::ERR_CANNOT_DELETE, _path); // qDebug() << "Deleting directory " << _path << " failed (" << dwLastErr << ")"; } } } finished(); } void FileProtocol::chown(const QUrl &url, const QString &, const QString &) { error(KIO::ERR_CANNOT_CHOWN, url.toLocalFile()); } void FileProtocol::stat(const QUrl &url) { if (!url.isLocalFile()) { QUrl redir(url); redir.setScheme(config()->readEntry("DefaultRemoteProtocol", "smb")); redirection(redir); // qDebug() << "redirecting to " << redir; finished(); return; } const QString sDetails = metaData(QLatin1String("details")); int details = sDetails.isEmpty() ? 2 : sDetails.toInt(); // qDebug() << "FileProtocol::stat details=" << details; const QString localFile = url.toLocalFile(); QFileInfo fileInfo(localFile); if (!fileInfo.exists()) { error(KIO::ERR_DOES_NOT_EXIST, localFile); return; } UDSEntry entry = createUDSEntryWin(fileInfo); statEntry(entry); finished(); } + +PrivilegeOperationReturnValue FileProtocol::execWithElevatedPrivilege(int, ActionType, const QVariant &, + const QVariant &, const QVariant &) +{ + return PrivilegeOperationReturnValue::failure(); +}