Fix GetDiskUsedSize not work (#20474)

Signed-off-by: xige-16 <xi.ge@zilliz.com>

Signed-off-by: xige-16 <xi.ge@zilliz.com>
pull/20749/head
xige-16 2022-11-21 17:47:17 +08:00 committed by GitHub
parent 15badd0263
commit 34c88cea32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 4 deletions

View File

@ -202,6 +202,9 @@ LocalChunkManager::GetSizeOfDir(const std::string& dir) {
if (boost::filesystem::is_regular_file(it->path())) { if (boost::filesystem::is_regular_file(it->path())) {
total_file_size += boost::filesystem::file_size(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; return total_file_size;

View File

@ -229,3 +229,37 @@ TEST_F(LocalChunkManagerTest, ReadOffset) {
exist = lcm.DirExist(path_prefix); exist = lcm.DirExist(path_prefix);
EXPECT_EQ(exist, false); 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);
}

View File

@ -84,9 +84,9 @@ func GetCProtoBlob(cProto *C.CProto) []byte {
func GetLocalUsedSize() (int64, error) { func GetLocalUsedSize() (int64, error) {
var availableSize int64 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") err := HandleCStatus(&status, "get local used size failed")
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -79,9 +79,9 @@ func HandleCStatus(status *C.CStatus, extraInfo string) error {
func GetLocalUsedSize() (int64, error) { func GetLocalUsedSize() (int64, error) {
var availableSize int64 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") err := HandleCStatus(&status, "get local used size failed")
if err != nil { if err != nil {
return 0, err return 0, err