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

相关文章

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

C#如何去掉文件夹或文件名非法字符

《C#如何去掉文件夹或文件名非法字符》:本文主要介绍C#如何去掉文件夹或文件名非法字符的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#去掉文件夹或文件名非法字符net类库提供了非法字符的数组这里还有个小窍门总结C#去掉文件夹或文件名非法字符实现有输入字

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1

通过cmd获取网卡速率的代码

《通过cmd获取网卡速率的代码》今天从群里看到通过bat获取网卡速率两段代码,感觉还不错,学习bat的朋友可以参考一下... 1、本机有线网卡支持的最高速度:%v%@echo off & setlocal enabledelayedexpansionecho 代码开始echo 65001编码获取: >