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 1, 2021
1 parent 6e3b3fc commit 3c53e68
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
60 changes: 57 additions & 3 deletions src/libsync/vfs/cfapi/cfapiwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,75 @@ void CALLBACK cfApiFetchDataCallback(const CF_CALLBACK_INFO *callbackInfo, const
}

qint64 offset = 0;
QByteArray protrudingData;
bool protrudingDataLeft = false;

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

QByteArray dataToSent;
// CFAPI expectes sended blocks to be of a specified size.
// Only the last sended block is allowed to be smaller than the specified block size
// and therefore marks the end of the transmission.
static constexpr auto CFAPI_BLOCK_SIZE = 4096;

int dataIndex = 0;
// Is there any data left from last run?
if (!protrudingData.isEmpty()) {
QByteArray dataToSent;
dataToSent.append(protrudingData);

// Fill up remaining first block if possible
const auto fillupSize = CFAPI_BLOCK_SIZE - dataToSent.size();
if (fillupSize > 0) {
dataToSent.append(data.left(fillupSize));
dataIndex += fillupSize;
}

// Send the block
qDebug(lcCfApiWrapper) << "Send protruding data that is left from last run. Size:" << dataToSent.size();
sendTransferInfo(dataToSent, offset);
offset += dataToSent.length();
}

// Do we still have data to sent?
if (dataIndex < data.size()) {
const auto blocksCount = (data.size() - (dataIndex + 1)) / CFAPI_BLOCK_SIZE;
if (blocksCount != 0) {
// Send the blocks one by one
for (int i = 0; i < blocksCount; ++i) {
auto dataToSent = data.mid(dataIndex, CFAPI_BLOCK_SIZE);
qDebug(lcCfApiWrapper) << "Send data block. Size:" << dataToSent.size();
sendTransferInfo(dataToSent, offset);
offset += dataToSent.length();
dataIndex += CFAPI_BLOCK_SIZE;
}
}

// Do we still have data left? If yes, save this data for the next run and send it then
protrudingDataLeft = false;
if (dataIndex < data.size()) {
protrudingDataLeft = true;
protrudingData = data.mid(dataIndex, data.size() - dataIndex);
qDebug(lcCfApiWrapper) << "Save protruding data for next run. Size:" << protrudingData.size();
}
}
});

QObject::connect(vfs, &OCC::VfsCfApi::hydrationRequestFinished, &loop, [&](const QString &id, int s) {
qDebug(lcCfApiWrapper) << "Hydration finished for request" << id;
if (requestId == id) {
if (protrudingDataLeft) {
qDebug(lcCfApiWrapper) << "Send protruding data left. Size:" << protrudingData.size();
sendTransferInfo(protrudingData, offset);
}

const auto status = static_cast<OCC::HydrationJob::Status>(s);
qCInfo(lcCfApiWrapper) << "Hydration done for" << path << requestId << status;
if (status != OCC::HydrationJob::Success) {
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 3c53e68

Please sign in to comment.