eeerahul
eeerahul

Reputation: 1627

Finding the size of a directory

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

Upvotes: 2

Views: 850

Answers (2)

Rubens Farias
Rubens Farias

Reputation: 57956

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

Upvotes: 1

sehe
sehe

Reputation: 393114

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

Upvotes: 2

Related Questions