-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathfile.c
309 lines (269 loc) · 8.55 KB
/
file.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* PROJECT: Native Shell
* COPYRIGHT: LGPL; See LICENSE in the top level directory
* FILE: file.c
* DESCRIPTION: This module implements commands for dealing with files and directories.
* DEVELOPERS: See CONTRIBUTORS.md in the top level directory
*/
#include "precomp.h"
/*++
* @name RtlCliGetCurrentDirectory
*
* The RtlCliGetCurrentDirectory routine provides a way to get the current
* directory.
*
* @param CurrentDirectory
* The current directory.
*
* @return ULONG
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
ULONG
RtlCliGetCurrentDirectory(IN OUT PWSTR CurrentDirectory)
{
return RtlGetCurrentDirectory_U(MAX_PATH * sizeof(WCHAR),
CurrentDirectory);
}
/*++
* @name RtlCliSetCurrentDirectory
*
* The RtlCliSetCurrentDirectory routine provides a way to change the current
* directory.
*
* @param Directory
* The directory to change to.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliSetCurrentDirectory(PCHAR Directory)
{
WCHAR buf[MAX_PATH];
UNICODE_STRING us;
if (NULL == Directory)
{
return STATUS_UNSUCCESSFUL;
}
// Full path contains at least two symbols, the second is ':'
if (strnlen(Directory, MAX_PATH) >= 2 && Directory[1] == ':')
{
RtlCreateUnicodeStringFromAsciiz(&us, Directory);
RtlSetCurrentDirectory_U(&us);
RtlFreeUnicodeString(&us);
return STATUS_SUCCESS;
}
GetFullPath(Directory, buf, TRUE);
RtlInitUnicodeString(&us, buf);
RtlSetCurrentDirectory_U(&us);
return STATUS_SUCCESS;
}
VOID RtlCliDumpFileInfo(PFILE_BOTH_DIR_INFORMATION DirInfo)
{
PWCHAR Null;
WCHAR Save;
TIME_FIELDS Time;
CHAR SizeString[16];
WCHAR ShortString[12 + 1];
WCHAR FileString[MAX_PATH + 1];
WCHAR FileStringSize[100];
WCHAR ShortStringSize[100];
UINT file_size = 0;
UINT short_size = 0;
// The filename isn't null-terminated, and the next structure follows
// right after it. So, we save the next char (which ends up being the
// NextEntryOffset of the next structure), then temporarly clear it so
// that the RtlCliDisplayString can treat it as a null-terminated string
Null = (PWCHAR)((PBYTE)DirInfo->FileName + DirInfo->FileNameLength);
Save = *Null;
*Null = 0;
// Get the last access time
RtlSystemTimeToLocalTime(&DirInfo->CreationTime, &DirInfo->CreationTime);
RtlTimeToTimeFields(&DirInfo->CreationTime, &Time);
// Don't display sizes for directories
if (!(DirInfo->FileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
sprintf(SizeString, "%d", DirInfo->AllocationSize.LowPart);
}
else
{
sprintf(SizeString, " ", DirInfo->AllocationSize.LowPart);
}
// Display this entry
file_size = DirInfo->FileNameLength / sizeof(WCHAR);
short_size = DirInfo->ShortNameLength / sizeof(WCHAR);
swprintf(ShortStringSize, L"%d", short_size);
swprintf(FileStringSize, L"%d", file_size);
if (DirInfo->ShortNameLength)
{
memset(ShortString, 0x00, (12 + 1) * sizeof(WCHAR));
wcsncpy(ShortString, DirInfo->ShortName, short_size);
}
else
{
swprintf(ShortString, L" ");
}
if (DirInfo->FileNameLength)
{
memset(FileString, 0x00, (MAX_PATH + 1) * sizeof(WCHAR));
wcsncpy(FileString, DirInfo->FileName, file_size);
}
else
{
swprintf(FileString, L" ");
}
RtlCliDisplayString("%02d.%02d.%04d %02d:%02d %s %9s %-28S %12S\n",
Time.Day,
Time.Month,
Time.Year,
Time.Hour,
Time.Minute,
DirInfo->FileAttributes & FILE_ATTRIBUTE_DIRECTORY ? "<DIR>" : " ",
SizeString,
FileString,
ShortString);
// Restore the character that was here before
*Null = Save;
}
/*++
* @name RtlCliListDirectory
*
* The RtlCliListDirectory routine lists the current directory contents.
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliListDirectory(PWCHAR CurrentDirectory)
{
UNICODE_STRING DirectoryString;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE DirectoryHandle;
NTSTATUS Status;
IO_STATUS_BLOCK IoStatusBlock;
BOOLEAN FirstQuery = TRUE;
PFILE_BOTH_DIR_INFORMATION DirectoryInfo, Entry;
HANDLE EventHandle;
CHAR i, c;
// Convert dir to NT Format
if (!RtlDosPathNameToNtPathName_U(CurrentDirectory,
&DirectoryString,
NULL,
NULL))
{
return STATUS_UNSUCCESSFUL;
}
// Initialize the object attributes
RtlCliDisplayString(" Directory of %S\n\n", CurrentDirectory);
InitializeObjectAttributes(&ObjectAttributes,
&DirectoryString,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
// Open the directory
Status = ZwCreateFile(&DirectoryHandle,
FILE_LIST_DIRECTORY,
&ObjectAttributes,
&IoStatusBlock,
NULL,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_OPEN,
FILE_DIRECTORY_FILE,
NULL,
0);
if (!NT_SUCCESS(Status))
{
return Status;
}
// Allocate space for directory entry information
DirectoryInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, 4096);
if (!DirectoryInfo)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
// Create the event to wait on
InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
Status = NtCreateEvent(&EventHandle,
EVENT_ALL_ACCESS,
&ObjectAttributes,
SynchronizationEvent,
FALSE);
if (!NT_SUCCESS(Status))
{
return Status;
}
// Start loop
i = 0;
for (;;)
{
// Get the contents of the directory, adding up the size as we go
Status = ZwQueryDirectoryFile(DirectoryHandle,
EventHandle,
NULL,
0,
&IoStatusBlock,
DirectoryInfo,
4096,
FileBothDirectoryInformation,
FALSE,
NULL,
FirstQuery);
if (Status == STATUS_PENDING)
{
// Wait on the event
NtWaitForSingleObject(EventHandle, FALSE, NULL);
Status = IoStatusBlock.Status;
}
// Check for success
if (!NT_SUCCESS(Status))
{
// Nothing left to enumerate. Close handles and free memory
ZwClose(DirectoryHandle);
RtlFreeHeap(RtlGetProcessHeap(), 0, DirectoryInfo);
return STATUS_SUCCESS;
}
// Loop every directory
Entry = DirectoryInfo;
while (Entry)
{
// List the file
RtlCliDumpFileInfo(Entry);
if (++i > 20)
{
i = 0;
RtlCliDisplayString("Continue listing (y/n):");
while (TRUE)
{
c = RtlCliGetChar(hKeyboard);
if (c == 'n' || c == 'N')
{
RtlCliDisplayString("\n");
return STATUS_SUCCESS;
}
if (c == 'y' || c == 'Y')
{
break;
}
}
RtlCliDisplayString("\n");
}
// Make sure we still have a file
if (!Entry->NextEntryOffset)
break;
// Move to the next one
Entry = (PFILE_BOTH_DIR_INFORMATION)((ULONG_PTR)Entry +
Entry->NextEntryOffset);
}
// This isn't the first scan anymore
FirstQuery = FALSE;
}
}