Commit 61a7d1db authored by Fabian Vogt's avatar Fabian Vogt

Merge branch 'convertUrl' into 'master'

Add a remoteUrl() method, callable over DBus

See merge request kde/kio-fuse!21
parents 64836d01 7378fd08
......@@ -78,6 +78,26 @@ bool KIOFuseService::start(struct fuse_args &args, QString mountpoint, bool fore
return registerServiceDaemonized();
}
QString KIOFuseService::remoteUrl(const QString& localPath)
{
// Massage URL into something KIOFuseVFS may understand.
QDir mountpoint(m_mountpoint);
QString relativePath = mountpoint.relativeFilePath(localPath);
// If relativePath is empty or starts with ../, this would get error out
QUrl remoteUrl = kiofusevfs.localPathToRemoteUrl(relativePath);
if(remoteUrl.isEmpty())
{
sendErrorReply(
QStringLiteral("org.kde.KIOFuse.VFS.Error.RemoteURLNotFound"),
QStringLiteral("The given path does not have a remote URL equivalent: %1").arg(localPath)
);
return QString();
}
return remoteUrl.toString(QUrl::RemovePassword);
}
QString KIOFuseService::mountUrl(const QString& remoteUrl, const QDBusMessage& message)
{
message.setDelayedReply(true);
......
......@@ -24,12 +24,13 @@
#include <QObject>
#include <QDBusMessage>
#include <QDBusContext>
#include <QTemporaryDir>
#include <QStandardPaths>
#include "kiofusevfs.h"
class KIOFuseService : public QObject
class KIOFuseService : public QObject, protected QDBusContext
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.KIOFuse.VFS")
......@@ -44,6 +45,8 @@ public:
public Q_SLOTS:
/** Mounts a URL onto the filesystem, and returns the local path back. */
QString mountUrl(const QString &remoteUrl, const QDBusMessage &message);
/** Converts a local path into a remote URL if it is mounted within the VFS */
QString remoteUrl(const QString &localPath);
private:
/** Registers the kio-fuse process as the org.kde.KIOFuse service.
......
......@@ -1360,6 +1360,18 @@ fuse_ino_t KIOFuseVFS::insertNode(const std::shared_ptr<KIOFuseNode> &node, fuse
return ino;
}
QUrl KIOFuseVFS::localPathToRemoteUrl(const QString& localPath) const
{
auto node = nodeForIno(KIOFuseIno::Root);
for (const auto &segment : localPath.split(QStringLiteral("/")))
{
node = nodeByName(node, segment);
if(!node)
return {};
}
return remoteUrl(node);
}
QUrl KIOFuseVFS::sanitizeNullAuthority(QUrl url) const
{
// Workaround to allow url with scheme "file"
......
......@@ -65,6 +65,8 @@ public:
void setUseFileJob(bool useFileJob);
/** Runs KIO::stat on url and adds a node to the tree if successful. Calls the callback at the end. */
void mountUrl(QUrl url, std::function<void(const std::shared_ptr<KIOFuseNode>&, int)> callback);
/** Converts a local path into a remote URL if it is mounted within the VFS */
QUrl localPathToRemoteUrl(const QString &localPath) const;
/** Returns the path upwards until a root node. */
QString virtualPath(const std::shared_ptr<KIOFuseNode> &node) const;
......
......@@ -46,6 +46,7 @@ private Q_SLOTS:
void cleanupTestCase();
void testDBusErrorReply();
void testLocalPathToRemoteUrl();
void testLocalFileOps();
void testLocalDirOps();
void testCreationOps();
......@@ -119,6 +120,30 @@ void FileOpsTest::testDBusErrorReply()
QCOMPARE(reply.error().name(), QStringLiteral("org.kde.KIOFuse.VFS.Error.SchemeNotSupported"));
}
void FileOpsTest::testLocalPathToRemoteUrl()
{
QDBusPendingReply<QString> errorReply;
// mtp:/ -> Remote URL can't possibly be location of KIOFuse mount.
// / -> Root can't possibly be location of KIOFuse mount.
// m_mountDir -> Whilst this is in the KIOFuse mount, no remote URL exists for it
for(auto url : {QStringLiteral("mtp:/"), QStringLiteral("/"), m_mountDir.path()})
{
errorReply = m_kiofuse_iface.remoteUrl(url);
errorReply.waitForFinished();
QVERIFY2(errorReply.isError(), qPrintable(url));
QCOMPARE(errorReply.error().name(), QStringLiteral("org.kde.KIOFuse.VFS.Error.RemoteURLNotFound"));
}
QTemporaryFile localFile;
QVERIFY(localFile.open());
localFile.close(); // Force creation of file to avoid empty fileName()
QString remoteUrl = QStringLiteral("file://%1").arg(localFile.fileName());
QString reply = m_kiofuse_iface.mountUrl(remoteUrl).value();
QVERIFY(!reply.isEmpty());
QString calculatedRemoteUrl = m_kiofuse_iface.remoteUrl(reply).value();
QCOMPARE(remoteUrl, calculatedRemoteUrl);
}
void FileOpsTest::testLocalFileOps()
{
QTemporaryFile localFile;
......
......@@ -5,5 +5,9 @@
<arg name="remoteUrl" type="s" direction="in"/>
<arg type="s" direction="out"/>
</method>
<method name="remoteUrl">
<arg name="localPath" type="s" direction="in"/>
<arg type="s" direction="out"/>
</method>
</interface>
</node>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment