diff --git a/base/include/directory_ex.h b/base/include/directory_ex.h index 1686b24f1fa2d26be72123ed6a35ed88ac256e7c..7d4f2bdbed4eec2370a7e04a16c56d68980506ac 100644 --- a/base/include/directory_ex.h +++ b/base/include/directory_ex.h @@ -30,7 +30,6 @@ namespace OHOS { * program. */ std::string GetCurrentProcFullFileName(); -std::string GetCurrentProcFullFileName(); /** * @brief Get the absolute path of the current program. diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index fd8a366018cd079b483621e2bf5e73bdcf3480ea..b2cf1b6dd29d198b55fe8247ef1a72eb4995294d 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -14,6 +14,7 @@ */ #include "directory_ex.h" +#include #include #include #include "securec.h" @@ -192,16 +193,47 @@ bool IsEmptyFolder(const string& path) uint64_t GetFolderSize(const string& path) { - vector files; + std::queue que; + que.emplace(path); + struct stat statbuf = {0}; - GetDirFiles(path, files); uint64_t totalSize = 0; - for (auto& file : files) { - if (stat(file.c_str(), &statbuf) == 0) { - totalSize += statbuf.st_size; + string fileNameString; + string curDir; + + DIR *dir; + struct dirent *ptr; + + while (!que.empty()) { + curDir = que.front(); + que.pop(); + + dir = opendir(curDir.c_str()); + if (dir == nullptr) { + break; } - } + while (true) { + ptr = readdir(dir); + if (ptr == nullptr) { + break; + } + if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) { + continue; + } + + fileNameString = IncludeTrailingPathDelimiter(curDir) + string(ptr->d_name); + if (ptr->d_type == DT_DIR) { + que.emplace(fileNameString); + continue; + } + + if (stat(fileNameString.c_str(), &statbuf) == 0) { + totalSize += statbuf.st_size; + } + } + closedir(dir); + } return totalSize; } diff --git a/base/test/unittest/common/utils_directory_test.cpp b/base/test/unittest/common/utils_directory_test.cpp index d8d0f95d5201177123dc701f08ca484ec1f2fbfd..67140d02a4867c6cd503899230b35c4b569c72c6 100644 --- a/base/test/unittest/common/utils_directory_test.cpp +++ b/base/test/unittest/common/utils_directory_test.cpp @@ -243,6 +243,54 @@ HWTEST_F(UtilsDirectoryTest, testGetFolderSize001, TestSize.Level0) EXPECT_EQ(ret, true); } +/* + * @tc.name: testGetFolderSize002 + * @tc.desc: test for recursive GetFolderSize + */ +HWTEST_F(UtilsDirectoryTest, testGetFolderSize002, TestSize.Level0) +{ + string rootPath = GetCurrentProcPath() + "root/"; + string subPath1 = rootPath + "subDir1_1/"; + string subPath2 = rootPath + "subDir1_2/"; + string subPath3 = rootPath + "subDir1_1/subDir_2/subDir_3/"; + bool ret = ForceCreateDirectory(subPath3); + EXPECT_EQ(ret, true); + ret = ForceCreateDirectory(subPath2); + EXPECT_EQ(ret, true); + string txtIn1 = "This is a line.\n"; + string txtIn2 = "This is another line.\n"; + ofstream out1(subPath1 + "test1.txt"); + if (out1.is_open()) { + out1 << txtIn1.c_str(); + out1 << txtIn2.c_str(); + out1.close(); + } + ofstream out2(subPath2 + "test2.txt"); + if (out2.is_open()) { + out2 << txtIn1.c_str(); + out2.close(); + } + ofstream out3(subPath3 + "test3.txt"); + if (out3.is_open()) { + out3 << txtIn2.c_str(); + out3.close(); + } + uint64_t resultsize = GetFolderSize(rootPath); + uint64_t resultcomp = txtIn1.size() + txtIn2.size() + txtIn1.size() + txtIn2.size(); + EXPECT_EQ(resultsize, resultcomp); + + mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; + ret = ChangeModeFile(subPath1 + "test1.txt", mode); + EXPECT_EQ(ret, true); + + mode = S_IRUSR | S_IRGRP | S_IROTH; + ret = ChangeModeDirectory(rootPath, mode); + EXPECT_EQ(ret, true); + + ret = ForceRemoveDirectory(rootPath); + EXPECT_EQ(ret, true); +} + /* * @tc.name: testChangeModeFile001 * @tc.desc: test whether the folder exists