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
= GetBestFileNameFromRecord(FileRecord
);
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
,
161 PFILE_DIRECTORY_INFORMATION Info
,
165 PFILENAME_ATTRIBUTE FileName
;
166 PSTANDARD_INFORMATION StdInfo
;
168 DPRINT("NtfsGetDirectoryInformation() called\n");
170 FileName
= GetBestFileNameFromRecord(FileRecord
);
171 ASSERT(FileName
!= NULL
);
173 StdInfo
= GetStandardInformationFromRecord(FileRecord
);
174 ASSERT(StdInfo
!= NULL
);
176 Length
= FileName
->NameLength
* sizeof (WCHAR
);
177 if ((sizeof(FILE_DIRECTORY_INFORMATION
) + Length
) > BufferLength
)
178 return(STATUS_BUFFER_OVERFLOW
);
180 Info
->FileNameLength
= Length
;
181 Info
->NextEntryOffset
=
182 ROUND_UP(sizeof(FILE_DIRECTORY_INFORMATION
) + Length
, sizeof(ULONG
));
183 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
185 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
186 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
187 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
188 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
190 /* Convert file flags */
191 NtfsFileFlagsToAttributes(FileName
->FileAttributes
| StdInfo
->FileAttribute
, &Info
->FileAttributes
);
193 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
194 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
196 Info
->FileIndex
= MFTIndex
;
198 return STATUS_SUCCESS
;
203 NtfsGetFullDirectoryInformation(PDEVICE_EXTENSION DeviceExt
,
204 PFILE_RECORD_HEADER FileRecord
,
205 PNTFS_ATTR_CONTEXT DataContext
,
207 PFILE_FULL_DIRECTORY_INFORMATION Info
,
211 PFILENAME_ATTRIBUTE FileName
;
212 PSTANDARD_INFORMATION StdInfo
;
214 DPRINT("NtfsGetFullDirectoryInformation() called\n");
216 FileName
= GetBestFileNameFromRecord(FileRecord
);
217 ASSERT(FileName
!= NULL
);
219 StdInfo
= GetStandardInformationFromRecord(FileRecord
);
220 ASSERT(StdInfo
!= NULL
);
222 Length
= FileName
->NameLength
* sizeof (WCHAR
);
223 if ((sizeof(FILE_FULL_DIRECTORY_INFORMATION
) + Length
) > BufferLength
)
224 return(STATUS_BUFFER_OVERFLOW
);
226 Info
->FileNameLength
= Length
;
227 Info
->NextEntryOffset
=
228 ROUND_UP(sizeof(FILE_FULL_DIRECTORY_INFORMATION
) + Length
, sizeof(ULONG
));
229 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
231 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
232 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
233 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
234 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
236 /* Convert file flags */
237 NtfsFileFlagsToAttributes(FileName
->FileAttributes
| StdInfo
->FileAttribute
, &Info
->FileAttributes
);
239 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
240 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
242 Info
->FileIndex
= MFTIndex
;
245 return STATUS_SUCCESS
;
250 NtfsGetBothDirectoryInformation(PDEVICE_EXTENSION DeviceExt
,
251 PFILE_RECORD_HEADER FileRecord
,
252 PNTFS_ATTR_CONTEXT DataContext
,
254 PFILE_BOTH_DIR_INFORMATION Info
,
258 PFILENAME_ATTRIBUTE FileName
, ShortFileName
;
259 PSTANDARD_INFORMATION StdInfo
;
261 DPRINT("NtfsGetBothDirectoryInformation() called\n");
263 FileName
= GetBestFileNameFromRecord(FileRecord
);
264 ASSERT(FileName
!= NULL
);
265 ShortFileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_DOS
);
267 StdInfo
= GetStandardInformationFromRecord(FileRecord
);
268 ASSERT(StdInfo
!= NULL
);
270 Length
= FileName
->NameLength
* sizeof (WCHAR
);
271 if ((sizeof(FILE_BOTH_DIR_INFORMATION
) + Length
) > BufferLength
)
272 return(STATUS_BUFFER_OVERFLOW
);
274 Info
->FileNameLength
= Length
;
275 Info
->NextEntryOffset
=
276 ROUND_UP(sizeof(FILE_BOTH_DIR_INFORMATION
) + Length
, sizeof(ULONG
));
277 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
281 /* Should we upcase the filename? */
282 ASSERT(ShortFileName
->NameLength
<= ARRAYSIZE(Info
->ShortName
));
283 Info
->ShortNameLength
= ShortFileName
->NameLength
* sizeof(WCHAR
);
284 RtlCopyMemory(Info
->ShortName
, ShortFileName
->Name
, Info
->ShortNameLength
);
288 Info
->ShortName
[0] = 0;
289 Info
->ShortNameLength
= 0;
292 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
293 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
294 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
295 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
297 /* Convert file flags */
298 NtfsFileFlagsToAttributes(FileName
->FileAttributes
| StdInfo
->FileAttribute
, &Info
->FileAttributes
);
300 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
301 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
303 Info
->FileIndex
= MFTIndex
;
306 return STATUS_SUCCESS
;
311 NtfsQueryDirectory(PNTFS_IRP_CONTEXT IrpContext
)
314 PDEVICE_OBJECT DeviceObject
;
315 PDEVICE_EXTENSION DeviceExtension
;
316 LONG BufferLength
= 0;
317 PUNICODE_STRING SearchPattern
= NULL
;
318 FILE_INFORMATION_CLASS FileInformationClass
;
320 PUCHAR Buffer
= NULL
;
321 PFILE_NAMES_INFORMATION Buffer0
= NULL
;
324 BOOLEAN First
= FALSE
;
325 PIO_STACK_LOCATION Stack
;
326 PFILE_OBJECT FileObject
;
327 NTSTATUS Status
= STATUS_SUCCESS
;
328 PFILE_RECORD_HEADER FileRecord
;
329 PNTFS_ATTR_CONTEXT DataContext
;
330 ULONGLONG MFTRecord
, OldMFTRecord
= 0;
331 UNICODE_STRING Pattern
;
333 DPRINT1("NtfsQueryDirectory() called\n");
336 Irp
= IrpContext
->Irp
;
337 DeviceObject
= IrpContext
->DeviceObject
;
339 DeviceExtension
= DeviceObject
->DeviceExtension
;
340 Stack
= IoGetCurrentIrpStackLocation(Irp
);
341 FileObject
= Stack
->FileObject
;
343 Ccb
= (PNTFS_CCB
)FileObject
->FsContext2
;
344 Fcb
= (PNTFS_FCB
)FileObject
->FsContext
;
346 /* Obtain the callers parameters */
347 BufferLength
= Stack
->Parameters
.QueryDirectory
.Length
;
348 SearchPattern
= Stack
->Parameters
.QueryDirectory
.FileName
;
349 FileInformationClass
= Stack
->Parameters
.QueryDirectory
.FileInformationClass
;
350 FileIndex
= Stack
->Parameters
.QueryDirectory
.FileIndex
;
352 if (SearchPattern
!= NULL
)
354 if (!Ccb
->DirectorySearchPattern
)
358 Pattern
.MaximumLength
= SearchPattern
->Length
+ sizeof(WCHAR
);
359 Ccb
->DirectorySearchPattern
= Pattern
.Buffer
=
360 ExAllocatePoolWithTag(NonPagedPool
, Pattern
.MaximumLength
, TAG_NTFS
);
361 if (!Ccb
->DirectorySearchPattern
)
363 return STATUS_INSUFFICIENT_RESOURCES
;
366 memcpy(Ccb
->DirectorySearchPattern
, SearchPattern
->Buffer
, SearchPattern
->Length
);
367 Ccb
->DirectorySearchPattern
[SearchPattern
->Length
/ sizeof(WCHAR
)] = 0;
370 else if (!Ccb
->DirectorySearchPattern
)
373 Ccb
->DirectorySearchPattern
= ExAllocatePoolWithTag(NonPagedPool
, 2 * sizeof(WCHAR
), TAG_NTFS
);
374 if (!Ccb
->DirectorySearchPattern
)
376 return STATUS_INSUFFICIENT_RESOURCES
;
379 Ccb
->DirectorySearchPattern
[0] = L
'*';
380 Ccb
->DirectorySearchPattern
[1] = 0;
383 RtlInitUnicodeString(&Pattern
, Ccb
->DirectorySearchPattern
);
384 DPRINT("Search pattern '%S'\n", Ccb
->DirectorySearchPattern
);
385 DPRINT("In: '%S'\n", Fcb
->PathName
);
387 /* Determine directory index */
388 if (Stack
->Flags
& SL_INDEX_SPECIFIED
)
390 Ccb
->Entry
= Ccb
->CurrentByteOffset
.u
.LowPart
;
392 else if (First
|| (Stack
->Flags
& SL_RESTART_SCAN
))
397 /* Determine Buffer for result */
400 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
404 Buffer
= Irp
->UserBuffer
;
407 DPRINT("Buffer=%p tofind=%S\n", Buffer
, Ccb
->DirectorySearchPattern
);
409 while (Status
== STATUS_SUCCESS
&& BufferLength
> 0)
411 Status
= NtfsFindFileAt(DeviceExtension
,
419 if (NT_SUCCESS(Status
))
421 /* HACK: files with both a short name and a long name are present twice in the index.
422 * Ignore the second entry, if it is immediately following the first one.
424 if (MFTRecord
== OldMFTRecord
)
426 DPRINT("Ignoring duplicate MFT entry 0x%x\n", MFTRecord
);
428 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
431 OldMFTRecord
= MFTRecord
;
433 switch (FileInformationClass
)
435 case FileNameInformation
:
436 Status
= NtfsGetNameInformation(DeviceExtension
,
439 (PFILE_NAMES_INFORMATION
)Buffer
,
443 case FileDirectoryInformation
:
444 Status
= NtfsGetDirectoryInformation(DeviceExtension
,
448 (PFILE_DIRECTORY_INFORMATION
)Buffer
,
452 case FileFullDirectoryInformation
:
453 Status
= NtfsGetFullDirectoryInformation(DeviceExtension
,
457 (PFILE_FULL_DIRECTORY_INFORMATION
)Buffer
,
461 case FileBothDirectoryInformation
:
462 Status
= NtfsGetBothDirectoryInformation(DeviceExtension
,
466 (PFILE_BOTH_DIR_INFORMATION
)Buffer
,
471 Status
= STATUS_INVALID_INFO_CLASS
;
474 if (Status
== STATUS_BUFFER_OVERFLOW
)
478 Buffer0
->NextEntryOffset
= 0;
487 Buffer0
->NextEntryOffset
= 0;
492 Status
= STATUS_NO_SUCH_FILE
;
496 Status
= STATUS_NO_MORE_FILES
;
501 Buffer0
= (PFILE_NAMES_INFORMATION
)Buffer
;
502 Buffer0
->FileIndex
= FileIndex
++;
505 if (Stack
->Flags
& SL_RETURN_SINGLE_ENTRY
)
509 BufferLength
-= Buffer0
->NextEntryOffset
;
510 Buffer
+= Buffer0
->NextEntryOffset
;
511 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
516 Buffer0
->NextEntryOffset
= 0;
521 Status
= STATUS_SUCCESS
;
530 NtfsFsdDirectoryControl(PDEVICE_OBJECT DeviceObject
,
533 PNTFS_IRP_CONTEXT IrpContext
= NULL
;
534 NTSTATUS Status
= STATUS_UNSUCCESSFUL
;
536 DPRINT1("NtfsDirectoryControl() called\n");
538 FsRtlEnterFileSystem();
539 ASSERT(DeviceObject
);
542 NtfsIsIrpTopLevel(Irp
);
544 IrpContext
= NtfsAllocateIrpContext(DeviceObject
, Irp
);
547 switch (IrpContext
->MinorFunction
)
549 case IRP_MN_QUERY_DIRECTORY
:
550 Status
= NtfsQueryDirectory(IrpContext
);
553 case IRP_MN_NOTIFY_CHANGE_DIRECTORY
:
554 DPRINT1("IRP_MN_NOTIFY_CHANGE_DIRECTORY\n");
555 Status
= STATUS_NOT_IMPLEMENTED
;
559 Status
= STATUS_INVALID_DEVICE_REQUEST
;
564 Status
= STATUS_INSUFFICIENT_RESOURCES
;
566 Irp
->IoStatus
.Status
= Status
;
567 Irp
->IoStatus
.Information
= 0;
568 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
571 ExFreePoolWithTag(IrpContext
, 'PRIN');
573 IoSetTopLevelIrp(NULL
);
574 FsRtlExitFileSystem();