Commit be58fdd2 authored by Leonid Baraz's avatar Leonid Baraz Committed by Commit Bot

Make sure new file is actually created.

Bug: b:165851833
Change-Id: Id4d65e2426663783d510a72a8a1f298462e3d2a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2378655
Commit-Queue: Leonid Baraz <lbaraz@chromium.org>
Auto-Submit: Leonid Baraz <lbaraz@chromium.org>
Reviewed-by: default avatarZach Trudo <zatrudo@google.com>
Cr-Commit-Position: refs/heads/master@{#802085}
parent 4840ab8b
...@@ -307,24 +307,29 @@ StatusOr<scoped_refptr<StorageQueue::SingleFile>> StorageQueue::AssignLastFile( ...@@ -307,24 +307,29 @@ StatusOr<scoped_refptr<StorageQueue::SingleFile>> StorageQueue::AssignLastFile(
// The last file will become too large, asynchronously close it and add // The last file will become too large, asynchronously close it and add
// new. // new.
last_file->Close(); last_file->Close();
auto insert_result = files_.emplace( ASSIGN_OR_RETURN(last_file, OpenNewWriteableFile());
next_seq_number_,
base::MakeRefCounted<SingleFile>(
options_.directory()
.Append(options_.file_prefix())
.AddExtensionASCII(base::NumberToString(next_seq_number_)),
/*size=*/0));
if (!insert_result.second) {
return Status(
error::ALREADY_EXISTS,
base::StrCat({"Sequence number already assigned: '",
base::NumberToString(next_seq_number_), "'"}));
}
last_file = insert_result.first->second;
} }
return last_file; return last_file;
} }
StatusOr<scoped_refptr<StorageQueue::SingleFile>>
StorageQueue::OpenNewWriteableFile() {
DCHECK_CALLED_ON_VALID_SEQUENCE(storage_queue_sequence_checker_);
auto new_file = base::MakeRefCounted<SingleFile>(
options_.directory()
.Append(options_.file_prefix())
.AddExtensionASCII(base::NumberToString(next_seq_number_)),
/*size=*/0);
RETURN_IF_ERROR(new_file->Open(/*read_only=*/false));
auto insert_result = files_.emplace(next_seq_number_, new_file);
if (!insert_result.second) {
return Status(error::ALREADY_EXISTS,
base::StrCat({"Sequence number already assigned: '",
base::NumberToString(next_seq_number_), "'"}));
}
return new_file;
}
Status StorageQueue::WriteHeaderAndBlock( Status StorageQueue::WriteHeaderAndBlock(
base::span<const uint8_t> data, base::span<const uint8_t> data,
scoped_refptr<StorageQueue::SingleFile> file) { scoped_refptr<StorageQueue::SingleFile> file) {
...@@ -676,18 +681,7 @@ Status StorageQueue::SwitchLastFileIfNotEmpty() { ...@@ -676,18 +681,7 @@ Status StorageQueue::SwitchLastFileIfNotEmpty() {
return Status::StatusOK(); // Already empty. return Status::StatusOK(); // Already empty.
} }
files_.rbegin()->second->Close(); files_.rbegin()->second->Close();
auto insert_result = files_.emplace( ASSIGN_OR_RETURN(scoped_refptr<SingleFile> last_file, OpenNewWriteableFile());
next_seq_number_,
base::MakeRefCounted<SingleFile>(
options_.directory()
.Append(options_.file_prefix())
.AddExtensionASCII(base::NumberToString(next_seq_number_)),
/*size=*/0));
if (!insert_result.second) {
return Status(error::ALREADY_EXISTS,
base::StrCat({"Sequence number already assigned: '",
base::NumberToString(next_seq_number_), "'"}));
}
return Status::StatusOK(); return Status::StatusOK();
} }
......
...@@ -262,6 +262,10 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> { ...@@ -262,6 +262,10 @@ class StorageQueue : public base::RefCountedThreadSafe<StorageQueue> {
// file will be created and assigned to be the last one. // file will be created and assigned to be the last one.
StatusOr<scoped_refptr<SingleFile>> AssignLastFile(size_t size); StatusOr<scoped_refptr<SingleFile>> AssignLastFile(size_t size);
// Helper method for Write() and Read(): creates and opens a new empty
// writeable file, adding it to |files_|.
StatusOr<scoped_refptr<SingleFile>> OpenNewWriteableFile();
// Helper method for Write(): composes record header and writes it to the // Helper method for Write(): composes record header and writes it to the
// file, followed by data. // file, followed by data.
Status WriteHeaderAndBlock(base::span<const uint8_t> data, Status WriteHeaderAndBlock(base::span<const uint8_t> data,
......
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