Solved

Getting cluster sizes in FAT32

Posted on 1997-07-27
11
564 Views
Last Modified: 2013-12-04
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!
0
Comment
Question by:Vertigo
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 3

Expert Comment

by:jaba
Comment Utility
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).
/--------------------------------------------------------------------------------------
0
 

Author Comment

by:Vertigo
Comment Utility
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.  
0
 
LVL 1

Expert Comment

by:mosfet
Comment Utility
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.
0
 

Author Comment

by:Vertigo
Comment Utility
How do I get this information??
0
 
LVL 1

Expert Comment

by:mosfet
Comment Utility
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Vertigo
Comment Utility
By the way, I am using Visual C++ 5.0.  
0
 
LVL 4

Expert Comment

by:emmons
Comment Utility
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.

0
 

Author Comment

by:Vertigo
Comment Utility
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?
0
 
LVL 4

Expert Comment

by:emmons
Comment Utility
[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.

0
 

Author Comment

by:Vertigo
Comment Utility
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.  

0
 
LVL 1

Accepted Solution

by:
cph earned 200 total points
Comment Utility
Well, in front of the code written by emmons, you just have to type the keyword asm.

It will look like this:
asm {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
}

Put this in a function or where ever you want...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
Are you looking to recover an email message or a contact you just deleted mistakenly? Or you are searching for a contact that you erased from your MS Outlook ‘Contacts’ folder and now realized that it was important.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now