-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathntfileio.cpp
More file actions
201 lines (177 loc) · 4.49 KB
/
Copy pathntfileio.cpp
File metadata and controls
201 lines (177 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#ifndef _DLL
#define _DLL
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef UNICODE
#define UNICODE
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <ntdll.h>
#include <intsafe.h>
#include <winstrct.h>
#include <ntfileio.hpp>
#ifndef USHORT_MAX
#define USHORT_MAX INTSAFE_USHORT_MAX
#endif
HANDLE
NativeCreateDirectory(IN HANDLE RootDirectory,
IN PUNICODE_STRING Name,
IN ACCESS_MASK DesiredAccess,
IN ULONG ObjectOpenAttributes,
OUT PULONG_PTR CreationResult OPTIONAL,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG CreateOptions,
IN BOOL CreateParentDirectories)
{
// If "File" is current directory marker ".", we need to pass just
// root directory and an empty name to directory create routines
UNICODE_STRING name = { 0 };
if ((Name->Length != 2) ? true : (Name->Buffer[0] != L'.'))
name = *Name;
OBJECT_ATTRIBUTES object_attributes;
InitializeObjectAttributes(&object_attributes,
&name,
ObjectOpenAttributes,
RootDirectory,
NULL);
IO_STATUS_BLOCK io_status;
HANDLE handle;
for (;;)
{
NTSTATUS status =
NtCreateFile(&handle,
DesiredAccess | SYNCHRONIZE,
&object_attributes,
&io_status,
NULL,
FileAttributes,
ShareAccess,
FILE_OPEN_IF,
CreateOptions |
FILE_DIRECTORY_FILE |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (NT_SUCCESS(status))
{
if (CreationResult != NULL)
*CreationResult = io_status.Information;
return handle;
}
if (CreateParentDirectories &&
(status == STATUS_OBJECT_PATH_NOT_FOUND))
{
UNICODE_STRING parent_dir;
SplitPath(Name, &parent_dir, NULL);
if (parent_dir.Length > 0)
{
parent_dir.Length -= 2;
ULONG_PTR ParentCreationResult;
HANDLE parent_dir_handle =
NativeCreateDirectory(RootDirectory,
&parent_dir,
FILE_LIST_DIRECTORY | FILE_TRAVERSE,
ObjectOpenAttributes,
&ParentCreationResult,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ | FILE_SHARE_WRITE |
FILE_SHARE_DELETE,
0,
TRUE);
if (parent_dir_handle != INVALID_HANDLE_VALUE)
{
NtClose(parent_dir_handle);
if (ParentCreationResult == FILE_CREATED)
continue;
}
}
}
SetLastError(RtlNtStatusToDosError(status));
return INVALID_HANDLE_VALUE;
}
}
HANDLE
NativeCreateFile(IN HANDLE RootDirectory,
IN PUNICODE_STRING Name,
IN ACCESS_MASK DesiredAccess,
IN ULONG ObjectOpenAttributes,
OUT PULONG_PTR CreationResult OPTIONAL,
IN PLARGE_INTEGER AllocationSize OPTIONAL,
IN ULONG FileAttributes,
IN ULONG ShareAccess,
IN ULONG CreateDisposition,
IN ULONG CreateOptions,
IN BOOL CreateParentDirectories)
{
OBJECT_ATTRIBUTES object_attributes;
InitializeObjectAttributes(&object_attributes,
Name,
ObjectOpenAttributes,
RootDirectory,
NULL);
IO_STATUS_BLOCK io_status;
HANDLE handle;
for (;;)
{
NTSTATUS status =
NtCreateFile(&handle,
DesiredAccess | SYNCHRONIZE,
&object_attributes,
&io_status,
AllocationSize,
FileAttributes,
ShareAccess,
CreateDisposition,
CreateOptions |
FILE_NON_DIRECTORY_FILE |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0);
if (NT_SUCCESS(status))
{
if (CreationResult != NULL)
*CreationResult = io_status.Information;
return handle;
}
if (CreateParentDirectories &&
(status == STATUS_OBJECT_PATH_NOT_FOUND))
{
UNICODE_STRING parent_dir = *Name;
for (;
parent_dir.Length > 0;
parent_dir.Length -= 2)
if (parent_dir.Buffer[(parent_dir.Length >> 1) - 1] == L'\\')
break;
if (parent_dir.Length > 0)
{
parent_dir.Length -= 2;
ULONG_PTR ParentCreationResult;
HANDLE parent_dir_handle =
NativeCreateDirectory(RootDirectory,
&parent_dir,
FILE_LIST_DIRECTORY | FILE_TRAVERSE,
ObjectOpenAttributes,
&ParentCreationResult,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ | FILE_SHARE_WRITE |
FILE_SHARE_DELETE,
0,
TRUE);
if (parent_dir_handle != INVALID_HANDLE_VALUE)
{
NtClose(parent_dir_handle);
if (ParentCreationResult == FILE_CREATED)
continue;
}
}
}
SetLastError(RtlNtStatusToDosError(status));
return INVALID_HANDLE_VALUE;
}
}