Return absl::optional<size_t> from FileWrapper::FileSize()

Bug: None
Change-Id: If5219a8f7f7e81ea660b0495c48f96adb6948228
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/348860
Commit-Queue: Björn Terelius <terelius@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42206}
This commit is contained in:
Björn Terelius 2024-04-23 16:51:38 +02:00 committed by WebRTC LUCI CQ
parent af65d4bada
commit 3baefbf2dd
5 changed files with 27 additions and 21 deletions

View file

@ -53,7 +53,7 @@ using webrtc_event_logging::ToUnsigned;
namespace webrtc {
namespace {
constexpr int64_t kMaxLogSize = 250000000;
constexpr size_t kMaxLogSize = 250000000;
constexpr size_t kIpv4Overhead = 20;
constexpr size_t kIpv6Overhead = 40;
@ -1127,17 +1127,17 @@ ParsedRtcEventLog::ParseStatus ParsedRtcEventLog::ParseFile(
}
// Compute file size.
long signed_filesize = file.FileSize(); // NOLINT(runtime/int)
RTC_PARSE_CHECK_OR_RETURN_GE(signed_filesize, 0);
RTC_PARSE_CHECK_OR_RETURN_LE(signed_filesize, kMaxLogSize);
size_t filesize = rtc::checked_cast<size_t>(signed_filesize);
absl::optional<size_t> file_size = file.FileSize();
RTC_PARSE_CHECK_OR_RETURN(file_size.has_value());
RTC_PARSE_CHECK_OR_RETURN_GE(*file_size, 0u);
RTC_PARSE_CHECK_OR_RETURN_LE(*file_size, kMaxLogSize);
// Read file into memory.
std::string buffer(filesize, '\0');
std::string buffer(*file_size, '\0');
size_t bytes_read = file.Read(&buffer[0], buffer.size());
if (bytes_read != filesize) {
if (bytes_read != *file_size) {
RTC_LOG(LS_WARNING) << "Failed to read file " << filename;
RTC_PARSE_CHECK_OR_RETURN_EQ(bytes_read, filesize);
RTC_PARSE_CHECK_OR_RETURN_EQ(bytes_read, *file_size);
}
return ParseStream(buffer);

View file

@ -30,7 +30,10 @@ rtc_library("file_wrapper") {
"..:criticalsection",
"..:safe_conversions",
]
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
absl_deps = [
"//third_party/abseil-cpp/absl/strings",
"//third_party/abseil-cpp/absl/types:optional",
]
}
if (rtc_include_tests) {

View file

@ -17,6 +17,7 @@
#include <string>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "rtc_base/checks.h"
#include "rtc_base/numerics/safe_conversions.h"
@ -81,20 +82,20 @@ bool FileWrapper::SeekTo(int64_t position) {
return fseek(file_, rtc::checked_cast<long>(position), SEEK_SET) == 0;
}
long FileWrapper::FileSize() {
absl::optional<size_t> FileWrapper::FileSize() {
if (file_ == nullptr)
return -1;
return absl::nullopt;
long original_position = ftell(file_);
if (original_position < 0)
return -1;
return absl::nullopt;
int seek_error = fseek(file_, 0, SEEK_END);
if (seek_error)
return -1;
return absl::nullopt;
long file_size = ftell(file_);
seek_error = fseek(file_, original_position, SEEK_SET);
if (seek_error)
return -1;
return file_size;
return absl::nullopt;
return rtc::checked_cast<size_t>(file_size);
}
bool FileWrapper::Flush() {

View file

@ -18,6 +18,7 @@
#include <string>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
// Implementation that can read (exclusive) or write from/to a file.
@ -89,7 +90,7 @@ class FileWrapper final {
// Returns the file size or -1 if a size could not be determined.
// (A file size might not exists for non-seekable files or file-like
// objects, for example /dev/tty on unix.)
long FileSize();
absl::optional<size_t> FileSize();
// Returns number of bytes read. Short count indicates EOF or error.
size_t Read(void* buf, size_t length);

View file

@ -37,11 +37,12 @@ TEST(RtcEventLogAnalyzerBindingsTest, ProducesCharts) {
"rtc_event_log/rtc_event_log_500kbps", "binarypb");
webrtc::FileWrapper file = webrtc::FileWrapper::OpenReadOnly(file_name);
ASSERT_TRUE(file.is_open());
int64_t file_size = file.FileSize();
ASSERT_LE(file_size, kInputBufferSize);
ASSERT_GT(file_size, 0);
size_t input_size = file.Read(input.get(), static_cast<size_t>(file_size));
ASSERT_EQ(static_cast<size_t>(file_size), input_size);
absl::optional<size_t> file_size = file.FileSize();
ASSERT_TRUE(file_size.has_value());
ASSERT_LE(*file_size, static_cast<size_t>(kInputBufferSize));
ASSERT_GT(*file_size, 0u);
size_t input_size = file.Read(input.get(), *file_size);
ASSERT_EQ(*file_size, input_size);
// Call analyzer.
uint32_t output_size = kOutputBufferSize;