I have a target board running linux which has approximately around 5 million+ files in the directory. (This directory doesn't have any sub directories) If i execute this program it takes several minutes to get the total space information. Is there a faster way to accomplish this? Thanks
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/stat.h>
#include <errno.h>
void calcSpace(char *path, long long int *totalSpace)
{
DIR *dir; /* dir structure we are reading */
struct dirent *ent; /* directory entry currently being processed */
char absPath[200];
struct stat statbuf; /* buffer for stat()*/
long long int fileCount=0;
fprintf(stderr, "Opening dir %s\n", path);
dir = opendir(path);
if(NULL == dir) {
perror(path);
return;
}
while((ent = readdir(dir)))
{
fileCount++;
sprintf(absPath, "%s/%s", path, ent->d_name);
if(stat(absPath, &statbuf)) {
perror(absPath);
return;
}
*totalSpace= (*totalSpace) + statbuf.st_size;
}
fprintf(stderr, "Closing dir %s\n", path);
printf("fileCount=%lld.\n", fileCount);
closedir(dir);
}
int main(int argc, char *argv[])
{
char *dir;
long long int totalSpace=0;
if(argc > 1)
dir = argv[1];
else
dir = ".";
calcSpace(dir, &totalSpace);
printf("totalSpace=%lld\n", totalSpace);
return 0;
}
ftw
to avoid extra call tostat
. it would also be a bit helpful to avoid dereferencing*totalSpace
on each iteration – Pointedsystem ("du -s")
after changing to the directory (orpopen
)? – Clearancedirent
does not contain the file size, even thoughreaddir
reads the directory entry, whch contains the file size. So there is no way to prevent the exta call tostat
. Unless you would modifyreaddir
anddirent
to contain the file size, which could be possible as this is an embedded board with probably its own library implementation. – Dillyreaddir
and customize it in this way. – Yearchdir
, no dereferencing or even profiling won't help because the cost are in the calls to readdir and stat. – Dilly