本文主要是介绍libc 获取文件/文件夹/存储设备 size,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
libc获取文件大小
用stat系统调用即可strcut stat
st_size代表文件size
st_mode文件类型与文件权限
#include <sys/stat.h>DESCRIPTION
The <sys/stat.h> header shall define the structure of the data
returned by the functions fstat(), lstat(), and stat().The stat structure shall contain at least the following members:
struct stat
{
dev_t st_dev Device ID of device containing file.
ino_t st_ino File serial number.
mode_t st_mode Mode of file (see below).
nlink_t st_nlink Number of hard links to the file.
uid_t st_uid User ID of file.
gid_t st_gid Group ID of file. dev_t st_rdev Device ID (if file is character or block special). off_t st_size For regular files, the file size in bytes. For symbolic links, the length in bytes of the pathname contained in the symbolic link.
[SHM]For a shared memory object, the length in bytes. [TYM]For a typed memory object, the length in bytes. For other file types, the use of this field is unspecified.
time_t st_atime Time of last access.
time_t st_mtime Time of last data modification.
time_t st_ctime Time of last status change. blksize_t st_blksize A file system-specific preferred I/O block size for this object. In some file system types, this may vary from file to file.
blkcnt_t st_blocks Number of blocks allocated for this object.
}The following symbolic names for the values of type mode_t shall
also be defined.File type:S_IFMT
Type of file.
S_IFBLK
Block special.
S_IFCHR
Character special.
S_IFIFO
FIFO special.
S_IFREG
Regular.
S_IFDIR
Directory.
S_IFLNK
Symbolic link.
S_IFSOCK
Socket.
File mode bits:S_IRWXU
Read, write, execute/search by owner.
S_IRUSR
Read permission, owner.
S_IWUSR
Write permission, owner.
S_IXUSR
Execute/search permission, owner.
S_IRWXG
Read, write, execute/search by group.
S_IRGRP
Read permission, group.
S_IWGRP
Write permission, group.
S_IXGRP
Execute/search permission, group.
S_IRWXO
Read, write, execute/search by others.
S_IROTH
Read permission, others.
S_IWOTH
Write permission, others.
S_IXOTH
Execute/search permission, others.
S_ISUID
Set-user-ID on execution.
S_ISGID
Set-group-ID on execution.
S_ISVTX
On directories, restricted deletion flag.
libc获取文件夹大小
文件夹的大小如果用如上方法size位4096,要想获取文件夹以及子目录所有文件的大小要遍历整个子目录并统计。可以使用函数ftw(),man手册定义与使用方法如下
ftw - traverse (walk) a file tree#include <ftw.h>int ftw(const char *path, int (*fn)(const char *,const struct stat *ptr, int flag), int ndirs);DESCRIPTION
The ftw() function recursively descends the directory hierarchy
rooted in path. For each object in the hierarchy, ftw() calls
the function pointed to by fn, passing it a pointer to a
null-terminated character string containing the name of
the object, a pointer to a stat structure containing
information about the object, and an integer. Possible
values of the integer, defined in the <ftw.h> header, are:
FTW_D
For a directory.
FTW_DNR
For a directory that cannot be read.
FTW_F
For a file.
FTW_SL
For a symbolic link (but see also FTW_NS below).
FTW_NS
For an object other than a symbolic link on which stat() could
not successfully be executed. If the object is a symbolic link
and stat() failed, it is unspecified whether ftw() passes
FTW_SL or FTW_NS to the user-supplied function.
If the integer is FTW_DNR, descendants of that directory will
not be processed. If the integer is FTW_NS, the stat structure
will contain undefined values. An example of an object that
would cause FTW_NS to be passed to the function pointed to by
fn would be a file in a directory with read but without execute
(search) permission.The ftw() function visits a directory before visiting any of its descendants.The ftw() function uses at most one file descriptor for each level in the tree.The argument ndirs should be in the range of 1 to {OPEN_MAX}.The tree traversal continues until the tree is exhausted, an invocation of
fn returns a non-zero value, or some error, other than [EACCES], is
detected within ftw().The ndirs argument specifies the maximum number of directory streams
or file descriptors or both available for use by ftw() while traversing
the tree. When ftw() returns it closes any directory streams and
file descriptors it uses not counting any opened by the
application-supplied fn() function.
static unsigned int total = 0;int sum(const char *fpath, const struct stat *sb, int typeflag) {total += sb->st_size;return 0;
}static unsigned long DirSize(const char* path)
{if (ftw(path, &sum, 1)) {printf("ftw error\n");return 0;} return total;
}
除了以上两种情况之外有时候还需要获取挂载的存储设备的size,f_blocks代表总块数量,f_avail代表空闲块数量。path为同一个设备下的任何目录都可以获得该设备的设备的各种size
sys/statvfs.h - VFS File System information structure#include <sys/statvfs.h> DESCRIPTION
The <sys/statvfs.h> header shall define the statvfs structure that includes
at least the following members:struct statvfs
{
unsigned long f_bsize File system block size.
unsigned long f_frsize Fundamental file system block size.
fsblkcnt_t f_blocks Total number of blocks on file systemin units of f_frsize.
fsblkcnt_t f_bfree Total number of free blocks.
fsblkcnt_t f_bavail Number of free blocks available to non-privileged process.
fsfilcnt_t f_files Total number of file serial numbers.
fsfilcnt_t f_ffree Total number of free file serial numbers.
fsfilcnt_t f_favail Number of file serial numbers available to non-privileged process.
unsigned long f_fsid File system ID.
unsigned long f_flag Bit mask of f_flag values.
unsigned long f_namemax Maximum filename length.
}
int ret;struct statvfs vfs;state = statvfs(PATH,&vfs);if(ret < 0){ printf("read error!!!\n");} block_size = vfs.f_bsize;total_size = vfs.f_blocks * block_size;free_size = vfs.f_bfree * block_size;used_size = (vfs.f_blocks - vfs.f_bavail) * block_size;avail_size = vfs.f_bavail * block_size;
这篇关于libc 获取文件/文件夹/存储设备 size的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!