3 * Copyright (C) 2002, 2003, 2014 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/dirctl.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
24 * Pierre Schweitzer (pierre@reactos.org)
25 * Hervé Poussineau (hpoussin@reactos.org)
28 /* INCLUDES *****************************************************************/
35 /* FUNCTIONS ****************************************************************/
39 CdfsGetEntryName(PDEVICE_EXTENSION DeviceExt
,
42 PLARGE_INTEGER StreamOffset
,
49 * FUNCTION: Retrieves the file name, be it in short or long file name format
56 ULONG BlockOffset
= 0;
58 Record
= (PDIR_RECORD
)*Block
;
59 while(Index
< *pIndex
)
61 BlockOffset
+= Record
->RecordLength
;
62 Offset
+= Record
->RecordLength
;
64 Record
= (PDIR_RECORD
)(*Block
+ BlockOffset
);
65 if (BlockOffset
>= BLOCKSIZE
|| Record
->RecordLength
== 0)
67 DPRINT("Map next sector\n");
68 CcUnpinData(*Context
);
69 StreamOffset
->QuadPart
+= BLOCKSIZE
;
70 Offset
= ROUND_UP(Offset
, BLOCKSIZE
);
73 if (!CcMapData(DeviceExt
->StreamFileObject
,
78 DPRINT("CcMapData() failed\n");
79 return(STATUS_UNSUCCESSFUL
);
81 Record
= (PDIR_RECORD
)(*Block
+ BlockOffset
);
84 if (Offset
>= DirLength
)
85 return(STATUS_NO_MORE_ENTRIES
);
90 DPRINT("Index %lu RecordLength %lu Offset %lu\n",
91 Index
, Record
->RecordLength
, Offset
);
93 if (Record
->FileIdLength
== 1 && Record
->FileId
[0] == 0)
97 else if (Record
->FileIdLength
== 1 && Record
->FileId
[0] == 1)
103 if (DeviceExt
->CdInfo
.JolietLevel
== 0)
107 for (i
= 0; i
< Record
->FileIdLength
&& Record
->FileId
[i
] != ';'; i
++)
108 Name
[i
] = (WCHAR
)Record
->FileId
[i
];
113 CdfsSwapString(Name
, Record
->FileId
, Record
->FileIdLength
);
117 DPRINT("Name '%S'\n", Name
);
123 return(STATUS_SUCCESS
);
129 NtfsGetNameInformation(PDEVICE_EXTENSION DeviceExt
,
130 PFILE_RECORD_HEADER FileRecord
,
131 PNTFS_ATTR_CONTEXT DataContext
,
132 PFILE_NAMES_INFORMATION Info
,
136 PFILENAME_ATTRIBUTE FileName
;
138 DPRINT("NtfsGetNameInformation() called\n");
140 FileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_WIN32
);
141 ASSERT(FileName
!= NULL
);
143 Length
= FileName
->NameLength
* sizeof (WCHAR
);
144 if ((sizeof(FILE_NAMES_INFORMATION
) + Length
) > BufferLength
)
145 return(STATUS_BUFFER_OVERFLOW
);
147 Info
->FileNameLength
= Length
;
148 Info
->NextEntryOffset
=
149 ROUND_UP(sizeof(FILE_NAMES_INFORMATION
) + Length
, sizeof(ULONG
));
150 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
152 return(STATUS_SUCCESS
);
157 NtfsGetDirectoryInformation(PDEVICE_EXTENSION DeviceExt
,
158 PFILE_RECORD_HEADER FileRecord
,
159 PNTFS_ATTR_CONTEXT DataContext
,
160 PFILE_DIRECTORY_INFORMATION Info
,
164 PFILENAME_ATTRIBUTE FileName
;
166 DPRINT("NtfsGetDirectoryInformation() called\n");
168 FileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_WIN32
);
169 ASSERT(FileName
!= NULL
);
171 Length
= FileName
->NameLength
* sizeof (WCHAR
);
172 if ((sizeof(FILE_DIRECTORY_INFORMATION
) + Length
) > BufferLength
)
173 return(STATUS_BUFFER_OVERFLOW
);
175 Info
->FileNameLength
= Length
;
176 Info
->NextEntryOffset
=
177 ROUND_UP(sizeof(FILE_DIRECTORY_INFORMATION
) + Length
, sizeof(ULONG
));
178 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
180 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
181 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
182 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
183 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
185 /* Convert file flags */
186 NtfsFileFlagsToAttributes(FileName
->FileAttributes
, &Info
->FileAttributes
);
188 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
189 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
193 return STATUS_SUCCESS
;
198 NtfsGetFullDirectoryInformation(PDEVICE_EXTENSION DeviceExt
,
199 PFILE_RECORD_HEADER FileRecord
,
200 PNTFS_ATTR_CONTEXT DataContext
,
201 PFILE_FULL_DIRECTORY_INFORMATION Info
,
205 PFILENAME_ATTRIBUTE FileName
;
207 DPRINT("NtfsGetFullDirectoryInformation() called\n");
209 FileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_WIN32
);
210 ASSERT(FileName
!= NULL
);
212 Length
= FileName
->NameLength
* sizeof (WCHAR
);
213 if ((sizeof(FILE_FULL_DIRECTORY_INFORMATION
) + Length
) > BufferLength
)
214 return(STATUS_BUFFER_OVERFLOW
);
216 Info
->FileNameLength
= Length
;
217 Info
->NextEntryOffset
=
218 ROUND_UP(sizeof(FILE_FULL_DIRECTORY_INFORMATION
) + Length
, sizeof(ULONG
));
219 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
221 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
222 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
223 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
224 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
226 /* Convert file flags */
227 NtfsFileFlagsToAttributes(FileName
->FileAttributes
, &Info
->FileAttributes
);
229 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
230 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
235 return STATUS_SUCCESS
;
240 NtfsGetBothDirectoryInformation(PDEVICE_EXTENSION DeviceExt
,
241 PFILE_RECORD_HEADER FileRecord
,
242 PNTFS_ATTR_CONTEXT DataContext
,
243 PFILE_BOTH_DIR_INFORMATION Info
,
247 PFILENAME_ATTRIBUTE FileName
, ShortFileName
;
249 DPRINT("NtfsGetBothDirectoryInformation() called\n");
251 FileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_WIN32
);
252 ASSERT(FileName
!= NULL
);
253 ShortFileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_DOS
);
255 Length
= FileName
->NameLength
* sizeof (WCHAR
);
256 if ((sizeof(FILE_BOTH_DIR_INFORMATION
) + Length
) > BufferLength
)
257 return(STATUS_BUFFER_OVERFLOW
);
259 Info
->FileNameLength
= Length
;
260 Info
->NextEntryOffset
=
261 ROUND_UP(sizeof(FILE_BOTH_DIR_INFORMATION
) + Length
, sizeof(ULONG
));
262 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
266 /* Should we upcase the filename? */
267 ASSERT(ShortFileName
->NameLength
<= ARRAYSIZE(Info
->ShortName
));
268 Info
->ShortNameLength
= ShortFileName
->NameLength
* sizeof(WCHAR
);
269 RtlCopyMemory(Info
->ShortName
, ShortFileName
->Name
, Info
->ShortNameLength
);
273 Info
->ShortName
[0] = 0;
274 Info
->ShortNameLength
= 0;
277 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
278 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
279 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
280 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
282 /* Convert file flags */
283 NtfsFileFlagsToAttributes(FileName
->FileAttributes
, &Info
->FileAttributes
);
285 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
286 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
291 return STATUS_SUCCESS
;
296 NtfsQueryDirectory(PNTFS_IRP_CONTEXT IrpContext
)
299 PDEVICE_OBJECT DeviceObject
;
300 PDEVICE_EXTENSION DeviceExtension
;
301 LONG BufferLength
= 0;
302 PUNICODE_STRING SearchPattern
= NULL
;
303 FILE_INFORMATION_CLASS FileInformationClass
;
305 PUCHAR Buffer
= NULL
;
306 PFILE_NAMES_INFORMATION Buffer0
= NULL
;
309 BOOLEAN First
= FALSE
;
310 PIO_STACK_LOCATION Stack
;
311 PFILE_OBJECT FileObject
;
312 NTSTATUS Status
= STATUS_SUCCESS
;
313 PFILE_RECORD_HEADER FileRecord
;
314 PNTFS_ATTR_CONTEXT DataContext
;
315 ULONGLONG MFTRecord
, OldMFTRecord
= 0;
316 UNICODE_STRING Pattern
;
318 DPRINT1("NtfsQueryDirectory() called\n");
321 Irp
= IrpContext
->Irp
;
322 DeviceObject
= IrpContext
->DeviceObject
;
324 DeviceExtension
= DeviceObject
->DeviceExtension
;
325 Stack
= IoGetCurrentIrpStackLocation(Irp
);
326 FileObject
= Stack
->FileObject
;
328 Ccb
= (PNTFS_CCB
)FileObject
->FsContext2
;
329 Fcb
= (PNTFS_FCB
)FileObject
->FsContext
;
331 /* Obtain the callers parameters */
332 BufferLength
= Stack
->Parameters
.QueryDirectory
.Length
;
333 SearchPattern
= Stack
->Parameters
.QueryDirectory
.FileName
;
334 FileInformationClass
= Stack
->Parameters
.QueryDirectory
.FileInformationClass
;
335 FileIndex
= Stack
->Parameters
.QueryDirectory
.FileIndex
;
337 if (SearchPattern
!= NULL
)
339 if (!Ccb
->DirectorySearchPattern
)
343 Pattern
.MaximumLength
= SearchPattern
->Length
+ sizeof(WCHAR
);
344 Ccb
->DirectorySearchPattern
= Pattern
.Buffer
=
345 ExAllocatePoolWithTag(NonPagedPool
, Pattern
.MaximumLength
, TAG_NTFS
);
346 if (!Ccb
->DirectorySearchPattern
)
348 return STATUS_INSUFFICIENT_RESOURCES
;
351 Status
= RtlUpcaseUnicodeString(&Pattern
, SearchPattern
, FALSE
);
352 if (!NT_SUCCESS(Status
))
354 DPRINT1("RtlUpcaseUnicodeString('%wZ') failed with status 0x%08lx\n", &Pattern
, Status
);
355 ExFreePoolWithTag(Ccb
->DirectorySearchPattern
, TAG_NTFS
);
356 Ccb
->DirectorySearchPattern
= NULL
;
359 Ccb
->DirectorySearchPattern
[SearchPattern
->Length
/ sizeof(WCHAR
)] = 0;
362 else if (!Ccb
->DirectorySearchPattern
)
365 Ccb
->DirectorySearchPattern
= ExAllocatePoolWithTag(NonPagedPool
, 2 * sizeof(WCHAR
), TAG_NTFS
);
366 if (!Ccb
->DirectorySearchPattern
)
368 return STATUS_INSUFFICIENT_RESOURCES
;
371 Ccb
->DirectorySearchPattern
[0] = L
'*';
372 Ccb
->DirectorySearchPattern
[1] = 0;
375 RtlInitUnicodeString(&Pattern
, Ccb
->DirectorySearchPattern
);
376 DPRINT("Search pattern '%S'\n", Ccb
->DirectorySearchPattern
);
377 DPRINT("In: '%S'\n", Fcb
->PathName
);
379 /* Determine directory index */
380 if (Stack
->Flags
& SL_INDEX_SPECIFIED
)
382 Ccb
->Entry
= Ccb
->CurrentByteOffset
.u
.LowPart
;
384 else if (First
|| (Stack
->Flags
& SL_RESTART_SCAN
))
389 /* Determine Buffer for result */
392 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
396 Buffer
= Irp
->UserBuffer
;
399 DPRINT("Buffer=%p tofind=%S\n", Buffer
, Ccb
->DirectorySearchPattern
);
401 while (Status
== STATUS_SUCCESS
&& BufferLength
> 0)
403 Status
= NtfsFindFileAt(DeviceExtension
,
411 if (NT_SUCCESS(Status
))
413 /* HACK: files with both a short name and a long name are present twice in the index.
414 * Ignore the second entry, if it is immediately following the first one.
416 if (MFTRecord
== OldMFTRecord
)
418 DPRINT("Ignoring duplicate MFT entry 0x%x\n", MFTRecord
);
420 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
423 OldMFTRecord
= MFTRecord
;
425 switch (FileInformationClass
)
427 case FileNameInformation
:
428 Status
= NtfsGetNameInformation(DeviceExtension
,
431 (PFILE_NAMES_INFORMATION
)Buffer
,
435 case FileDirectoryInformation
:
436 Status
= NtfsGetDirectoryInformation(DeviceExtension
,
439 (PFILE_DIRECTORY_INFORMATION
)Buffer
,
443 case FileFullDirectoryInformation
:
444 Status
= NtfsGetFullDirectoryInformation(DeviceExtension
,
447 (PFILE_FULL_DIRECTORY_INFORMATION
)Buffer
,
451 case FileBothDirectoryInformation
:
452 Status
= NtfsGetBothDirectoryInformation(DeviceExtension
,
455 (PFILE_BOTH_DIR_INFORMATION
)Buffer
,
460 Status
= STATUS_INVALID_INFO_CLASS
;
463 if (Status
== STATUS_BUFFER_OVERFLOW
)
467 Buffer0
->NextEntryOffset
= 0;
476 Buffer0
->NextEntryOffset
= 0;
481 Status
= STATUS_NO_SUCH_FILE
;
485 Status
= STATUS_NO_MORE_FILES
;
490 Buffer0
= (PFILE_NAMES_INFORMATION
)Buffer
;
491 Buffer0
->FileIndex
= FileIndex
++;
494 if (Stack
->Flags
& SL_RETURN_SINGLE_ENTRY
)
498 BufferLength
-= Buffer0
->NextEntryOffset
;
499 Buffer
+= Buffer0
->NextEntryOffset
;
500 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
505 Buffer0
->NextEntryOffset
= 0;
510 Status
= STATUS_SUCCESS
;
519 NtfsFsdDirectoryControl(PDEVICE_OBJECT DeviceObject
,
522 PNTFS_IRP_CONTEXT IrpContext
= NULL
;
523 NTSTATUS Status
= STATUS_UNSUCCESSFUL
;
525 DPRINT1("NtfsDirectoryControl() called\n");
527 FsRtlEnterFileSystem();
528 ASSERT(DeviceObject
);
531 NtfsIsIrpTopLevel(Irp
);
533 IrpContext
= NtfsAllocateIrpContext(DeviceObject
, Irp
);
536 switch (IrpContext
->MinorFunction
)
538 case IRP_MN_QUERY_DIRECTORY
:
539 Status
= NtfsQueryDirectory(IrpContext
);
542 case IRP_MN_NOTIFY_CHANGE_DIRECTORY
:
543 DPRINT1("IRP_MN_NOTIFY_CHANGE_DIRECTORY\n");
544 Status
= STATUS_NOT_IMPLEMENTED
;
548 Status
= STATUS_INVALID_DEVICE_REQUEST
;
553 Status
= STATUS_INSUFFICIENT_RESOURCES
;
555 Irp
->IoStatus
.Status
= Status
;
556 Irp
->IoStatus
.Information
= 0;
557 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
560 ExFreePoolWithTag(IrpContext
, 'PRIN');
562 IoSetTopLevelIrp(NULL
);
563 FsRtlExitFileSystem();