From ed705a319e671631e17f74ac2fbc84d102f45a09 Mon Sep 17 00:00:00 2001 From: Mathias Tillman Date: Sun, 14 Sep 2014 16:39:44 +0200 Subject: [PATCH 1/1] Add new KIO job, KIO::fileSystemFreeSpace, that allows you to get a filesystem's total and available space. REVIEW: 120134 --- src/core/CMakeLists.txt | 2 + src/core/commands_p.h | 3 +- src/core/filesystemfreespacejob.cpp | 88 +++++++++++++++++++++++++++++++++++++ src/core/filesystemfreespacejob.h | 73 ++++++++++++++++++++++++++++++ src/core/slavebase.cpp | 20 ++++++++- src/core/slavebase.h | 3 +- src/ioslaves/file/file.cpp | 31 +++++++++++++ src/ioslaves/file/file.h | 5 +++ 8 files changed, 221 insertions(+), 4 deletions(-) create mode 100644 src/core/filesystemfreespacejob.cpp create mode 100644 src/core/filesystemfreespacejob.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index f1f8848..7043e49 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -66,6 +66,7 @@ set(kiocore_SRCS statjob.cpp storedtransferjob.cpp transferjob.cpp + filesystemfreespacejob.cpp scheduler.cpp slaveconfig.cpp kprotocolmanager.cpp @@ -194,6 +195,7 @@ ecm_generate_headers(KIOCore_CamelCase_HEADERS AuthInfo DavJob DesktopExecParser + FileSystemFreeSpaceJob PREFIX KIO REQUIRED_HEADERS KIO_namespaced_HEADERS diff --git a/src/core/commands_p.h b/src/core/commands_p.h index 4b6bfa2..a8ddeba 100644 --- a/src/core/commands_p.h +++ b/src/core/commands_p.h @@ -63,7 +63,8 @@ enum Command { CMD_WRITE = 91, CMD_SEEK = 92, CMD_CLOSE = 93, - CMD_HOST_INFO = 94 + CMD_HOST_INFO = 94, + CMD_FILESYSTEMFREESPACE = 95 // Add new ones here once a release is done, to avoid breaking binary compatibility. // Note that protocol-specific commands shouldn't be added here, but should use special. }; diff --git a/src/core/filesystemfreespacejob.cpp b/src/core/filesystemfreespacejob.cpp new file mode 100644 index 0000000..2b85ad2 --- /dev/null +++ b/src/core/filesystemfreespacejob.cpp @@ -0,0 +1,88 @@ +/* This file is part of the KDE libraries + Copyright (C) 2000 Stephan Kulow + 2000-2009 David Faure + 2014 Mathias Tillman + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "filesystemfreespacejob.h" +#include "job.h" +#include "job_p.h" +#include + +using namespace KIO; + +class KIO::FileSystemFreeSpaceJobPrivate: public SimpleJobPrivate +{ +public: + FileSystemFreeSpaceJobPrivate(const QUrl &url, int command, const QByteArray &packedArgs) + : SimpleJobPrivate(url, command, packedArgs) + { } + + /** + * @internal + * Called by the scheduler when a @p slave gets to + * work on this job. + * @param slave the slave that starts working on this job + */ + void start(Slave *slave) Q_DECL_OVERRIDE; + + Q_DECLARE_PUBLIC(FileSystemFreeSpaceJob) + + static inline FileSystemFreeSpaceJob *newJob(const QUrl &url, int command, const QByteArray &packedArgs) + { + FileSystemFreeSpaceJob *job = new FileSystemFreeSpaceJob(*new FileSystemFreeSpaceJobPrivate(url, command, packedArgs)); + job->setUiDelegate(KIO::createDefaultJobUiDelegate()); + return job; + } +}; + +FileSystemFreeSpaceJob::FileSystemFreeSpaceJob(FileSystemFreeSpaceJobPrivate &dd) + : SimpleJob(dd) +{ +} + +FileSystemFreeSpaceJob::~FileSystemFreeSpaceJob() +{ +} + +void FileSystemFreeSpaceJobPrivate::start(Slave *slave) +{ + Q_Q(FileSystemFreeSpaceJob); + SimpleJobPrivate::start(slave); +} + +void FileSystemFreeSpaceJob::slotFinished() +{ + Q_D(FileSystemFreeSpaceJob); + + KIO::filesize_t total = queryMetaData("total").toULongLong(); + KIO::filesize_t available = queryMetaData("available").toULongLong(); + + emit result(this, total, available); + + // Return slave to the scheduler + SimpleJob::slotFinished(); +} + +KIO::FileSystemFreeSpaceJob *KIO::fileSystemFreeSpace(const QUrl &url) +{ + KIO_ARGS << url; + return FileSystemFreeSpaceJobPrivate::newJob(url, CMD_FILESYSTEMFREESPACE, packedArgs); +} + +#include "moc_filesystemfreespacejob.cpp" diff --git a/src/core/filesystemfreespacejob.h b/src/core/filesystemfreespacejob.h new file mode 100644 index 0000000..9754d5f --- /dev/null +++ b/src/core/filesystemfreespacejob.h @@ -0,0 +1,73 @@ +/* This file is part of the KDE libraries + Copyright (C) 2000 Stephan Kulow + 2000-2009 David Faure + 2014 Mathias Tillman + + 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 FILESYSTEMFREESPACEJOB_H +#define FILESYSTEMFREESPACEJOB_H + +#include "kiocore_export.h" +#include "simplejob.h" + +namespace KIO +{ + +class FileSystemFreeSpaceJobPrivate; +/** + * A KIO job that retrieves the total and available size of a filesystem. + * @since 5.3 + */ +class KIOCORE_EXPORT FileSystemFreeSpaceJob : public SimpleJob +{ + + Q_OBJECT + +public: + ~FileSystemFreeSpaceJob(); + +Q_SIGNALS: + /** + * Signals the result + * @param job the job that is redirected + * @param size total amount of space + * @param available amount of free space + */ + void result(KIO::Job *job, KIO::filesize_t size, KIO::filesize_t available); + +protected Q_SLOTS: + void slotFinished() Q_DECL_OVERRIDE; + +public: + FileSystemFreeSpaceJob(FileSystemFreeSpaceJobPrivate &dd); + +private: + Q_DECLARE_PRIVATE(FileSystemFreeSpaceJob) +}; + +/** + * Get a filesystem's total and available space. + * + * @param url Url to the filesystem. + * @return the job handling the operation. + */ +KIOCORE_EXPORT FileSystemFreeSpaceJob *fileSystemFreeSpace(const QUrl &url); + +} + +#endif /* FILESYSTEMFREESPACEJOB_H */ diff --git a/src/core/slavebase.cpp b/src/core/slavebase.cpp index 7280996..01844f5 100644 --- a/src/core/slavebase.cpp +++ b/src/core/slavebase.cpp @@ -1261,6 +1261,16 @@ void SlaveBase::dispatch(int command, const QByteArray &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. @@ -1387,9 +1397,15 @@ void SlaveBase::send(int cmd, const QByteArray &arr) } } -void SlaveBase::virtual_hook(int, void *) +void SlaveBase::virtual_hook(int id, void *data) { - /*BASE::virtual_hook( id, data );*/ + Q_UNUSED(data); + + switch(id) { + case GetFileSystemFreeSpace: { + error(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(mProtocol, CMD_FILESYSTEMFREESPACE)); + } break; + } } void SlaveBase::lookupHost(const QString &host) diff --git a/src/core/slavebase.h b/src/core/slavebase.h index 3b7aefa..d1d94de 100644 --- a/src/core/slavebase.h +++ b/src/core/slavebase.h @@ -932,7 +932,8 @@ protected: MetaData mIncomingMetaData; enum VirtualFunctionId { - AppConnectionMade = 0 + AppConnectionMade = 0, + GetFileSystemFreeSpace = 1 // KF6 TODO: Turn into a virtual method }; virtual void virtual_hook(int id, void *data); diff --git a/src/ioslaves/file/file.cpp b/src/ioslaves/file/file.cpp index 9eef9eb..1a2a767 100644 --- a/src/ioslaves/file/file.cpp +++ b/src/ioslaves/file/file.cpp @@ -33,6 +33,8 @@ #include #include +#include + #include "kioglobal_p.h" #include @@ -1298,3 +1300,32 @@ bool FileProtocol::deleteRecursive(const QString &path) return true; } +void FileProtocol::fileSystemFreeSpace(const QUrl &url) +{ + if (url.isLocalFile()) { + const KDiskFreeSpaceInfo spaceInfo = KDiskFreeSpaceInfo::freeSpaceInfo(url.toLocalFile()); + if (spaceInfo.isValid()) { + setMetaData(QString::fromLatin1("total"), QString::number(spaceInfo.size())); + setMetaData(QString::fromLatin1("available"), QString::number(spaceInfo.available())); + + finished(); + } else { + error(KIO::ERR_COULD_NOT_STAT, url.url()); + } + } else { + error(KIO::ERR_UNSUPPORTED_PROTOCOL, url.url()); + } +} + +void FileProtocol::virtual_hook(int id, void *data) +{ + switch(id) { + case SlaveBase::GetFileSystemFreeSpace: { + QUrl *url = static_cast(data); + fileSystemFreeSpace(*url); + } break; + default: { + SlaveBase::virtual_hook(id, data); + } break; + } +} diff --git a/src/ioslaves/file/file.h b/src/ioslaves/file/file.h index a6e3c3f..d966ac1 100644 --- a/src/ioslaves/file/file.h +++ b/src/ioslaves/file/file.h @@ -84,6 +84,9 @@ public: 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); @@ -92,6 +95,8 @@ private: QString getGroupName(KGroupId gid) const; bool deleteRecursive(const QString &path); + void fileSystemFreeSpace(const QUrl &url); // KF6 TODO: Turn into virtual method in SlaveBase + private: mutable QHash mUsercache; mutable QHash mGroupcache; -- 2.1.0