Advertisement
yorath

NTFS Hardlink

Dec 12th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. typedef struct _HARDLINK_INFO
  2. {
  3.     LPCTSTR FileName;
  4.     _SEARCH_INFO *Parent;
  5.     _SEARCH_INFO *Sibling;
  6. } HARDLINK_INFO, *PHARDLINK_INFO;
  7.  
  8. typedef struct _SEARCH_INFO
  9. {
  10.     std::list<HARDLINK_INFO> HardLinkInfo;
  11.     FILETIME CreationTime;
  12.     FILETIME LastAccessTime;
  13.     FILETIME LastWriteTime;
  14.     FILETIME ChangeTime;
  15.     ULARGE_INTEGER DataSize;
  16.     ULARGE_INTEGER AllocatedSize;
  17.     ULONG FileAttributes;
  18.     _SEARCH_INFO *Child;
  19. } SEARCH_INFO, *PSEARCH_INFO;
  20.  
  21. VOID
  22.     FetchSearchInfo(
  23.     __inout PDISKHANDLE DiskHandle,
  24.     __in PFILE_RECORD_HEADER FileRecord,
  25.     __out PSEARCH_INFO SearchInfo)
  26. {
  27.     PATTRIBUTE Attribute = (PATTRIBUTE)((ULONG_PTR)FileRecord + FileRecord->AttributeOffset);
  28.  
  29.     while (Attribute < (PATTRIBUTE)((ULONG_PTR)FileRecord + FileRecord->BytesInUse) &&
  30.         Attribute->AttributeType != AttributeInvalid)
  31.     {
  32.         if (Attribute->AttributeType == AttributeFileName)
  33.         {
  34.             PFILENAME_ATTRIBUTE FileName =
  35.                 (PFILENAME_ATTRIBUTE)((PUCHAR)Attribute + ((PRESIDENT_ATTRIBUTE)Attribute)->ValueOffset);
  36.  
  37.             if (FileName->NameType == POSIX_NAME || FileName->NameType == WIN32_NAME
  38.                 || FileName->NameType == WIN32DOS_NAME)
  39.             {
  40. #ifndef _UNICODE
  41.                 WideCharToMultiByte(CP_ACP, 0, FileName->Name, FileName->NameLength,
  42.                     FileName->Name, FileName->NameLength, NULL, NULL);
  43. #endif
  44.                 HARDLINK_INFO HardLinkInfo;
  45.                 ZeroMemory(&HardLinkInfo, sizeof(HARDLINK_INFO));
  46.  
  47.                 std::list<HARDLINK_INFO>::iterator Iterator;
  48.                 for (Iterator = SearchInfo->HardLinkInfo.begin();
  49.                     Iterator != SearchInfo->HardLinkInfo.end();
  50.                     ++Iterator)
  51.                 {
  52.                     if (Iterator->FileName != NULL &&
  53.                         RtlEqualMemory(FileName->Name, Iterator->FileName, FileName->NameLength))
  54.                         HardLinkInfo.FileName = Iterator->FileName;
  55.                 }
  56.                 if (HardLinkInfo.FileName == NULL)
  57.                 {
  58.                     HardLinkInfo.FileName = AllocAndCopyString(DiskHandle->HeapBlock,
  59.                         FileName->Name, FileName->NameLength);
  60.                     if (HardLinkInfo.FileName == NULL)
  61.                     {
  62.                         PRINT((TEXT("AllocAndCopyString failed\n")));
  63.                         continue;
  64.                     }
  65.                 }
  66.                 HardLinkInfo.Parent = &DiskHandle->SearchInfo[FileName->DirectoryFileReferenceNumber.LowPart];
  67.                 SearchInfo->HardLinkInfo.push_back(HardLinkInfo);
  68.                 SearchInfo->CreationTime = FileName->CreationTime;
  69.                 SearchInfo->LastAccessTime = FileName->LastAccessTime;
  70.                 SearchInfo->LastWriteTime = FileName->LastWriteTime;
  71.                 SearchInfo->ChangeTime = FileName->ChangeTime;
  72.                 SearchInfo->FileAttributes = FileName->FileAttributes;
  73.  
  74.                 if (HardLinkInfo.Parent != SearchInfo)
  75.                 {
  76.                     PSEARCH_INFO *SiblingSearchInfo = &HardLinkInfo.Parent->Child;
  77.                     while(*SiblingSearchInfo != NULL)
  78.                     {
  79.                         for (Iterator = (*SiblingSearchInfo)->HardLinkInfo.begin();
  80.                             Iterator != (*SiblingSearchInfo)->HardLinkInfo.end();
  81.                             ++Iterator)
  82.                         {
  83.                             if (Iterator->Parent = HardLinkInfo.Parent)
  84.                             {
  85.                                 SiblingSearchInfo = &Iterator->Sibling;
  86.                                 break;
  87.                             }
  88.                         }
  89.                     }
  90.                     *SiblingSearchInfo = SearchInfo;
  91.                 }
  92.  
  93.                 if (FileRecord->BaseFileRecord.QuadPart != 0)
  94.                 {
  95.                     PSEARCH_INFO ParentSearchInfo = &DiskHandle->SearchInfo[FileRecord->BaseFileRecord.LowPart];
  96.                     CopyMemory(ParentSearchInfo, SearchInfo, sizeof(SEARCH_INFO));
  97.  
  98.                     // We don't need this node when searching files
  99.                     for (Iterator = SearchInfo->HardLinkInfo.begin();
  100.                         Iterator != SearchInfo->HardLinkInfo.end();
  101.                         ++Iterator)
  102.                         Iterator->FileName = NULL;
  103.                 }
  104.                 else
  105.                 {
  106.                     PRINT((TEXT("MFT: %d, FileName:"), FileRecord->MFTRecordNumber));
  107.                     for (Iterator = SearchInfo->HardLinkInfo.begin();
  108.                         Iterator != SearchInfo->HardLinkInfo.end();
  109.                         ++Iterator)
  110.                         PRINT((TEXT(" %s"), Iterator->FileName));
  111.                     PRINT((TEXT("\n")));
  112.                    
  113.                     DiskHandle->TotalFiles++;
  114.                 }
  115.             }
  116.         }
  117.         else if (Attribute->AttributeType == AttributeData)
  118.         {
  119.             SearchInfo->AllocatedSize = ((PNONRESIDENT_ATTRIBUTE)Attribute)->AllocatedSize;
  120.             SearchInfo->DataSize = ((PNONRESIDENT_ATTRIBUTE)Attribute)->DataSize;
  121.         }
  122.         Attribute = (PATTRIBUTE)((ULONG_PTR)Attribute + Attribute->Length);
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement