[NTFS] - Add a few fixes and improvements, most from CR-123:
authorTrevor Thompson <tmt256@email.vccs.edu>
Wed, 5 Jul 2017 16:29:16 +0000 (16:29 +0000)
committerThomas Faber <thomas.faber@reactos.org>
Sun, 10 Dec 2017 10:14:58 +0000 (11:14 +0100)
-Add ATTR_RECORD_ALIGNMENT define to replace magic value of 8 when we need to adjust an attribute's beginning or length.
-Don't use int's.
-GetPackedByteCount() - Remove unused "bytes" variable.

svn path=/branches/GSoC_2016/NTFS/; revision=75288

drivers/filesystems/ntfs/attrib.c
drivers/filesystems/ntfs/btree.c
drivers/filesystems/ntfs/create.c
drivers/filesystems/ntfs/mft.c
drivers/filesystems/ntfs/ntfs.h

index 018c69b..7dcdabb 100644 (file)
@@ -77,7 +77,7 @@ AddData(PFILE_RECORD_HEADER FileRecord,
 
     AttributeAddress->Type = AttributeData;
     AttributeAddress->Length = ResidentHeaderLength;
 
     AttributeAddress->Type = AttributeData;
     AttributeAddress->Length = ResidentHeaderLength;
-    AttributeAddress->Length = ALIGN_UP_BY(AttributeAddress->Length, 8);
+    AttributeAddress->Length = ALIGN_UP_BY(AttributeAddress->Length, ATTR_RECORD_ALIGNMENT);
     AttributeAddress->Resident.ValueLength = 0;
     AttributeAddress->Resident.ValueOffset = ResidentHeaderLength;
 
     AttributeAddress->Resident.ValueLength = 0;
     AttributeAddress->Resident.ValueOffset = ResidentHeaderLength;
 
@@ -241,7 +241,7 @@ AddFileName(PFILE_RECORD_HEADER FileRecord,
 
     AttributeAddress->Length = ResidentHeaderLength +
         FIELD_OFFSET(FILENAME_ATTRIBUTE, Name) + FilenameNoPath.Length;
 
     AttributeAddress->Length = ResidentHeaderLength +
         FIELD_OFFSET(FILENAME_ATTRIBUTE, Name) + FilenameNoPath.Length;
-    AttributeAddress->Length = ALIGN_UP_BY(AttributeAddress->Length, 8);
+    AttributeAddress->Length = ALIGN_UP_BY(AttributeAddress->Length, ATTR_RECORD_ALIGNMENT);
 
     AttributeAddress->Resident.ValueLength = FIELD_OFFSET(FILENAME_ATTRIBUTE, Name) + FilenameNoPath.Length;
     AttributeAddress->Resident.ValueOffset = ResidentHeaderLength;
 
     AttributeAddress->Resident.ValueLength = FIELD_OFFSET(FILENAME_ATTRIBUTE, Name) + FilenameNoPath.Length;
     AttributeAddress->Resident.ValueOffset = ResidentHeaderLength;
@@ -363,7 +363,7 @@ AddRun(PNTFS_VCB Vcb,
 
         // calculate position of end markers
         NextAttributeOffset = AttrOffset + AttrContext->Record.NonResident.MappingPairsOffset + RunBufferSize;
 
         // calculate position of end markers
         NextAttributeOffset = AttrOffset + AttrContext->Record.NonResident.MappingPairsOffset + RunBufferSize;
-        NextAttributeOffset = ALIGN_UP_BY(NextAttributeOffset, 8);
+        NextAttributeOffset = ALIGN_UP_BY(NextAttributeOffset, ATTR_RECORD_ALIGNMENT);
 
         // Update the length
         DestinationAttribute->Length = NextAttributeOffset - AttrOffset;
 
         // Update the length
         DestinationAttribute->Length = NextAttributeOffset - AttrOffset;
@@ -437,7 +437,7 @@ AddStandardInformation(PFILE_RECORD_HEADER FileRecord,
 
     AttributeAddress->Type = AttributeStandardInformation;
     AttributeAddress->Length = sizeof(STANDARD_INFORMATION) + ResidentHeaderLength;
 
     AttributeAddress->Type = AttributeStandardInformation;
     AttributeAddress->Length = sizeof(STANDARD_INFORMATION) + ResidentHeaderLength;
-    AttributeAddress->Length = ALIGN_UP_BY(AttributeAddress->Length, 8);
+    AttributeAddress->Length = ALIGN_UP_BY(AttributeAddress->Length, ATTR_RECORD_ALIGNMENT);
     AttributeAddress->Resident.ValueLength = sizeof(STANDARD_INFORMATION);
     AttributeAddress->Resident.ValueOffset = ResidentHeaderLength;
     AttributeAddress->Instance = FileRecord->NextAttributeNumber++;
     AttributeAddress->Resident.ValueLength = sizeof(STANDARD_INFORMATION);
     AttributeAddress->Resident.ValueOffset = ResidentHeaderLength;
     AttributeAddress->Instance = FileRecord->NextAttributeNumber++;
@@ -847,7 +847,8 @@ FreeClusters(PNTFS_VCB Vcb,
     if (NextAttribute->Type == AttributeEnd)
     {
         // update attribute length
     if (NextAttribute->Type == AttributeEnd)
     {
         // update attribute length
-        AttrContext->Record.Length = ALIGN_UP_BY(AttrContext->Record.NonResident.MappingPairsOffset + RunBufferSize, 8);
+        AttrContext->Record.Length = ALIGN_UP_BY(AttrContext->Record.NonResident.MappingPairsOffset + RunBufferSize,
+                                                 ATTR_RECORD_ALIGNMENT);
         DestinationAttribute->Length = AttrContext->Record.Length;
 
         // write end markers
         DestinationAttribute->Length = AttrContext->Record.Length;
 
         // write end markers
@@ -1459,7 +1460,6 @@ UCHAR
 GetPackedByteCount(LONGLONG NumberToPack,
                    BOOLEAN IsSigned)
 {
 GetPackedByteCount(LONGLONG NumberToPack,
                    BOOLEAN IsSigned)
 {
-    int bytes = 0;
     if (!IsSigned)
     {
         if (NumberToPack >= 0x0100000000000000)
     if (!IsSigned)
     {
         if (NumberToPack >= 0x0100000000000000)
@@ -1496,7 +1496,6 @@ GetPackedByteCount(LONGLONG NumberToPack,
             return 3;
         if (NumberToPack >= 0x0000000000000080)
             return 2;
             return 3;
         if (NumberToPack >= 0x0000000000000080)
             return 2;
-        return 1;
     }
     else
     {
     }
     else
     {
@@ -1515,9 +1514,8 @@ GetPackedByteCount(LONGLONG NumberToPack,
             return 3;
         if (NumberToPack <= 0xffffffffffffff80)
             return 2;
             return 3;
         if (NumberToPack <= 0xffffffffffffff80)
             return 2;
-        return 1;
     }
     }
-    return bytes;
+    return 1;
 }
 
 NTSTATUS
 }
 
 NTSTATUS
index f4641d0..eff2b15 100644 (file)
@@ -271,7 +271,7 @@ CreateIndexRootFromBTree(PDEVICE_EXTENSION DeviceExt,
                          PINDEX_ROOT_ATTRIBUTE *IndexRoot,
                          ULONG *Length)
 {
                          PINDEX_ROOT_ATTRIBUTE *IndexRoot,
                          ULONG *Length)
 {
-    int i;
+    ULONG i;
     PB_TREE_KEY CurrentKey;
     PINDEX_ENTRY_ATTRIBUTE CurrentNodeEntry;
     PINDEX_ROOT_ATTRIBUTE NewIndexRoot = ExAllocatePoolWithTag(NonPagedPool,
     PB_TREE_KEY CurrentKey;
     PINDEX_ENTRY_ATTRIBUTE CurrentNodeEntry;
     PINDEX_ROOT_ATTRIBUTE NewIndexRoot = ExAllocatePoolWithTag(NonPagedPool,
@@ -366,7 +366,7 @@ DestroyBTreeNode(PB_TREE_FILENAME_NODE Node)
 {
     PB_TREE_KEY NextKey;
     PB_TREE_KEY CurrentKey = Node->FirstKey;
 {
     PB_TREE_KEY NextKey;
     PB_TREE_KEY CurrentKey = Node->FirstKey;
-    int i;
+    ULONG i;
     for (i = 0; i < Node->KeyCount; i++)
     {
         NT_ASSERT(CurrentKey);
     for (i = 0; i < Node->KeyCount; i++)
     {
         NT_ASSERT(CurrentKey);
@@ -400,9 +400,9 @@ DestroyBTree(PB_TREE Tree)
 }
 
 VOID
 }
 
 VOID
-DumpBTreeKey(PB_TREE_KEY Key, int Number, int Depth)
+DumpBTreeKey(PB_TREE_KEY Key, ULONG Number, ULONG Depth)
 {
 {
-    int i;
+    ULONG i;
     for (i = 0; i < Depth; i++)
         DbgPrint(" ");
     DbgPrint(" Key #%d", Number);
     for (i = 0; i < Depth; i++)
         DbgPrint(" ");
     DbgPrint(" Key #%d", Number);
@@ -420,10 +420,10 @@ DumpBTreeKey(PB_TREE_KEY Key, int Number, int Depth)
 }
 
 VOID
 }
 
 VOID
-DumpBTreeNode(PB_TREE_FILENAME_NODE Node, int Number, int Depth)
+DumpBTreeNode(PB_TREE_FILENAME_NODE Node, ULONG Number, ULONG Depth)
 {
     PB_TREE_KEY CurrentKey;
 {
     PB_TREE_KEY CurrentKey;
-    int i;
+    ULONG i;
     for (i = 0; i < Depth; i++)
         DbgPrint(" ");
     DbgPrint("Node #%d, Depth %d\n", Number, Depth);
     for (i = 0; i < Depth; i++)
         DbgPrint(" ");
     DbgPrint("Node #%d, Depth %d\n", Number, Depth);
@@ -488,7 +488,7 @@ NtfsInsertKey(ULONGLONG FileReference,
     ULONG EntrySize = ALIGN_UP_BY(AttributeSize + FIELD_OFFSET(INDEX_ENTRY_ATTRIBUTE, FileName), 8);
     PINDEX_ENTRY_ATTRIBUTE NewEntry;
     PB_TREE_KEY NewKey, CurrentKey, PreviousKey;
     ULONG EntrySize = ALIGN_UP_BY(AttributeSize + FIELD_OFFSET(INDEX_ENTRY_ATTRIBUTE, FileName), 8);
     PINDEX_ENTRY_ATTRIBUTE NewEntry;
     PB_TREE_KEY NewKey, CurrentKey, PreviousKey;
-    int i;
+    ULONG i;
 
     DPRINT1("NtfsInsertKey(0x%02I64, %p, %p, %s)\n",
             FileReference,
 
     DPRINT1("NtfsInsertKey(0x%02I64, %p, %p, %s)\n",
             FileReference,
index 3c8ba81..8274d97 100644 (file)
@@ -699,7 +699,7 @@ NtfsCreateFileRecord(PDEVICE_EXTENSION DeviceExt,
     // setup other file record fields
     FileRecord->SequenceNumber = 1;
     FileRecord->AttributeOffset = FileRecord->Ntfs.UsaOffset + (2 * FileRecord->Ntfs.UsaCount);
     // setup other file record fields
     FileRecord->SequenceNumber = 1;
     FileRecord->AttributeOffset = FileRecord->Ntfs.UsaOffset + (2 * FileRecord->Ntfs.UsaCount);
-    FileRecord->AttributeOffset = ALIGN_UP_BY(FileRecord->AttributeOffset, 8);
+    FileRecord->AttributeOffset = ALIGN_UP_BY(FileRecord->AttributeOffset, ATTR_RECORD_ALIGNMENT);
     FileRecord->Flags = FRH_IN_USE;
     FileRecord->BytesInUse = FileRecord->AttributeOffset + sizeof(ULONG) * 2;
    
     FileRecord->Flags = FRH_IN_USE;
     FileRecord->BytesInUse = FileRecord->AttributeOffset + sizeof(ULONG) * 2;
    
index 539b8f2..87820e6 100644 (file)
@@ -373,7 +373,7 @@ InternalSetResidentAttributeLength(PNTFS_ATTR_CONTEXT AttrContext,
     // Ensure NextAttributeOffset is aligned to an 8-byte boundary
     if (NextAttributeOffset % 8 != 0)
     {
     // Ensure NextAttributeOffset is aligned to an 8-byte boundary
     if (NextAttributeOffset % 8 != 0)
     {
-        USHORT Padding = 8 - (NextAttributeOffset % 8);
+        USHORT Padding = ATTR_RECORD_ALIGNMENT - (NextAttributeOffset % ATTR_RECORD_ALIGNMENT);
         NextAttributeOffset += Padding;
         AttrContext->Record.Length += Padding;
         Destination->Length += Padding;
         NextAttributeOffset += Padding;
         AttrContext->Record.Length += Padding;
         Destination->Length += Padding;
@@ -731,7 +731,7 @@ SetResidentAttributeDataLength(PDEVICE_EXTENSION Vcb,
                 // update the end of the file record
                 // calculate position of end markers (1 byte for empty data run)
                 EndAttributeOffset = AttrOffset + AttrContext->Record.NonResident.MappingPairsOffset + 1;
                 // update the end of the file record
                 // calculate position of end markers (1 byte for empty data run)
                 EndAttributeOffset = AttrOffset + AttrContext->Record.NonResident.MappingPairsOffset + 1;
-                EndAttributeOffset = ALIGN_UP_BY(EndAttributeOffset, 8);
+                EndAttributeOffset = ALIGN_UP_BY(EndAttributeOffset, ATTR_RECORD_ALIGNMENT);
 
                 // Update the length
                 Destination->Length = EndAttributeOffset - AttrOffset;
 
                 // Update the length
                 Destination->Length = EndAttributeOffset - AttrOffset;
index eb4d6f3..ddb876d 100644 (file)
@@ -293,6 +293,10 @@ typedef struct
     };
 } NTFS_ATTR_RECORD, *PNTFS_ATTR_RECORD;
 
     };
 } NTFS_ATTR_RECORD, *PNTFS_ATTR_RECORD;
 
+// The beginning and length of an attribute record are always aligned to an 8-byte boundary,
+// relative to the beginning of the file record.
+#define ATTR_RECORD_ALIGNMENT 8
+
 typedef struct
 {
     ULONGLONG CreationTime;
 typedef struct
 {
     ULONGLONG CreationTime;
@@ -408,7 +412,7 @@ typedef struct _B_TREE_KEY
 // A key's sub-node precedes that key in the ordered list.
 typedef struct
 {
 // A key's sub-node precedes that key in the ordered list.
 typedef struct
 {
-    int KeyCount;
+    ULONG KeyCount;
     PB_TREE_KEY FirstKey;
 } B_TREE_FILENAME_NODE, *PB_TREE_FILENAME_NODE;
 
     PB_TREE_KEY FirstKey;
 } B_TREE_FILENAME_NODE, *PB_TREE_FILENAME_NODE;