Skip to content

Commit eb8810d

Browse files
Commit
1 parent b665304 commit eb8810d

8 files changed

Lines changed: 49 additions & 44 deletions

File tree

dist/addon.node

0 Bytes
Binary file not shown.

include/placeholders_interface/Placeholders.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class Placeholders
1010
static void ForceShellRefresh(const std::wstring &path);
1111
static void UpdateSyncStatus(const std::wstring &filePath, bool syncState, bool isDirectory);
1212
static HRESULT UpdatePinState(const std::wstring &path, const PinState state);
13-
static void ConvertToPlaceholder(const std::wstring &path, const std::wstring &placeholderId);
1413
static std::string GetFileIdentity(const std::wstring &filePath);
1514
static void UpdateFileIdentity(const std::wstring &filePath, const std::wstring &fileIdentity, bool isDirectory);
1615
static FileState GetPlaceholderInfo(const std::wstring &directoryPath);

include/virtual_drive/convert_to_placeholder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
#include <node_api.h>
44

5-
napi_value convert_to_placeholder_impl(napi_env env, napi_callback_info args);
5+
napi_value convert_to_placeholder_wrapper(napi_env env, napi_callback_info args);
6+
void convert_to_placeholder(const std::wstring &path, const std::wstring &placeholderId);

native-src/placeholders_interface/Planceholders.cpp

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <cctype>
1616
#include <windows.h>
1717
#include <shlobj.h>
18+
#include "convert_to_placeholder.h"
1819

1920
using namespace std;
2021

@@ -61,40 +62,6 @@ void Placeholders::MaintainIdentity(std::wstring &fullPath, PCWSTR itemIdentity,
6162
}
6263
}
6364

