|
2 | 2 | #include "napi_extract_args.h" |
3 | 3 | #include "stdafx.h" |
4 | 4 |
|
5 | | -napi_value dehydrate_file(napi_env env, napi_callback_info info) { |
| 5 | +napi_value dehydrate_file(napi_env env, napi_callback_info info) |
| 6 | +{ |
6 | 7 | auto [rawPath] = napi_extract_args<std::wstring>(env, info); |
7 | | - const wchar_t* path = rawPath.c_str(); |
| 8 | + const wchar_t *path = rawPath.c_str(); |
8 | 9 |
|
9 | 10 | DWORD attrib = GetFileAttributesW(path); |
10 | 11 |
|
11 | | - if (attrib & FILE_ATTRIBUTE_DIRECTORY) { |
| 12 | + if (attrib & FILE_ATTRIBUTE_DIRECTORY) |
| 13 | + { |
12 | 14 | throw std::runtime_error("Cannot dehydrate folder"); |
13 | 15 | } |
14 | 16 |
|
15 | | - winrt::handle placeholder(CreateFileW(path, 0, FILE_READ_DATA, nullptr, OPEN_EXISTING, 0, nullptr)); |
| 17 | + winrt::handle fileHandle(CreateFileW(path, 0, FILE_READ_DATA, nullptr, OPEN_EXISTING, 0, nullptr)); |
16 | 18 |
|
17 | 19 | LARGE_INTEGER offset; |
18 | 20 | offset.QuadPart = 0; |
19 | 21 | LARGE_INTEGER length; |
20 | | - GetFileSizeEx(placeholder.get(), &length); |
21 | | - |
22 | | - HRESULT hr = CfDehydratePlaceholder(placeholder.get(), offset, length, CF_DEHYDRATE_FLAG_NONE, NULL); |
23 | | - |
24 | | - if (SUCCEEDED(hr)) { |
25 | | - return nullptr; |
26 | | - } |
27 | | - |
28 | | - DWORD err = HRESULT_CODE(hr); |
29 | | - |
30 | | - if (err == ERROR_SHARING_VIOLATION || err == ERROR_CLOUD_FILE_IN_USE) { |
31 | | - // MessageBoxW( |
32 | | - // nullptr, |
33 | | - // L"Unable to free up space because the file is currently in use.\nPlease close the file and try again.", |
34 | | - // L"File in use", |
35 | | - // MB_OK | MB_ICONWARNING | MB_SYSTEMMODAL); |
36 | | - } |
37 | | - |
38 | | - winrt::throw_hresult(hr); |
| 22 | + GetFileSizeEx(fileHandle.get(), &length); |
| 23 | + |
| 24 | + winrt::check_hresult(CfDehydratePlaceholder( |
| 25 | + fileHandle.get(), |
| 26 | + offset, |
| 27 | + length, |
| 28 | + CF_DEHYDRATE_FLAG_NONE, |
| 29 | + nullptr)); |
39 | 30 | } |
0 commit comments