[NTFS]
[reactos.git] / reactos / drivers / filesystems / ntfs / mft.c
index 1ad70f1..f91e67b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ReactOS kernel
- *  Copyright (C) 2002 ReactOS Team
+ *  Copyright (C) 2002, 2014 ReactOS Team
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  * PROJECT:          ReactOS kernel
  * FILE:             drivers/filesystem/ntfs/mft.c
  * PURPOSE:          NTFS filesystem driver
- * PROGRAMMER:       Eric Kohl
- *                   Updated by Valentin Verkhovsky  2003/09/12
+ * PROGRAMMERS:      Eric Kohl
+ *                   Valentin Verkhovsky
+ *                   Pierre Schweitzer (pierre@reactos.org)
+ *                   HervĂ© Poussineau (hpoussin@reactos.org)
  */
 
 /* INCLUDES *****************************************************************/
@@ -85,7 +87,7 @@ FindAttributeHelper(PDEVICE_EXTENSION Vcb,
                     PCWSTR Name,
                     ULONG NameLength)
 {
-    DPRINT1("FindAttributeHelper(%p, %p, %p, 0x%x, %S, %u)\n", Vcb, AttrRecord, AttrRecordEnd, Type, Name, NameLength);
+    DPRINT("FindAttributeHelper(%p, %p, %p, 0x%x, %S, %u)\n", Vcb, AttrRecord, AttrRecordEnd, Type, Name, NameLength);
 
     while (AttrRecord < AttrRecordEnd)
     {
@@ -103,9 +105,6 @@ FindAttributeHelper(PDEVICE_EXTENSION Vcb,
             PNTFS_ATTR_RECORD ListAttrRecord;
             PNTFS_ATTR_RECORD ListAttrRecordEnd;
 
-            // Do not handle non-resident yet
-            ASSERT(!(AttrRecord->IsNonResident & 1));
-
             ListContext = PrepareAttributeContext(AttrRecord);
 
             ListSize = AttributeDataLength(&ListContext->Record);
@@ -133,7 +132,7 @@ FindAttributeHelper(PDEVICE_EXTENSION Vcb,
 
                 if (Context != NULL)
                 {
-                    DPRINT("Found context = %p\n", Context);
+                    if (AttrRecord->IsNonResident) DPRINT("Found context = %p\n", Context);
                     return Context;
                 }
             }
@@ -146,7 +145,7 @@ FindAttributeHelper(PDEVICE_EXTENSION Vcb,
                 PWCHAR AttrName;
 
                 AttrName = (PWCHAR)((PCHAR)AttrRecord + AttrRecord->NameOffset);
-                DPRINT("%s, %s\n", AttrName, Name);
+                DPRINT("%.*S, %.*S\n", AttrRecord->NameLength, AttrName, NameLength, Name);
                 if (RtlCompareMemory(AttrName, Name, NameLength << 1) == (NameLength << 1))
                 {
                     /* Found it, fill up the context and return. */
@@ -180,7 +179,7 @@ FindAttribute(PDEVICE_EXTENSION Vcb,
     PNTFS_ATTR_RECORD AttrRecord;
     PNTFS_ATTR_RECORD AttrRecordEnd;
 
-    DPRINT1("NtfsFindAttribute(%p, %p, %u, %S, %u, %p)\n", Vcb, MftRecord, Type, Name, NameLength, AttrCtx);
+    DPRINT("FindAttribute(%p, %p, 0x%x, %S, %u, %p)\n", Vcb, MftRecord, Type, Name, NameLength, AttrCtx);
 
     AttrRecord = (PNTFS_ATTR_RECORD)((PCHAR)MftRecord + MftRecord->AttributeOffset);
     AttrRecordEnd = (PNTFS_ATTR_RECORD)((PCHAR)MftRecord + Vcb->NtfsInfo.BytesPerFileRecord);
@@ -308,6 +307,7 @@ ReadAttribute(PDEVICE_EXTENSION Vcb,
     Status = NtfsReadDisk(Vcb->StorageDevice,
                           DataRunStartLCN * Vcb->NtfsInfo.BytesPerCluster + Offset - CurrentOffset,
                           ReadLength,
+                          Vcb->NtfsInfo.BytesPerSector,
                           (PVOID)Buffer,
                           FALSE);
     if (NT_SUCCESS(Status))
@@ -342,6 +342,7 @@ ReadAttribute(PDEVICE_EXTENSION Vcb,
                 Status = NtfsReadDisk(Vcb->StorageDevice,
                                       DataRunStartLCN * Vcb->NtfsInfo.BytesPerCluster,
                                       ReadLength,
+                                      Vcb->NtfsInfo.BytesPerSector,
                                       (PVOID)Buffer,
                                       FALSE);
                 if (!NT_SUCCESS(Status))
@@ -397,12 +398,12 @@ ReadFileRecord(PDEVICE_EXTENSION Vcb,
 {
     ULONGLONG BytesRead;
 
-    DPRINT1("ReadFileRecord(%p, %I64x, %p)\n", Vcb, index, file);
+    DPRINT("ReadFileRecord(%p, %I64x, %p)\n", Vcb, index, file);
 
     BytesRead = ReadAttribute(Vcb, Vcb->MFTContext, index * Vcb->NtfsInfo.BytesPerFileRecord, (PCHAR)file, Vcb->NtfsInfo.BytesPerFileRecord);
     if (BytesRead != Vcb->NtfsInfo.BytesPerFileRecord)
     {
-        DPRINT1("ReadFileRecord failed: %u read, %u expected\n", BytesRead, Vcb->NtfsInfo.BytesPerFileRecord);
+        DPRINT1("ReadFileRecord failed: %I64u read, %u expected\n", BytesRead, Vcb->NtfsInfo.BytesPerFileRecord);
         return STATUS_PARTIAL_COPY;
     }
 
@@ -469,7 +470,7 @@ CompareFileName(PUNICODE_STRING FileName,
 
     EntryName.Buffer = IndexEntry->FileName.Name;
     EntryName.Length = 
-    EntryName.MaximumLength = IndexEntry->FileName.NameLength;
+    EntryName.MaximumLength = IndexEntry->FileName.NameLength * sizeof(WCHAR);
 
     if (DirSearch)
     {
@@ -477,7 +478,7 @@ CompareFileName(PUNICODE_STRING FileName,
     }
     else
     {
-        return (RtlCompareUnicodeString(FileName, &EntryName, (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX)) == TRUE);
+        return (RtlCompareUnicodeString(FileName, &EntryName, (IndexEntry->FileName.NameType != NTFS_FILE_NAME_POSIX)) == 0);
     }
 }
 
@@ -488,8 +489,7 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
                   PUNICODE_STRING FileName,
                   PULONG FirstEntry,
                   BOOLEAN DirSearch,
-                  ULONGLONG *OutMFTIndex,
-                  PWSTR OutName)
+                  ULONGLONG *OutMFTIndex)
 {
     PFILE_RECORD_HEADER MftRecord;
     //ULONG Magic;
@@ -508,7 +508,7 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
     NTSTATUS Status;
     ULONG CurrentEntry = 0;
 
-    DPRINT1("NtfsFindMftRecord(%p, %I64d, %wZ, %p, %u, %p, %p)\n", Vcb, MFTIndex, FileName, FirstEntry, DirSearch, OutMFTIndex, OutName);
+    DPRINT("NtfsFindMftRecord(%p, %I64d, %wZ, %p, %u, %p)\n", Vcb, MFTIndex, FileName, FirstEntry, DirSearch, OutMFTIndex);
 
     MftRecord = ExAllocatePoolWithTag(NonPagedPool,
                                       Vcb->NtfsInfo.BytesPerFileRecord,
@@ -554,8 +554,6 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
             {
                 *OutMFTIndex = (IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK);
                 *FirstEntry = CurrentEntry;
-                RtlCopyMemory(OutName, IndexEntry->FileName.Name, IndexEntry->FileName.NameLength);
-                OutName[IndexEntry->FileName.NameLength / sizeof(WCHAR)] = UNICODE_NULL;
                 ExFreePoolWithTag(IndexRecord, TAG_NTFS);
                 ExFreePoolWithTag(MftRecord, TAG_NTFS);
                 return STATUS_SUCCESS;
@@ -634,9 +632,9 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
                 IndexBuffer = (PINDEX_BUFFER)IndexRecord;
                 ASSERT(IndexBuffer->Ntfs.Type == 'XDNI');
                 ASSERT(IndexBuffer->Header.AllocatedSize + 0x18 == IndexBlockSize);
-                IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)(&IndexBuffer->Header + IndexBuffer->Header.FirstEntryOffset);
-                IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)(&IndexBuffer->Header + IndexBuffer->Header.TotalSizeOfEntries);
-                //ASSERT(IndexEntryEnd <= (PINDEX_ENTRY_ATTRIBUTE)((ULONG_PTR)IndexBuffer + IndexBlockSize)); FIXME: Why doesn't it work?
+                IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((ULONG_PTR)&IndexBuffer->Header + IndexBuffer->Header.FirstEntryOffset);
+                IndexEntryEnd = (PINDEX_ENTRY_ATTRIBUTE)((ULONG_PTR)&IndexBuffer->Header + IndexBuffer->Header.TotalSizeOfEntries);
+                ASSERT(IndexEntryEnd <= (PINDEX_ENTRY_ATTRIBUTE)((ULONG_PTR)IndexBuffer + IndexBlockSize));
 
                 while (IndexEntry < IndexEntryEnd &&
                        !(IndexEntry->Flags & NTFS_INDEX_ENTRY_END))
@@ -648,8 +646,6 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
                         DPRINT("File found\n");
                         *OutMFTIndex = (IndexEntry->Data.Directory.IndexedFile & NTFS_MFT_MASK);
                         *FirstEntry = CurrentEntry;
-                        RtlCopyMemory(OutName, IndexEntry->FileName.Name, IndexEntry->FileName.NameLength);
-                        OutName[IndexEntry->FileName.NameLength / sizeof(WCHAR)] = UNICODE_NULL;
                         ExFreePoolWithTag(BitmapData, TAG_NTFS);
                         ExFreePoolWithTag(IndexRecord, TAG_NTFS);
                         ExFreePoolWithTag(MftRecord, TAG_NTFS);
@@ -658,6 +654,7 @@ NtfsFindMftRecord(PDEVICE_EXTENSION Vcb,
                     }
 
                     ++CurrentEntry;
+                    ASSERT(IndexEntry->Length >= sizeof(INDEX_ENTRY_ATTRIBUTE));
                     IndexEntry = (PINDEX_ENTRY_ATTRIBUTE)((PCHAR)IndexEntry + IndexEntry->Length);
                 }
 
@@ -689,25 +686,24 @@ NtfsLookupFileAt(PDEVICE_EXTENSION Vcb,
 {
     UNICODE_STRING Current, Remaining;
     NTSTATUS Status;
-    WCHAR FoundName[MAX_PATH + 1];
-    ULONG FirstEntry = 0, Length;
+    ULONG FirstEntry = 0;
 
-    DPRINT1("NtfsLookupFileAt(%p, %wZ, %p, %p, %I64x)\n", Vcb, PathName, FileRecord, DataContext, CurrentMFTIndex);
+    DPRINT("NtfsLookupFileAt(%p, %wZ, %p, %p, %I64x)\n", Vcb, PathName, FileRecord, DataContext, CurrentMFTIndex);
 
     FsRtlDissectName(*PathName, &Current, &Remaining);
 
     while (Current.Length != 0)
     {
-        DPRINT1("Current: %wZ\n", &Current);
+        DPRINT("Current: %wZ\n", &Current);
 
-        Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, &Current, &FirstEntry, FALSE, &CurrentMFTIndex, FoundName);
+        Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, &Current, &FirstEntry, FALSE, &CurrentMFTIndex);
         if (!NT_SUCCESS(Status))
         {
             return Status;
         }
 
         if (Remaining.Length == 0)
-            return STATUS_OBJECT_PATH_NOT_FOUND;
+            break;
 
         FsRtlDissectName(Current, &Current, &Remaining);
     }
@@ -727,14 +723,19 @@ NtfsLookupFileAt(PDEVICE_EXTENSION Vcb,
         return Status;
     }
 
-    Length = wcslen(FoundName) * sizeof(WCHAR);
-
-    Status = FindAttribute(Vcb, *FileRecord, AttributeData, FoundName, Length, DataContext);
-    if (!NT_SUCCESS(Status))
+    if (!((*FileRecord)->Flags & FRH_DIRECTORY))
     {
-        DPRINT("NtfsLookupFileAt: Can't find data attribute\n");
-        ExFreePoolWithTag(*FileRecord, TAG_NTFS);
-        return Status;
+        Status = FindAttribute(Vcb, *FileRecord, AttributeData, L"", 0, DataContext);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT("NtfsLookupFileAt: Can't find data attribute\n");
+            ExFreePoolWithTag(*FileRecord, TAG_NTFS);
+            return Status;
+        }
+    }
+    else
+    {
+        *DataContext = NULL;
     }
 
     *MFTIndex = CurrentMFTIndex;
@@ -762,14 +763,13 @@ NtfsFindFileAt(PDEVICE_EXTENSION Vcb,
                ULONGLONG CurrentMFTIndex)
 {
     NTSTATUS Status;
-    WCHAR FoundName[MAX_PATH + 1];
-    ULONG Length;
 
-    DPRINT1("NtfsFindFileAt(%p, %wZ, %p, %p, %p, %p, %I64x)\n", Vcb, SearchPattern, FirstEntry, FileRecord, DataContext, MFTIndex, CurrentMFTIndex);
+    DPRINT("NtfsFindFileAt(%p, %wZ, %p, %p, %p, %p, %I64x)\n", Vcb, SearchPattern, FirstEntry, FileRecord, DataContext, MFTIndex, CurrentMFTIndex);
 
-    Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, SearchPattern, FirstEntry, TRUE, &CurrentMFTIndex, FoundName);
+    Status = NtfsFindMftRecord(Vcb, CurrentMFTIndex, SearchPattern, FirstEntry, TRUE, &CurrentMFTIndex);
     if (!NT_SUCCESS(Status))
     {
+        DPRINT("NtfsFindFileAt: NtfsFindMftRecord() failed with status 0x%08lx\n", Status);
         return Status;
     }
 
@@ -788,14 +788,19 @@ NtfsFindFileAt(PDEVICE_EXTENSION Vcb,
         return Status;
     }
 
-    Length = wcslen(FoundName) * sizeof(WCHAR);
-
-    Status = FindAttribute(Vcb, *FileRecord, AttributeData, FoundName, Length, DataContext);
-    if (!NT_SUCCESS(Status))
+    if (!((*FileRecord)->Flags & FRH_DIRECTORY))
     {
-        DPRINT("NtfsFindFileAt: Can't find data attribute\n");
-        ExFreePoolWithTag(*FileRecord, TAG_NTFS);
-        return Status;
+        Status = FindAttribute(Vcb, *FileRecord, AttributeData, L"", 0, DataContext);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT("NtfsFindFileAt: Can't find data attribute\n");
+            ExFreePoolWithTag(*FileRecord, TAG_NTFS);
+            return Status;
+        }
+    }
+    else
+    {
+        *DataContext = NULL;
     }
 
     *MFTIndex = CurrentMFTIndex;