查找目录的大小 [英] Finding the size of a directory
本文介绍了查找目录的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是这样一个函数的伪代码:跟随一个递归方法请告诉我是否还有其他方法。
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 ++;
}
}
}
}
解决方案
使用nftw的典型例子:
请注意,随着面试问题的出现,他们可能会希望看到您考虑
- 遍历顺序
- 权限(无法访问的子文件夹等)
- ondisk vs.表面大小
- 符号链接,硬链接(树外)重复计数?
- 稀疏文件
- 表现
以下代码以务实的方式解决了大部分问题:
。
#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 uncessible = 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 ++;打破;
case FTW_NS:
case FTW_SL:
case FTW_SLN:symlinks ++;打破;
case FTW_DNR:不可访问++;打破;
case FTW_F:files ++;打破;
}
total + = sb-> st_size;
blocks512 + = sb-> st_blocks;
return 0; / *告诉nftw()继续* /
}
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(总大小:%7jd\\\
,总计);
printf(在%jd文件和%jd目录(%jd符号链接和%jd不可访问的目录)\\\
,文件,目录,符号链接,不可访问);
printf(磁盘上的大小%jd * 512b =%jd\\\
,blocks512,blocks512< 9);
exit(EXIT_SUCCESS);
}
这是张贴为以前获得目录大小和大小的最快方法。典型输出:
总大小: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屋!
查看全文