Code:
// sector_checked_cluster_used_original.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "sector_checked_cluster_used_original.h"
#include <WinIoCtl.h>
#include <stdio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CWinApp theApp;
using namespace std;
DWORD BytesReturned,dwBytesReturnedh;
int i=0;
UINT32 *BitmapDetail;
DWORD dwError;
LPDWORD lpBytesReturned;
int iCopy_Count=0;
LONGLONG llCountUsed=0;
LONGLONG llCountFree=0;
LONGLONG llTotalCount=0;
LONGLONG llVolume_Size=106928640; //volume size
LONGLONG llChunk_Size=512; // chunk size
LONGLONG Rest_Size=llVolume_Size;
LPVOID bBuffer1 = VirtualAlloc(NULL,512,MEM_COMMIT,PAGE_READWRITE);
LPVOID bBuffer2 = VirtualAlloc(NULL,512,MEM_COMMIT,PAGE_READWRITE);
BOOL IsClusterUsed (UINT64 Cluster)
{
return ((BitmapDetail[Cluster / 32] & (1 << (Cluster % 32))) ? TRUE : FALSE);
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
HANDLE h_MyDrive_Src,h_MyDrive_Dest,hDevice;
DWORD dwRetBytes1,dwRetBytes2;
hDevice = CreateFile( _T("\\\\.\\PhysicalDrive0"),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hDevice==INVALID_HANDLE_VALUE)
{
printf("\nUnable to Open the Device: Error Number is : %d\n",GetLastError());
}
//Source partition
h_MyDrive_Src = CreateFile( L"\\\\.\\N:",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,
NULL);
if (h_MyDrive_Src==INVALID_HANDLE_VALUE)
{
printf("\nUnable to Open the Device: Error Number : %d\n",GetLastError());
}
//Destination partition -NTFS- 2GB
h_MyDrive_Dest=CreateFile( L"\\\\.\\O:",
GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,
NULL);
if (h_MyDrive_Dest==INVALID_HANDLE_VALUE)
{
printf("\nUnable to Open the Device: Error Number:%d\n",GetLastError());
}
//Getting the Bitmap of Volume
STARTING_LCN_INPUT_BUFFER StartingLCN;
VOLUME_BITMAP_BUFFER *Bitmap = NULL;
UINT32 BitmapSize;
DWORD BytesReturned1;
BOOL Result;
ULONGLONG ClusterCount = 0;
StartingLCN.StartingLcn.QuadPart = 0;
BitmapSize = sizeof (VOLUME_BITMAP_BUFFER) + 4;
Bitmap = (VOLUME_BITMAP_BUFFER *) malloc (BitmapSize);
Result = DeviceIoControl(
h_MyDrive_Src,
FSCTL_GET_VOLUME_BITMAP,
&StartingLCN,
sizeof (StartingLCN),
Bitmap,
BitmapSize,
&BytesReturned1,
NULL);
if (Result == FALSE &&GetLastError()!= ERROR_MORE_DATA)
{
free (Bitmap);
goto label12;
}
ClusterCount = Bitmap->BitmapSize.QuadPart -
Bitmap->StartingLcn.QuadPart; //TOT noof clusters
cout<<"\n Tot no of clusters :"<<ClusterCount<<"\n";
BitmapSize = sizeof (VOLUME_BITMAP_BUFFER) + (Bitmap->BitmapSize.QuadPart / 8) + 1;
Bitmap = (VOLUME_BITMAP_BUFFER *) realloc (Bitmap, BitmapSize);
Result = DeviceIoControl(
h_MyDrive_Src,
FSCTL_GET_VOLUME_BITMAP,
&StartingLCN,
sizeof (StartingLCN),
Bitmap,
BitmapSize,
&BytesReturned,
NULL);
DWORD LastError = GetLastError ();
if (Result == FALSE)
{
printf ("\nCouldn't properly read volume bitmap\n");
free (Bitmap);
goto label12;
}
BitmapDetail = (UINT32 *) malloc (sizeof(UINT32) * (1 + (ClusterCount / 32)));
memcpy (BitmapDetail, Bitmap->Buffer, sizeof(UINT32) * (1 + (ClusterCount / 32)));
UINT64 Max;
UINT64 cc;
Max=ClusterCount;
UINT64 usedcluster=0;
for (cc = 0; cc < Max; cc++)
{
if(!ReadFile(h_MyDrive_Src,bBuffer1,llChunk_Size,&dwRetBytes1,0))
{
printf("\nUnable to Read the Drive Error: %d\n",GetLastError());
}
if ( IsClusterUsed (cc)) // return 1 from buffer
{
bBuffer2=bBuffer1; // copied the read data to another buffer
llCountUsed++;
printf("\n used %I64d",llCountUsed);
llTotalCount++;
if(!WriteFile(h_MyDrive_Dest,bBuffer2,llChunk_Size,&dwRetBytes2,0))
{
printf("\nUnable to write the Drive Error: %d\n",GetLastError());
}
}
else
{
llCountFree++;
llTotalCount++;
printf("\n free %I64d",llCountFree);
}
}
cout<<"\n Total no of clusters :"<<llTotalCount<<"\n";
cout<<"\n Used no of clusters :"<<llCountUsed<<"\n";
cout<<"\n Free no of clusters :"<<llCountFree<<"\n";
/*
//lock source
BOOL bResult_unlockkk = DeviceIoControl(h_MyDrive_Src2,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(!bResult_unlockkk)
{
printf("Lock Volume Failed1 %d \n",GetLastError());
}
//lock desti
BOOL bResult_unlock111 = DeviceIoControl(h_MyDrive_Dest2,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(!bResult_unlock111)
{
printf("Lock Volume Failed2 %d \n",GetLastError());
}
while(rest_size>0)
{
if(rest_size<chunk_size)
{
LPVOID bMBR2 = VirtualAlloc(NULL,rest_size,MEM_COMMIT,PAGE_READWRITE);
chunk_size=rest_size;
goto label;
}
label:
if(!ReadFile(h_MyDrive_Src2, bMBR2,chunk_size, &dwRetBytes1,0)) //1 cluster
{
printf("\nUnable to Read the Drive with Error Number is : %d\n",GetLastError());
}
if(!WriteFile(h_MyDrive_Dest2,bMBR2,chunk_size,&dwRetBytes2,0))
{
printf("\nUnable to write the Drive with Error Number is : %d\n",GetLastError());
}
FlushFileBuffers(h_MyDrive_Src2);
FlushFileBuffers(h_MyDrive_Dest2);
cout<<"\n"<<cnt++;
rest_size=rest_size-chunk_size;
}
//------------------------------------------------------
//unlock source
bResult_unlock = DeviceIoControl(h_MyDrive_Src2,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(!bResult_unlock)
{
printf("UnLock Volume Failed %d \n",GetLastError());
}
//unlock desti
bResult_unlock1 = DeviceIoControl(h_MyDrive_Dest2,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(!bResult_unlock1)
{
printf("UnLock Volume Failed %d \n",GetLastError());
}
*/
CloseHandle(h_MyDrive_Src); //close source
CloseHandle(h_MyDrive_Dest);
BOOL bupdate= DeviceIoControl(
hDevice,
IOCTL_DISK_UPDATE_PROPERTIES,
NULL,NULL,NULL,NULL,&dwBytesReturnedh,NULL);
CloseHandle(hDevice);
label12:
cout<<"\n Press any key to continue...........\n\n";
char c=getchar();
}
return nRetCode;
}
--------------------------------------------------------------------------------
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum