2020-12-31 23:33:43 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2020-12-31 23:33:43 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2020-12-23 09:25:27 +01:00
|
|
|
#pragma once
|
2020-12-23 16:08:34 +01:00
|
|
|
|
2020-12-23 09:25:27 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
#ifdef TD_PORT_WINDOWS
|
2020-12-23 16:08:34 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-12-23 09:25:27 +01:00
|
|
|
#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
|
|
|
|
inline HMODULE GetKernelModule() {
|
2020-12-23 16:08:34 +01:00
|
|
|
static const auto kernel_module = []() -> HMODULE {
|
2020-12-23 09:25:27 +01:00
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
if (VirtualQuery(VirtualQuery, &mbi, sizeof(MEMORY_BASIC_INFORMATION))) {
|
2020-12-23 16:08:34 +01:00
|
|
|
return reinterpret_cast<HMODULE>(mbi.AllocationBase);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
2020-12-23 16:08:34 +01:00
|
|
|
return nullptr;
|
|
|
|
}();
|
|
|
|
return kernel_module;
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline HMODULE LoadLibrary(LPCTSTR lpFileName) {
|
2020-12-23 16:08:34 +01:00
|
|
|
using pLoadLibrary = HMODULE(WINAPI *)(_In_ LPCTSTR);
|
|
|
|
static const auto proc_load_library =
|
|
|
|
reinterpret_cast<pLoadLibrary>(GetProcAddress(GetKernelModule(), "LoadLibraryW"));
|
|
|
|
return proc_load_library(lpFileName);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline HMODULE GetFromAppModule() {
|
2020-12-23 16:08:34 +01:00
|
|
|
static const HMODULE from_app_module = LoadLibrary(L"api-ms-win-core-file-fromapp-l1-1-0.dll");
|
|
|
|
return from_app_module;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <int num, class T>
|
|
|
|
T *get_from_app_function(const char *name, T *original_func) {
|
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
|
|
|
|
return original_func;
|
|
|
|
#else
|
|
|
|
static T *func = [name, original_func]() -> T * {
|
|
|
|
auto func_pointer = GetProcAddress(GetFromAppModule(), name);
|
|
|
|
if (func_pointer == nullptr) {
|
|
|
|
return original_func;
|
|
|
|
}
|
|
|
|
return reinterpret_cast<T *>(func_pointer);
|
|
|
|
}();
|
|
|
|
return func;
|
|
|
|
#endif
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline HANDLE CreateFile2FromAppW(_In_ LPCWSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode,
|
|
|
|
_In_ DWORD dwCreationDisposition,
|
|
|
|
_In_opt_ LPCREATEFILE2_EXTENDED_PARAMETERS pCreateExParams) {
|
2020-12-23 16:08:34 +01:00
|
|
|
auto func = get_from_app_function<0>("CreateFile2FromAppW", &CreateFile2);
|
|
|
|
return func(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BOOL CreateDirectoryFromAppW(_In_ LPCWSTR lpPathName, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes) {
|
2020-12-23 16:08:34 +01:00
|
|
|
auto func = get_from_app_function<1>("CreateDirectoryFromAppW", &CreateDirectory);
|
|
|
|
return func(lpPathName, lpSecurityAttributes);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BOOL RemoveDirectoryFromAppW(_In_ LPCWSTR lpPathName) {
|
2020-12-23 16:08:34 +01:00
|
|
|
auto func = get_from_app_function<2>("RemoveDirectoryFromAppW", &RemoveDirectory);
|
|
|
|
return func(lpPathName);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BOOL DeleteFileFromAppW(_In_ LPCWSTR lpFileName) {
|
2020-12-23 16:08:34 +01:00
|
|
|
auto func = get_from_app_function<3>("DeleteFileFromAppW", &DeleteFile);
|
|
|
|
return func(lpFileName);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline BOOL MoveFileExFromAppW(_In_ LPCWSTR lpExistingFileName, _In_ LPCWSTR lpNewFileName, _In_ DWORD dwFlags) {
|
2021-04-04 22:39:45 +02:00
|
|
|
auto func = get_from_app_function<4>("MoveFileFromAppW", static_cast<BOOL(WINAPI *)(LPCWSTR, LPCWSTR)>(nullptr));
|
|
|
|
if (func == nullptr || (dwFlags & ~MOVEFILE_REPLACE_EXISTING) != 0) {
|
2021-04-04 01:58:59 +02:00
|
|
|
// if can't find MoveFileFromAppW or have unsupported flags, call MoveFileEx directly
|
|
|
|
return MoveFileEx(lpExistingFileName, lpNewFileName, dwFlags);
|
|
|
|
}
|
|
|
|
if ((dwFlags & MOVEFILE_REPLACE_EXISTING) != 0) {
|
2020-12-23 16:08:34 +01:00
|
|
|
td::DeleteFileFromAppW(lpNewFileName);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
2021-04-04 01:58:59 +02:00
|
|
|
return func(lpExistingFileName, lpNewFileName);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline HANDLE FindFirstFileExFromAppW(_In_ LPCWSTR lpFileName, _In_ FINDEX_INFO_LEVELS fInfoLevelId,
|
|
|
|
_Out_writes_bytes_(sizeof(WIN32_FIND_DATAW)) LPVOID lpFindFileData,
|
|
|
|
_In_ FINDEX_SEARCH_OPS fSearchOp, _Reserved_ LPVOID lpSearchFilter,
|
|
|
|
_In_ DWORD dwAdditionalFlags) {
|
2020-12-23 16:08:34 +01:00
|
|
|
auto func = get_from_app_function<5>("FindFirstFileExFromAppW", &FindFirstFileEx);
|
|
|
|
return func(lpFileName, fInfoLevelId, lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
|
2020-12-23 09:25:27 +01:00
|
|
|
}
|
2020-12-23 16:08:34 +01:00
|
|
|
|
2020-12-23 09:25:27 +01:00
|
|
|
} // namespace td
|
2020-12-23 16:08:34 +01:00
|
|
|
|
2020-12-23 09:25:27 +01:00
|
|
|
#endif
|