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

相关文章

如何利用Java获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

Java通过反射获取方法参数名的方式小结

《Java通过反射获取方法参数名的方式小结》这篇文章主要为大家详细介绍了Java如何通过反射获取方法参数名的方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、前言2、解决方式方式2.1: 添加编译参数配置 -parameters方式2.2: 使用Spring的内部工具类 -

Java如何获取视频文件的视频时长

《Java如何获取视频文件的视频时长》文章介绍了如何使用Java获取视频文件的视频时长,包括导入maven依赖和代码案例,同时,也讨论了在运行过程中遇到的SLF4J加载问题,并给出了解决方案... 目录Java获取视频文件的视频时长1、导入maven依赖2、代码案例3、SLF4J: Failed to lo

使用Java实现获取客户端IP地址

《使用Java实现获取客户端IP地址》这篇文章主要为大家详细介绍了如何使用Java实现获取客户端IP地址,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 首先是获取 IP,直接上代码import org.springframework.web.context.request.Requ

C++实现获取本机MAC地址与IP地址

《C++实现获取本机MAC地址与IP地址》这篇文章主要为大家详细介绍了C++实现获取本机MAC地址与IP地址的两种方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实际工作中,项目上常常需要获取本机的IP地址和MAC地址,在此使用两种方案获取1.MFC中获取IP和MAC地址获取

C/C++通过IP获取局域网网卡MAC地址

《C/C++通过IP获取局域网网卡MAC地址》这篇文章主要为大家详细介绍了C++如何通过Win32API函数SendARP从IP地址获取局域网内网卡的MAC地址,感兴趣的小伙伴可以跟随小编一起学习一下... C/C++通过IP获取局域网网卡MAC地址通过win32 SendARP获取MAC地址代码#i

5分钟获取deepseek api并搭建简易问答应用

《5分钟获取deepseekapi并搭建简易问答应用》本文主要介绍了5分钟获取deepseekapi并搭建简易问答应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需... 目录1、获取api2、获取base_url和chat_model3、配置模型参数方法一:终端中临时将加

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

Python脚本实现自动删除C盘临时文件夹

《Python脚本实现自动删除C盘临时文件夹》在日常使用电脑的过程中,临时文件夹往往会积累大量的无用数据,占用宝贵的磁盘空间,下面我们就来看看Python如何通过脚本实现自动删除C盘临时文件夹吧... 目录一、准备工作二、python脚本编写三、脚本解析四、运行脚本五、案例演示六、注意事项七、总结在日常使用