diff --git a/autotests/unit/file/CMakeLists.txt b/autotests/unit/file/CMakeLists.txt --- a/autotests/unit/file/CMakeLists.txt +++ b/autotests/unit/file/CMakeLists.txt @@ -26,10 +26,10 @@ # # File Watch # -set(fileWatch_SRC filewatchtest.cpp ../lib/xattrdetector.cpp) +set(fileWatch_SRC filewatchtest.cpp) ecm_add_test(${fileWatch_SRC} TEST_NAME "filewatchtest" - LINK_LIBRARIES Qt5::Test Qt5::DBus KF5::Baloo baloofilecommon + LINK_LIBRARIES Qt5::Test Qt5::DBus KF5::Baloo KF5::FileMetaData baloofilecommon ) # diff --git a/autotests/unit/file/filewatchtest.cpp b/autotests/unit/file/filewatchtest.cpp --- a/autotests/unit/file/filewatchtest.cpp +++ b/autotests/unit/file/filewatchtest.cpp @@ -23,12 +23,11 @@ #include "database.h" #include "fileindexerconfig.h" #include "pendingfilequeue.h" -#include "../lib/xattrdetector.h" -#include "../lib/baloo_xattr_p.h" #include #include #include +#include namespace Baloo { @@ -126,14 +125,14 @@ // // Set an Xattr // - XattrDetector detector; - if (!detector.isSupported(dbDir.path())) { - qWarning() << "Xattr not supported on this filesystem"; + KFileMetaData::UserMetaData umd(fileUrl); + if (!umd.isSupported()) { + qWarning() << "Xattr not supported on this filesystem:" << fileUrl; return; } const QString userComment(QStringLiteral("UserComment")); - QVERIFY(baloo_setxattr(fileUrl, QLatin1String("user.xdg.comment"), userComment) != -1); + QVERIFY(umd.setUserComment(userComment) == KFileMetaData::UserMetaData::NoError); QVERIFY(spyIndexXattr.wait()); QCOMPARE(spyIndexNew.count(), 0); diff --git a/autotests/unit/lib/baloo_xattr_p.h b/autotests/unit/lib/baloo_xattr_p.h deleted file mode 100644 --- a/autotests/unit/lib/baloo_xattr_p.h +++ /dev/null @@ -1,127 +0,0 @@ -/* - * This file is part of the KDE Baloo Project - * Copyright (C) 2014 Raphael Kubo da Costa - * - * 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) 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 - * 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, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#ifndef BALOO_XATTR_P_H -#define BALOO_XATTR_P_H - -#include -#include -#include - -#if defined(Q_OS_LINUX) || defined(__GLIBC__) -#include -#include -#elif defined(Q_OS_MAC) -#include -#include -#elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) -#include -#include -#endif - -inline ssize_t baloo_getxattr(const QString& path, const QString& name, QString* value) -{ - const QByteArray p = QFile::encodeName(path); - const char* encodedPath = p.constData(); - - const QByteArray n = name.toUtf8(); - const char* attributeName = n.constData(); - - // First get the size of the data we are going to get to reserve the right amount of space. -#if defined(Q_OS_LINUX) || (defined(__GLIBC__) && !defined(__stub_getxattr)) - const ssize_t size = getxattr(encodedPath, attributeName, nullptr, 0); -#elif defined(Q_OS_MAC) - const ssize_t size = getxattr(encodedPath, attributeName, NULL, 0, 0, 0); -#elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) - const ssize_t size = extattr_get_file(encodedPath, EXTATTR_NAMESPACE_USER, attributeName, NULL, 0); -#else - const ssize_t size = 0; -#endif - - if (size <= 0) { - if (value) { - value->clear(); - } - return size; - } - - QByteArray data(size, Qt::Uninitialized); - -#if defined(Q_OS_LINUX) || (defined(__GLIBC__) && !defined(__stub_getxattr)) - const ssize_t r = getxattr(encodedPath, attributeName, data.data(), size); -#elif defined(Q_OS_MAC) - const ssize_t r = getxattr(encodedPath, attributeName, data.data(), size, 0, 0); -#elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) - const ssize_t r = extattr_get_file(encodedPath, EXTATTR_NAMESPACE_USER, attributeName, data.data(), size); -#else - const ssize_t r = 0; -#endif - - *value = QString::fromUtf8(data); - return r; -} - -inline int baloo_setxattr(const QString& path, const QString& name, const QString& value) -{ - const QByteArray p = QFile::encodeName(path); - const char* encodedPath = p.constData(); - - const QByteArray n = name.toUtf8(); - const char* attributeName = n.constData(); - - const QByteArray v = value.toUtf8(); - const void* attributeValue = v.constData(); - - const size_t valueSize = v.size(); - -#if defined(Q_OS_LINUX) || (defined(__GLIBC__) && !defined(__stub_setxattr)) - return setxattr(encodedPath, attributeName, attributeValue, valueSize, 0); -#elif defined(Q_OS_MAC) - return setxattr(encodedPath, attributeName, attributeValue, valueSize, 0, 0); -#elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) - const ssize_t count = extattr_set_file(encodedPath, EXTATTR_NAMESPACE_USER, attributeName, attributeValue, valueSize); - return count == -1 ? -1 : 0; -#else - return -1; -#endif -} - - -inline int baloo_removexattr(const QString& path, const QString& name) -{ - const QByteArray p = QFile::encodeName(path); - const char* encodedPath = p.constData(); - - const QByteArray n = name.toUtf8(); - const char* attributeName = n.constData(); - - #if defined(Q_OS_LINUX) || (defined(__GLIBC__) && !defined(__stub_removexattr)) - return removexattr(encodedPath, attributeName); - #elif defined(Q_OS_MAC) - return removexattr(encodedPath, attributeName, XATTR_NOFOLLOW ); - #elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) - return extattr_delete_file (encodedPath, EXTATTR_NAMESPACE_USER, attributeName); - #else - return -1; - #endif - -} - -#endif // BALOO_XATTR_P_H diff --git a/autotests/unit/lib/xattrdetector.h b/autotests/unit/lib/xattrdetector.h deleted file mode 100644 --- a/autotests/unit/lib/xattrdetector.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * This file is part of the KDE Baloo Project - * Copyright (C) 2014 Vishesh Handa - * - * 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) 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 - * 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, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#ifndef BALOO_XATTRDETECTOR_H -#define BALOO_XATTRDETECTOR_H - -#include - -namespace Baloo { - -/** - * \class XattrDetector xattrdetector.h - * - * \brief This class is used to detect if a file supports the adding of - * extended attributes to given a file path. - * - * The creation of this object is expensive, so avoid it as much as possible - * - * \author Vishesh Handa - */ -class XattrDetector : public QObject -{ -public: - explicit XattrDetector(QObject* parent = nullptr); - ~XattrDetector() override; - - /** - * Checks if the give local url \p path supports - * saving of extended attributes in file system - */ - bool isSupported(const QString& path); - -private: - class Private; - Private* d; -}; - -} - -#endif // BALOO_XATTRDETECTOR_H diff --git a/autotests/unit/lib/xattrdetector.cpp b/autotests/unit/lib/xattrdetector.cpp deleted file mode 100644 --- a/autotests/unit/lib/xattrdetector.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* - * This file is part of the KDE Baloo Project - * Copyright (C) 2014 Vishesh Handa - * - * 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) 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 - * 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, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -#include "xattrdetector.h" -#include "baloo_xattr_p.h" - -#include -#include -#include -#include -#include -#include - -using namespace Baloo; - -class XattrDetector::Private { -public: - QStringList m_unSupportedPaths; - QStringList m_supportedPaths; - - void init(); - bool m_initialized; -}; - -XattrDetector::XattrDetector(QObject* parent) - : QObject(parent) - , d(new Private) -{ - d->m_initialized = false; -} - -void XattrDetector::Private::init() -{ - const QList devices - = Solid::Device::listFromType(Solid::DeviceInterface::StorageAccess); - - QStringList mountPaths; - for (const Solid::Device& dev : devices) { - const Solid::StorageAccess* sa = dev.as(); - if (!sa->isAccessible()) - continue; - - mountPaths << sa->filePath(); - } - mountPaths << QDir::homePath(); - - for (const QString& mountPath : qAsConst(mountPaths)) { - while (1) { - QString randFile = QLatin1String("baloo-xattr-check-") + QUuid::createUuid().toString(); - const QString url = mountPath + QDir::separator() + randFile; - if (QFile::exists(url)) - continue; - - QFile file(url); - if (!file.open(QIODevice::WriteOnly)) { - m_unSupportedPaths << mountPath; - break; - } - file.close(); - - int ret = baloo_setxattr(url, QStringLiteral("test"), QStringLiteral("0")); - if (ret != -1) { - // Check the actual error? - m_unSupportedPaths << mountPath; - } - else { - m_supportedPaths << mountPath; - } - - QFile::remove(url); - break; - } - } - m_unSupportedPaths << QStringLiteral("/tmp") << QStringLiteral("/proc"); - qDebug() << "supportedPaths:" << m_supportedPaths; - qDebug() << "UnsupportedPaths:" << m_unSupportedPaths; - m_initialized = true; -} - -XattrDetector::~XattrDetector() -{ - delete d; -} - -bool XattrDetector::isSupported(const QString& path) -{ -#ifdef Q_OS_WIN - return false; -#endif - if (!d->m_initialized) - d->init(); - - for (const QString& p : qAsConst(d->m_supportedPaths)) { - if (path.startsWith(p)) - return true; - } - - for (const QString& p : qAsConst(d->m_unSupportedPaths)) { - if (path.startsWith(p)) - return false; - } - - return true; -}