1 /* Copyright (c) Mark Harmstone 2016-17
3 * This file is part of WinBtrfs.
5 * WinBtrfs is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public Licence as published by
7 * the Free Software Foundation, either version 3 of the Licence, or
8 * (at your option) any later version.
10 * WinBtrfs is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public Licence for more details.
15 * You should have received a copy of the GNU Lesser General Public Licence
16 * along with WinBtrfs. If not, see <http://www.gnu.org/licenses/>. */
20 /* C++ backwards-compatibility */
22 #if defined(_MSC_VER) && (_MSC_VER < 1900)
27 #define ISOLATION_AWARE_ENABLED 1
28 #define STRSAFE_NO_DEPRECATE
31 #define WINVER 0x0A00 // Windows 10
32 #define _WIN32_WINNT 0x0A00
39 #define WIN32_NO_STATUS
43 #include <ndk/iofuncs.h>
44 #include <ndk/obfuncs.h>
48 #define string_view string
49 #define wstring_view wstring
55 #include "../btrfsioctl.h"
58 #include "btrfsioctl.h"
64 #define STATUS_SUCCESS (NTSTATUS)0x00000000
65 #define STATUS_BUFFER_OVERFLOW (NTSTATUS)0x80000005
66 #define STATUS_END_OF_FILE (NTSTATUS)0xc0000011
67 #define STATUS_MORE_PROCESSING_REQUIRED (NTSTATUS)0xc0000016
68 #define STATUS_BUFFER_TOO_SMALL (NTSTATUS)0xc0000023
69 #define STATUS_DEVICE_NOT_READY (NTSTATUS)0xc00000a3
70 #define STATUS_CANNOT_DELETE (NTSTATUS)0xc0000121
71 #define STATUS_NOT_FOUND (NTSTATUS)0xc0000225
74 #define BLOCK_FLAG_DATA 0x001
75 #define BLOCK_FLAG_SYSTEM 0x002
76 #define BLOCK_FLAG_METADATA 0x004
77 #define BLOCK_FLAG_RAID0 0x008
78 #define BLOCK_FLAG_RAID1 0x010
79 #define BLOCK_FLAG_DUPLICATE 0x020
80 #define BLOCK_FLAG_RAID10 0x040
81 #define BLOCK_FLAG_RAID5 0x080
82 #define BLOCK_FLAG_RAID6 0x100
84 #define BTRFS_TYPE_FILE 1
85 #define BTRFS_TYPE_DIRECTORY 2
86 #define BTRFS_TYPE_CHARDEV 3
87 #define BTRFS_TYPE_BLOCKDEV 4
88 #define BTRFS_TYPE_FIFO 5
89 #define BTRFS_TYPE_SOCKET 6
90 #define BTRFS_TYPE_SYMLINK 7
93 #define funcname __FUNCTION__
95 #define funcname __func__
99 #pragma warning(disable: 4800)
106 NTSYSCALLAPI NTSTATUS NTAPI
NtFsControlFile(HANDLE FileHandle
, HANDLE Event
, PIO_APC_ROUTINE ApcRoutine
, PVOID ApcContext
,
107 PIO_STATUS_BLOCK IoStatusBlock
, ULONG FsControlCode
, PVOID InputBuffer
, ULONG InputBufferLength
,
108 PVOID OutputBuffer
, ULONG OutputBufferLength
);
110 NTSTATUS NTAPI
NtReadFile(HANDLE FileHandle
, HANDLE Event
, PIO_APC_ROUTINE ApcRoutine
, PVOID ApcContext
, PIO_STATUS_BLOCK IoStatusBlock
, PVOID Buffer
,
111 ULONG Length
, PLARGE_INTEGER ByteOffset
, PULONG Key
);
113 NTSTATUS WINAPI
NtSetEaFile(HANDLE FileHandle
, PIO_STATUS_BLOCK IoStatusBlock
, PVOID Buffer
, ULONG Length
);
115 NTSTATUS WINAPI
NtSetSecurityObject(HANDLE Handle
, SECURITY_INFORMATION SecurityInformation
, PSECURITY_DESCRIPTOR SecurityDescriptor
);
117 NTSTATUS NTAPI
NtQueryInformationFile(HANDLE hFile
, PIO_STATUS_BLOCK io
, PVOID ptr
, ULONG len
, FILE_INFORMATION_CLASS FileInformationClass
);
119 NTSTATUS NTAPI
NtSetInformationFile(HANDLE hFile
, PIO_STATUS_BLOCK io
, PVOID ptr
, ULONG len
, FILE_INFORMATION_CLASS FileInformationClass
);
122 #define FileBasicInformation (FILE_INFORMATION_CLASS)4
123 #define FileStandardInformation (FILE_INFORMATION_CLASS)5
124 #define FileDispositionInformation (FILE_INFORMATION_CLASS)13
125 #define FileEndOfFileInformation (FILE_INFORMATION_CLASS)20
126 #define FileStreamInformation (FILE_INFORMATION_CLASS)22
128 typedef enum _FSINFOCLASS
{
129 FileFsVolumeInformation
= 1,
130 FileFsLabelInformation
,
131 FileFsSizeInformation
,
132 FileFsDeviceInformation
,
133 FileFsAttributeInformation
,
134 FileFsControlInformation
,
135 FileFsFullSizeInformation
,
136 FileFsObjectIdInformation
,
137 FileFsDriverPathInformation
,
138 FileFsVolumeFlagsInformation
,
139 FileFsSectorSizeInformation
,
140 FileFsDataCopyInformation
,
141 FileFsMetadataSizeInformation
,
142 FileFsFullSizeInformationEx
,
143 FileFsMaximumInformation
144 } FS_INFORMATION_CLASS
, *PFS_INFORMATION_CLASS
;
146 typedef struct _FILE_STREAM_INFORMATION
{
147 ULONG NextEntryOffset
;
148 ULONG StreamNameLength
;
149 LARGE_INTEGER StreamSize
;
150 LARGE_INTEGER StreamAllocationSize
;
152 } FILE_STREAM_INFORMATION
, *PFILE_STREAM_INFORMATION
;
155 NTSTATUS NTAPI
NtQueryVolumeInformationFile(HANDLE FileHandle
, PIO_STATUS_BLOCK IoStatusBlock
, PVOID FsInformation
, ULONG Length
,
156 FS_INFORMATION_CLASS FsInformationClass
);
163 typedef struct _REPARSE_DATA_BUFFER
{
165 USHORT ReparseDataLength
;
170 USHORT SubstituteNameOffset
;
171 USHORT SubstituteNameLength
;
172 USHORT PrintNameOffset
;
173 USHORT PrintNameLength
;
176 } SymbolicLinkReparseBuffer
;
179 USHORT SubstituteNameOffset
;
180 USHORT SubstituteNameLength
;
181 USHORT PrintNameOffset
;
182 USHORT PrintNameLength
;
184 } MountPointReparseBuffer
;
188 } GenericReparseBuffer
;
190 } REPARSE_DATA_BUFFER
, *PREPARSE_DATA_BUFFER
;
192 #define SYMLINK_FLAG_RELATIVE 1
195 #ifndef FILE_SUPPORTS_BLOCK_REFCOUNTING
197 typedef struct _DUPLICATE_EXTENTS_DATA
{
199 LARGE_INTEGER SourceFileOffset
;
200 LARGE_INTEGER TargetFileOffset
;
201 LARGE_INTEGER ByteCount
;
202 } DUPLICATE_EXTENTS_DATA
, *PDUPLICATE_EXTENTS_DATA
;
204 #define FSCTL_DUPLICATE_EXTENTS_TO_FILE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 209, METHOD_BUFFERED, FILE_WRITE_ACCESS)
206 typedef struct _FSCTL_GET_INTEGRITY_INFORMATION_BUFFER
{
207 WORD ChecksumAlgorithm
;
210 DWORD ChecksumChunkSizeInBytes
;
211 DWORD ClusterSizeInBytes
;
212 } FSCTL_GET_INTEGRITY_INFORMATION_BUFFER
, *PFSCTL_GET_INTEGRITY_INFORMATION_BUFFER
;
214 typedef struct _FSCTL_SET_INTEGRITY_INFORMATION_BUFFER
{
215 WORD ChecksumAlgorithm
;
218 } FSCTL_SET_INTEGRITY_INFORMATION_BUFFER
, *PFSCTL_SET_INTEGRITY_INFORMATION_BUFFER
;
220 #define FSCTL_GET_INTEGRITY_INFORMATION CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 159, METHOD_BUFFERED, FILE_ANY_ACCESS)
221 #define FSCTL_SET_INTEGRITY_INFORMATION CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 160, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
229 h
= INVALID_HANDLE_VALUE
;
233 win_handle(HANDLE nh
) {
238 if (h
!= INVALID_HANDLE_VALUE
)
242 operator HANDLE() const {
246 win_handle
& operator=(const HANDLE nh
) {
247 if (h
!= INVALID_HANDLE_VALUE
)
255 HANDLE
* operator&() {
261 HANDLE h
= INVALID_HANDLE_VALUE
;
271 h
= INVALID_HANDLE_VALUE
;
275 fff_handle(HANDLE nh
) {
280 if (h
!= INVALID_HANDLE_VALUE
)
284 operator HANDLE() const {
288 fff_handle
& operator=(const HANDLE nh
) {
289 if (h
!= INVALID_HANDLE_VALUE
)
297 HANDLE
* operator&() {
303 HANDLE h
= INVALID_HANDLE_VALUE
;
313 h
= INVALID_HANDLE_VALUE
;
317 nt_handle(HANDLE nh
) {
322 if (h
!= INVALID_HANDLE_VALUE
)
326 operator HANDLE() const {
330 nt_handle
& operator=(const HANDLE nh
) {
331 if (h
!= INVALID_HANDLE_VALUE
)
339 HANDLE
* operator&() {
345 HANDLE h
= INVALID_HANDLE_VALUE
;
351 class string_error
: public exception
{
353 string_error(int resno
, ...);
355 const char* what() const noexcept
{
364 class last_error
: public exception
{
366 last_error(DWORD errnum
);
368 const char* what() const noexcept
{
376 class ntstatus_error
: public exception
{
378 ntstatus_error(NTSTATUS Status
);
380 const char* what() const noexcept
{
391 inline wstring
to_wstring(uint8_t a
) { WCHAR buffer
[16]; swprintf(buffer
, L
"%d", a
); return wstring(buffer
); }
392 inline wstring
to_wstring(uint16_t a
) { WCHAR buffer
[16]; swprintf(buffer
, L
"%d", a
); return wstring(buffer
); }
393 inline wstring
to_wstring(uint32_t a
) { WCHAR buffer
[32]; swprintf(buffer
, L
"%ld", a
); return wstring(buffer
); }
394 inline wstring
to_wstring(uint64_t a
) { WCHAR buffer
[64]; swprintf(buffer
, L
"%I64d", a
); return wstring(buffer
); }
397 extern HMODULE module
;
398 void format_size(uint64_t size
, wstring
& s
, bool show_bytes
);
399 void set_dpi_aware();
400 wstring
format_message(ULONG last_error
);
401 wstring
format_ntstatus(NTSTATUS Status
);
402 bool load_string(HMODULE module
, UINT id
, wstring
& s
);
403 void wstring_sprintf(wstring
& s
, wstring fmt
, ...);
404 void command_line_to_args(LPWSTR cmdline
, vector
<wstring
>& args
);
405 wstring
utf8_to_utf16(const string_view
& utf8
);
406 void error_message(HWND hwnd
, const char* msg
);