Skip to content
Empty file.
2 changes: 1 addition & 1 deletion examples/drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import VirtualDrive from "@/virtual-drive";

import settings from "./settings";

export const drive = new VirtualDrive(settings.syncRootPath, settings.defaultLogPath);
export const drive = new VirtualDrive(settings.syncRootPath, settings.providerid, settings.defaultLogPath);
export const logger = createLogger(settings.defaultLogPath);
4 changes: 2 additions & 2 deletions examples/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const handlers = { handleAdd, handleHydrate, handleDehydrate, handleChangeSize }
const notify = { onTaskSuccess: async () => undefined, onTaskProcessing: async () => undefined };
const queueManager = new QueueManager(handlers, notify, settings.queuePersistPath);

drive.registerSyncRoot(settings.driveName, settings.driveVersion, settings.providerid, callbacks, settings.iconPath);
drive.registerSyncRoot(settings.driveName, settings.driveVersion, callbacks, settings.iconPath);
drive.connectSyncRoot();

try {
Expand All @@ -29,5 +29,5 @@ try {
} catch (error) {
logger.error(error);
drive.disconnectSyncRoot();
VirtualDrive.unregisterSyncRoot(settings.syncRootPath);
drive.unregisterSyncRoot();
}
4 changes: 2 additions & 2 deletions examples/unregister.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import VirtualDrive from "@/virtual-drive";

import { drive } from "./drive";
import { deleteInfoItems } from "./info-items-manager";
import settings from "./settings";

VirtualDrive.unregisterSyncRoot(settings.syncRootPath);
drive.unregisterSyncRoot();
deleteInfoItems();
8 changes: 5 additions & 3 deletions include/sync_root_interface/SyncRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class SyncRoot
public:
static HRESULT RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *providerName, const wchar_t *providerVersion, const GUID &providerId, const wchar_t *logoPath);
static HRESULT ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallbacks syncCallbacks, napi_env env, CF_CONNECTION_KEY *connectionKey);
static HRESULT DisconnectSyncRoot();
static HRESULT UnregisterSyncRoot();
static std::list<ItemInfo> GetItemsSyncRoot(const wchar_t *syncRootPath);
static HRESULT DisconnectSyncRoot(const wchar_t *syncRootPath);
static HRESULT UnregisterSyncRoot(const GUID &providerId);
static std::string GetFileIdentity(const wchar_t *path);
static void HydrateFile(const wchar_t *filePath);
static void DehydrateFile(const wchar_t *filePath);
static void DeleteFileSyncRoot(const wchar_t *path);

private:
CF_CONNECTION_KEY connectionKey;
};
57 changes: 32 additions & 25 deletions native-src/sync_root_interface/SyncRoot.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "stdafx.h"
#include "SyncRoot.h"
#include "Callbacks.h"
#include "SyncRoot.h"
#include "stdafx.h"
#include <iostream>
#include <iostream>
#include <filesystem>
Expand All @@ -9,6 +9,7 @@
namespace fs = std::filesystem;
// variable to disconect
CF_CONNECTION_KEY gloablConnectionKey;
std::map<std::wstring, CF_CONNECTION_KEY> connectionMap;

