CFileHelpers: rework methode getDirSize(), makes it work recursively

Origin commit data
------------------
Commit: a185ad34d4
Author: Thilo Graf <dbt@novatux.de>
Date: 2015-02-07 (Sat, 07 Feb 2015)
This commit is contained in:
2015-02-07 18:43:30 +01:00
parent 65900171d0
commit 5977418188

View File

@@ -724,28 +724,35 @@ bool CFileHelpers::removeDir(const char *Dir)
u_int64_t CFileHelpers::getDirSize(const char *dirname) u_int64_t CFileHelpers::getDirSize(const char *dirname)
{ {
DIR *d; DIR *dir;
struct dirent *de; char fullDirName[500];
struct stat buf; struct dirent *dirPnt;
int exists; struct stat cur_file;
u_int64_t total_size = 0; uint64_t total_size = 0;
d = opendir(dirname); //open current dir
if (d == NULL) { sprintf(fullDirName, "%s/", dirname);
perror("prsize"); if((dir = opendir(fullDirName)) == NULL) {
fprintf(stderr, "Couldn't open %s\n", fullDirName);
return 0; return 0;
} }
for (de = readdir(d); de != NULL; de = readdir(d)) { //go through the directory
exists = stat(de->d_name, &buf); while( (dirPnt = readdir(dir)) != NULL ) {
if (exists < 0) { if(strcmp((*dirPnt).d_name, "..") == 0 || strcmp((*dirPnt).d_name, ".") == 0)
fprintf(stderr, "Couldn't stat %s\n", de->d_name); continue;
} else {
total_size += buf.st_size;
}
}
closedir(d); //create current filepath
sprintf(fullDirName, "%s/%s", dirname, (*dirPnt).d_name);
if(stat(fullDirName, &cur_file) == -1)
continue;
if(cur_file.st_mode & S_IFREG) //file...
total_size += cur_file.st_size;
else if(cur_file.st_mode & S_IFDIR) //dir...
total_size += getDirSize(fullDirName);
}
closedir(dir);
return total_size; return total_size;
} }