不精确的取目录大小
1 #include <stdlib.h> 2 #include <sys/stat.h> 3 #include <dirent.h> 4 5 6 void GetDirSize(const char* dirname, int64_t* size) { 7 ::DIR* dp = opendir(dirname); 8 if (dp) { 9 struct ::dirent* entry; 10 struct ::stat statbuf; 11 char entry_path[1024] = {0}; 12 while ((entry = readdir(dp)) != NULL){ 13 memset(entry_path, 0 , 1024); 14 sprintf(entry_path, "%s/%s", dirname, entry->d_name); 15 lstat(entry_path, &statbuf); 16 if (S_ISDIR(statbuf.st_mode)){ 17 if (strcmp(".", entry->d_name) && 18 strcmp("..", entry->d_name)){ 19 GetDirSize(entry_path, size); 20 } 21 } 22 *size += statbuf.st_size; 23 }// while 24 } 25 else { 26 std::cout << __FILE__ <<"::"<< __LINE__ <<"::"<< __FUNCTION__ << std::endl; 27 std::cout << "Open dir:" <<dirname << " faild" << std::endl; 28 _exit(EXIT_FAILURE); 29 } 30 }