void TransformInputCallbacksToSyncCallbacks(napi_env env, InputSyncCallbacks input)
{
Expand Down Expand Up @@ -115,16 +116,17 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p
{
try
{
auto syncRootID = providerId;
// Convert GUID to string for syncRootID
wchar_t syncRootID[39];
StringFromGUID2(providerId, syncRootID, 39);

winrt::StorageProviderSyncRootInfo info;
info.Id(L"syncRootID");
info.Id(syncRootID);

auto folder = winrt::StorageFolder::GetFolderFromPathAsync(syncRootPath).get();
info.Path(folder);

// The string can be in any form acceptable to SHLoadIndirectString.

info.DisplayNameResource(providerName);

std::wstring completeIconResource = std::wstring(logoPath) + L",0";
Expand All @@ -135,7 +137,7 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p
info.HydrationPolicyModifier(winrt::StorageProviderHydrationPolicyModifier::None);
info.PopulationPolicy(winrt::StorageProviderPopulationPolicy::AlwaysFull);
info.InSyncPolicy(winrt::StorageProviderInSyncPolicy::FileCreationTime | winrt::StorageProviderInSyncPolicy::DirectoryCreationTime);
info.Version(L"1.0.0");
info.Version(providerVersion);
info.ShowSiblingsAsGroup(false);
info.HardlinkPolicy(winrt::StorageProviderHardlinkPolicy::None);

Expand All @@ -145,9 +147,8 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p
// Context
std::wstring syncRootIdentity(syncRootPath);
syncRootIdentity.append(L"->");
syncRootIdentity.append(L"TestProvider");
syncRootIdentity.append(providerName);

wchar_t const contextString[] = L"TestProviderContextString";
winrt::IBuffer contextBuffer = winrt::CryptographicBuffer::ConvertStringToBinary(syncRootIdentity.data(), winrt::BinaryStringEncoding::Utf8);
info.Context(contextBuffer);

Expand All @@ -165,20 +166,26 @@ HRESULT SyncRoot::RegisterSyncRoot(const wchar_t *syncRootPath, const wchar_t *p
catch (...)
{
wprintf(L"Could not register the sync root, hr %08x\n", static_cast<HRESULT>(winrt::to_hresult()));
return E_FAIL;
}
}

HRESULT SyncRoot::UnregisterSyncRoot()
HRESULT SyncRoot::UnregisterSyncRoot(const GUID &providerId)
{
try
{
// Convert GUID to string for syncRootID
wchar_t syncRootID[39];
StringFromGUID2(providerId, syncRootID, 39);

Logger::getInstance().log("Unregistering sync root.", LogLevel::INFO);
winrt::StorageProviderSyncRootManager::Unregister(L"syncRootID");
winrt::StorageProviderSyncRootManager::Unregister(syncRootID);
return S_OK;
}
catch (...)
{
wprintf(L"Could not unregister the sync root, hr %08x\n", static_cast<HRESULT>(winrt::to_hresult()));
return E_FAIL;
}
}

Expand All @@ -205,38 +212,38 @@ HRESULT SyncRoot::ConnectSyncRoot(const wchar_t *syncRootPath, InputSyncCallback
CF_CONNECT_FLAG_REQUIRE_PROCESS_INFO | CF_CONNECT_FLAG_REQUIRE_FULL_FILE_PATH,
connectionKey);
wprintf(L"Connection key: %llu\n", *connectionKey);
gloablConnectionKey = *connectionKey;
if (SUCCEEDED(hr))
{
connectionMap[syncRootPath] = *connectionKey;
}
return hr;
}
catch (const std::exception &e)
{
wprintf(L"Excepción capturada: %hs\n", e.what());
// Aquí puedes decidir si retornar un código de error específico o mantener el E_FAIL.
return E_FAIL;
}
catch (...)
{
wprintf(L"Excepción desconocida capturada\n");
// Igualmente, puedes decidir el código de error a retornar.
return E_FAIL;
}
}

// disconection sync root
HRESULT SyncRoot::DisconnectSyncRoot()
HRESULT SyncRoot::DisconnectSyncRoot(const wchar_t *syncRootPath)
{
Logger::getInstance().log("Disconnecting sync root.", LogLevel::INFO);
try
auto it = connectionMap.find(syncRootPath);
if (it != connectionMap.end())
{
HRESULT hr = CfDisconnectSyncRoot(gloablConnectionKey);
HRESULT hr = CfDisconnectSyncRoot(it->second);
if (SUCCEEDED(hr))
{
connectionMap.erase(it);
}
return hr;
}
catch (const std::exception &e)
{
Logger::getInstance().log("Exception caught: " + std::string(e.what()), LogLevel::ERROR);
}
catch (...)
{
Logger::getInstance().log("Unknown exception caught.", LogLevel::ERROR);
}
return E_FAIL;
}

// struct
Expand Down
28 changes: 18 additions & 10 deletions native-src/virtual_drive/Wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,27 @@ napi_value UnregisterSyncRootWrapper(napi_env env, napi_callback_info args)

if (argc < 1)
{
napi_throw_error(env, nullptr, "The sync root path is required for UnregisterSyncRoot");
napi_throw_error(env, nullptr, "The provider ID is required for UnregisterSyncRoot");
return nullptr;
}

LPCWSTR syncRootPath;
size_t pathLength;
napi_get_value_string_utf16(env, argv[0], nullptr, 0, &pathLength);
syncRootPath = new WCHAR[pathLength + 1];
napi_get_value_string_utf16(env, argv[0], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(syncRootPath)), pathLength + 1, nullptr);
GUID providerId;
LPCWSTR providerIdStr;
size_t providerIdStrLength;
napi_get_value_string_utf16(env, argv[0], nullptr, 0, &providerIdStrLength);
providerIdStr = new WCHAR[providerIdStrLength + 1];
napi_get_value_string_utf16(env, argv[0], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(providerIdStr)), providerIdStrLength + 1, nullptr);

HRESULT result = SyncRoot::UnregisterSyncRoot();
if (FAILED(CLSIDFromString(providerIdStr, &providerId)))
{
napi_throw_error(env, nullptr, "Invalid GUID format");
delete[] providerIdStr;
return nullptr;
}

delete[] syncRootPath;
HRESULT result = SyncRoot::UnregisterSyncRoot(providerId);

delete[] providerIdStr;

napi_value napiResult;
napi_create_int32(env, static_cast<int32_t>(result), &napiResult);
Expand Down Expand Up @@ -429,8 +437,8 @@ napi_value DisconnectSyncRootWrapper(napi_env env, napi_callback_info args)
syncRootPath = new WCHAR[pathLength + 1];
napi_get_value_string_utf16(env, argv[0], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(syncRootPath)), pathLength + 1, nullptr);

HRESULT result = SyncRoot::DisconnectSyncRoot();
// wprintf(L"DisconnectSyncRootWrapper: %08x\n", static_cast<HRESULT>(result));
HRESULT result = SyncRoot::DisconnectSyncRoot(syncRootPath);

delete[] syncRootPath;

napi_value napiResult;
Expand Down
8 changes: 4 additions & 4 deletions src/addon-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export class Addon {
return this.parseAddonZod("connectSyncRoot", result);
}

unregisterSyncRoot({ syncRootPath }: { syncRootPath: string }) {
const result = addon.unregisterSyncRoot(syncRootPath);
unregisterSyncRoot({ providerId }: { providerId: string }) {
const result = addon.unregisterSyncRoot(providerId);
return this.parseAddonZod("unregisterSyncRoot", result);
}

disconnectSyncRoot() {
return addon.disconnectSyncRoot(this.syncRootPath);
disconnectSyncRoot({ syncRootPath }: { syncRootPath: string }) {
return addon.disconnectSyncRoot(syncRootPath);
}

addLogger({ logPath }: { logPath: string }) {
Expand Down
6 changes: 5 additions & 1 deletion src/types/callbacks.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export type NapiCallbackFunction = (...args: any[]) => any;

export type FilePlaceholderIdPrefixType = "FILE:";

export type FilePlaceholderId = `${FilePlaceholderIdPrefixType}${string}`;

export type TFetchDataCallback = (
id: string,
id: FilePlaceholderId,
callback: (data: boolean, path: string, errorHandler?: () => void) => Promise<{ finished: boolean; progress: number }>,
) => void;

Expand Down
Loading