diff --git a/binding.gyp b/binding.gyp index 8f2aacf2..8037b860 100644 --- a/binding.gyp +++ b/binding.gyp @@ -14,15 +14,11 @@ } }, "sources": [ - "native-src/logger/Logger.cpp", - "native-src/logger/LoggerPath.cpp", "native-src/main.cpp", "native-src/placeholders_interface/Planceholders.cpp", "native-src/sync_root_interface/SyncRoot.cpp", - "native-src/sync_root_interface/Utilities.cpp", "native-src/sync_root_interface/callbacks/CancelFetchData/CancelFetchDataCallback.cpp", "native-src/sync_root_interface/callbacks/FetchData/FetchData.cpp", - "native-src/sync_root_interface/callbacks/FetchData/FileCopierWithProgress.cpp", "native-src/sync_root_interface/callbacks/FetchData/TransferContext.cpp", "native-src/virtual_drive/Wrappers.cpp", "native-src/virtual_drive/connect_sync_root.cpp", @@ -42,7 +38,6 @@ ], "include_dirs": [ "include", - "include/logger", "include/napi_helpers", "include/placeholders_interface", "include/sync_root_interface", diff --git a/dist/addon.node b/dist/addon.node index a03f4f63..0572bdfa 100644 Binary files a/dist/addon.node and b/dist/addon.node differ diff --git a/include/logger/Logger.h b/include/logger/Logger.h deleted file mode 100644 index 50453bf3..00000000 --- a/include/logger/Logger.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef ERROR -#undef ERROR -#endif - -enum class LogLevel { - DEBUG, - INFO, - WARN, - TRACE, - ERROR, - FATAL -}; - -extern std::string loggerPath; -class Logger { -public: - static Logger& getInstance() { - static Logger instance; - return instance; - } - - void log(const std::string &message, LogLevel level, WORD color = 0); - - Logger(const Logger&) = delete; - Logger& operator=(const Logger&) = delete; - - std::string toString(LogLevel level); - - static std::wstring fromUtf8ToWide(const std::string& utf8Str); - - static std::string fromWStringToString(const std::wstring wstr); -private: - explicit Logger(); - ~Logger(); - - std::ofstream log_file; - std::mutex log_mutex; -}; diff --git a/include/logger/LoggerPath.h b/include/logger/LoggerPath.h deleted file mode 100644 index a8e05cc6..00000000 --- a/include/logger/LoggerPath.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include - -class LoggerPath { -public: - static void set(const std::string& path); - static std::string get(); - -private: - static std::string path_; - static std::mutex mutex_; -}; diff --git a/include/sync_root_interface/Utilities.h b/include/sync_root_interface/Utilities.h index 34b67cd3..8dfbd8a2 100644 --- a/include/sync_root_interface/Utilities.h +++ b/include/sync_root_interface/Utilities.h @@ -1,24 +1,8 @@ #pragma once -#include + class Utilities { public: - static void ApplyTransferStateToFile(_In_ LPCWSTR fullPath, _In_ CF_CALLBACK_INFO &callbackInfo, UINT64 total, UINT64 completed); - - static winrt::com_array - ConvertSidToStringSid(_In_ PSID sid) - { - winrt::com_array string; - if (::ConvertSidToStringSidW(sid, winrt::put_abi(string))) - { - return string; - } - else - { - throw std::bad_alloc(); - } - }; - inline static LARGE_INTEGER JsTimestampToLargeInteger(int64_t jsTimestamp) { const int64_t EPOCH_DIFFERENCE = 11644473600000LL; diff --git a/include/sync_root_interface/callbacks/FileCopierWithProgress.h b/include/sync_root_interface/callbacks/FileCopierWithProgress.h deleted file mode 100644 index 134601cc..00000000 --- a/include/sync_root_interface/callbacks/FileCopierWithProgress.h +++ /dev/null @@ -1,23 +0,0 @@ -// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF -// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO -// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A -// PARTICULAR PURPOSE. -// -// Copyright (c) Microsoft Corporation. All rights reserved -#include -#include - -#pragma once - -class FileCopierWithProgress -{ -public: - static HRESULT TransferData( - _In_ CF_CONNECTION_KEY connectionKey, - _In_ LARGE_INTEGER transferKey, - _In_reads_bytes_opt_(length.QuadPart) LPCVOID transferData, - _In_ LARGE_INTEGER startingOffset, - _In_ LARGE_INTEGER length, - _In_ NTSTATUS completionStatus); - -}; \ No newline at end of file diff --git a/include/virtual_drive/Wrappers.h b/include/virtual_drive/Wrappers.h index 2fcac9d9..1c7516a2 100644 --- a/include/virtual_drive/Wrappers.h +++ b/include/virtual_drive/Wrappers.h @@ -9,7 +9,6 @@ napi_value RegisterSyncRootWrapper(napi_env env, napi_callback_info args); napi_value GetRegisteredSyncRootsWrapper(napi_env env, napi_callback_info args); napi_value ConnectSyncRootWrapper(napi_env env, napi_callback_info args); napi_value DisconnectSyncRootWrapper(napi_env env, napi_callback_info args); -napi_value addLoggerPathWrapper(napi_env env, napi_callback_info args); napi_value UpdateSyncStatusWrapper(napi_env env, napi_callback_info args); napi_value GetPlaceholderStateWrapper(napi_env env, napi_callback_info args); napi_value ConvertToPlaceholderWrapper(napi_env env, napi_callback_info args); diff --git a/native-src/logger/Logger.cpp b/native-src/logger/Logger.cpp deleted file mode 100644 index a6e9bf23..00000000 --- a/native-src/logger/Logger.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "Logger.h" -#include "LoggerPath.h" - -Logger::Logger() : log_file(LoggerPath::get(), std::ios::app) { - std::wstring widePath = fromUtf8ToWide(LoggerPath::get()); - wprintf(L"Logger path: %ls\n", widePath.c_str()); - - std::string path = LoggerPath::get(); - if (!log_file.is_open() && !path.empty()) { - throw std::runtime_error("No se pudo abrir el archivo de log."); - } -} - -Logger::~Logger() { - if (log_file.is_open()) { - log_file.close(); - } -} - -std::wstring Logger::fromUtf8ToWide(const std::string& utf8Str) { - if (utf8Str.empty()) return std::wstring(); - - int wideSize = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, nullptr, 0); - if (wideSize <= 0) return std::wstring(); - - std::unique_ptr wideStr(new wchar_t[wideSize]); - MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, wideStr.get(), wideSize); - - return std::wstring(wideStr.get()); -} - -void Logger::log(const std::string &message, LogLevel level, WORD color) { - std::lock_guard guard(log_mutex); - - auto now = std::chrono::system_clock::now(); - auto now_as_time_t = std::chrono::system_clock::to_time_t(now); - auto now_ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; - std::tm now_tm = *std::localtime(&now_as_time_t); - - std::ostringstream time_stream; - time_stream << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S"); - time_stream << '.' << std::setfill('0') << std::setw(3) << now_ms.count(); - - std::string level_str = toString(level); - std::transform(level_str.begin(), level_str.end(), level_str.begin(), ::tolower); - - log_file << "[" << time_stream.str() << "] [" << level_str << "] " << message << std::endl; - - HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - CONSOLE_SCREEN_BUFFER_INFO consoleInfo; - GetConsoleScreenBufferInfo(hConsole, &consoleInfo); - WORD saved_attributes = consoleInfo.wAttributes; - - if (color != 0) { - SetConsoleTextAttribute(hConsole, color); - } - - printf("[%s] [%s] %s\n", time_stream.str().c_str(), level_str.c_str(), message.c_str()); - - if (color != 0) { - SetConsoleTextAttribute(hConsole, saved_attributes); - } -} - -std::string Logger::toString(LogLevel level) { - switch (level) { - case LogLevel::DEBUG: return "DEBUG"; - case LogLevel::INFO: return "INFO"; - case LogLevel::WARN: return "WARN"; - case LogLevel::TRACE: return "TRACE"; - case LogLevel::ERROR: return "ERROR"; - case LogLevel::FATAL: return "FATAL"; - default: return "UNKNOWN"; - } -} - -std::string Logger::fromWStringToString(const std::wstring wstr) { - if (wstr.empty()) return std::string(); - - int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); - std::string strTo(size_needed, 0); - WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); - return strTo; -} diff --git a/native-src/logger/LoggerPath.cpp b/native-src/logger/LoggerPath.cpp deleted file mode 100644 index 99cddca9..00000000 --- a/native-src/logger/LoggerPath.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "LoggerPath.h" - -std::string LoggerPath::path_ = ""; -std::mutex LoggerPath::mutex_; - -void LoggerPath::set(const std::string& path) { - std::lock_guard guard(mutex_); - path_ = path; -} - -std::string LoggerPath::get() { - std::lock_guard guard(mutex_); - return path_; -} \ No newline at end of file diff --git a/native-src/main.cpp b/native-src/main.cpp index f8e07b7d..f4feb67e 100644 --- a/native-src/main.cpp +++ b/native-src/main.cpp @@ -11,7 +11,6 @@ napi_value init(napi_env env, napi_value exports) {"connectSyncRoot", nullptr, ConnectSyncRootWrapper, nullptr, nullptr, nullptr, napi_default, nullptr}, {"createFolderPlaceholder", nullptr, CreateFolderPlaceholderWrapper, nullptr, nullptr, nullptr, napi_default, nullptr}, {"disconnectSyncRoot", nullptr, DisconnectSyncRootWrapper, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"addLoggerPath", nullptr, addLoggerPathWrapper, nullptr, nullptr, nullptr, napi_default, nullptr}, {"updateSyncStatus", nullptr, UpdateSyncStatusWrapper, nullptr, nullptr, nullptr, napi_default, nullptr}, {"getPlaceholderState", nullptr, GetPlaceholderStateWrapper, nullptr, nullptr, nullptr, napi_default, nullptr}, {"convertToPlaceholder", nullptr, ConvertToPlaceholderWrapper, nullptr, nullptr, nullptr, napi_default, nullptr}, diff --git a/native-src/placeholders_interface/Planceholders.cpp b/native-src/placeholders_interface/Planceholders.cpp index 90f0d6c6..fd96fdb1 100644 --- a/native-src/placeholders_interface/Planceholders.cpp +++ b/native-src/placeholders_interface/Planceholders.cpp @@ -1,6 +1,5 @@ #include "stdafx.h" #include "Placeholders.h" -#include "Logger.h" #include #include #include diff --git a/native-src/sync_root_interface/Utilities.cpp b/native-src/sync_root_interface/Utilities.cpp deleted file mode 100644 index 98626db4..00000000 --- a/native-src/sync_root_interface/Utilities.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "stdafx.h" -#include -#include -#include -#include "Utilities.h" -#include - -DEFINE_PROPERTYKEY(PKEY_StorageProviderTransferProgress, 0xE77E90DF, 0x6271, 0x4F5B, 0x83, 0x4F, 0x2D, 0xD1, 0xF2, 0x45, 0xDD, 0xA4, 4); - -void Utilities::ApplyTransferStateToFile(_In_ PCWSTR fullPath, _In_ CF_CALLBACK_INFO &callbackInfo, UINT64 total, UINT64 completed) -{ - Logger::getInstance().log("ApplyTransferStateToFile", LogLevel::INFO); - // Tell the Cloud File API about progress so that toasts can be displayed - - HRESULT hr1 = CfReportProviderProgress( - callbackInfo.ConnectionKey, - callbackInfo.TransferKey, - LongLongToLargeInteger(total), - LongLongToLargeInteger(completed)); - - if (FAILED(hr1)) - { - wprintf(L"Failed to call CfReportProviderProgress with %08x\n", hr1); - return; - } - else - { - wprintf(L"Succesfully called CfReportProviderProgress \"%s\" with %llu/%llu\n", fullPath, completed, total); - } - // wprintf(L"Succesfully called CfReportProviderProgress \"%s\" with %llu/%llu\n", fullPath, completed, total); - - // Tell the Shell so File Explorer can display the progress bar in its view - try - { - // First, get the Volatile property store for the file. That's where the properties are maintained. - winrt::com_ptr shellItem; - winrt::com_ptr propStoreVolatile; - - winrt::check_hresult(SHCreateItemFromParsingName(fullPath, nullptr, __uuidof(shellItem), shellItem.put_void())); - - // wprintf(L"transfer-> propStoreVolatile \"%s\"\n", propStoreVolatile); - winrt::check_hresult( - shellItem->GetPropertyStore( - GETPROPERTYSTOREFLAGS::GPS_READWRITE | GETPROPERTYSTOREFLAGS::GPS_VOLATILEPROPERTIESONLY, - __uuidof(propStoreVolatile), - propStoreVolatile.put_void())); - // wprintf(L"transfer-> shellItem \"%s\"\n", shellItem); - // The PKEY_StorageProviderTransferProgress property works with a UINT64 array that is two elements, with - // element 0 being the amount of data transferred, and element 1 being the total amount - // that will be transferred. - if (completed < total) - { - PROPVARIANT pvProgress, pvStatus; - UINT64 values[]{completed, total}; - InitPropVariantFromUInt64Vector(values, ARRAYSIZE(values), &pvProgress); - InitPropVariantFromUInt32(SYNC_TRANSFER_STATUS::STS_TRANSFERRING, &pvStatus); - - propStoreVolatile->SetValue(PKEY_StorageProviderTransferProgress, pvProgress); - propStoreVolatile->SetValue(PKEY_SyncTransferStatus, pvStatus); - propStoreVolatile->Commit(); - - PropVariantClear(&pvProgress); - } - else - { - PROPVARIANT empty; - PropVariantInit(&empty); - propStoreVolatile->SetValue(PKEY_StorageProviderTransferProgress, empty); - propStoreVolatile->SetValue(PKEY_SyncTransferStatus, empty); - propStoreVolatile->Commit(); - - HANDLE h = CreateFileW(fullPath, - FILE_WRITE_ATTRIBUTES, - FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, - nullptr, - OPEN_EXISTING, - FILE_FLAG_OPEN_REPARSE_POINT, - nullptr); - if (h != INVALID_HANDLE_VALUE) - { - CfSetInSyncState(h, CF_IN_SYNC_STATE_IN_SYNC, - CF_SET_IN_SYNC_FLAG_NONE, nullptr); - CloseHandle(h); - } - } - } - catch (...) - { - // winrt::to_hresult() will eat the exception if it is a result of winrt::check_hresult, - // otherwise the exception will get rethrown and this method will crash out as it should - wprintf(L"Failed to Set Transfer Progress on \"%s\" with %08x\n", fullPath, static_cast(winrt::to_hresult())); - } -} diff --git a/native-src/sync_root_interface/callbacks/FetchData/FileCopierWithProgress.cpp b/native-src/sync_root_interface/callbacks/FetchData/FileCopierWithProgress.cpp deleted file mode 100644 index 3083214a..00000000 --- a/native-src/sync_root_interface/callbacks/FetchData/FileCopierWithProgress.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "stdafx.h" -#include -#include -#include -#include -#include "FileCopierWithProgress.h" -#include "Utilities.h" -#include -#include "Logger.h" - -namespace fs = std::filesystem; - -// 100MB chunks -#define CHUNKSIZE (4096 * 25600) -// Arbitrary delay per chunk, again, so you can actually see the progress bar -// move -#define CHUNKDELAYMS 250 - -#define FIELD_SIZE(type, field) (sizeof(((type *)0)->field)) -#define CF_SIZE_OF_OP_PARAM(field) \ - (FIELD_OFFSET(CF_OPERATION_PARAMETERS, field) + \ - FIELD_SIZE(CF_OPERATION_PARAMETERS, field)) - -HRESULT FileCopierWithProgress::TransferData( - _In_ CF_CONNECTION_KEY connectionKey, - _In_ LARGE_INTEGER transferKey, - _In_reads_bytes_opt_(length.QuadPart) LPCVOID transferData, - _In_ LARGE_INTEGER startingOffset, - _In_ LARGE_INTEGER length, - _In_ NTSTATUS completionStatus) -{ - Logger::getInstance().log("TransferData", LogLevel::INFO); - CF_OPERATION_INFO opInfo = {0}; - CF_OPERATION_PARAMETERS opParams = {0}; - // wprintf(L"[%04x:%04x] - TransferData\n", GetCurrentProcessId(), GetCurrentThreadId()); - opInfo.StructSize = sizeof(opInfo); - opInfo.Type = CF_OPERATION_TYPE_TRANSFER_DATA; - opInfo.ConnectionKey = connectionKey; - opInfo.TransferKey = transferKey; - opParams.ParamSize = CF_SIZE_OF_OP_PARAM(TransferData); - opParams.TransferData.CompletionStatus = completionStatus; - opParams.TransferData.Buffer = transferData; - opParams.TransferData.Offset = startingOffset; - opParams.TransferData.Length = length; - - HRESULT hr = CfExecute(&opInfo, &opParams); - if (FAILED(hr)) - { - wprintf(L"Error in CfExecute(), HRESULT: %lx\n", hr); - } - printf("TransferData: %s\n", SUCCEEDED(hr) ? "Succeeded" : "Failed"); - - return hr; -} \ No newline at end of file diff --git a/native-src/virtual_drive/Wrappers.cpp b/native-src/virtual_drive/Wrappers.cpp index 2e087185..5b2b5446 100644 --- a/native-src/virtual_drive/Wrappers.cpp +++ b/native-src/virtual_drive/Wrappers.cpp @@ -2,8 +2,6 @@ #include #include "Placeholders.h" #include "Callbacks.h" -#include "LoggerPath.h" -#include #include #include #include @@ -57,46 +55,6 @@ napi_value DisconnectSyncRootWrapper(napi_env env, napi_callback_info args) return NAPI_SAFE_WRAP(env, args, disconnect_sync_root); } -napi_value addLoggerPathWrapper(napi_env env, napi_callback_info args) -{ - size_t argc = 1; - napi_value argv[1]; - - napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr); - if (argc < 1) - { - napi_throw_error(env, nullptr, "The path is required for addLoggerPath"); - return nullptr; - } - - // Obtener la longitud de la cadena UTF-16. - size_t pathLength; - napi_get_value_string_utf16(env, argv[0], nullptr, 0, &pathLength); - - // Crear un buffer para la cadena UTF-16. - std::unique_ptr widePath(new wchar_t[pathLength + 1]); - - // Obtener la cadena UTF-16. - napi_get_value_string_utf16(env, argv[0], reinterpret_cast(widePath.get()), pathLength + 1, nullptr); - - // Obtener la longitud necesaria para la cadena UTF-8. - int utf8Length = WideCharToMultiByte(CP_UTF8, 0, widePath.get(), -1, nullptr, 0, nullptr, nullptr); - - // Crear un buffer para la cadena UTF-8. - std::unique_ptr utf8Path(new char[utf8Length]); - - // Realizar la conversión de UTF-16 a UTF-8. - WideCharToMultiByte(CP_UTF8, 0, widePath.get(), -1, utf8Path.get(), utf8Length, nullptr, nullptr); - - // Inicializar el logger con la ruta UTF-8. - LoggerPath::set(std::string(utf8Path.get())); - - // Devolver un valor booleano verdadero. - napi_value result; - napi_get_boolean(env, true, &result); - return result; -} - napi_value UpdateSyncStatusWrapper(napi_env env, napi_callback_info args) { return NAPI_SAFE_WRAP(env, args, update_sync_status_wrapper);