Skip to content

Commit

Permalink
Fix review comments.
Browse files Browse the repository at this point in the history
Signed-off-by: allexzander <blackslayer4@gmail.com>
  • Loading branch information
allexzander committed Mar 19, 2021
1 parent d2c84bc commit 4a95402
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions src/libsync/vfs/cfapi/cfapiwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Q_LOGGING_CATEGORY(lcCfApiWrapper, "nextcloud.sync.vfs.cfapi.wrapper", QtInfoMsg
FIELD_SIZE( CF_OPERATION_PARAMETERS, field ) )

namespace {
void cfApiSendTransferInfo(const CF_CONNECTION_KEY &connectionKey, const CF_TRANSFER_KEY &transferKey, NTSTATUS status, void *buffer, qint64 offset, qint64 length)
void cfApiSendTransferInfo(const CF_CONNECTION_KEY &connectionKey, const CF_TRANSFER_KEY &transferKey, NTSTATUS status, void *buffer, qint64 offset, qint64 currentBlockLength, qint64 totalLength)
{

CF_OPERATION_INFO opInfo = { 0 };
Expand All @@ -51,11 +51,24 @@ void cfApiSendTransferInfo(const CF_CONNECTION_KEY &connectionKey, const CF_TRAN
opParams.TransferData.CompletionStatus = status;
opParams.TransferData.Buffer = buffer;
opParams.TransferData.Offset.QuadPart = offset;
opParams.TransferData.Length.QuadPart = length;
opParams.TransferData.Length.QuadPart = currentBlockLength;

const qint64 result = CfExecute(&opInfo, &opParams);
if (result != S_OK) {
qCCritical(lcCfApiWrapper) << "Couldn't send transfer info" << QString::number(transferKey.QuadPart, 16) << ":" << result << QString::fromWCharArray(_com_error(result).ErrorMessage());
const qint64 cfExecuteresult = CfExecute(&opInfo, &opParams);
if (cfExecuteresult != S_OK) {
qCCritical(lcCfApiWrapper) << "Couldn't send transfer info" << QString::number(transferKey.QuadPart, 16) << ":" << cfExecuteresult << QString::fromWCharArray(_com_error(cfExecuteresult).ErrorMessage());
}

// refresh Windows Copy Dialog progress
LARGE_INTEGER progressTotal;
progressTotal.QuadPart = totalLength;

LARGE_INTEGER progressCompleted;
progressCompleted.QuadPart = offset;

const qint64 cfReportProgressresult = CfReportProviderProgress(connectionKey, transferKey, progressTotal, progressCompleted);

if (cfReportProgressresult != S_OK) {
qCCritical(lcCfApiWrapper) << "Couldn't report provider progress" << QString::number(transferKey.QuadPart, 16) << ":" << cfReportProgressresult << QString::fromWCharArray(_com_error(cfReportProgressresult).ErrorMessage());
}
}

Expand All @@ -69,7 +82,8 @@ void CALLBACK cfApiFetchDataCallback(const CF_CALLBACK_INFO *callbackInfo, const
STATUS_UNSUCCESSFUL,
nullptr,
callbackParameters->FetchData.RequiredFileOffset.QuadPart,
callbackParameters->FetchData.RequiredLength.QuadPart);
callbackParameters->FetchData.RequiredLength.QuadPart,
callbackInfo->FileSize.QuadPart);
};

const auto sendTransferInfo = [=](QByteArray &data, qint64 offset) {
Expand All @@ -79,12 +93,8 @@ void CALLBACK cfApiFetchDataCallback(const CF_CALLBACK_INFO *callbackInfo, const
STATUS_SUCCESS,
data.data(),
offset,
data.length());

LARGE_INTEGER progressCompleted;
progressCompleted.QuadPart = offset;

CfReportProviderProgress(callbackInfo->ConnectionKey, callbackInfo->TransferKey, callbackInfo->FileSize, progressCompleted);
data.length(),
callbackInfo->FileSize.QuadPart);
};

auto vfs = reinterpret_cast<OCC::VfsCfApi *>(callbackInfo->CallbackContext);
Expand Down Expand Up @@ -299,47 +309,48 @@ OCC::Optional<OCC::PinStateEnums::PinState> OCC::CfApiWrapper::PlaceHolderInfo::

QString convertSidToStringSid(void *sid)
{
QString result;

wchar_t *stringSid = nullptr;
if (::ConvertSidToStringSid(sid, &stringSid)) {
result = QString::fromWCharArray(stringSid);
}

if (stringSid) {
::LocalFree(stringSid);
stringSid = nullptr;
if (!ConvertSidToStringSid(sid, &stringSid)) {
return {};
}

return result;
const auto result = QString::fromWCharArray(stringSid);
LocalFree(stringSid);
return QString::fromWCharArray(stringSid);
}

QString retrieveWindowsSID()
std::unique_ptr<TOKEN_USER> getCurrentTokenInformation()
{
// FYI: code here is mostly identical to Windows-Classic-Examples/Samples/CloudMirror
QScopedPointer<TOKEN_USER> tokenInfo;

const auto tokenHandle = GetCurrentThreadEffectiveToken();

DWORD tokenInfoSize = { 0 };
// first - get the required size
if (!::GetTokenInformation(tokenHandle, TokenUser, nullptr, 0, &tokenInfoSize))
{
if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
// now, use the obtained size to allocate TOKEN_USER
tokenInfo.reset(reinterpret_cast<TOKEN_USER*>(new char[tokenInfoSize]));
if (!::GetTokenInformation(tokenHandle, TokenUser, tokenInfo.get(), tokenInfoSize, &tokenInfoSize)) {
return QString();
}
}
auto tokenInfoSize = DWORD{0};

const auto tokenSizeCallSucceeded = ::GetTokenInformation(tokenHandle, TokenUser, nullptr, 0, &tokenInfoSize);
const auto lastError = GetLastError();
Q_ASSERT(!tokenSizeCallSucceeded && lastError == ERROR_INSUFFICIENT_BUFFER);
if (tokenSizeCallSucceeded || lastError != ERROR_INSUFFICIENT_BUFFER) {
qCCritical(lcCfApiWrapper) << "GetTokenInformation for token size has failed with error" << lastError;
return std::unique_ptr<TOKEN_USER>();
}

std::unique_ptr<TOKEN_USER> tokenInfo;

tokenInfo.reset(reinterpret_cast<TOKEN_USER*>(new char[tokenInfoSize]));
if (!::GetTokenInformation(tokenHandle, TokenUser, tokenInfo.get(), tokenInfoSize, &tokenInfoSize)) {
qCCritical(lcCfApiWrapper) << "GetTokenInformation failed with error" << lastError;
return std::unique_ptr<TOKEN_USER>();
}

if (!tokenInfo) {
return QString();
return tokenInfo;
}

QString retrieveWindowsSID()
{
if (const auto tokenInfo = getCurrentTokenInformation()) {
return convertSidToStringSid(tokenInfo->User.Sid);
}

return convertSidToStringSid(tokenInfo->User.Sid);
return QString();
}

bool createSyncRootRegistryKeys(const QString &providerName, const QString &folderAlias, const QString &displayName, const QString &accountDisplayName, const QString &syncRootPath)
Expand Down

0 comments on commit 4a95402

Please sign in to comment.