[wpiutil] Return wpi::expected from MemoryBuffer::GetFile (#7069)

This commit is contained in:
Joseph Eng
2024-09-12 23:45:35 -07:00
committed by GitHub
parent d44b651558
commit 1f3ef019ce
15 changed files with 87 additions and 103 deletions

View File

@@ -351,15 +351,14 @@ void NetworkServer::HandleLocal() {
}
void NetworkServer::LoadPersistent() {
std::error_code ec;
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
wpi::MemoryBuffer::GetFile(m_persistentFilename, ec);
if (fileBuffer == nullptr || ec.value() != 0) {
auto fileBuffer = wpi::MemoryBuffer::GetFile(m_persistentFilename);
if (!fileBuffer) {
INFO(
"could not open persistent file '{}': {} "
"(this can be ignored if you aren't expecting persistent values)",
m_persistentFilename, ec.message());
m_persistentFilename, fileBuffer.error().message());
// backup file
std::error_code ec;
fs::copy_file(m_persistentFilename, m_persistentFilename + ".bak",
std::filesystem::copy_options::overwrite_existing, ec);
// try to write an empty file so it doesn't happen again
@@ -370,7 +369,8 @@ void NetworkServer::LoadPersistent() {
}
return;
}
m_persistentData = std::string{fileBuffer->begin(), fileBuffer->end()};
m_persistentData =
std::string{fileBuffer.value()->begin(), fileBuffer.value()->end()};
DEBUG4("read data: {}", m_persistentData);
}