I am trying to use statvfs to find free space on the file system.
Here is the code:
const char* Connection::getDiskInfo()
{
struct statvfs vfs;
int nRet = statvfs( "/u0", &vfs );
if( nRet ) return NULL;
char* pOut = (char*)malloc( 256 );
memset( pOut, 0, 256 );
sprintf( pOut, "<disk letter='%s' total='%lu' free='%lu' totalfree='%lu'/>",
"/", ( vfs.f_bsize * vfs.f_blocks ) / ( 1024 * 1024 ),
( vfs.f_bsize * vfs.f_bavail ) / ( 1024 * 1024 ),
( vfs.f_bsize * vfs.f_bfree ) / ( 1024 * 1024 ));
return pOut;
}
In the debugger (NetBeans 6.9), I see the corresponding values for the statvfs struct:
f_bavail = 105811542
f_bfree = 111586082
f_blocks = 111873644
f_bsize = 4096
this should give me total = 437006, but my result insists that the sum = 2830. It is clear that I am doing something ignorant in my formatting or math.
If I add the line:
unsigned long x = ( vfs.f_bsize * vfs.f_blocks );
x is evaluated to 2967912448, while the debugger shows me the corresponding values (see above).
: Linux version 2.6.18-194.17.1.el5PAE
i386
I read other entries here referring to this function, and they make it pretty simple. So where am I going astray?
source
share