Repository: milvus-io/milvus
File: internal/core/src/storage/LocalChunkManager.cpp (C++)

Code before:
                      "remove local directory:{} failed, error: {}, files: {}",
                      dir,
                      err.message(),
                      files));
    }
}

// GetSizeOfDir is used to get the size of a directory, it will recursively
// get the size of all files and subdirectories in the directory.
// If file/directory doesn't exist, ignore it because it may be removed concurrently;
int64_t
LocalChunkManager::GetSizeOfDir(const std::string& dir) {
    boost::filesystem::path dirPath(dir);
    boost::system::error_code ec;
    bool is_dir = boost::filesystem::is_directory(dirPath, ec);
    if (ec) {
        if (ec.value() == boost::system::errc::no_such_file_or_directory) {
            return 0;
        }
        ThrowInfo(
            FileReadFailed,
            fmt::format(
                "check directory {} failed, error: {}", dir, ec.message()));
    }
    if (!is_dir) {
        ThrowInfo(FileReadFailed, dir + "is not a directory");
    }

    // Iterate directory entries directly without copying to vector
    boost::filesystem::directory_iterator it(dirPath, ec);
    if (ec) {
        if (ec.value() == boost::system::errc::no_such_file_or_directory) {
            return 0;
        }
        ThrowInfo(
            FileReadFailed,
            fmt::format(
                "iterate directory {} failed, error: {}", dir, ec.message()));
    }

    int64_t total_file_size = 0;
    boost::filesystem::directory_iterator end_it;
    for (; it != end_it; ++it) {
        const auto& path = it->path();

        // Check if current entry is a regular file
        boost::system::error_code path_ec;
        bool is_file = boost::filesystem::is_regular_file(path, path_ec);
        if (path_ec) {
            if (path_ec.value() ==
                boost::system::errc::no_such_file_or_directory) {

Review comment (suggestion):
Useless check.
If `is_dir`, it can still be deleted after checking.
if not `is_dir`, the next iterator will report error code.

Don't do redundant check for this TOCTOU scenairo

Code after:
                      "remove local directory:{} failed, error: {}, files: {}",
                      dir,
                      err.message(),
                      files));
    }
}

// GetSizeOfDir is used to get the size of a directory, it will recursively
// get the size of all files and subdirectories in the directory.
// If file/directory doesn't exist, ignore it because it may be removed concurrently;
int64_t
LocalChunkManager::GetSizeOfDir(const std::string& dir) {
    boost::filesystem::path dirPath(dir);
    boost::system::error_code it_ec;
    boost::filesystem::directory_iterator it(dirPath, it_ec);
    if (it_ec) {
        if (it_ec.value() == boost::system::errc::no_such_file_or_directory) {
            return 0;
        }
        ThrowInfo(FileReadFailed,
                  fmt::format("iterate directory {} failed, error: {}",
                              dir,
                              it_ec.message()));
    }

    int64_t total_file_size = 0;
    boost::filesystem::directory_iterator end_it;
    for (; it != end_it; ++it) {
        boost::system::error_code status_ec;
        auto status = it->status(status_ec);
        if (status_ec) {
            if (status_ec.value() ==
                boost::system::errc::no_such_file_or_directory) {
                continue;
            }
            ThrowInfo(FileReadFailed,
                      fmt::format("get status of {} failed, error: {}",
                                  it->path().string(),
                                  status_ec.message()));
        }

        // Check if current entry is a regular file
        if (boost::filesystem::is_regular_file(status)) {
            boost::system::error_code file_size_ec;
            auto file_size =
                boost::filesystem::file_size(it->path(), file_size_ec);
            if (file_size_ec) {
                if (file_size_ec.value() ==
                    boost::system::errc::no_such_file_or_directory) {
                    continue;
                }