Closed Thread
Results 1 to 3 of 3

Thread: Duplicate partition makes problems?

  1. #1
    krishnampkkm is offline Newbie
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Duplicate partition makes problems?

    Hii

    I duplicated my NTFS partition.And in that partition some of the folders is not opening " .While opening the message comes "File or directory is corrupted and and unreadable".

    Here in my application I use the control code"FSCTL_GET_VOLUME_BITMAP " to get the cluster usage(cluster is used or free) details.And I'm copying only used clusters from my source partition to destination partition.

    And I think the files in the duplicated partition not getting the link among them correctly.
    Because I am copying the used clusters only so the free space in the source partition is discarded.So I've re-initialize the link...?

    Can I do that one...???
    How to do that....?
    Where I've to make change...? In the volume boot record of that duplicated volume..?

    Thanking you
    Krish

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Duplicate partition makes problems?

    I think a bug in your program blew up the NTFS system. Could you post your code?
    sudo rm -rf /

  4. #3
    krishnampkkm is offline Newbie
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Re: Duplicate partition makes problems?

    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;
    }
    --------------------------------------------------------------------------------

    Here in my application I'm not relocationg MFT;just copied the used clusters onlyyy...
    I've to reallocate the MFT contents....?

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. moving duplicate lines.
    By csharpit in forum C# Programming
    Replies: 10
    Last Post: 09-13-2011, 04:06 AM
  2. Duplicate SQL insert [CI]
    By Whitey in forum PHP Development
    Replies: 0
    Last Post: 08-10-2011, 02:24 PM
  3. Help finding duplicate digits
    By skastu01 in forum C and C++
    Replies: 1
    Last Post: 08-03-2010, 09:55 PM
  4. duplicate select
    By techker in forum PHP Development
    Replies: 15
    Last Post: 02-22-2009, 04:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts