查找目录的大小

发布于 2024-12-08 13:55:25 字数 587 浏览 1 评论 0原文

我在思科面试时遇到这样的问题:编写一个函数来查找目录的大小?

以下是此类函数的伪代码,它遵循递归方法。请告诉我是否还有其他方法。

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++;
            }
        }
    }
}

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++;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光礼记 2024-12-15 13:55:25

使用 nftw 的典型示例:

请注意,随着面试问题的进行,他们可能希望看到您考虑

  • 遍历顺序
  • 许可(不可访问的子文件夹等)
  • 磁盘上的大小与表观大小
  • 符号链接,硬链接(树外?重复计数?)
  • 稀疏文件
  • 性能

以下代码确实以务实的方式解决了大多数这些问题:

.

#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);
}

这被发布为最快的方法获取目录大小和磁盘上的大小之前。典型输出:

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

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
因为看清所以看轻 2024-12-15 13:55:25

也许为大型文件集添加更多空间和更好的子目录导航。

long DirectoryLength(DirectoryInfo dir)
{
    long size = 0;
    foreach (FileInfo file in dir.GetFiles())
        size += file.Length;

    foreach (DirectoryInfo sub in dir.GetDirectories())
        size += DirectoryLength(sub);

    return size;
}

Maybe adding more room for large file sets and a better subdirectory navigation.

long DirectoryLength(DirectoryInfo dir)
{
    long size = 0;
    foreach (FileInfo file in dir.GetFiles())
        size += file.Length;

    foreach (DirectoryInfo sub in dir.GetDirectories())
        size += DirectoryLength(sub);

    return size;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文