From 2b98b251cdc10d3a2112e05338c4979e5de2226e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Gonz=C3=A1lez?= Date: Thu, 22 May 2025 13:20:33 +0200 Subject: [PATCH] refactor: clean up code by removing commented-out sections and unnecessary comments in PlaceHolderInfo, Placeholders, SyncRoot, and Wrappers --- .../PlaceHolderInfo.cpp | 5 +- .../placeholders_interface/Planceholders.cpp | 35 +++-------- native-src/sync_root_interface/SyncRoot.cpp | 12 ---- native-src/virtual_drive/Wrappers.cpp | 58 ++----------------- 4 files changed, 13 insertions(+), 97 deletions(-) diff --git a/native-src/placeholders_interface/PlaceHolderInfo.cpp b/native-src/placeholders_interface/PlaceHolderInfo.cpp index a690eeb3..69d3d470 100644 --- a/native-src/placeholders_interface/PlaceHolderInfo.cpp +++ b/native-src/placeholders_interface/PlaceHolderInfo.cpp @@ -136,7 +136,7 @@ std::optional PlaceHolderInfo::FileIdentity() const } printf("FILE OPTIONAL: %d\n", _data->FileIdentity[0]); - return _data->FileIdentity[0]; // Devuelve el primer byte del array + return _data->FileIdentity[0]; } void FileHandle::deletePlaceholderInfo(CF_PLACEHOLDER_BASIC_INFO *info) @@ -193,7 +193,6 @@ FileHandle handleForPath(const std::wstring &wPath) } else { - // Convert only for logging purposes std::wstring_convert> converter; std::string path = converter.to_bytes(wPath); printf("Could not CfOpenFileWithOplock for path: %s with error: %ld\n", path.c_str(), openResult); @@ -202,7 +201,7 @@ FileHandle handleForPath(const std::wstring &wPath) else if (std::filesystem::is_regular_file(pathFs)) { HANDLE handle = CreateFileW( - wPath.c_str(), // Use wide string path directly + wPath.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, diff --git a/native-src/placeholders_interface/Planceholders.cpp b/native-src/placeholders_interface/Planceholders.cpp index a3485bb7..5daad850 100644 --- a/native-src/placeholders_interface/Planceholders.cpp +++ b/native-src/placeholders_interface/Planceholders.cpp @@ -80,8 +80,6 @@ void Placeholders::CreateOne( prop.Id(1); prop.Value(L"Value1"); prop.IconResource(L"shell32.dll,-44"); - - // UpdateSyncStatus(fullDestPath, true, false); } catch (...) { @@ -121,10 +119,6 @@ void Placeholders::MaintainIdentity(std::wstring &fullPath, PCWSTR itemIdentity, Placeholders::UpdateFileIdentity(fullPath, itemIdentityStrW, isDirectory); } } - else - { - // Handle error as needed - } } } @@ -140,30 +134,27 @@ void Placeholders::CreateEntry( FILETIME lastAccessTime, _In_ PCWSTR destPath) { - std::wstring fullDestPath = std::wstring(destPath) + L"\\" + std::wstring(itemName); CF_PLACEHOLDER_CREATE_INFO cloudEntry = {}; std::wstring relativeName(itemIdentity); cloudEntry.FileIdentity = relativeName.c_str(); cloudEntry.FileIdentityLength = static_cast((relativeName.size() + 1) * sizeof(WCHAR)); cloudEntry.RelativeFileName = itemName; - cloudEntry.Flags = CF_PLACEHOLDER_CREATE_FLAG_DISABLE_ON_DEMAND_POPULATION; // -> desactive download on demand + cloudEntry.Flags = CF_PLACEHOLDER_CREATE_FLAG_DISABLE_ON_DEMAND_POPULATION; cloudEntry.FsMetadata.BasicInfo.FileAttributes = FILE_ATTRIBUTE_DIRECTORY; cloudEntry.FsMetadata.BasicInfo.CreationTime = Utilities::FileTimeToLargeInteger(creationTime); cloudEntry.FsMetadata.BasicInfo.LastWriteTime = Utilities::FileTimeToLargeInteger(lastWriteTime); try { - // TODO: si existe o es placeholder return if (DirectoryExists(fullDestPath.c_str())) { Placeholders::ConvertToPlaceholder(fullDestPath, itemIdentity); Placeholders::MaintainIdentity(fullDestPath, itemIdentity, true); - return; // No hacer nada si ya existe + return; } - if (isDirectory) // TODO: the function createEntry is used to create only folders (directories), so this if is always true + if (isDirectory) { - // wprintf(L"Create directory, full destination path: %ls, fullDestPath.c_str()"); PathRemoveFileSpecW(&fullDestPath[0]); HRESULT hr = CfCreatePlaceholders(fullDestPath.c_str(), &cloudEntry, 1, CF_CREATE_FLAG_NONE, NULL); if (FAILED(hr)) @@ -207,7 +198,6 @@ bool Placeholders::ConvertToPlaceholder(const std::wstring &fullPath, const std: if (fileHandle == INVALID_HANDLE_VALUE) { - // Manejar el error al abrir el archivo return false; } @@ -222,24 +212,21 @@ bool Placeholders::ConvertToPlaceholder(const std::wstring &fullPath, const std: if (FAILED(hr)) { - // Manejar el error al convertir a marcador de posición if (hr != 0x8007017C) { wprintf(L"[ConvertToPlaceholder] Error converting to placeholder, ConvertToPlaceholder failed with HRESULT 0x%X\n", hr); } - // Puedes obtener información detallada sobre el error usando FormatMessage LPVOID errorMsg; FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, - 0, // Default language + 0, (LPWSTR)&errorMsg, 0, NULL); - // Liberar el buffer de mensaje de error LocalFree(errorMsg); CloseHandle(fileHandle); @@ -248,24 +235,16 @@ bool Placeholders::ConvertToPlaceholder(const std::wstring &fullPath, const std: if (!isDirectory) { - HRESULT hrPinState = CfSetPinState(fileHandle, CF_PIN_STATE_PINNED, CF_SET_PIN_FLAG_NONE, nullptr); - if (FAILED(hrPinState)) - { - std::wstring errorMessage = Utilities::GetErrorMessageCloudFiles(hrPinState); - wprintf(L"[ConvertToPlaceholder] Error setting pin state, HRESULT: 0x%X\nDetails: %s\n", hrPinState, errorMessage.c_str()); - CloseHandle(fileHandle); - return false; - } + Placeholders::UpdatePinState(fullPath, PinState::AlwaysLocal); } CloseHandle(fileHandle); wprintf(L"[ConvertToPlaceholder] Successfully converted to placeholder: %ls\n", fullPath.c_str()); return true; } - catch (const winrt::hresult_error &error) + catch (...) { - // Manejar excepciones desconocidas - wprintf(L"[ConvertToPlaceholder] Unknown exception occurred\n"); + wprintf(L"[ConvertToPlaceholder] Failed to convert to placeholder with %08x\n", static_cast(winrt::to_hresult())); return false; } } diff --git a/native-src/sync_root_interface/SyncRoot.cpp b/native-src/sync_root_interface/SyncRoot.cpp index 6049075b..2dc6fd63 100644 --- a/native-src/sync_root_interface/SyncRoot.cpp +++ b/native-src/sync_root_interface/SyncRoot.cpp @@ -42,8 +42,6 @@ void SyncRoot::HydrateFile(const wchar_t *filePath) if (attrib & FILE_ATTRIBUTE_PINNED) { - // if (!(attrib & FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS)) - // { Logger::getInstance().log("Hydration file init", LogLevel::INFO); auto start = std::chrono::steady_clock::now(); @@ -68,19 +66,12 @@ void SyncRoot::HydrateFile(const wchar_t *filePath) wprintf(L"Hydration finished %ls\n", filePath); } } - // } - // else - // { - // wprintf(L"File is already hydrated: %ls\n", filePath); - // Logger::getInstance().log("File is already hydrated " + Logger::fromWStringToString(filePath), LogLevel::INFO); - // } } } } 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)) @@ -94,7 +85,6 @@ 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); HRESULT hr = CfDehydratePlaceholder(placeholder.get(), offset, length, CF_DEHYDRATE_FLAG_NONE, NULL); @@ -115,12 +105,10 @@ void SyncRoot::DehydrateFile(const wchar_t *filePath) { 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); } } } diff --git a/native-src/virtual_drive/Wrappers.cpp b/native-src/virtual_drive/Wrappers.cpp index 1c4906f7..c7cee1d0 100644 --- a/native-src/virtual_drive/Wrappers.cpp +++ b/native-src/virtual_drive/Wrappers.cpp @@ -204,7 +204,11 @@ napi_value RegisterSyncRootWrapper(napi_env env, napi_callback_info args) HRESULT result = SyncRoot::RegisterSyncRoot(syncRootPath, providerName, providerVersion, providerId, logoPath); + delete[] syncRootPath; + delete[] providerName; + delete[] providerVersion; delete[] providerIdStr; + delete[] logoPath; napi_value napiResult; napi_create_int32(env, static_cast(result), &napiResult); @@ -837,60 +841,6 @@ napi_value HydrateFileWrapper(napi_env env, napi_callback_info args) return promise; } -// napi_value HydrateFileWrapper(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 HydrateFile"); -// return nullptr; -// } - -// // Obtener el argumento de JavaScript y convertirlo a una cadena de C++ -// size_t pathLength; -// napi_get_value_string_utf16(env, argv[0], nullptr, 0, &pathLength); -// std::wstring fullPath(pathLength, L'\0'); -// napi_get_value_string_utf16(env, argv[0], reinterpret_cast(&fullPath[0]), pathLength + 1, nullptr); - -// // Crear una promesa -// napi_deferred deferred; -// napi_value promise; -// napi_create_promise(env, &deferred, &promise); - -// // Crear un handle scope para manejar la creación de objetos V8 -// napi_handle_scope handleScope; -// napi_open_handle_scope(env, &handleScope); - -// // Usar un bloque de manejo de V8 -// { -// v8::Isolate* isolate = v8::Isolate::GetCurrent(); -// v8::HandleScope scope(isolate); - -// try { -// Logger::getInstance().log("init... " + Logger::fromWStringToString(fullPath.c_str()), LogLevel::INFO); -// SyncRoot::HydrateFile(fullPath.c_str()); - -// napi_value result; -// napi_get_undefined(env, &result); -// napi_resolve_deferred(env, deferred, result); -// } catch (const std::exception& e) { -// napi_value error; -// napi_create_string_utf8(env, e.what(), NAPI_AUTO_LENGTH, &error); -// napi_reject_deferred(env, deferred, error); -// } catch (...) { -// napi_value error; -// napi_create_string_utf8(env, "Unknown error", NAPI_AUTO_LENGTH, &error); -// napi_reject_deferred(env, deferred, error); -// } -// } - -// // Cerrar el handle scope -// napi_close_handle_scope(env, handleScope); - -// return promise; -// } // Wrapper for DehydrateFile napi_value DehydrateFileWrapper(napi_env env, napi_callback_info args)