Luc*_*lon 6 c++ unix linux macos posix
我正在编写一个跨平台的应用程序,我需要总的可用磁盘空间.对于posix系统(Linux和Macos),我使用的是statvfs.我创建了这个C++方法:
long OSSpecificPosix::getFreeDiskSpace(const char* absoluteFilePath) {
struct statvfs buf;
if (!statvfs(absoluteFilePath, &buf)) {
unsigned long blksize, blocks, freeblks, disk_size, used, free;
blksize = buf.f_bsize;
blocks = buf.f_blocks;
freeblks = buf.f_bfree;
disk_size = blocks*blksize;
free = freeblks*blksize;
used = disk_size - free;
return free;
}
else {
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我得到了一些我无法理解的奇怪价值观.例如:f_blocks = 73242188 f_bsize = 1048576 f_bfree = 50393643 ...
这些值是以位,字节还是其他形式存在的?我在这里读的stackoverflow那些应该是字节,但后来我得到的总字节数是:f_bsize*f_bfree = 1048576*50393643但这意味着49212.542GB ......太多了......
我是否对代码或其他任何内容做错了?谢谢!
I don't know OSX well enough to predict this is definitely the answer, but f_blocks and f_bfree actually refer to "fundamental blocks", or "fragments" (which are of size buf.f_frsize bytes), not the "filesystem block size" (which is buf.f_bsize bytes):
http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/statvfs.h.html
f_bsize is just a hint what the preferred size is for I/O operations, it's not necessarily anything to do with how the filesystem is divided.