You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UNIX环境下C语言如何获取目录磁盘块总数?stat函数可行吗?

关于ls -l中total值的获取与stat函数的使用

首先纠正一个小误解:你以为ls -l开头的total #是目录本身占用的磁盘块总数,其实不是——它是该目录下所有**非./..的文件(包括隐藏文件)**所占用的磁盘块总和,而且这个数值是以512字节为单位统计的(POSIX标准规定,和文件系统实际块大小无关)。

回到你的问题:可以用stat系列函数来获取这个总和,但不能直接通过目录的stat结果得到,你需要遍历目录下的每个文件,对每个文件调用stat()或lstat(),累加它们的st_blocks字段,最终得到的就是total值。

关键细节说明

  • 为什么用lstat()而非stat()?
    ls -l默认不会跟随符号链接去统计目标文件的块数,而是统计符号链接本身的存储块数,所以用lstat()更准确;如果你需要模拟ls -lL(跟随链接)的行为,才用stat()。
  • 必须跳过.和..
    这两个特殊目录是当前目录和父目录的引用,统计它们会导致结果错误,一定要在遍历中排除。
  • st_blocks的单位
    POSIX标准规定st_blocks以512字节为单位,所以累加后的结果和ls -l输出的total数值完全一致,不需要额外转换。

简单代码示例

#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>

int main(int argc, char *argv[]) {
    DIR *dir_stream;
    struct dirent *dir_entry;
    struct stat file_stats;
    off_t total_blocks = 0;
    char full_path[PATH_MAX];

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <target-directory>\n", argv[0]);
        return 1;
    }

    dir_stream = opendir(argv[1]);
    if (!dir_stream) {
        perror("Failed to open directory");
        return 1;
    }

    while ((dir_entry = readdir(dir_stream)) != NULL) {
        // Skip current and parent directory entries
        if (strcmp(dir_entry->d_name, ".") == 0 || strcmp(dir_entry->d_name, "..") == 0) {
            continue;
        }

        // Build full path to the file
        snprintf(full_path, sizeof(full_path), "%s/%s", argv[1], dir_entry->d_name);
        
        // Get file stats (use lstat to handle symlinks correctly)
        if (lstat(full_path, &file_stats) == -1) {
            perror("Failed to get file stats");
            continue;
        }

        total_blocks += file_stats.st_blocks;
    }

    closedir(dir_stream);
    printf("total %lld\n", (long long)total_blocks);
    return 0;
}

这段代码会遍历指定目录,累加所有符合条件的文件的磁盘块数,输出结果和ls -l的total完全一致。

内容的提问来源于stack exchange,提问作者noel880

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:58:15