Link to home
Start Free TrialLog in
Avatar of Vertigo
Vertigo

asked on

Getting cluster sizes in FAT32

For part of my program, I need to find out how many bytes per cluster are used on any of the disks.  So far, I have used the Win32 API function GetDiskFreeSpace(), which worked well under normal versions of Windows95.  Then, I upgraded my computer and now have Windows95 OSR2 release, which means I have a FAT32 hard disk that is over 2GB and uses 4k clusters.  When I realized the results from the program were incorrect, I checked the online help, and found that GetDiskFreeSpace() does not work on OSR2 machines.   It lists a replacement for it, GetDiskFreeSpaceEx(), but this only returns the number of bytes free, and not the sectors and cluster information, meaning it's useless for my purposes.  

Can you please help me get the bytes per cluster information in any way that I can?  I can either use a different method only for OSR2, or I can just use it all the time.  Please Help!
Avatar of jaba
jaba

Look into knowledge base , article "Description of FAT32 File System" , Article ID: Q154997
It describing FAT32 file system. In this article you can read - cluster size in FAT 32 is 4k. What why you should to use GetDiskFreeSpaceEx in you programms.

/--------------------------------------------------------------------------------------
Remarks

Note that the values obtained by this function are of type ULARGE_INTEGER. Be careful not to truncate these values to 32 bits.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Windows 95 OSR 2: 

The GetDiskFreeSpaceEx function is available on Windows 95 systems beginning with OEM Service Release 2 (OSR 2).
/--------------------------------------------------------------------------------------
Avatar of Vertigo

ASKER

If you also happen to notice on this page, it says:
-------------
(that is, 4K clusters for drives up to 8 GB in size)
-------------
and OSR2 supports drives of up to 2 terrabytes.   While most people would not have drives over 8GB, I don't want to limit it to that.  
Load the boot sector of the hard disk (normally the first sector).

Here is the format for the beginning of the boot sector:

JMP 3 bytes
OEM 8 bytes
;Start of Bios Parameter block
Sector Size 1 word
Cluster Size 1 byte

You don't need the rest of the boot sector.

The cluster size is in sectors. The Sector size is in bytes.
So, size of a Cluster in bytes would be:

Cluster Size(bytes)=Cluster Size(sectors) * Sector Size(bytes)

A byte is 8 bits, a word is 16 bits.
In C++ or C, a byte would be the equivilant of a char,
             a word would be the equivilant of an int.
Avatar of Vertigo

ASKER

How do I get this information??
You need to use a function that will read sectors from a disk. In C++ this function is biosdisk for physical sectors, and something like absread/write for logical sectors. If this is too over your head, there are normally some functions included with your compiler that will read this information for you without doing all the disk editing.
Avatar of Vertigo

ASKER

By the way, I am using Visual C++ 5.0.  
have you tried Get_ExtFreeSpace

[Windows 95 only.]
A structure used by Int 21h Function 7303h Get_ExtFreeSpace (FAT32). The structure contains information about the drive's total disk space and free disk space.
This structure is implemented in Windows OEM Service Release 2 and later.
ExtGetDskFreSpcStruc STRUC
    ExtFree_Size                      DW    ?
    ExtFree_Level                     DW    ?
    ExtFree_SectorsPerCluster         DD    ?
    ExtFree_BytesPerSector            DD    ?
    ExtFree_AvailableClusters         DD    ?
    ExtFree_TotalClusters             DD    ?
    ExtFree_AvailablePhysSectors      DD    ?
    ExtFree_TotalPhysSectors          DD    ?
    ExtFree_AvailableAllocationUnits  DD    ?
    ExtFree_TotalAllocationUnits      DD    ?
    ExtFree_Rsvd                      DD    2 DUP (?)
ExtGetDskFreSpcStruc ENDS
 
Members
ExtFree_Size
A return value that represents the size of the ExtGetDskFreSpcStruc structure, in bytes. The Get_ExtFreeSpace sub-function returns the size value to this field. This member is used only as a return value.
ExtFree_Level
An input and return level value. This must be initialized to zero.
ExtFree_SectorsPerCluster
The number of sectors per cluster.
ExtFree_BytesPerSector
The number of bytes per sector.
ExtFree_AvailClusters
The number of available clusters.
ExtFree_TotalClusters
The total number of clusters on the drive.
ExtFree_AvailablePhysSectors
The number of physical sectors available on the drive, without adjustment for compression.
ExtFree_TotalPhysSectors
The total number of physical sectors on the drive, without adjustment for compression.
ExtFree_AvailableAllocationUnits
The number of available allocation units on the drive, without adjustment for compression.
ExtFree_TotalAllocationUnits
The total number of allocation units on the drive, without adjustment for compression.
ExtFree_Rsvd
Reserved field.

Avatar of Vertigo

ASKER

That would provide the information I need..however I have no idea how to use that in a program.  I am not very familer with using this type of thing, and even less so under Windows.. Do you know of any example of something like this I could look at?
[Windows 95 only.]
Returns the total disk space and the free disk space.
mov  dx, seg Buffer
mov  es, dx
mov  di, offset Buffer    ;See below
mov  es:[di].ExtGetDskFreSpcStruc.ExtFree_Level, ExpectLevel
mov  cx, BufferSize       ;See below
 
mov  dx, seg DriveName    ;See below
mov  ds, dx
mov  dx, offset DriveName ;See below

mov  ax, 7303h            ;Get_ExtFreeSpace
int  21h

jc  error_handler         ;carry set means error

Parameters
Buffer
The address of the buffer at ES:DI that will receive the disk space information.
BufferSize
The size (in bytes) of the buffer.
DriveName
The address of a null terminated string at DS:DX. In real mode, this must be in standard form ("C:\"). In Windows, either the standard name or a universal naming convention form ("\\Server\Share") is acceptable.
Return Values
Clears the carry flag and returns the total disk space and the free disk space of the specified drive, in the form of an ExtGetDskFreSpcStruc (FAT32) structure, to a buffer at ES:DI. It is recommended to initialize the buffer to the level value the application expects to receive.

Avatar of Vertigo

ASKER

Can you give me any more help about actually using this in a C++ program...  I have no idea how to get at any of the information that is mentioned.  The important information I need is bytes per sector and sectors per cluster.  

ASKER CERTIFIED SOLUTION
Avatar of cph
cph

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial