找到一个目录的大小 [英] Finding the size of a directory

查看:121
本文介绍了找到一个目录的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个问题在思科的采访:编写一个函数来找到一个目录的大小

以下是伪code代表这样的功能,即如下的递归方法。请告诉我,如果有可以是任意其他的方法也。

  INT directorySize(DirectoryHandle DH)
{
    INT大小= 0;
    如果(!DH)
    {
        DirectoryHandle DH1 = directoryOpen(目录路径);
    }
    其他
    {
        DH1 = DH;
    }

    而(DH1)
    {
        如果(TRUE = IsDirectory(DH1))
        {
            大小+ = directorysize(DH1);
        }
        否则,如果(TRUE == ISFILE(DH1))
        {
            的FileHandle FH = DH1;
            而(EOF!= FH)
            {
                尺寸++;
            }
        }
    }
}
 

解决方案

使用nftw的典型的例子:

请注意,由于面试问题去,他们可能会希望看到你想

  • 遍历顺序
  • 许可(无法访问子文件夹等)
  • 在大小ondisk与外观尺寸
  • 符号链接,硬链接(外树?重复计数?)
  • 稀疏文件
  • 性能

下面code并解决大多数问题,以务实的方式:

 的#define _XOPEN_SOURCE 500
#包括< ftw.h>
#包括< stdio.h中>
#包括< stdlib.h中>
#包括< stdint.h>

静态uintmax_t型总= 0ul;
静态uintmax_t型文件= 0ul;
静态uintmax_t型目录= 0ul;
静态uintmax_t型符号链接= 0ul;
静态uintmax_t型人迹罕至= 0ul;
静态uintmax_t型blocks512 = 0ul;

静态INT
display_info(为const char * fpath,常量结构统计* SB,
             INT tflag,结构FTW * ftwbuf)
{
    开关(tflag)
    {
        案例FTW_D:
        案例FTW_DP:目录++;打破;
        案例FTW_NS:
        案例FTW_SL:
        案例FTW_SLN:符号链接++;打破;
        案例FTW_DNR:人迹罕至++;打破;
        案例FTW_F:文件++;打破;
    }
    共有+ = SB-> st_size;
    blocks512 + = SB->的st_blocks;
    返回0; / *告诉nftw()继续* /
}

INT
主(INT ARGC,字符* argv的[])
{
    诠释标志= FTW_DEPTH | FTW_MOUNT | FTW_PHYS;

    如果(nftw((argc个2)?:的argv [1],display_info,20,标志)==  -  1)
    {
        PERROR(nftw);
        出口(EXIT_FAILURE);
    }

    的printf(总大小:%7jd \ N,总);
    的printf(在%JD文件和%JD目录(%JD符号链接和%JD无法访问的目录)\ N,文件,目录,符号链接,无法访问);
    的printf(磁盘大小%JD * 512B =%JD \ N,blocks512,blocks512<< 9);

    出口(EXIT_SUCCESS);
}
 

这是张贴<一个href="http://stackoverflow.com/questions/7533786/fastest-ways-to-get-a-directory-size-and-size-on-disk/7534792#7534792">Fastest之前的方法获取目录的大小和磁盘大小。典型的输出:

 总大小:28433001733
在878794的文件和87047目录(73318符号链接和0人迹罕至的目录)
磁盘大小59942192 * 512B = 30690402304
 

I got this question in a Cisco interview: write a function to find the size of a directory?

Following is the pseudocode for such a function, that follows a recursive approach. Please tell me if there can be any other approach also.

int directorySize(DirectoryHandle dh)
{
    int size=0;
    if (!dh)
    {
        DirectoryHandle dh1 = directoryOpen("Directory_path");
    }
    else
    {
        dh1 = dh;
    }

    while (dh1)
    {
        if (TRUE=IsDirectory(dh1))
        {
            size += directorysize(dh1);
        }
        else if (TRUE == IsFile(dh1))
        {
            FileHandle fh = dh1;
            while (EOF != fh)
            {
                size++;
            }
        }
    }
}

解决方案

Canonical example of using nftw:

Note that as interview questions go, they will probably want to see you thinking about

  • traversal order
  • permission (inaccessible subfolder etc.)
  • size ondisk vs. apparent size
  • symlinks, hardlinks (outside the tree? duplicate counting?)
  • sparse files
  • performance

The following code does address most of these issues in a pragmatic fashion:

.

#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

static uintmax_t total        = 0ul;
static uintmax_t files        = 0ul;
static uintmax_t directories  = 0ul;
static uintmax_t symlinks     = 0ul;
static uintmax_t inaccessible = 0ul;
static uintmax_t blocks512    = 0ul;

static int
display_info(const char *fpath, const struct stat *sb,
             int tflag, struct FTW *ftwbuf)
{
    switch(tflag)
    {
        case FTW_D:
        case FTW_DP:  directories++;  break;
        case FTW_NS:
        case FTW_SL:
        case FTW_SLN: symlinks++;     break;
        case FTW_DNR: inaccessible++; break;
        case FTW_F:   files++;        break;
    }
    total += sb->st_size;
    blocks512 += sb->st_blocks;
    return 0; /* To tell nftw() to continue */
}

int
main(int argc, char *argv[])
{
    int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;

    if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags) == -1)
    {
        perror("nftw");
        exit(EXIT_FAILURE);
    }

    printf("Total size: %7jd\n", total);
    printf("In %jd files and %jd directories (%jd symlinks and %jd inaccessible directories)\n", files, directories, symlinks, inaccessible);
    printf("Size on disk %jd * 512b = %jd\n", blocks512, blocks512<<9);

    exit(EXIT_SUCCESS);
}

This was posted as Fastest ways to get a directory Size and Size on disk before. Typical output:

Total size: 28433001733
In 878794 files and 87047 directories (73318 symlinks and 0 inaccessible directories)
Size on disk 59942192 * 512b = 30690402304

这篇关于找到一个目录的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