include <sys/statfs.h> or <sys/vfs.h>
int GetStorageInfo(char * MountPoint, //SD卡随便一个分区 int *Capacity, // 想要获取的空间大小 int type) //获取什么类型的空间 { struct statfs statFS; //系统stat的结构体 U64 usedBytes = 0; U64 freeBytes = 0; U64 totalBytes = 0; U64 endSpace = 0; if (statfs(MountPoint, &statFS) == -1){ //获取分区的状态 printf("statfs failed for path->[%s]\n", MountPoint); return(-1); } totalBytes = (U64)statFS.f_blocks * (U64)statFS.f_frsize; //详细的分区总容量, 以字节为单位 freeBytes = (U64)statFS.f_bfree * (U64)statFS.f_frsize; //详细的剩余空间容量,以字节为单位 usedBytes = (U64)(totalBytes - freeBytes); //详细的使用空间容量,以字节为单位 switch( type ) { case 1: endSpace = totalBytes/1024; //以KB为单位的总容量 break; case 2: endSpace = usedBytes/1024; //以KB为单位的使用空间 break; case 3: endSpace = freeBytes/1024; //以KB为单位的剩余空间 break; default: return ( -1 ); } *Capacity = endSpace; //这个不用说了吧 return 0; }
ref:Here
沒有留言:
張貼留言