linux opendir计算目录大小
u64 PuOsGetDirSize(const char *pchDir)
{
u64 qwTotalSize=0;
struct stat statbuf;
struct dirent *dirp;
DIR *dp;
if(NULL==(dp=opendir(pchDir)))
{
return 0;
}
lstat(pchDir,&statbuf);
qwTotalSize+=statbuf.st_size;
while(NULL!=(dirp=readdir(dp)))
{
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
continue;
char aszTmpdir[PUMAX_STR256_LEN]={0};
sprintf(aszTmpdir, "%s/%s", pchDir, dirp->d_name);
lstat(aszTmpdir, &statbuf);
if(S_ISDIR(statbuf.st_mode))
{
u64 qwsubDirSize = PuOsGetDirSize(aszTmpdir);
qwTotalSize+=qwsubDirSize;
}
else if(S_ISREG(statbuf.st_mode))
{
qwTotalSize+=statbuf.st_size;
}
else
{
PuOsPrintf( PU_LOGLEVEL_SYSTEM, TRUE, PU_MODULE_OSAPI,"PuOsGetDirSize not regular file\n");
continue;
}
}
closedir(dp);
return qwTotalSize;
}