开发者

statvfs issue - printf / debugger

Im trying to use statvfs to find free space on a file system.

Here's the code:

const char* Connection::getDiskInfo()
{
struct statvfs vfs;
int nRet = sta开发者_运维问答tvfs( "/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 appropriate 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 output insists that total=2830. Clearly Im doing something ignorant in my formatting or math.

If I add the line:

unsigned long x = ( vfs.f_bsize * vfs.f_blocks );

x evaluates to 2967912448 while the debugger shows me the appropriate values (see above).

system: Linux version 2.6.18-194.17.1.el5PAE

i386

I've read the other entries here referring to this function and they make it seem pretty straightforward. So where did I go astray?


What is the size of fsblkcnt_t? If it's 32-bit then it's an overflow problem and you simply need to temporarily use a 64-bit size during the calculation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