64-
void Placeholders::ConvertToPlaceholder(const std::wstring &path, const std::wstring &placeholderId)
65-
{
66-
bool isDirectory = fs::is_directory(path);
67-
68-
winrt::file_handle fileHandle{CreateFileW(
69-
path.c_str(),
70-
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
71-
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
72-
nullptr,
73-
OPEN_EXISTING,
74-
isDirectory ? FILE_FLAG_BACKUP_SEMANTICS : 0,
75-
nullptr)};
76-
77-
if (!fileHandle)
78-
{
79-
throw std::runtime_error("Failed to open file: " + std::to_string(GetLastError()));
80-
}
81-
82-
CF_CONVERT_FLAGS convertFlags = CF_CONVERT_FLAG_MARK_IN_SYNC;
83-
USN convertUsn;
84-
OVERLAPPED overlapped = {};
85-
86-
LPCVOID idStrLPCVOID = static_cast<LPCVOID>(placeholderId.c_str());
87-
DWORD idStrByteLength = static_cast<DWORD>(placeholderId.size() * sizeof(wchar_t));
88-
89-
HRESULT hr = CfConvertToPlaceholder(fileHandle.get(), idStrLPCVOID, idStrByteLength, convertFlags, &convertUsn, &overlapped);
90-
91-
// Only throw if it's not "already a placeholder" error
92-
if (hr != 0x8007017C)
93-
{
94-
winrt::check_hresult(hr);
95-
}
96-
}
97-
9865
/**
9966
* @brief Mark a file or directory as synchronized
10067
* @param filePath path to the file or directory
@@ -147,7 +114,7 @@ void Placeholders::UpdateSyncStatus(const std::wstring &filePath,
147114
break;
148115

149116
case ERROR_CLOUD_FILE_NOT_IN_SYNC:
150-
ConvertToPlaceholder(filePath, L"temp_identity");
117+
convert_to_placeholder(filePath, L"temp_identity");
151118
hr = CfSetInSyncState(h, sync, CF_SET_IN_SYNC_FLAG_NONE, nullptr);
152119
wprintf(L"[UpdateSyncStatus] Retry CfSetInSyncState\n");
153120
break;

native-src/virtual_drive/Wrappers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ napi_value GetPlaceholderStateWrapper(napi_env env, napi_callback_info args) {
104104
}
105105

106106
napi_value ConvertToPlaceholderWrapper(napi_env env, napi_callback_info args) {
107-
return NAPI_SAFE_WRAP(env, args, convert_to_placeholder_impl);
107+
return NAPI_SAFE_WRAP(env, args, convert_to_placeholder_wrapper);
108108
}
109109

110110
napi_value HydrateFileWrapper(napi_env env, napi_callback_info args) {
Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
1-
#include <Windows.h>
1+
#include <filesystem>
2+
#include <windows.h>
3+
#include "stdafx.h"
24
#include "napi_extract_args.h"
3-
#include "Placeholders.h"
45

5-
napi_value convert_to_placeholder_impl(napi_env env, napi_callback_info info) {
6+
void convert_to_placeholder(const std::wstring &path, const std::wstring &placeholderId)
7+
{
8+
bool isDirectory = std::filesystem::is_directory(path);
9+
10+
winrt::file_handle fileHandle{CreateFileW(
11+
path.c_str(),
12+
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
13+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
14+
nullptr,
15+
OPEN_EXISTING,
16+
isDirectory ? FILE_FLAG_BACKUP_SEMANTICS : 0,
17+
nullptr)};
18+
19+
if (!fileHandle)
20+
{
21+
throw std::runtime_error("Failed to open item: " + std::to_string(GetLastError()));
22+
}
23+
24+
CF_CONVERT_FLAGS convertFlags = CF_CONVERT_FLAG_MARK_IN_SYNC;
25+
USN convertUsn;
26+
OVERLAPPED overlapped = {};
27+
28+
LPCVOID idStrLPCVOID = static_cast<LPCVOID>(placeholderId.c_str());
29+
DWORD idStrByteLength = static_cast<DWORD>(placeholderId.size() * sizeof(wchar_t));
30+
31+
HRESULT hr = CfConvertToPlaceholder(fileHandle.get(), idStrLPCVOID, idStrByteLength, convertFlags, &convertUsn, &overlapped);
32+
33+
// Only throw if it's not "already a placeholder" error
34+
if (hr != 0x8007017C)
35+
{
36+
winrt::check_hresult(hr);
37+
}
38+
}
39+
40+
napi_value convert_to_placeholder_wrapper(napi_env env, napi_callback_info info)
41+
{
642
auto [path, placeholderId] = napi_extract_args<std::wstring, std::wstring>(env, info);
743

8-
Placeholders::ConvertToPlaceholder(path.c_str(), placeholderId.c_str());
44+
convert_to_placeholder(path.c_str(), placeholderId.c_str());
945

1046
return nullptr;
1147
}

native-src/virtual_drive/create_file_placeholder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <filesystem>
22
#include <windows.h>
33
#include "Placeholders.h"
4+
#include "convert_to_placeholder.h"
45
#include "Utilities.h"
56
#include "napi_extract_args.h"
67

@@ -17,7 +18,7 @@ napi_value create_file_placeholder_impl(napi_env env, napi_callback_info info)
1718

1819
if (std::filesystem::exists(path))
1920
{
20-
Placeholders::ConvertToPlaceholder(path, placeholderId);
21+
convert_to_placeholder(path, placeholderId);
2122
Placeholders::MaintainIdentity(path, placeholderId.c_str(), false);
2223
return nullptr;
2324
}

native-src/virtual_drive/create_folder_placeholder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <filesystem>
22
#include <windows.h>
33
#include "Placeholders.h"
4+
#include "convert_to_placeholder.h"
45
#include "Utilities.h"
56
#include "napi_extract_args.h"
67

@@ -17,7 +18,7 @@ napi_value create_folder_placeholder_impl(napi_env env, napi_callback_info info)
1718

1819
if (std::filesystem::exists(path))
1920
{
20-
Placeholders::ConvertToPlaceholder(path, placeholderId);
21+
convert_to_placeholder(path, placeholderId);
2122
Placeholders::MaintainIdentity(path, placeholderId.c_str(), true);
2223
return nullptr;
2324
}

0 commit comments

Comments
 (0)