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/attrib.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
25 * Hervé Poussineau (hpoussin@reactos.org)
26 * Pierre Schweitzer (pierre@reactos.org)
29 /* INCLUDES *****************************************************************/
36 /* FUNCTIONS ****************************************************************/
39 DecodeRun(PUCHAR DataRun
,
40 LONGLONG
*DataRunOffset
,
41 ULONGLONG
*DataRunLength
)
43 UCHAR DataRunOffsetSize
;
44 UCHAR DataRunLengthSize
;
47 DataRunOffsetSize
= (*DataRun
>> 4) & 0xF;
48 DataRunLengthSize
= *DataRun
& 0xF;
52 for (i
= 0; i
< DataRunLengthSize
; i
++)
54 *DataRunLength
+= ((ULONG64
)*DataRun
) << (i
* 8);
58 /* NTFS 3+ sparse files */
59 if (DataRunOffsetSize
== 0)
65 for (i
= 0; i
< DataRunOffsetSize
- 1; i
++)
67 *DataRunOffset
+= ((ULONG64
)*DataRun
) << (i
* 8);
70 /* The last byte contains sign so we must process it different way. */
71 *DataRunOffset
= ((LONG64
)(CHAR
)(*(DataRun
++)) << (i
* 8)) + *DataRunOffset
;
74 DPRINT("DataRunOffsetSize: %x\n", DataRunOffsetSize
);
75 DPRINT("DataRunLengthSize: %x\n", DataRunLengthSize
);
76 DPRINT("DataRunOffset: %x\n", *DataRunOffset
);
77 DPRINT("DataRunLength: %x\n", *DataRunLength
);
83 FindRun(PNTFS_ATTR_RECORD NresAttr
,
88 if (vcn
< NresAttr
->NonResident
.LowestVCN
|| vcn
> NresAttr
->NonResident
.HighestVCN
)
91 DecodeRun((PUCHAR
)((ULONG_PTR
)NresAttr
+ NresAttr
->NonResident
.MappingPairsOffset
), (PLONGLONG
)lcn
, count
);
98 InternalReadNonResidentAttributes(PFIND_ATTR_CONTXT Context
)
101 PNTFS_ATTR_RECORD Attribute
;
102 PNTFS_ATTR_CONTEXT ListContext
;
104 DPRINT("InternalReadNonResidentAttributes(%p)\n", Context
);
106 Attribute
= Context
->CurrAttr
;
107 ASSERT(Attribute
->Type
== AttributeAttributeList
);
109 if (Context
->OnlyResident
)
111 Context
->NonResidentStart
= NULL
;
112 Context
->NonResidentEnd
= NULL
;
113 return STATUS_SUCCESS
;
116 if (Context
->NonResidentStart
!= NULL
)
118 return STATUS_FILE_CORRUPT_ERROR
;
121 ListContext
= PrepareAttributeContext(Attribute
);
122 ListSize
= AttributeDataLength(&ListContext
->Record
);
123 if (ListSize
> 0xFFFFFFFF)
125 ReleaseAttributeContext(ListContext
);
126 return STATUS_BUFFER_OVERFLOW
;
129 Context
->NonResidentStart
= ExAllocatePoolWithTag(NonPagedPool
, (ULONG
)ListSize
, TAG_NTFS
);
130 if (Context
->NonResidentStart
== NULL
)
132 ReleaseAttributeContext(ListContext
);
133 return STATUS_INSUFFICIENT_RESOURCES
;
136 if (ReadAttribute(Context
->Vcb
, ListContext
, 0, (PCHAR
)Context
->NonResidentStart
, (ULONG
)ListSize
) != ListSize
)
138 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
139 Context
->NonResidentStart
= NULL
;
140 ReleaseAttributeContext(ListContext
);
141 return STATUS_FILE_CORRUPT_ERROR
;
144 ReleaseAttributeContext(ListContext
);
145 Context
->NonResidentEnd
= (PNTFS_ATTR_RECORD
)((PCHAR
)Context
->NonResidentStart
+ ListSize
);
146 return STATUS_SUCCESS
;
151 InternalGetNextAttribute(PFIND_ATTR_CONTXT Context
)
153 if (Context
->CurrAttr
== (PVOID
)-1)
158 if (Context
->CurrAttr
>= Context
->FirstAttr
&&
159 Context
->CurrAttr
< Context
->LastAttr
)
161 if (Context
->CurrAttr
->Length
== 0)
163 DPRINT1("Broken length!\n");
164 Context
->CurrAttr
= (PVOID
)-1;
168 Context
->CurrAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
169 if (Context
->CurrAttr
< Context
->LastAttr
&&
170 Context
->CurrAttr
->Type
!= AttributeEnd
)
172 return Context
->CurrAttr
;
176 if (Context
->NonResidentStart
== NULL
)
178 Context
->CurrAttr
= (PVOID
)-1;
182 if (Context
->CurrAttr
< Context
->NonResidentStart
||
183 Context
->CurrAttr
>= Context
->NonResidentEnd
)
185 Context
->CurrAttr
= Context
->NonResidentStart
;
187 else if (Context
->CurrAttr
->Length
!= 0)
189 Context
->CurrAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
193 DPRINT1("Broken length!\n");
194 Context
->CurrAttr
= (PVOID
)-1;
198 if (Context
->CurrAttr
< Context
->NonResidentEnd
&&
199 Context
->CurrAttr
->Type
!= AttributeEnd
)
201 return Context
->CurrAttr
;
204 Context
->CurrAttr
= (PVOID
)-1;
209 FindFirstAttribute(PFIND_ATTR_CONTXT Context
,
210 PDEVICE_EXTENSION Vcb
,
211 PFILE_RECORD_HEADER FileRecord
,
212 BOOLEAN OnlyResident
,
213 PNTFS_ATTR_RECORD
* Attribute
)
217 DPRINT("FindFistAttribute(%p, %p, %p, %p, %u, %p)\n", Context
, Vcb
, FileRecord
, OnlyResident
, Attribute
);
220 Context
->OnlyResident
= OnlyResident
;
221 Context
->FirstAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
222 Context
->CurrAttr
= Context
->FirstAttr
;
223 Context
->LastAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
);
224 Context
->NonResidentStart
= NULL
;
225 Context
->NonResidentEnd
= NULL
;
227 if (Context
->FirstAttr
->Type
== AttributeEnd
)
229 Context
->CurrAttr
= (PVOID
)-1;
230 return STATUS_END_OF_FILE
;
232 else if (Context
->FirstAttr
->Type
== AttributeAttributeList
)
234 Status
= InternalReadNonResidentAttributes(Context
);
235 if (!NT_SUCCESS(Status
))
240 *Attribute
= InternalGetNextAttribute(Context
);
241 if (*Attribute
== NULL
)
243 return STATUS_END_OF_FILE
;
248 *Attribute
= Context
->CurrAttr
;
251 return STATUS_SUCCESS
;
255 FindNextAttribute(PFIND_ATTR_CONTXT Context
,
256 PNTFS_ATTR_RECORD
* Attribute
)
260 DPRINT("FindNextAttribute(%p, %p)\n", Context
, Attribute
);
262 *Attribute
= InternalGetNextAttribute(Context
);
263 if (*Attribute
== NULL
)
265 return STATUS_END_OF_FILE
;
268 if (Context
->CurrAttr
->Type
!= AttributeAttributeList
)
270 return STATUS_SUCCESS
;
273 Status
= InternalReadNonResidentAttributes(Context
);
274 if (!NT_SUCCESS(Status
))
279 *Attribute
= InternalGetNextAttribute(Context
);
280 if (*Attribute
== NULL
)
282 return STATUS_END_OF_FILE
;
285 return STATUS_SUCCESS
;
289 FindCloseAttribute(PFIND_ATTR_CONTXT Context
)
291 if (Context
->NonResidentStart
!= NULL
)
293 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
294 Context
->NonResidentStart
= NULL
;
300 NtfsDumpFileNameAttribute(PNTFS_ATTR_RECORD Attribute
)
302 PFILENAME_ATTRIBUTE FileNameAttr
;
304 DbgPrint(" $FILE_NAME ");
306 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
308 FileNameAttr
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
309 DbgPrint(" (%x) '%.*S' ", FileNameAttr
->NameType
, FileNameAttr
->NameLength
, FileNameAttr
->Name
);
310 DbgPrint(" '%x' ", FileNameAttr
->FileAttributes
);
316 NtfsDumpStandardInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
318 PSTANDARD_INFORMATION StandardInfoAttr
;
320 DbgPrint(" $STANDARD_INFORMATION ");
322 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
324 StandardInfoAttr
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
325 DbgPrint(" '%x' ", StandardInfoAttr
->FileAttribute
);
331 NtfsDumpVolumeNameAttribute(PNTFS_ATTR_RECORD Attribute
)
335 DbgPrint(" $VOLUME_NAME ");
337 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
339 VolumeName
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
340 DbgPrint(" '%.*S' ", Attribute
->Resident
.ValueLength
/ sizeof(WCHAR
), VolumeName
);
346 NtfsDumpVolumeInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
348 PVOLINFO_ATTRIBUTE VolInfoAttr
;
350 DbgPrint(" $VOLUME_INFORMATION ");
352 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
354 VolInfoAttr
= (PVOLINFO_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
355 DbgPrint(" NTFS Version %u.%u Flags 0x%04hx ",
356 VolInfoAttr
->MajorVersion
,
357 VolInfoAttr
->MinorVersion
,
364 NtfsDumpIndexRootAttribute(PNTFS_ATTR_RECORD Attribute
)
366 PINDEX_ROOT_ATTRIBUTE IndexRootAttr
;
368 IndexRootAttr
= (PINDEX_ROOT_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
370 if (IndexRootAttr
->AttributeType
== AttributeFileName
)
371 ASSERT(IndexRootAttr
->CollationRule
== COLLATION_FILE_NAME
);
373 DbgPrint(" $INDEX_ROOT (%uB, %u) ", IndexRootAttr
->SizeOfEntry
, IndexRootAttr
->ClustersPerIndexRecord
);
375 if (IndexRootAttr
->Header
.Flags
== INDEX_ROOT_SMALL
)
377 DbgPrint(" (small) ");
381 ASSERT(IndexRootAttr
->Header
.Flags
== INDEX_ROOT_LARGE
);
382 DbgPrint(" (large) ");
389 NtfsDumpAttribute(PDEVICE_EXTENSION Vcb
,
390 PNTFS_ATTR_RECORD Attribute
)
395 ULONGLONG runcount
= 0;
397 switch (Attribute
->Type
)
399 case AttributeFileName
:
400 NtfsDumpFileNameAttribute(Attribute
);
403 case AttributeStandardInformation
:
404 NtfsDumpStandardInformationAttribute(Attribute
);
407 case AttributeObjectId
:
408 DbgPrint(" $OBJECT_ID ");
411 case AttributeSecurityDescriptor
:
412 DbgPrint(" $SECURITY_DESCRIPTOR ");
415 case AttributeVolumeName
:
416 NtfsDumpVolumeNameAttribute(Attribute
);
419 case AttributeVolumeInformation
:
420 NtfsDumpVolumeInformationAttribute(Attribute
);
425 //DataBuf = ExAllocatePool(NonPagedPool,AttributeLengthAllocated(Attribute));
428 case AttributeIndexRoot
:
429 NtfsDumpIndexRootAttribute(Attribute
);
432 case AttributeIndexAllocation
:
433 DbgPrint(" $INDEX_ALLOCATION ");
436 case AttributeBitmap
:
437 DbgPrint(" $BITMAP ");
440 case AttributeReparsePoint
:
441 DbgPrint(" $REPARSE_POINT ");
444 case AttributeEAInformation
:
445 DbgPrint(" $EA_INFORMATION ");
452 case AttributePropertySet
:
453 DbgPrint(" $PROPERTY_SET ");
456 case AttributeLoggedUtilityStream
:
457 DbgPrint(" $LOGGED_UTILITY_STREAM ");
461 DbgPrint(" Attribute %lx ",
466 if (Attribute
->Type
!= AttributeAttributeList
)
468 if (Attribute
->NameLength
!= 0)
470 Name
.Length
= Attribute
->NameLength
* sizeof(WCHAR
);
471 Name
.MaximumLength
= Name
.Length
;
472 Name
.Buffer
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->NameOffset
);
474 DbgPrint("'%wZ' ", &Name
);
478 Attribute
->IsNonResident
? "non-resident" : "resident");
480 if (Attribute
->IsNonResident
)
482 FindRun(Attribute
,0,&lcn
, &runcount
);
484 DbgPrint(" AllocatedSize %I64u DataSize %I64u\n",
485 Attribute
->NonResident
.AllocatedSize
, Attribute
->NonResident
.DataSize
);
486 DbgPrint(" logical clusters: %I64u - %I64u\n",
487 lcn
, lcn
+ runcount
- 1);
494 NtfsDumpFileAttributes(PDEVICE_EXTENSION Vcb
,
495 PFILE_RECORD_HEADER FileRecord
)
498 FIND_ATTR_CONTXT Context
;
499 PNTFS_ATTR_RECORD Attribute
;
501 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
502 while (NT_SUCCESS(Status
))
504 NtfsDumpAttribute(Vcb
, Attribute
);
506 Status
= FindNextAttribute(&Context
, &Attribute
);
509 FindCloseAttribute(&Context
);
513 GetFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
514 PFILE_RECORD_HEADER FileRecord
,
517 FIND_ATTR_CONTXT Context
;
518 PNTFS_ATTR_RECORD Attribute
;
519 PFILENAME_ATTRIBUTE Name
;
522 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
523 while (NT_SUCCESS(Status
))
525 if (Attribute
->Type
== AttributeFileName
)
527 Name
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
528 if (Name
->NameType
== NameType
||
529 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_WIN32
) ||
530 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_DOS
))
532 FindCloseAttribute(&Context
);
537 Status
= FindNextAttribute(&Context
, &Attribute
);
540 FindCloseAttribute(&Context
);
544 PSTANDARD_INFORMATION
545 GetStandardInformationFromRecord(PDEVICE_EXTENSION Vcb
,
546 PFILE_RECORD_HEADER FileRecord
)
549 FIND_ATTR_CONTXT Context
;
550 PNTFS_ATTR_RECORD Attribute
;
551 PSTANDARD_INFORMATION StdInfo
;
553 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
554 while (NT_SUCCESS(Status
))
556 if (Attribute
->Type
== AttributeStandardInformation
)
558 StdInfo
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
559 FindCloseAttribute(&Context
);
563 Status
= FindNextAttribute(&Context
, &Attribute
);
566 FindCloseAttribute(&Context
);
571 GetBestFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
572 PFILE_RECORD_HEADER FileRecord
)
574 PFILENAME_ATTRIBUTE FileName
;
576 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_POSIX
);
577 if (FileName
== NULL
)
579 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_WIN32
);
580 if (FileName
== NULL
)
582 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_DOS
);