Utils: Start adding some asynchronous API to FilePath

Especially with the "remote" scenarios synchronous operations cannot
be expected to work reasonably well.

This here starts with adding asynchronous versions to some of the
FilePath member functions, taking additional "Continuation" style.

This is not necessarily the final syntax (sugar like .then(...) comes
to mind...), but is simple enough for now for the few uses we have,
and it is too early to see what will be needed in the end.

Change-Id: Idf4dde1b77d04cafb81b6c024031145bdd91a762
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-08-30 15:42:21 +02:00
parent 3c4e7321bf
commit ebf0917b9f
7 changed files with 83 additions and 4 deletions

View File

@@ -738,6 +738,17 @@ QByteArray FilePath::fileContents(qint64 maxSize, qint64 offset) const
return f.readAll();
}
void FilePath::asyncFileContents(const Continuation<const QByteArray &> &cont,
qint64 maxSize, qint64 offset) const
{
if (needsDevice()) {
QTC_ASSERT(s_deviceHooks.asyncFileContents, return);
return s_deviceHooks.asyncFileContents(cont, *this, maxSize, offset);
}
cont(fileContents(maxSize, offset));
}
bool FilePath::writeFileContents(const QByteArray &data) const
{
if (needsDevice()) {
@@ -751,6 +762,17 @@ bool FilePath::writeFileContents(const QByteArray &data) const
return res == data.size();
}
void FilePath::asyncWriteFileContents(const Continuation<bool> &cont, const QByteArray &data) const
{
if (needsDevice()) {
QTC_ASSERT(s_deviceHooks.asyncWriteFileContents, return);
s_deviceHooks.asyncWriteFileContents(cont, *this, data);
return;
}
cont(writeFileContents(data));
}
bool FilePath::needsDevice() const
{
return !m_scheme.isEmpty();
@@ -1319,6 +1341,19 @@ bool FilePath::copyFile(const FilePath &target) const
return QFile::copy(path(), target.path());
}
void FilePath::asyncCopyFile(const std::function<void(bool)> &cont, const FilePath &target) const
{
if (host() != target.host()) {
asyncFileContents([cont, target](const QByteArray &ba) {
target.asyncWriteFileContents(cont, ba);
});
} else if (needsDevice()) {
s_deviceHooks.asyncCopyFile(cont, *this, target);
} else {
cont(copyFile(target));
}
}
bool FilePath::renameFile(const FilePath &target) const
{
if (needsDevice()) {