diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2025-02-19 15:35:42 +0100 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2025-02-21 14:46:30 +0100 |
commit | 14b7a5aff05509f0e7544972b6cbcf53840a542f (patch) | |
tree | 2a5f015e5202c16efc3ee23a5c991508552ba295 | |
parent | c1acb20b0f444683b5c2fe21b17aa712b00e445d (diff) | |
download | justbuild-14b7a5aff05509f0e7544972b6cbcf53840a542f.tar.gz |
FileSystemManager: Use IncrementalReader
-rw-r--r-- | src/buildtool/file_system/TARGETS | 1 | ||||
-rw-r--r-- | src/buildtool/file_system/file_system_manager.hpp | 40 |
2 files changed, 23 insertions, 18 deletions
diff --git a/src/buildtool/file_system/TARGETS b/src/buildtool/file_system/TARGETS index 5b8378d0..a55a900c 100644 --- a/src/buildtool/file_system/TARGETS +++ b/src/buildtool/file_system/TARGETS @@ -48,6 +48,7 @@ , ["src/buildtool/logging", "logging"] , ["src/buildtool/system", "system"] , ["src/utils/cpp", "expected"] + , ["src/utils/cpp", "incremental_reader"] , ["src/utils/cpp", "path"] ] , "stage": ["src", "buildtool", "file_system"] diff --git a/src/buildtool/file_system/file_system_manager.hpp b/src/buildtool/file_system/file_system_manager.hpp index 47f39ee9..aa151b7a 100644 --- a/src/buildtool/file_system/file_system_manager.hpp +++ b/src/buildtool/file_system/file_system_manager.hpp @@ -42,6 +42,7 @@ #include <functional> #include <optional> #include <string> +#include <string_view> #include <system_error> #include <unordered_set> #include <utility> @@ -55,6 +56,7 @@ #include "src/buildtool/logging/logger.hpp" #include "src/buildtool/system/system.hpp" #include "src/utils/cpp/expected.hpp" +#include "src/utils/cpp/incremental_reader.hpp" #include "src/utils/cpp/path.hpp" namespace detail { @@ -702,28 +704,30 @@ class FileSystemManager { file.string()); return std::nullopt; } + + auto const to_read = IncrementalReader::FromFile(kChunkSize, file); + if (not to_read.has_value()) { + Logger::Log(LogLevel::Debug, + "FileSystemManager: failed to create reader for {}\n{}", + file.string(), + to_read.error()); + return std::nullopt; + } + try { - std::string chunk{}; std::string content{}; - chunk.resize(kChunkSize); - std::ifstream file_reader(file.string(), std::ios::binary); - if (file_reader.is_open()) { - auto ssize = gsl::narrow<std::streamsize>(chunk.size()); - while (file_reader.good()) { - file_reader.read(chunk.data(), ssize); - auto count = file_reader.gcount(); - if (count == ssize) { - content += chunk; - } - else { - content += - chunk.substr(0, gsl::narrow<std::size_t>(count)); - } + content.reserve(to_read->GetContentSize()); + for (auto chunk : *to_read) { + if (not chunk.has_value()) { + Logger::Log(LogLevel::Error, + "reading file failed {}:\n{}", + file.string(), + chunk.error()); + return std::nullopt; } - file_reader.close(); - return content; + content.append(*chunk); } - return std::nullopt; + return content; } catch (std::exception const& e) { Logger::Log(LogLevel::Error, "reading file {}:\n{}", |