libdrv icon indicating copy to clipboard operation
libdrv copied to clipboard

Refactor: Extract common IRP initialization logic in CopyFile.cpp

Open Copilot opened this issue 2 months ago • 0 comments

Eliminated duplicate IRP allocation and initialization code across file I/O operations.

Changes

  • New helper: KfcAllocateAndInitializeIrp consolidates common setup (event binding, thread context, request mode)
  • Refactored functions: KfcGetFileStandardInformation, KfcRead, KfcWrite, KfcSetFileAllocation
  • Net impact: -71 lines of duplication, -12 total lines

Before

static VOID KfcRead(...) {
    KEVENT event{};
    PDEVICE_OBJECT fsdDevice = IoGetRelatedDeviceObject(FileObject);
    KeInitializeEvent(&event, SynchronizationEvent, FALSE);
    PIRP irp = IoAllocateIrp(fsdDevice->StackSize, FALSE);
    if (!irp) { /* error handling */ }
    
    irp->UserEvent = &event;
    irp->UserIosb = IoStatusBlock;
    irp->Tail.Overlay.Thread = PsGetCurrentThread();
    irp->Tail.Overlay.OriginalFileObject = FileObject;
    irp->RequestorMode = KernelMode;
    irp->Flags = IRP_READ_OPERATION;
    // ... operation-specific setup
}

After

static VOID KfcRead(...) {
    KEVENT event{};
    KeInitializeEvent(&event, SynchronizationEvent, FALSE);
    PIRP irp = KfcAllocateAndInitializeIrp(FileObject, &event, IoStatusBlock);
    if (!irp) { /* error handling */ }
    
    irp->Flags = IRP_READ_OPERATION;
    // ... operation-specific setup
}

All original behavior preserved including buffer initialization ordering.

Original prompt

Find and refactor duplicated code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Dec 11 '25 12:12 Copilot