How to acquire ext2 partition size?

This is a discussion on How to acquire ext2 partition size? within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I'm writing a program to make images from CompactFlash cards and vice versa. With the following code I get ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    7

    How to acquire ext2 partition size?

    Hello,

    I'm writing a program to make images from CompactFlash cards and vice versa. With the following code I get the list of available devices:

    Code:
    void win32_get_drives()
    {
    	HANDLE cf_card;
    	DWORD rc, dwError, dwRead;
    	char devices[100000];
    	char drive[MAX_PATH + 1];
    	char drive_full[MAX_PATH + 1 + 10];
    	char drive_to_open[MAX_PATH + 1 + 8];
    	char lpBuf[BUF_SIZE + 1];
    	char *tmp_str, *idx;
    	
    	cout << "Available devices:" << endl << "\t";
    
    	rc = QueryDosDevice(NULL, devices, sizeof(devices));
    	if(rc == 0)
    	{
    		dwError = GetLastError();
    		cout << "Error querying devices (" << dwError << ")" << endl;
    		exit(0);
    	}
    
    	tmp_str = devices;
    	while(1)
    	{
    
    		idx = strstr(tmp_str, "PhysicalDrive");	
    		if(idx == NULL)
    		{
    			if(tmp_str[1] == '\0')
    				break;
    			tmp_str += strlen(tmp_str) + 1;
    			continue;
    		}
    
    		strcpy(drive, idx);
    
    		sprintf(drive_full, "\\\\.\\%s\\", drive);
    		rc = GetDriveType(drive_full);
    		if(rc == DRIVE_REMOVABLE)
    		{
    			// open CompactFlash card
    			sprintf(drive_to_open, "\\\\.\\%s", drive);
    			cf_card = CreateFile(drive_to_open,  
    						GENERIC_READ,
    						0, 
    						NULL, 
    						OPEN_EXISTING,
    						FILE_ATTRIBUTE_NORMAL,
    						NULL);
    
    			if (cf_card != INVALID_HANDLE_VALUE)
    			{
    				rc = ReadFile(cf_card,lpBuf,BUF_SIZE,&dwRead,NULL);
    				if(rc) 
    					cout << drive;
    
    				CloseHandle(cf_card);
    			}
    		}
    
    		tmp_str += strlen(tmp_str) + 1;
    	}
    
    	cout << endl;
    }
    How can I acquire the size of the ext2 partition? My program can only handle 512Mb cards.

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    Why are you asking about ext2 on a Windows forum?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    7
    Because I need a program that works under Windows. For Linux I don't even need to write a program. It is everything there in the system utilities.

    In Windows it is a little bit complicated with device name and so on. But I need such a program for our customers that runs under Windows.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Why should you care about ext2 if you're making an image of the disk/card? Just read the size of the disk, and image the entire thing.

    If you must only image a single partition (how many flash cards have more than one partition? Although I've never seen a flash card with ext2 on it either...) then you'll need to read & parse the partition table - search google or wikipedia to find a specification.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    7
    I've found it. It is IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS (http://msdn2.microsoft.com/en-US/library/aa365194.aspx). With this IOCTL I can get the size of the CF card. See the code:

    Code:
    void small_test(char * DeviceName)
    {
    	HANDLE file;
    	VOLUME_DISK_EXTENTS OutBuffer;
    	DWORD OutBufferSize = sizeof(OutBuffer);
    	DWORD BytesReturned;
    	
    
    	file = CreateFile(DeviceName,  
    						GENERIC_READ, 
    						FILE_SHARE_READ,  
    						NULL, 
    						OPEN_EXISTING,
    						FILE_ATTRIBUTE_NORMAL,
    						NULL);
    
    
    	if (DeviceIoControl(file,IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &OutBuffer, OutBufferSize, &BytesReturned, NULL))
    	{
    		cout << "O.K.\n";
    		cout << "NumberOfDisExtents: " << OutBuffer.NumberOfDiskExtents << endl;
    		cout << "DiskNumber: " << OutBuffer.Extents[0].DiskNumber << endl; 
    		cout << "ExtentLength: " << OutBuffer.Extents[0].ExtentLength.QuadPart << endl; 
    		cout << "StartingOffset: " << OutBuffer.Extents[0].StartingOffset.QuadPart << endl; 
    	}
    	else
    	{
    		cout << "FAILED\n";
    	}
    
    	CloseHandle(file);
    }
    Best regards,
    pupkin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  3. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  4. File Size and File Size on Disk
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 07:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21