数据库内核

热衷于分享开源数据库使用、内核源码等相关技术,包括但不限于 MySQL/PostgreSQL/Greenplum/TiDB 等。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  58 随笔 :: 0 文章 :: 13 评论 :: 88744 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

本文首发于 2014-07-10 10:00:41

代码

#include <stdio.h>
#include <stdlib.h>
#include <glob.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define PATHSIZE 1024

static int path_noloop(const char *path)
{
    char *pos;

    pos = strrchr(path,'/');//定位最右边的'/'的位置

    if(strcmp(pos+1,".") == 0 || (strcmp(pos+1,"..") == 0))
        return 0;
    return 1;

}

static int64_t mydu(const char *path)
{
    int i;
    glob_t globres;
    int64_t sum;
    static struct stat statres;
    static char nextpath[PATHSIZE];

    if(lstat(path, &statres) < 0)
    {
        perror("lstat()");
        return 0;//exit(1);
    }

    if(!S_ISDIR(statres.st_mode))
        return statres.st_blocks;

    strncpy(nextpath, path,PATHSIZE);
    strncat(nextpath, "/*" , PATHSIZE);
    glob(nextpath,GLOB_NOSORT, NULL, &globres);

    strncpy(nextpath, path,PATHSIZE);
    strncat(nextpath, "/.*" , PATHSIZE);
    glob(nextpath,GLOB_NOSORT|GLOB_APPEND, NULL, &globres);

    sum = statres.st_blocks;

    for(i = 0 ;i < globres.gl_pathc ; i++)
    {
        if(path_noloop(globres.gl_pathv[i]))
            sum += mydu(globres.gl_pathv[i]);
    }

    return sum;
}

int main(int argc,char **argv)
{
    if(argc < 2)
    {
        fprintf(stderr,"Usage...\n");
        exit(1);
    }
    printf("%lld 512B blocks\n", (long long int)mydu(argv[1]));
    return 0;
}

编译

$ gcc -g -Wall testdu.c -o testdu

运行

  • testdf执行效果:
$ ./testdu /usr/bin
1766184 512B blocks
  • 原生df执行效果:
$ du -sh /usr/bin
859M	/usr/bin

欢迎关注我的微信公众号【数据库内核】:分享主流开源数据库和存储引擎相关技术。

MySQL数据库技术
posted on   DBKernel  阅读(64)  评论(0编辑  收藏  举报
编辑推荐:
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
阅读排行:
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· 开发的设计和重构,为开发效率服务
· 从零开始开发一个 MCP Server!
点击右上角即可分享
微信分享提示