3 * Copyright (C) 2002,2003 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 * PROGRAMMER: Eric Kohl
26 /* INCLUDES *****************************************************************/
33 /* FUNCTIONS ****************************************************************/
37 CdfsGetEntryName(PDEVICE_EXTENSION DeviceExt
,
40 PLARGE_INTEGER StreamOffset
,
47 * FUNCTION: Retrieves the file name, be it in short or long file name format
54 ULONG BlockOffset
= 0;
56 Record
= (PDIR_RECORD
)*Block
;
57 while(Index
< *pIndex
)
59 BlockOffset
+= Record
->RecordLength
;
60 Offset
+= Record
->RecordLength
;
62 Record
= (PDIR_RECORD
)(*Block
+ BlockOffset
);
63 if (BlockOffset
>= BLOCKSIZE
|| Record
->RecordLength
== 0)
65 DPRINT("Map next sector\n");
66 CcUnpinData(*Context
);
67 StreamOffset
->QuadPart
+= BLOCKSIZE
;
68 Offset
= ROUND_UP(Offset
, BLOCKSIZE
);
71 if (!CcMapData(DeviceExt
->StreamFileObject
,
76 DPRINT("CcMapData() failed\n");
77 return(STATUS_UNSUCCESSFUL
);
79 Record
= (PDIR_RECORD
)(*Block
+ BlockOffset
);
82 if (Offset
>= DirLength
)
83 return(STATUS_NO_MORE_ENTRIES
);
88 DPRINT("Index %lu RecordLength %lu Offset %lu\n",
89 Index
, Record
->RecordLength
, Offset
);
91 if (Record
->FileIdLength
== 1 && Record
->FileId
[0] == 0)
95 else if (Record
->FileIdLength
== 1 && Record
->FileId
[0] == 1)
101 if (DeviceExt
->CdInfo
.JolietLevel
== 0)
105 for (i
= 0; i
< Record
->FileIdLength
&& Record
->FileId
[i
] != ';'; i
++)
106 Name
[i
] = (WCHAR
)Record
->FileId
[i
];
111 CdfsSwapString(Name
, Record
->FileId
, Record
->FileIdLength
);
115 DPRINT("Name '%S'\n", Name
);
121 return(STATUS_SUCCESS
);
127 NtfsGetNameInformation(PDEVICE_EXTENSION DeviceExt
,
128 PFILE_RECORD_HEADER FileRecord
,
129 PNTFS_ATTR_CONTEXT DataContext
,
130 PFILE_NAMES_INFORMATION Info
,
134 PFILENAME_ATTRIBUTE FileName
;
136 DPRINT("NtfsGetNameInformation() called\n");
138 FileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_WIN32
);
139 ASSERT(FileName
!= NULL
);
141 Length
= FileName
->NameLength
* sizeof (WCHAR
);
142 if ((sizeof(FILE_NAMES_INFORMATION
) + Length
) > BufferLength
)
143 return(STATUS_BUFFER_OVERFLOW
);
145 Info
->FileNameLength
= Length
;
146 Info
->NextEntryOffset
=
147 ROUND_UP(sizeof(FILE_NAMES_INFORMATION
) + Length
, sizeof(ULONG
));
148 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
150 return(STATUS_SUCCESS
);
155 NtfsGetDirectoryInformation(PDEVICE_EXTENSION DeviceExt
,
156 PFILE_RECORD_HEADER FileRecord
,
157 PNTFS_ATTR_CONTEXT DataContext
,
158 PFILE_DIRECTORY_INFORMATION Info
,
162 PFILENAME_ATTRIBUTE FileName
;
164 DPRINT("NtfsGetDirectoryInformation() called\n");
166 FileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_WIN32
);
167 ASSERT(FileName
!= NULL
);
169 Length
= FileName
->NameLength
* sizeof (WCHAR
);
170 if ((sizeof(FILE_DIRECTORY_INFORMATION
) + Length
) > BufferLength
)
171 return(STATUS_BUFFER_OVERFLOW
);
173 Info
->FileNameLength
= Length
;
174 Info
->NextEntryOffset
=
175 ROUND_UP(sizeof(FILE_DIRECTORY_INFORMATION
) + Length
, sizeof(ULONG
));
176 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
178 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
179 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
180 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
181 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
183 /* Convert file flags */
184 NtfsFileFlagsToAttributes(FileName
->FileAttributes
, &Info
->FileAttributes
);
186 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
187 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
191 return STATUS_SUCCESS
;
196 NtfsGetFullDirectoryInformation(PDEVICE_EXTENSION DeviceExt
,
197 PFILE_RECORD_HEADER FileRecord
,
198 PNTFS_ATTR_CONTEXT DataContext
,
199 PFILE_FULL_DIRECTORY_INFORMATION Info
,
203 PFILENAME_ATTRIBUTE FileName
;
205 DPRINT("NtfsGetFullDirectoryInformation() called\n");
207 FileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_WIN32
);
208 ASSERT(FileName
!= NULL
);
210 Length
= FileName
->NameLength
* sizeof (WCHAR
);
211 if ((sizeof(FILE_FULL_DIRECTORY_INFORMATION
) + Length
) > BufferLength
)
212 return(STATUS_BUFFER_OVERFLOW
);
214 Info
->FileNameLength
= Length
;
215 Info
->NextEntryOffset
=
216 ROUND_UP(sizeof(FILE_FULL_DIRECTORY_INFORMATION
) + Length
, sizeof(ULONG
));
217 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
219 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
220 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
221 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
222 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
224 /* Convert file flags */
225 NtfsFileFlagsToAttributes(FileName
->FileAttributes
, &Info
->FileAttributes
);
227 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
228 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
233 return STATUS_SUCCESS
;
238 NtfsGetBothDirectoryInformation(PDEVICE_EXTENSION DeviceExt
,
239 PFILE_RECORD_HEADER FileRecord
,
240 PNTFS_ATTR_CONTEXT DataContext
,
241 PFILE_BOTH_DIR_INFORMATION Info
,
245 PFILENAME_ATTRIBUTE FileName
, ShortFileName
;
247 DPRINT("NtfsGetBothDirectoryInformation() called\n");
249 FileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_WIN32
);
250 ASSERT(FileName
!= NULL
);
251 ShortFileName
= GetFileNameFromRecord(FileRecord
, NTFS_FILE_NAME_DOS
);
253 Length
= FileName
->NameLength
* sizeof (WCHAR
);
254 if ((sizeof(FILE_BOTH_DIR_INFORMATION
) + Length
) > BufferLength
)
255 return(STATUS_BUFFER_OVERFLOW
);
257 Info
->FileNameLength
= Length
;
258 Info
->NextEntryOffset
=
259 ROUND_UP(sizeof(FILE_BOTH_DIR_INFORMATION
) + Length
, sizeof(ULONG
));
260 RtlCopyMemory(Info
->FileName
, FileName
->Name
, Length
);
264 /* Should we upcase the filename? */
265 ASSERT(ShortFileName
->NameLength
<= ARRAYSIZE(Info
->ShortName
));
266 Info
->ShortNameLength
= ShortFileName
->NameLength
* sizeof(WCHAR
);
267 RtlCopyMemory(Info
->ShortName
, ShortFileName
->Name
, Info
->ShortNameLength
);
271 Info
->ShortName
[0] = 0;
272 Info
->ShortNameLength
= 0;
275 Info
->CreationTime
.QuadPart
= FileName
->CreationTime
;
276 Info
->LastAccessTime
.QuadPart
= FileName
->LastAccessTime
;
277 Info
->LastWriteTime
.QuadPart
= FileName
->LastWriteTime
;
278 Info
->ChangeTime
.QuadPart
= FileName
->ChangeTime
;
280 /* Convert file flags */
281 NtfsFileFlagsToAttributes(FileName
->FileAttributes
, &Info
->FileAttributes
);
283 Info
->EndOfFile
.QuadPart
= FileName
->AllocatedSize
;
284 Info
->AllocationSize
.QuadPart
= ROUND_UP(FileName
->AllocatedSize
, DeviceExt
->NtfsInfo
.BytesPerCluster
);
289 return STATUS_SUCCESS
;
294 NtfsQueryDirectory(PNTFS_IRP_CONTEXT IrpContext
)
297 PDEVICE_OBJECT DeviceObject
;
298 PDEVICE_EXTENSION DeviceExtension
;
299 LONG BufferLength
= 0;
300 PUNICODE_STRING SearchPattern
= NULL
;
301 FILE_INFORMATION_CLASS FileInformationClass
;
303 PUCHAR Buffer
= NULL
;
304 PFILE_NAMES_INFORMATION Buffer0
= NULL
;
307 BOOLEAN First
= FALSE
;
309 PIO_STACK_LOCATION Stack
;
310 PFILE_OBJECT FileObject
;
311 NTSTATUS Status
= STATUS_SUCCESS
;
312 PFILE_RECORD_HEADER FileRecord
;
313 PNTFS_ATTR_CONTEXT DataContext
;
315 UNICODE_STRING Pattern
;
317 DPRINT1("NtfsQueryDirectory() called\n");
320 Irp
= IrpContext
->Irp
;
321 DeviceObject
= IrpContext
->DeviceObject
;
323 DeviceExtension
= DeviceObject
->DeviceExtension
;
324 Stack
= IoGetCurrentIrpStackLocation(Irp
);
325 FileObject
= Stack
->FileObject
;
327 Ccb
= (PNTFS_CCB
)FileObject
->FsContext2
;
328 Fcb
= (PNTFS_FCB
)FileObject
->FsContext
;
330 /* Obtain the callers parameters */
331 BufferLength
= Stack
->Parameters
.QueryDirectory
.Length
;
332 SearchPattern
= Stack
->Parameters
.QueryDirectory
.FileName
;
333 FileInformationClass
= Stack
->Parameters
.QueryDirectory
.FileInformationClass
;
334 FileIndex
= Stack
->Parameters
.QueryDirectory
.FileIndex
;
336 if (SearchPattern
!= NULL
)
338 if (!Ccb
->DirectorySearchPattern
)
341 Ccb
->DirectorySearchPattern
=
342 ExAllocatePoolWithTag(NonPagedPool
, SearchPattern
->Length
+ sizeof(WCHAR
), TAG_NTFS
);
343 if (!Ccb
->DirectorySearchPattern
)
345 return STATUS_INSUFFICIENT_RESOURCES
;
348 memcpy(Ccb
->DirectorySearchPattern
,
349 SearchPattern
->Buffer
,
350 SearchPattern
->Length
);
351 Ccb
->DirectorySearchPattern
[SearchPattern
->Length
/ sizeof(WCHAR
)] = 0;
354 else if (!Ccb
->DirectorySearchPattern
)
357 Ccb
->DirectorySearchPattern
= ExAllocatePoolWithTag(NonPagedPool
, 2 * sizeof(WCHAR
), TAG_NTFS
);
358 if (!Ccb
->DirectorySearchPattern
)
360 return STATUS_INSUFFICIENT_RESOURCES
;
363 Ccb
->DirectorySearchPattern
[0] = L
'*';
364 Ccb
->DirectorySearchPattern
[1] = 0;
367 RtlInitUnicodeString(&Pattern
, Ccb
->DirectorySearchPattern
);
368 WildCard
= FsRtlDoesNameContainWildCards(&Pattern
);
371 Status
= RtlUpcaseUnicodeString(&Pattern
, &Pattern
, FALSE
);
372 if (!NT_SUCCESS(Status
))
374 DPRINT1("RtlUpcaseUnicodeString('%wZ') failed with status 0x%08lx\n", &Pattern
, Status
);
379 DPRINT1("Search pattern '%S'\n", Ccb
->DirectorySearchPattern
);
380 DPRINT1("In: '%S'\n", Fcb
->PathName
);
382 /* Determine directory index */
383 if (Stack
->Flags
& SL_INDEX_SPECIFIED
)
385 Ccb
->Entry
= Ccb
->CurrentByteOffset
.u
.LowPart
;
387 else if (First
|| (Stack
->Flags
& SL_RESTART_SCAN
))
392 /* Determine Buffer for result */
395 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
399 Buffer
= Irp
->UserBuffer
;
402 DPRINT("Buffer=%p tofind=%S\n", Buffer
, Ccb
->DirectorySearchPattern
);
404 while (Status
== STATUS_SUCCESS
&& BufferLength
> 0)
406 Status
= NtfsFindFileAt(DeviceExtension
,
413 //DPRINT("Found %S, Status=%x, entry %x\n", TempFcb.ObjectName, Status, Ccb->Entry);
415 if (NT_SUCCESS(Status
))
417 switch (FileInformationClass
)
419 case FileNameInformation
:
420 Status
= NtfsGetNameInformation(DeviceExtension
,
423 (PFILE_NAMES_INFORMATION
)Buffer
,
427 case FileDirectoryInformation
:
428 Status
= NtfsGetDirectoryInformation(DeviceExtension
,
431 (PFILE_DIRECTORY_INFORMATION
)Buffer
,
435 case FileFullDirectoryInformation
:
436 Status
= NtfsGetFullDirectoryInformation(DeviceExtension
,
439 (PFILE_FULL_DIRECTORY_INFORMATION
)Buffer
,
443 case FileBothDirectoryInformation
:
444 Status
= NtfsGetBothDirectoryInformation(DeviceExtension
,
447 (PFILE_BOTH_DIR_INFORMATION
)Buffer
,
452 Status
= STATUS_INVALID_INFO_CLASS
;
455 if (Status
== STATUS_BUFFER_OVERFLOW
)
459 Buffer0
->NextEntryOffset
= 0;
468 Buffer0
->NextEntryOffset
= 0;
473 Status
= STATUS_NO_SUCH_FILE
;
477 Status
= STATUS_NO_MORE_FILES
;
482 Buffer0
= (PFILE_NAMES_INFORMATION
)Buffer
;
483 Buffer0
->FileIndex
= FileIndex
++;
486 if (Stack
->Flags
& SL_RETURN_SINGLE_ENTRY
)
490 BufferLength
-= Buffer0
->NextEntryOffset
;
491 Buffer
+= Buffer0
->NextEntryOffset
;
492 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
497 Buffer0
->NextEntryOffset
= 0;
502 Status
= STATUS_SUCCESS
;
511 NtfsFsdDirectoryControl(PDEVICE_OBJECT DeviceObject
,
514 PNTFS_IRP_CONTEXT IrpContext
= NULL
;
515 NTSTATUS Status
= STATUS_UNSUCCESSFUL
;
517 DPRINT1("NtfsDirectoryControl() called\n");
519 FsRtlEnterFileSystem();
520 ASSERT(DeviceObject
);
523 NtfsIsIrpTopLevel(Irp
);
525 IrpContext
= NtfsAllocateIrpContext(DeviceObject
, Irp
);
528 switch (IrpContext
->MinorFunction
)
530 case IRP_MN_QUERY_DIRECTORY
:
531 Status
= NtfsQueryDirectory(IrpContext
);
534 case IRP_MN_NOTIFY_CHANGE_DIRECTORY
:
535 DPRINT1("IRP_MN_NOTIFY_CHANGE_DIRECTORY\n");
536 Status
= STATUS_NOT_IMPLEMENTED
;
540 Status
= STATUS_INVALID_DEVICE_REQUEST
;
545 Status
= STATUS_INSUFFICIENT_RESOURCES
;
547 Irp
->IoStatus
.Status
= Status
;
548 Irp
->IoStatus
.Information
= 0;
549 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
552 ExFreePoolWithTag(IrpContext
, 'PRIN');
554 IoSetTopLevelIrp(NULL
);
555 FsRtlExitFileSystem();