在手机终端开发的时候,我们需要关注手机剩余空间,因为手机不像电脑一样空间宽裕,当设备空间比较少得时候需要释放空间.
用法:先引入头文件
#include <sys/param.h>
#include <sys/mount.h>
代码片段:
+ (long long) freeDiskSpaceInBytesByMB{
struct statfs buf;
long long freespace = -1;
if(statfs("/var", &buf) >= 0){
freespace = (long long)(buf.f_bsize * buf.f_bfree);
}
return freespace/1024/1024;
}
先来说说statfs这个东东,这是C语言函数,下面是官方的介绍:
#if __DARWIN_64_BIT_INO_T
struct statfs __DARWIN_STRUCT_STATFS64;
#else /* !__DARWIN_64_BIT_INO_T */
/*
* LP64 - WARNING - must be kept in sync with struct user_statfs in mount_internal.h.
*/
struct statfs {
short f_otype; /* TEMPORARY SHADOW COPY OF f_type */
short f_oflags; /* TEMPORARY SHADOW COPY OF f_flags */
long f_bsize; /* fundamental file system block size */
long f_iosize; /* optimal transfer block size */
long f_blocks; /* total data blocks in file system */
long f_bfree; /* free blocks in fs */
long f_bavail; /* free blocks avail to non-superuser */
long f_files; /* total file nodes in file system */
long f_ffree; /* free file nodes in fs */
fsid_t f_fsid; /* file system id */
uid_t f_owner; /* user that mounted the filesystem */
short f_reserved1; /* spare for later */
short f_type; /* type of filesystem */
long f_flags; /* copy of mount exported flags */
long f_reserved2[2]; /* reserved for future use */
char f_fstypename[MFSNAMELEN]; /* fs type name */
char f_mntonname[MNAMELEN]; /* directory on which mounted */
char f_mntfromname[MNAMELEN];/* mounted filesystem */
char f_reserved3; /* For alignment */
long f_reserved4[4]; /* For future use */
};
#endif /* __DARWIN_64_BIT_INO_T */