mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-12 13:20:44 +01:00
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:
parent
af65d4bada
commit
3baefbf2dd
5 changed files with 27 additions and 21 deletions
|
@ -53,7 +53,7 @@ using webrtc_event_logging::ToUnsigned;
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
constexpr int64_t kMaxLogSize = 250000000;
|
constexpr size_t kMaxLogSize = 250000000;
|
||||||
|
|
||||||
constexpr size_t kIpv4Overhead = 20;
|
constexpr size_t kIpv4Overhead = 20;
|
||||||
constexpr size_t kIpv6Overhead = 40;
|
constexpr size_t kIpv6Overhead = 40;
|
||||||
|
@ -1127,17 +1127,17 @@ ParsedRtcEventLog::ParseStatus ParsedRtcEventLog::ParseFile(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute file size.
|
// Compute file size.
|
||||||
long signed_filesize = file.FileSize(); // NOLINT(runtime/int)
|
absl::optional<size_t> file_size = file.FileSize();
|
||||||
RTC_PARSE_CHECK_OR_RETURN_GE(signed_filesize, 0);
|
RTC_PARSE_CHECK_OR_RETURN(file_size.has_value());
|
||||||
RTC_PARSE_CHECK_OR_RETURN_LE(signed_filesize, kMaxLogSize);
|
RTC_PARSE_CHECK_OR_RETURN_GE(*file_size, 0u);
|
||||||
size_t filesize = rtc::checked_cast<size_t>(signed_filesize);
|
RTC_PARSE_CHECK_OR_RETURN_LE(*file_size, kMaxLogSize);
|
||||||
|
|
||||||
// Read file into memory.
|
// 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());
|
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_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);
|
return ParseStream(buffer);
|
||||||
|
|
|
@ -30,7 +30,10 @@ rtc_library("file_wrapper") {
|
||||||
"..:criticalsection",
|
"..:criticalsection",
|
||||||
"..:safe_conversions",
|
"..: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) {
|
if (rtc_include_tests) {
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
|
#include "absl/types/optional.h"
|
||||||
#include "rtc_base/checks.h"
|
#include "rtc_base/checks.h"
|
||||||
#include "rtc_base/numerics/safe_conversions.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;
|
return fseek(file_, rtc::checked_cast<long>(position), SEEK_SET) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
long FileWrapper::FileSize() {
|
absl::optional<size_t> FileWrapper::FileSize() {
|
||||||
if (file_ == nullptr)
|
if (file_ == nullptr)
|
||||||
return -1;
|
return absl::nullopt;
|
||||||
long original_position = ftell(file_);
|
long original_position = ftell(file_);
|
||||||
if (original_position < 0)
|
if (original_position < 0)
|
||||||
return -1;
|
return absl::nullopt;
|
||||||
int seek_error = fseek(file_, 0, SEEK_END);
|
int seek_error = fseek(file_, 0, SEEK_END);
|
||||||
if (seek_error)
|
if (seek_error)
|
||||||
return -1;
|
return absl::nullopt;
|
||||||
long file_size = ftell(file_);
|
long file_size = ftell(file_);
|
||||||
seek_error = fseek(file_, original_position, SEEK_SET);
|
seek_error = fseek(file_, original_position, SEEK_SET);
|
||||||
if (seek_error)
|
if (seek_error)
|
||||||
return -1;
|
return absl::nullopt;
|
||||||
return file_size;
|
return rtc::checked_cast<size_t>(file_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileWrapper::Flush() {
|
bool FileWrapper::Flush() {
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
|
#include "absl/types/optional.h"
|
||||||
|
|
||||||
// Implementation that can read (exclusive) or write from/to a file.
|
// 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.
|
// 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
|
// (A file size might not exists for non-seekable files or file-like
|
||||||
// objects, for example /dev/tty on unix.)
|
// 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.
|
// Returns number of bytes read. Short count indicates EOF or error.
|
||||||
size_t Read(void* buf, size_t length);
|
size_t Read(void* buf, size_t length);
|
||||||
|
|
|
@ -37,11 +37,12 @@ TEST(RtcEventLogAnalyzerBindingsTest, ProducesCharts) {
|
||||||
"rtc_event_log/rtc_event_log_500kbps", "binarypb");
|
"rtc_event_log/rtc_event_log_500kbps", "binarypb");
|
||||||
webrtc::FileWrapper file = webrtc::FileWrapper::OpenReadOnly(file_name);
|
webrtc::FileWrapper file = webrtc::FileWrapper::OpenReadOnly(file_name);
|
||||||
ASSERT_TRUE(file.is_open());
|
ASSERT_TRUE(file.is_open());
|
||||||
int64_t file_size = file.FileSize();
|
absl::optional<size_t> file_size = file.FileSize();
|
||||||
ASSERT_LE(file_size, kInputBufferSize);
|
ASSERT_TRUE(file_size.has_value());
|
||||||
ASSERT_GT(file_size, 0);
|
ASSERT_LE(*file_size, static_cast<size_t>(kInputBufferSize));
|
||||||
size_t input_size = file.Read(input.get(), static_cast<size_t>(file_size));
|
ASSERT_GT(*file_size, 0u);
|
||||||
ASSERT_EQ(static_cast<size_t>(file_size), input_size);
|
size_t input_size = file.Read(input.get(), *file_size);
|
||||||
|
ASSERT_EQ(*file_size, input_size);
|
||||||
|
|
||||||
// Call analyzer.
|
// Call analyzer.
|
||||||
uint32_t output_size = kOutputBufferSize;
|
uint32_t output_size = kOutputBufferSize;
|
||||||
|
|
Loading…
Reference in a new issue