Home > Backend Development > C++ > How can I efficiently detect file changes on an NTFS volume using the FSCTL_ENUM_USN_DATA function?

How can I efficiently detect file changes on an NTFS volume using the FSCTL_ENUM_USN_DATA function?

DDD
Release: 2024-10-29 08:14:30
Original
616 people have browsed it

How can I efficiently detect file changes on an NTFS volume using the FSCTL_ENUM_USN_DATA function?

Detecting Changes on a Volume: A Detailed Solution

To effectively detect file deletions, modifications, and creations on an NTFS volume, you can utilize the FSCTL_ENUM_USN_DATA function. This approach offers several advantages:

  • Fast Enumeration: It efficiently scans the volume, retrieving only existing files with a performance of over 6000 records per second.
  • Detailed Information: It provides comprehensive data, including file flags and USNs, enabling precise change detection methods.
  • Hierarchical File Data: By matching parent IDs with file IDs, you can reconstruct the complete file path for each detected file.

Implementation Steps:

  1. Enumerate Files: Use FSCTL_ENUM_USN_DATA to retrieve records for all existing files.
  2. Identify Changes: Analyze the file flags and USNs to determine which files have been modified, created, or deleted.
  3. Reconstruct File Paths: Match parent IDs with file IDs to obtain the full paths of the affected files.

An example C program demonstrating this approach is provided below, searching for files named "test.txt" and displaying information about their changes and parent directories:

<code class="c++">#include <Windows.h>
#include <stdio.h>

#define BUFFER_SIZE (1024 * 1024)

int main() {
  HANDLE drive = CreateFileW(L"\\?\c:", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING, NULL);

  MFT_ENUM_DATA mft_enum_data;
  USN maxusn;
  USN_RECORD *record;

  // Query USN journal for information
  if (DeviceIoControl(drive, FSCTL_QUERY_USN_JOURNAL, NULL, 0, &maxusn, sizeof(USN), NULL, NULL)) {
    mft_enum_data.StartFileReferenceNumber = 0;
    mft_enum_data.LowUsn = 0;
    mft_enum_data.HighUsn = maxusn;
    
    DWORDLONG nextid, filecount = 0;

    for (;;) {
      void *buffer = VirtualAlloc(NULL, BUFFER_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

      if (DeviceIoControl(drive, FSCTL_ENUM_USN_DATA, &mft_enum_data, sizeof(mft_enum_data), buffer, BUFFER_SIZE, NULL, NULL)) {
        nextid = *((DWORDLONG *)buffer);

        record = (USN_RECORD *)((USN *)buffer + 1);
        while (record < (USN_RECORD *)(((BYTE *)buffer) + BUFFER_SIZE)) {
          filecount++;
          WCHAR *filename = (WCHAR *)(((BYTE *)record) + record->FileNameOffset);
          if (wcsncmp(filename, L"test.txt", 8) == 0) {
            printf("=================================================================\n");
            printf("RecordLength: %u\n", record->RecordLength);
            printf("MajorVersion: %u\n", (DWORD)record->MajorVersion);
            printf("MinorVersion: %u\n", (DWORD)record->MinorVersion);
            printf("FileReferenceNumber: %lu\n", record->FileReferenceNumber);
            printf("ParentFRN: %lu\n", record->ParentFileReferenceNumber);
            printf("USN: %lu\n", record->Usn);
            printf("Timestamp: %lu\n", record->TimeStamp);
            printf("Reason: %u\n", record->Reason);
            printf("SourceInfo: %u\n", record->SourceInfo);
            printf("SecurityId: %u\n", record->SecurityId);
            printf("FileAttributes: %x\n", record->FileAttributes);
            printf("FileNameLength: %u\n", (DWORD)record->FileNameLength);
            printf("FileName: %.*ls\n", record->FileNameLength, filename);
            
            // Reconstruct file path by matching parent file reference numbers
            DWORD bytecount;
            if (DeviceIoControl(drive, FSCTL_ENUM_USN_DATA, &mft_enum_data, sizeof(mft_enum_data), buffer, BUFFER_SIZE, &bytecount, NULL)) {
              USN_RECORD *parent_record = (USN_RECORD *)((USN *)buffer + 1);
              if (parent_record->FileReferenceNumber == record->ParentFileReferenceNumber) {
                printf("Parent File:\n");
                printf("=================================================================\n");
                printf("FileName: %.*ls\n", parent_record->FileNameLength, (WCHAR *)(((BYTE *)parent_record) + parent_record->FileNameOffset));
              }
            }
          }
          record = (USN_RECORD *)(((BYTE *)record) + record->RecordLength);
        }
        mft_enum_data.StartFileReferenceNumber = nextid;
      } else {
        printf("FSCTL_ENUM_USN_DATA failed\n");
        break;
      }
      if (nextid == 0) break;
    }
    printf("Total Files: %lu\n", filecount);
  } else {
    printf("FSCTL_QUERY_USN_JOURNAL failed\n");
  }

  if (drive != INVALID_HANDLE_VALUE)
    CloseHandle(drive);

  return 0;
}</code>
Copy after login

The above is the detailed content of How can I efficiently detect file changes on an NTFS volume using the FSCTL_ENUM_USN_DATA function?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template