pw_blob_store: Update checking in reader::Open
- Add check for valid to read before doing the check for valid offset.
- Change GetMemoryMappedBlob() to return ConstByteSpan.
Change-Id: Ibf0b60fed00520030df5066fcf8fc7c235825a30
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/18440
Reviewed-by: Ewout van Bekkum <ewout@google.com>
Commit-Queue: David Rogers <davidrogers@google.com>
diff --git a/pw_blob_store/blob_store.cc b/pw_blob_store/blob_store.cc
index 7744a4b..a1181c4 100644
--- a/pw_blob_store/blob_store.cc
+++ b/pw_blob_store/blob_store.cc
@@ -423,7 +423,7 @@
return partition_.Read(offset, dest.first(read_size));
}
-Result<ByteSpan> BlobStore::GetMemoryMappedBlob() const {
+Result<ConstByteSpan> BlobStore::GetMemoryMappedBlob() const {
if (!ValidToRead()) {
return Status::FAILED_PRECONDITION;
}
@@ -432,7 +432,7 @@
if (mcu_address == nullptr) {
return Status::UNIMPLEMENTED;
}
- return std::span(mcu_address, ReadableDataBytes());
+ return ConstByteSpan(mcu_address, ReadableDataBytes());
}
size_t BlobStore::ReadableDataBytes() const {
diff --git a/pw_blob_store/blob_store_chunk_write_test.cc b/pw_blob_store/blob_store_chunk_write_test.cc
index 42dac62..ae9ef68 100644
--- a/pw_blob_store/blob_store_chunk_write_test.cc
+++ b/pw_blob_store/blob_store_chunk_write_test.cc
@@ -84,13 +84,13 @@
// Use reader to check for valid data.
BlobStore::BlobReader reader(blob);
ASSERT_EQ(Status::OK, reader.Open());
- Result<ByteSpan> result = reader.GetMemoryMappedBlob();
+ Result<ConstByteSpan> result = reader.GetMemoryMappedBlob();
ASSERT_TRUE(result.ok());
VerifyFlash(result.value());
EXPECT_EQ(Status::OK, reader.Close());
}
- void VerifyFlash(ByteSpan verify_bytes) {
+ void VerifyFlash(ConstByteSpan verify_bytes) {
// Should be defined as same size.
EXPECT_EQ(source_buffer_.size(), flash_.buffer().size_bytes());
diff --git a/pw_blob_store/blob_store_deferred_write_test.cc b/pw_blob_store/blob_store_deferred_write_test.cc
index a957308..9ccc581 100644
--- a/pw_blob_store/blob_store_deferred_write_test.cc
+++ b/pw_blob_store/blob_store_deferred_write_test.cc
@@ -93,13 +93,13 @@
// Use reader to check for valid data.
BlobStore::BlobReader reader(blob);
ASSERT_EQ(Status::OK, reader.Open());
- Result<ByteSpan> result = reader.GetMemoryMappedBlob();
+ Result<ConstByteSpan> result = reader.GetMemoryMappedBlob();
ASSERT_TRUE(result.ok());
VerifyFlash(result.value());
EXPECT_EQ(Status::OK, reader.Close());
}
- void VerifyFlash(ByteSpan verify_bytes) {
+ void VerifyFlash(ConstByteSpan verify_bytes) {
// Should be defined as same size.
EXPECT_EQ(buffer_.size(), flash_.buffer().size_bytes());
diff --git a/pw_blob_store/blob_store_test.cc b/pw_blob_store/blob_store_test.cc
index 55f98f1..30cd64f 100644
--- a/pw_blob_store/blob_store_test.cc
+++ b/pw_blob_store/blob_store_test.cc
@@ -72,7 +72,7 @@
// Use reader to check for valid data.
BlobStore::BlobReader reader(blob);
ASSERT_EQ(Status::OK, reader.Open());
- Result<ByteSpan> result = reader.GetMemoryMappedBlob();
+ Result<ConstByteSpan> result = reader.GetMemoryMappedBlob();
ASSERT_TRUE(result.ok());
VerifyFlash(result.value());
EXPECT_EQ(Status::OK, reader.Close());
@@ -91,7 +91,7 @@
// Use reader to check for valid data.
BlobStore::BlobReader reader1(blob);
ASSERT_EQ(Status::OK, reader1.Open());
- Result<ByteSpan> result = reader1.GetMemoryMappedBlob();
+ Result<ConstByteSpan> result = reader1.GetMemoryMappedBlob();
ASSERT_TRUE(result.ok());
VerifyFlash(result.value());
EXPECT_EQ(Status::OK, reader1.Close());
@@ -119,7 +119,7 @@
VerifyFlash(read_buffer_);
}
- void VerifyFlash(ByteSpan verify_bytes, size_t offset = 0) {
+ void VerifyFlash(ConstByteSpan verify_bytes, size_t offset = 0) {
// Should be defined as same size.
EXPECT_EQ(source_buffer_.size(), flash_.buffer().size_bytes());
diff --git a/pw_blob_store/public/pw_blob_store/blob_store.h b/pw_blob_store/public/pw_blob_store/blob_store.h
index 4c02790..d28c9d7 100644
--- a/pw_blob_store/public/pw_blob_store/blob_store.h
+++ b/pw_blob_store/public/pw_blob_store/blob_store.h
@@ -194,9 +194,14 @@
// Returns:
//
// OK - success.
+ // FAILED_PRECONDITION - No readable blob available.
+ // INVALID_ARGUMENT - Invalid offset.
// UNAVAILABLE - Unable to open, already open.
Status Open(size_t offset = 0) {
PW_DCHECK(!open_);
+ if (!store_.ValidToRead()) {
+ return Status::FAILED_PRECONDITION;
+ }
if (offset >= store_.ReadableDataBytes()) {
return Status::INVALID_ARGUMENT;
}
@@ -232,7 +237,7 @@
// OK with span - Valid span respresenting the blob data
// FAILED_PRECONDITION - Reader not open.
// UNIMPLEMENTED - Memory mapped access not supported for this blob.
- Result<ByteSpan> GetMemoryMappedBlob() {
+ Result<ConstByteSpan> GetMemoryMappedBlob() {
PW_DCHECK(open_);
return store_.GetMemoryMappedBlob();
}
@@ -417,7 +422,7 @@
// OK with span - Valid span respresenting the blob data
// FAILED_PRECONDITION - Blob not in a state to read data
// UNIMPLEMENTED - Memory mapped access not supported for this blob.
- Result<ByteSpan> GetMemoryMappedBlob() const;
+ Result<ConstByteSpan> GetMemoryMappedBlob() const;
// Size of blob/readable data, in bytes.
size_t ReadableDataBytes() const;