Commit 0ab4d22e authored by Scott Violet's avatar Scott Violet Committed by Chromium LUCI CQ

sessions: return read commands on read error

Prior to this patch if there was an error in reading the session
file nothing would be returned. This changes the code to return
what was parsed. The hope is that we are at least able to restore
some portion of the previous session rather than nothing.

BUG=648266
TEST=none

Change-Id: Ied629349fd1432b802e4454ab7092d420d143388
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2619010Reviewed-by: default avatarDavid Bienvenu <davidbienvenu@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842046}
parent 4a3a8019
...@@ -132,9 +132,7 @@ class SessionFileReader { ...@@ -132,9 +132,7 @@ class SessionFileReader {
bool ReadToMarker(); bool ReadToMarker();
// Reads a single command, returning it. A return value of null indicates // Reads a single command, returning it. A return value of null indicates
// either there are no commands, or there was an error. Use errored_ to // either there are no commands, or there was an error.
// distinguish the two. If null is returned, and there is no error, it means
// the end of file was successfully reached.
std::unique_ptr<sessions::SessionCommand> ReadCommand(); std::unique_ptr<sessions::SessionCommand> ReadCommand();
// Decrypts a previously encrypted command. Returns the new command on // Decrypts a previously encrypted command. Returns the new command on
...@@ -149,15 +147,11 @@ class SessionFileReader { ...@@ -149,15 +147,11 @@ class SessionFileReader {
// Shifts the unused portion of buffer_ to the beginning and fills the // Shifts the unused portion of buffer_ to the beginning and fills the
// remaining portion with data from the file. Returns false if the buffer // remaining portion with data from the file. Returns false if the buffer
// couldn't be filled. A return value of false only signals an error if // couldn't be filled or there was an error reading the file.
// errored_ is set to true.
bool FillBuffer(); bool FillBuffer();
bool is_header_valid_ = false; bool is_header_valid_ = false;
// Whether an error condition has been detected.
bool errored_ = false;
// As we read from the file, data goes here. // As we read from the file, data goes here.
std::string buffer_; std::string buffer_;
...@@ -193,14 +187,12 @@ SessionFileReader::Read() { ...@@ -193,14 +187,12 @@ SessionFileReader::Read() {
std::vector<std::unique_ptr<sessions::SessionCommand>> read_commands; std::vector<std::unique_ptr<sessions::SessionCommand>> read_commands;
for (std::unique_ptr<sessions::SessionCommand> command = ReadCommand(); for (std::unique_ptr<sessions::SessionCommand> command = ReadCommand();
command && !errored_; command = ReadCommand()) { command; command = ReadCommand()) {
if (command->id() != kInitialStateMarkerCommandId) if (command->id() != kInitialStateMarkerCommandId)
read_commands.push_back(std::move(command)); read_commands.push_back(std::move(command));
} }
// TODO(sky): remove `errored_`. It basically means there is an error reading, // Even if there was an error the commands are returned. The hope is at least
// but I think we should continue in this case. // some portion of the previous session is restored.
if (errored_)
return {};
return read_commands; return read_commands;
} }
...@@ -228,7 +220,7 @@ bool SessionFileReader::ReadToMarker() { ...@@ -228,7 +220,7 @@ bool SessionFileReader::ReadToMarker() {
// It's expected this is only called if the marker is supported. // It's expected this is only called if the marker is supported.
DCHECK(IsHeaderValid() && SupportsMarker()); DCHECK(IsHeaderValid() && SupportsMarker());
for (std::unique_ptr<sessions::SessionCommand> command = ReadCommand(); for (std::unique_ptr<sessions::SessionCommand> command = ReadCommand();
command && !errored_; command = ReadCommand()) { command; command = ReadCommand()) {
if (command->id() == kInitialStateMarkerCommandId) if (command->id() == kInitialStateMarkerCommandId)
return true; return true;
} }
...@@ -337,7 +329,7 @@ bool SessionFileReader::FillBuffer() { ...@@ -337,7 +329,7 @@ bool SessionFileReader::FillBuffer() {
int read_count = int read_count =
file_->ReadAtCurrentPos(&(buffer_[available_count_]), to_read); file_->ReadAtCurrentPos(&(buffer_[available_count_]), to_read);
if (read_count < 0) { if (read_count < 0) {
errored_ = true; // TODO(sky): communicate/log an error here.
return false; return false;
} }
if (read_count == 0) if (read_count == 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment