一个碎片整理程序的实现

申明: 代码是网上收集的,原则上只供学习使用

//====================================================================<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

//

// Defrag.h

//

// Copyright (C) 1997 Mark Russinovich

//

// Header file for defragmentation demonstration program. This file

// includes definitions for defragmentation File System Control

// commands, as well as the undocumented NtFsControl call.

//

//====================================================================

//--------------------------------------------------------------------

// D E F I N E S

//--------------------------------------------------------------------

//

// File System Control commands related to defragging

//

#define FSCTL_READ_MFT_RECORD 0x90068

#define FSCTL_GET_VOLUME_BITMAP 0x<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="9006" unitname="F">9006F</chmetcnv>

#define FSCTL_GET_RETRIEVAL_POINTERS 0x90073

#define FSCTL_MOVE_FILE 0x90074

//

// return code type

//

typedef UINT NTSTATUS;

//

// Error codes returned by NtFsControlFile (see NTSTATUS.H)

//

#define STATUS_SUCCESS ((NTSTATUS)0x<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="0" unitname="l">00000000L</chmetcnv>)

#define STATUS_BUFFER_OVERFLOW ((NTSTATUS)0x<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="80000005" unitname="l">80000005L</chmetcnv>)

#define STATUS_INVALID_PARAMETER ((NTSTATUS)0xC<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="0" unitname="dl">000000DL</chmetcnv>)

#define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="23" unitname="l">0000023L</chmetcnv>)

#define STATUS_ALREADY_COMMITTED ((NTSTATUS)0xC<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="21" unitname="l">0000021L</chmetcnv>)

#define STATUS_INVALID_DEVICE_REQUEST ((NTSTATUS)0xC<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="10" unitname="l">0000010L</chmetcnv>)

//--------------------------------------------------------------------

// F S C T L S P E C I F I C T Y P E D E F S

//--------------------------------------------------------------------

//

// This is the definition for a VCN/LCN (virtual cluster/logical cluster)

// mapping pair that is returned in the buffer passed to

// FSCTL_GET_RETRIEVAL_POINTERS

//

typedef struct {

ULONGLONG Vcn;

ULONGLONG Lcn;

} MAPPING_PAIR, *PMAPPING_PAIR;

//

// This is the definition for the buffer that FSCTL_GET_RETRIEVAL_POINTERS

// returns. It consists of a header followed by mapping pairs

//

typedef struct {

ULONG NumberOfPairs;

ULONGLONG StartVcn;

MAPPING_PAIR Pair[1];

} GET_RETRIEVAL_DESCRIPTOR, *PGET_RETRIEVAL_DESCRIPTOR;

//

// This is the definition of the buffer that FSCTL_GET_VOLUME_BITMAP

// returns. It consists of a header followed by the actual bitmap data

//

typedef struct {

ULONGLONG StartLcn;

ULONGLONG ClustersToEndOfVol;

BYTE Map[1];

} BITMAP_DESCRIPTOR, *PBITMAP_DESCRIPTOR;

//

// This is the definition for the data structure that is passed in to

// FSCTL_MOVE_FILE

//

typedef struct {

HANDLE FileHandle;

ULONG Reserved;

LARGE_INTEGER StartVcn;

LARGE_INTEGER TargetLcn;

ULONG NumVcns;

ULONG Reserved1;

} MOVEFILE_DESCRIPTOR, *PMOVEFILE_DESCRIPTOR;

//--------------------------------------------------------------------

// N T F S C O N T R O L F I L E D E F I N I T I O N S

//--------------------------------------------------------------------

//

// Prototype for NtFsControlFile and data structures

// used in its definition

//

//

// Io Status block (see NTDDK.H)

//

typedef struct _IO_STATUS_BLOCK {

NTSTATUS Status;

ULONG Information;

} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;

//

// Apc Routine (see NTDDK.H)

//

typedef VOID (*PIO_APC_ROUTINE) (

PVOID ApcContext,

PIO_STATUS_BLOCK IoStatusBlock,

ULONG Reserved

);

//

// The undocumented NtFsControlFile

//

// This function is used to send File System Control (FSCTL)

// commands into file system drivers. Its definition is

// in ntdll.dll (ntdll.lib), a file shipped with the NTDDK.

//

NTSTATUS (__stdcall *NtFsControlFile)(

HANDLE FileHandle,

HANDLE Event, // optional

PIO_APC_ROUTINE ApcRoutine, // optional

PVOID ApcContext, // optional

PIO_STATUS_BLOCK IoStatusBlock,

ULONG FsControlCode,

PVOID InputBuffer, // optional

ULONG InputBufferLength,

PVOID OutputBuffer, // optional

ULONG OutputBufferLength

);

//====================================================================

//

// Defrag.c

//

// Copyright (C) 1997 Mark Russinovich

//

// This program demonstrates the use of NT 4.0 FAT and NTFS cluster

// movement File System Control functions.

//

//====================================================================

#include <windows.h>

#include <stdio.h>

#include <conio.h>

#include "defrag.h"

//--------------------------------------------------------------------

// D E F I N E S

//--------------------------------------------------------------------

//

// Interval at which output is paused (in lines)

//

#define PAUSEINTERVAL 24

//

// Size of the buffer we read file mapping information into.

// The buffer is big enough to hold the 16 bytes that

// come back at the head of the buffer (the number of entries

// and the starting virtual cluster), as well as 512 pairs

// of [virtual cluster, logical cluster] pairs.

//

#define FILEMAPSIZE (512+2)

//

// Size of the bitmap buffer we pass in. Its large enough to

// hold information for the 16-byte header that's returned

// plus the indicated number of bytes, each of which has 8 bits

// (imagine that!)

//

#define BITMAPBYTES 4096

#define BITMAPSIZE (BITMAPBYTES+2*sizeof(ULONGLONG))

//

// Invalid longlong number

//

#define LLINVALID ((ULONGLONG) -1)

//--------------------------------------------------------------------

// G L O B A L S

//--------------------------------------------------------------------

//

// Handle for the raw volume that was opened

//

HANDLE VolumeHandle;

//

// Buffer to read file mapping information into

//

ULONGLONG FileMap[ FILEMAPSIZE ];

//

// Buffer thats passed to bitmap function

//

BYTE BitMap[ BITMAPSIZE ];

//

// Bit shifting array for efficient processing of the bitmap

//

BYTE BitShift[] = { 1, 2, 4, 8, 16, 32, 64, 128 };

//--------------------------------------------------------------------

// F U N C T I O N S

//--------------------------------------------------------------------

//--------------------------------------------------------------------

//

// PrintNtError

//

// Translates an NTDLL error code into its text equivalent. This

// only deals with ones commonly returned by defragmenting FS Control

// commands.

//--------------------------------------------------------------------

void PrintNtError( NTSTATUS Status )

{

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值