From 34c88cea32debdc307712d1a76113bf5bbac0332 Mon Sep 17 00:00:00 2001 From: xige-16 Date: Mon, 21 Nov 2022 17:47:17 +0800 Subject: [PATCH] Fix GetDiskUsedSize not work (#20474) Signed-off-by: xige-16 Signed-off-by: xige-16 --- .../core/src/storage/LocalChunkManager.cpp | 3 ++ .../unittest/test_local_chunk_manager.cpp | 34 +++++++++++++++++++ internal/querynode/cgo_helper.go | 4 +-- internal/util/indexcgowrapper/helper.go | 4 +-- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/internal/core/src/storage/LocalChunkManager.cpp b/internal/core/src/storage/LocalChunkManager.cpp index 6c79061210..e80f8875c8 100644 --- a/internal/core/src/storage/LocalChunkManager.cpp +++ b/internal/core/src/storage/LocalChunkManager.cpp @@ -202,6 +202,9 @@ LocalChunkManager::GetSizeOfDir(const std::string& dir) { if (boost::filesystem::is_regular_file(it->path())) { total_file_size += boost::filesystem::file_size(it->path()); } + if (boost::filesystem::is_directory(it->path())) { + total_file_size += GetSizeOfDir(it->path().string()); + } } return total_file_size; diff --git a/internal/core/unittest/test_local_chunk_manager.cpp b/internal/core/unittest/test_local_chunk_manager.cpp index c6fed3235a..7b47c67f62 100644 --- a/internal/core/unittest/test_local_chunk_manager.cpp +++ b/internal/core/unittest/test_local_chunk_manager.cpp @@ -229,3 +229,37 @@ TEST_F(LocalChunkManagerTest, ReadOffset) { exist = lcm.DirExist(path_prefix); EXPECT_EQ(exist, false); } + +TEST_F(LocalChunkManagerTest, GetSizeOfDir) { + auto& lcm = LocalChunkManager::GetInstance(); + auto path_prefix = lcm.GetPathPrefix(); + auto test_dir = path_prefix + "/" + "test_dir/"; + EXPECT_EQ(lcm.DirExist(test_dir), false); + lcm.CreateDir(test_dir); + EXPECT_EQ(lcm.DirExist(test_dir), true); + EXPECT_EQ(lcm.GetSizeOfDir(test_dir), 0); + + uint8_t data[] = {0x17, 0x32, 0x00, 0x34, 0x23, 0x23, 0x87, 0x98}; + // test get size of file in test_dir + auto file1 = test_dir + "file"; + auto res = lcm.CreateFile(file1); + EXPECT_EQ(res, true); + lcm.Write(file1, data, sizeof(data)); + EXPECT_EQ(lcm.GetSizeOfDir(test_dir), sizeof(data)); + lcm.Remove(file1); + auto exist = lcm.Exist(file1); + EXPECT_EQ(exist, false); + + // test get dir size with nested dirs + auto nest_dir = test_dir + "nest_dir/"; + auto file2 = nest_dir + "file"; + res = lcm.CreateFile(file2); + EXPECT_EQ(res, true); + lcm.Write(file2, data, sizeof(data)); + EXPECT_EQ(lcm.GetSizeOfDir(test_dir), sizeof(data)); + lcm.RemoveDir(test_dir); + + lcm.RemoveDir(path_prefix); + exist = lcm.DirExist(path_prefix); + EXPECT_EQ(exist, false); +} diff --git a/internal/querynode/cgo_helper.go b/internal/querynode/cgo_helper.go index 0bad1b612f..09cb968a3b 100644 --- a/internal/querynode/cgo_helper.go +++ b/internal/querynode/cgo_helper.go @@ -84,9 +84,9 @@ func GetCProtoBlob(cProto *C.CProto) []byte { func GetLocalUsedSize() (int64, error) { var availableSize int64 - cSize := C.int64_t(availableSize) + cSize := (*C.int64_t)(&availableSize) - status := C.GetLocalUsedSize(&cSize) + status := C.GetLocalUsedSize(cSize) err := HandleCStatus(&status, "get local used size failed") if err != nil { return 0, err diff --git a/internal/util/indexcgowrapper/helper.go b/internal/util/indexcgowrapper/helper.go index 3913e5bd8e..e774104f81 100644 --- a/internal/util/indexcgowrapper/helper.go +++ b/internal/util/indexcgowrapper/helper.go @@ -79,9 +79,9 @@ func HandleCStatus(status *C.CStatus, extraInfo string) error { func GetLocalUsedSize() (int64, error) { var availableSize int64 - cSize := C.int64_t(availableSize) + cSize := (*C.int64_t)(&availableSize) - status := C.GetLocalUsedSize(&cSize) + status := C.GetLocalUsedSize(cSize) err := HandleCStatus(&status, "get local used size failed") if err != nil { return 0, err