Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions native-src/sync_root_interface/SyncRoot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Logger.h"
#include <iostream>
#include <vector>
#include "Placeholders.h"

namespace fs = std::filesystem;
// variable to disconect
Expand Down Expand Up @@ -78,13 +79,38 @@ void SyncRoot::HydrateFile(const wchar_t *filePath)
}
}

bool IsFileInUse(const wchar_t* filePath) {
WIN32_FIND_DATAW findData;
HANDLE hFind = FindFirstFileW(filePath, &findData);

if (hFind == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
if (error == ERROR_SHARING_VIOLATION || error == ERROR_CLOUD_FILE_IN_USE) {
return true;
}
return false;
}

FindClose(hFind);
return false;
}

void SyncRoot::DehydrateFile(const wchar_t *filePath)
{
// Logger::getInstance().log("Dehydration file init", LogLevel::INFO);
wprintf(L"Dehydration file init %ls\n", filePath);
DWORD attrib = GetFileAttributesW(filePath);
if (!(attrib & FILE_ATTRIBUTE_DIRECTORY))
{
if (IsFileInUse(filePath)) {
wprintf(L"Cannot dehydrate because the file is currently in use: %ls\n", filePath);
MessageBoxW(
nullptr,
L"Unablaa to free up space because the file is currently in use.\nPlease close the file and try again.",
L"File in use",
MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);
return;
}

winrt::handle placeholder(CreateFileW(filePath, 0, FILE_READ_DATA, nullptr, OPEN_EXISTING, 0, nullptr));

LARGE_INTEGER offset;
Expand All @@ -94,8 +120,7 @@ void SyncRoot::DehydrateFile(const wchar_t *filePath)

if (attrib & FILE_ATTRIBUTE_UNPINNED)
{
// Logger::getInstance().log("Dehydrating file " + Logger::fromWStringToString(filePath), LogLevel::INFO);
wprintf(L"Dehydrating file starteeed %ls\n", filePath);
wprintf(L"Dehydrating file started %ls\n", filePath);
HRESULT hr = CfDehydratePlaceholder(placeholder.get(), offset, length, CF_DEHYDRATE_FLAG_NONE, NULL);

if (FAILED(hr))
Expand All @@ -104,23 +129,27 @@ void SyncRoot::DehydrateFile(const wchar_t *filePath)
if (err == ERROR_SHARING_VIOLATION || err == ERROR_CLOUD_FILE_IN_USE)
{
wprintf(L"Cannot dehydrate because the file is currently in use: %ls\n", filePath);

MessageBoxW(
nullptr,
L"Unable to free up space because the file is currently in use.\nPlease close the file and try again.",
L"Unablbb to free up space because the file is currently in use.\nPlease close the file and try again.",
L"File in use",
MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL);

// Only restore states if CfDehydratePlaceholder failed
try {
Placeholders::UpdatePinState(filePath, PinState::AlwaysLocal);
} catch (...) {
wprintf(L"Failed to restore file states: %ls\n", filePath);
}
}
else
{
wprintf(L"Error dehydrating file %ls\n", filePath);
}
// Logger::getInstance().log("Error dehydrating file " + Logger::fromWStringToString(filePath), LogLevel::ERROR);
}
else
{
wprintf(L"Dehydration finished %ls\n", filePath);
// Logger::getInstance().log("Dehydration finished " + Logger::fromWStringToString(filePath), LogLevel::INFO);
}
}
}
Expand Down
Loading