libc 获取文件/文件夹/存储设备 size

2023-11-30 12:48

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/437088

相关文章

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

Java发送SNMP至交换机获取交换机状态实现方式

《Java发送SNMP至交换机获取交换机状态实现方式》文章介绍使用SNMP4J库(2.7.0)通过RCF1213-MIB协议获取交换机单/多路状态,需开启SNMP支持,重点对比SNMPv1、v2c、v... 目录交换机协议SNMP库获取交换机单路状态获取交换机多路状态总结交换机协议这里使用的交换机协议为常

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

C#使用iText获取PDF的trailer数据的代码示例

《C#使用iText获取PDF的trailer数据的代码示例》开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所... 目录引言iText 核心概念C# 代码示例步骤 1: 确保已安装 iText步骤 2: C# 代码程

Spring Boot中获取IOC容器的多种方式

《SpringBoot中获取IOC容器的多种方式》本文主要介绍了SpringBoot中获取IOC容器的多种方式,包括直接注入、实现ApplicationContextAware接口、通过Spring... 目录1. 直接注入ApplicationContext2. 实现ApplicationContextA

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Python获取浏览器Cookies的四种方式小结

《Python获取浏览器Cookies的四种方式小结》在进行Web应用程序测试和开发时,获取浏览器Cookies是一项重要任务,本文我们介绍四种用Python获取浏览器Cookies的方式,具有一定的... 目录什么是 Cookie?1.使用Selenium库获取浏览器Cookies2.使用浏览器开发者工具

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +