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

相关文章

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介