diff --git a/binding.gyp b/binding.gyp index 951570ef..526139db 100644 --- a/binding.gyp +++ b/binding.gyp @@ -31,6 +31,7 @@ "native-src/virtual_drive/convert_to_placeholder.cpp", "native-src/virtual_drive/create_file_placeholder.cpp", "native-src/virtual_drive/create_folder_placeholder.cpp", + "native-src/virtual_drive/dehydrate_file.cpp", "native-src/virtual_drive/get_file_identity.cpp", "native-src/virtual_drive/get_registered_sync_roots/get_registered_sync_roots.cpp", "native-src/virtual_drive/get_registered_sync_roots/get_registered_sync_roots_wrapper.cpp", diff --git a/dist/addon.node b/dist/addon.node index 3fa8e008..0b4ee624 100644 Binary files a/dist/addon.node and b/dist/addon.node differ diff --git a/include/sync_root_interface/SyncRoot.h b/include/sync_root_interface/SyncRoot.h index a4ab9837..0294cb0f 100644 --- a/include/sync_root_interface/SyncRoot.h +++ b/include/sync_root_interface/SyncRoot.h @@ -20,7 +20,6 @@ class SyncRoot static HRESULT DisconnectSyncRoot(const wchar_t *syncRootPath); static std::string GetFileIdentity(const wchar_t *path); static void HydrateFile(const wchar_t *filePath); - static void DehydrateFile(const wchar_t *filePath); private: CF_CONNECTION_KEY connectionKey; diff --git a/include/virtual_drive/dehydrate_file.h b/include/virtual_drive/dehydrate_file.h new file mode 100644 index 00000000..b237f1db --- /dev/null +++ b/include/virtual_drive/dehydrate_file.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +napi_value dehydrate_file(napi_env env, napi_callback_info info); diff --git a/native-src/sync_root_interface/SyncRoot.cpp b/native-src/sync_root_interface/SyncRoot.cpp index a87c5045..1d0e4978 100644 --- a/native-src/sync_root_interface/SyncRoot.cpp +++ b/native-src/sync_root_interface/SyncRoot.cpp @@ -56,54 +56,6 @@ void SyncRoot::HydrateFile(const wchar_t *filePath) } } -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)) - { - winrt::handle placeholder(CreateFileW(filePath, 0, FILE_READ_DATA, nullptr, OPEN_EXISTING, 0, nullptr)); - - LARGE_INTEGER offset; - offset.QuadPart = 0; - LARGE_INTEGER length; - GetFileSizeEx(placeholder.get(), &length); - - if (attrib & FILE_ATTRIBUTE_UNPINNED) - { - // Logger::getInstance().log("Dehydrating file " + Logger::fromWStringToString(filePath), LogLevel::INFO); - wprintf(L"Dehydrating file starteeed %ls\n", filePath); - HRESULT hr = CfDehydratePlaceholder(placeholder.get(), offset, length, CF_DEHYDRATE_FLAG_NONE, NULL); - - if (FAILED(hr)) - { - DWORD err = HRESULT_CODE(hr); - 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"File in use", - MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); - } - 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); - } - } - } -} - HRESULT SyncRoot::ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallbacks syncCallbacks, napi_env env, CF_CONNECTION_KEY *connectionKey) { Utilities::AddFolderToSearchIndexer(syncRootPath); diff --git a/native-src/virtual_drive/Wrappers.cpp b/native-src/virtual_drive/Wrappers.cpp index 812c9bc8..2862bea2 100644 --- a/native-src/virtual_drive/Wrappers.cpp +++ b/native-src/virtual_drive/Wrappers.cpp @@ -17,6 +17,7 @@ #include "convert_to_placeholder.h" #include "get_registered_sync_roots_wrapper.h" #include "unregister_sync_root_wrapper.h" +#include "dehydrate_file.h" #include "NAPI_SAFE_WRAP.h" napi_value CreatePlaceholderFile(napi_env env, napi_callback_info args) { @@ -228,38 +229,12 @@ napi_value UpdateFileIdentityWrapper(napi_env env, napi_callback_info args) return result; } -napi_value HydrateFileWrapper(napi_env env, napi_callback_info args) -{ +napi_value HydrateFileWrapper(napi_env env, napi_callback_info args) { return NAPI_SAFE_WRAP(env, args, hydrate_file_impl); } -napi_value DehydrateFileWrapper(napi_env env, napi_callback_info args) -{ - size_t argc = 1; - napi_value argv[1]; - napi_value thisArg; - napi_get_cb_info(env, args, &argc, argv, &thisArg, nullptr); - - if (argc < 1) - { - napi_throw_type_error(env, nullptr, "The file path is required for DehydrateFile"); - return nullptr; - } - - // Obtener el argumento de JavaScript y convertirlo a una cadena de C++ - LPCWSTR fullPath; - size_t pathLength; - napi_get_value_string_utf16(env, argv[0], nullptr, 0, &pathLength); - fullPath = new WCHAR[pathLength + 1]; - napi_get_value_string_utf16(env, argv[0], reinterpret_cast(const_cast(fullPath)), pathLength + 1, nullptr); - - // Llamar a la función DehydrateFile - SyncRoot::DehydrateFile(fullPath); - - napi_value result; - napi_get_boolean(env, true, &result); - - return result; +napi_value DehydrateFileWrapper(napi_env env, napi_callback_info args) { + return NAPI_SAFE_WRAP(env, args, dehydrate_file); } napi_value GetPlaceholderAttributeWrapper(napi_env env, napi_callback_info args) diff --git a/native-src/virtual_drive/dehydrate_file.cpp b/native-src/virtual_drive/dehydrate_file.cpp new file mode 100644 index 00000000..4c6f8d0e --- /dev/null +++ b/native-src/virtual_drive/dehydrate_file.cpp @@ -0,0 +1,39 @@ +#include +#include "napi_extract_args.h" +#include "stdafx.h" + +napi_value dehydrate_file(napi_env env, napi_callback_info info) { + auto [rawPath] = napi_extract_args<1>(env, info); + const wchar_t* path = rawPath.c_str(); + + DWORD attrib = GetFileAttributesW(path); + + if (attrib & FILE_ATTRIBUTE_DIRECTORY) { + throw std::runtime_error("Cannot dehydrate folder"); + } + + winrt::handle placeholder(CreateFileW(path, 0, FILE_READ_DATA, nullptr, OPEN_EXISTING, 0, nullptr)); + + LARGE_INTEGER offset; + offset.QuadPart = 0; + LARGE_INTEGER length; + GetFileSizeEx(placeholder.get(), &length); + + HRESULT hr = CfDehydratePlaceholder(placeholder.get(), offset, length, CF_DEHYDRATE_FLAG_NONE, NULL); + + if (SUCCEEDED(hr)) { + return nullptr; + } + + DWORD err = HRESULT_CODE(hr); + + if (err == ERROR_SHARING_VIOLATION || err == ERROR_CLOUD_FILE_IN_USE) { + MessageBoxW( + nullptr, + L"Unable 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); + } + + winrt::throw_hresult(hr); +}