Skip to content

Commit

Permalink
VFS Windows: Fix download of large files
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Weilbach <felix.weilbach@nextcloud.com>
  • Loading branch information
Felix Weilbach committed Mar 4, 2021
1 parent 97cd611 commit 6af52d5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
39 changes: 34 additions & 5 deletions src/libsync/vfs/cfapi/cfapiwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void cfApiSendTransferInfo(const CF_CONNECTION_KEY &connectionKey, const CF_TRAN

void CALLBACK cfApiFetchDataCallback(const CF_CALLBACK_INFO *callbackInfo, const CF_CALLBACK_PARAMETERS *callbackParameters)
{
qDebug(lcCfApiWrapper) << "Fetch data callback called. File size:" << callbackInfo->FileSize.QuadPart;
const auto sendTransferError = [=] {
cfApiSendTransferInfo(callbackInfo->ConnectionKey,
callbackInfo->TransferKey,
Expand All @@ -70,6 +71,7 @@ void CALLBACK cfApiFetchDataCallback(const CF_CALLBACK_INFO *callbackInfo, const
};

const auto sendTransferInfo = [=](QByteArray &data, qint64 offset) {
qDebug(lcCfApiWrapper) << "Send transfer info. Offset:" << offset;
cfApiSendTransferInfo(callbackInfo->ConnectionKey,
callbackInfo->TransferKey,
STATUS_SUCCESS,
Expand Down Expand Up @@ -123,21 +125,43 @@ void CALLBACK cfApiFetchDataCallback(const CF_CALLBACK_INFO *callbackInfo, const
return;
}

qint64 offset = 0;
// CFAPI expects sent blocks to be of a multiple of a block size.
// Only the last sent block is allowed to be of a different size than
// a multiple of a block size
constexpr auto cfapiBlockSize = 4096;
qint64 dataOffset = 0;
QByteArray protrudingData;

const auto alignAndSendData = [&](const QByteArray &receivedData) {
QByteArray data = protrudingData + receivedData;
protrudingData.clear();
if (data.size() < cfapiBlockSize) {
protrudingData = data;
return;
}
const auto protudingSize = data.size() % cfapiBlockSize;
protrudingData = data.right(protudingSize);
data.chop(protudingSize);

qDebug(lcCfApiWrapper) << "Send data block. Size:" << data.size();
sendTransferInfo(data, dataOffset);
dataOffset += data.size();
};

QObject::connect(&socket, &QLocalSocket::readyRead, &loop, [&] {
auto data = socket.readAll();
if (data.isEmpty()) {
const auto receivedData = socket.readAll();
if (receivedData.isEmpty()) {
qCWarning(lcCfApiWrapper) << "Unexpected empty data received" << requestId;
sendTransferError();
protrudingData.clear();
loop.quit();
return;
}
sendTransferInfo(data, offset);
offset += data.length();
alignAndSendData(receivedData);
});

QObject::connect(vfs, &OCC::VfsCfApi::hydrationRequestFinished, &loop, [&](const QString &id, int s) {
qDebug(lcCfApiWrapper) << "Hydration finished for request" << id;
if (requestId == id) {
const auto status = static_cast<OCC::HydrationJob::Status>(s);
qCInfo(lcCfApiWrapper) << "Hydration done for" << path << requestId << status;
Expand All @@ -149,6 +173,11 @@ void CALLBACK cfApiFetchDataCallback(const CF_CALLBACK_INFO *callbackInfo, const
});

loop.exec();

if (!protrudingData.isEmpty()) {
qDebug(lcCfApiWrapper) << "Send remaining protruding data. Size:" << protrudingData.size();
sendTransferInfo(protrudingData, dataOffset);
}
}

CF_CALLBACK_REGISTRATION cfApiCallbacks[] = {
Expand Down
2 changes: 1 addition & 1 deletion src/libsync/vfs/cfapi/hydrationjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ void OCC::HydrationJob::emitFinished(Status status)
{
_status = status;
if (status == Success) {
_socket->disconnectFromServer();
connect(_socket, &QLocalSocket::disconnected, this, [=]{
_socket->close();
emit finished(this);
});
_socket->disconnectFromServer();
} else {
_socket->close();
emit finished(this);
Expand Down

0 comments on commit 6af52d5

Please sign in to comment.