af7776cb5a35416e18712631e19b41aeeae87f41
[reactos.git] / dll / shellext / shellbtrfs / shellext.h
1 /* Copyright (c) Mark Harmstone 2016-17
2 *
3 * This file is part of WinBtrfs.
4 *
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.
9 *
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.
14 *
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/>. */
17
18 #pragma once
19
20 /* C++ backwards-compatibility */
21 #ifdef __REACTOS__
22 #if defined(_MSC_VER) && (_MSC_VER < 1900)
23 #define noexcept
24 #endif
25 #endif
26
27 #define ISOLATION_AWARE_ENABLED 1
28 #define STRSAFE_NO_DEPRECATE
29
30 #ifndef __REACTOS__
31 #define WINVER 0x0A00 // Windows 10
32 #define _WIN32_WINNT 0x0A00
33 #endif
34
35 #ifndef __REACTOS__
36 #include <windows.h>
37 #include <winternl.h>
38 #else
39 #define WIN32_NO_STATUS
40 #include <windef.h>
41 #include <winbase.h>
42 #include <strsafe.h>
43 #include <ndk/iofuncs.h>
44 #include <ndk/obfuncs.h>
45 #endif
46 #include <string>
47 #ifdef __REACTOS__
48 #define string_view string
49 #define wstring_view wstring
50 #endif
51 #include <vector>
52 #include <stdint.h>
53 #ifndef __REACTOS__
54 #include "../btrfs.h"
55 #include "../btrfsioctl.h"
56 #else
57 #include "btrfs.h"
58 #include "btrfsioctl.h"
59 #endif
60
61 using namespace std;
62
63 #ifndef __REACTOS__
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
72 #endif
73
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
83
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
91
92 #ifdef _MSC_VER
93 #define funcname __FUNCTION__
94 #else
95 #define funcname __func__
96 #endif
97
98 #ifdef _MSC_VER
99 #pragma warning(disable: 4800)
100 #endif
101
102 #ifdef __cplusplus
103 extern "C" {
104 #endif
105 #ifndef __REACTOS__
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);
109
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);
112
113 NTSTATUS WINAPI NtSetEaFile(HANDLE FileHandle, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length);
114
115 NTSTATUS WINAPI NtSetSecurityObject(HANDLE Handle, SECURITY_INFORMATION SecurityInformation, PSECURITY_DESCRIPTOR SecurityDescriptor);
116
117 NTSTATUS NTAPI NtQueryInformationFile(HANDLE hFile, PIO_STATUS_BLOCK io, PVOID ptr, ULONG len, FILE_INFORMATION_CLASS FileInformationClass);
118
119 NTSTATUS NTAPI NtSetInformationFile(HANDLE hFile, PIO_STATUS_BLOCK io, PVOID ptr, ULONG len, FILE_INFORMATION_CLASS FileInformationClass);
120
121 #ifdef _MSC_VER
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
127
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;
145
146 typedef struct _FILE_STREAM_INFORMATION {
147 ULONG NextEntryOffset;
148 ULONG StreamNameLength;
149 LARGE_INTEGER StreamSize;
150 LARGE_INTEGER StreamAllocationSize;
151 WCHAR StreamName[1];
152 } FILE_STREAM_INFORMATION, *PFILE_STREAM_INFORMATION;
153 #endif
154
155 NTSTATUS NTAPI NtQueryVolumeInformationFile(HANDLE FileHandle, PIO_STATUS_BLOCK IoStatusBlock, PVOID FsInformation, ULONG Length,
156 FS_INFORMATION_CLASS FsInformationClass);
157 #endif
158 #ifdef __cplusplus
159 }
160 #endif
161
162 #ifndef __REACTOS__
163 typedef struct _REPARSE_DATA_BUFFER {
164 ULONG ReparseTag;
165 USHORT ReparseDataLength;
166 USHORT Reserved;
167
168 union {
169 struct {
170 USHORT SubstituteNameOffset;
171 USHORT SubstituteNameLength;
172 USHORT PrintNameOffset;
173 USHORT PrintNameLength;
174 ULONG Flags;
175 WCHAR PathBuffer[1];
176 } SymbolicLinkReparseBuffer;
177
178 struct {
179 USHORT SubstituteNameOffset;
180 USHORT SubstituteNameLength;
181 USHORT PrintNameOffset;
182 USHORT PrintNameLength;
183 WCHAR PathBuffer[1];
184 } MountPointReparseBuffer;
185
186 struct {
187 UCHAR DataBuffer[1];
188 } GenericReparseBuffer;
189 };
190 } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
191
192 #define SYMLINK_FLAG_RELATIVE 1
193 #endif
194
195 #ifndef FILE_SUPPORTS_BLOCK_REFCOUNTING
196
197 typedef struct _DUPLICATE_EXTENTS_DATA {
198 HANDLE FileHandle;
199 LARGE_INTEGER SourceFileOffset;
200 LARGE_INTEGER TargetFileOffset;
201 LARGE_INTEGER ByteCount;
202 } DUPLICATE_EXTENTS_DATA, *PDUPLICATE_EXTENTS_DATA;
203
204 #define FSCTL_DUPLICATE_EXTENTS_TO_FILE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 209, METHOD_BUFFERED, FILE_WRITE_ACCESS)
205
206 typedef struct _FSCTL_GET_INTEGRITY_INFORMATION_BUFFER {
207 WORD ChecksumAlgorithm;
208 WORD Reserved;
209 DWORD Flags;
210 DWORD ChecksumChunkSizeInBytes;
211 DWORD ClusterSizeInBytes;
212 } FSCTL_GET_INTEGRITY_INFORMATION_BUFFER, *PFSCTL_GET_INTEGRITY_INFORMATION_BUFFER;
213
214 typedef struct _FSCTL_SET_INTEGRITY_INFORMATION_BUFFER {
215 WORD ChecksumAlgorithm;
216 WORD Reserved;
217 DWORD Flags;
218 } FSCTL_SET_INTEGRITY_INFORMATION_BUFFER, *PFSCTL_SET_INTEGRITY_INFORMATION_BUFFER;
219
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)
222
223 #endif
224
225 class win_handle {
226 public:
227 win_handle() {
228 #ifdef __REACTOS__
229 h = INVALID_HANDLE_VALUE;
230 #endif
231 }
232
233 win_handle(HANDLE nh) {
234 h = nh;
235 }
236
237 ~win_handle() {
238 if (h != INVALID_HANDLE_VALUE)
239 CloseHandle(h);
240 }
241
242 operator HANDLE() const {
243 return h;
244 }
245
246 win_handle& operator=(const HANDLE nh) {
247 if (h != INVALID_HANDLE_VALUE)
248 CloseHandle(h);
249
250 h = nh;
251
252 return *this;
253 }
254
255 HANDLE* operator&() {
256 return &h;
257 }
258
259 private:
260 #ifndef __REACTOS__
261 HANDLE h = INVALID_HANDLE_VALUE;
262 #else
263 HANDLE h;
264 #endif
265 };
266
267 class fff_handle {
268 public:
269 fff_handle() {
270 #ifdef __REACTOS__
271 h = INVALID_HANDLE_VALUE;
272 #endif
273 }
274
275 fff_handle(HANDLE nh) {
276 h = nh;
277 }
278
279 ~fff_handle() {
280 if (h != INVALID_HANDLE_VALUE)
281 FindClose(h);
282 }
283
284 operator HANDLE() const {
285 return h;
286 }
287
288 fff_handle& operator=(const HANDLE nh) {
289 if (h != INVALID_HANDLE_VALUE)
290 FindClose(h);
291
292 h = nh;
293
294 return *this;
295 }
296
297 HANDLE* operator&() {
298 return &h;
299 }
300
301 private:
302 #ifndef __REACTOS__
303 HANDLE h = INVALID_HANDLE_VALUE;
304 #else
305 HANDLE h;
306 #endif
307 };
308
309 class nt_handle {
310 public:
311 nt_handle() {
312 #ifdef __REACTOS__
313 h = INVALID_HANDLE_VALUE;
314 #endif
315 }
316
317 nt_handle(HANDLE nh) {
318 h = nh;
319 }
320
321 ~nt_handle() {
322 if (h != INVALID_HANDLE_VALUE)
323 NtClose(h);
324 }
325
326 operator HANDLE() const {
327 return h;
328 }
329
330 nt_handle& operator=(const HANDLE nh) {
331 if (h != INVALID_HANDLE_VALUE)
332 NtClose(h);
333
334 h = nh;
335
336 return *this;
337 }
338
339 HANDLE* operator&() {
340 return &h;
341 }
342
343 private:
344 #ifndef __REACTOS__
345 HANDLE h = INVALID_HANDLE_VALUE;
346 #else
347 HANDLE h;
348 #endif
349 };
350
351 class string_error : public exception {
352 public:
353 string_error(int resno, ...);
354
355 const char* what() const noexcept {
356 return msg.c_str();
357 }
358
359 private:
360 string msg;
361 };
362
363
364 class last_error : public exception {
365 public:
366 last_error(DWORD errnum);
367
368 const char* what() const noexcept {
369 return msg.c_str();
370 }
371
372 private:
373 string msg;
374 };
375
376 class ntstatus_error : public exception {
377 public:
378 ntstatus_error(NTSTATUS Status);
379
380 const char* what() const noexcept {
381 return msg.c_str();
382 }
383
384 NTSTATUS Status;
385
386 private:
387 string msg;
388 };
389
390 #ifdef __REACTOS__
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); }
395 #endif
396
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);