Create new store and add storage API
This commit is contained in:
6
.github/workflows/cargo_fmt.yml
vendored
6
.github/workflows/cargo_fmt.yml
vendored
@@ -62,6 +62,12 @@ jobs:
|
||||
command: fmt
|
||||
args: --manifest-path libraries/crypto/Cargo.toml --all -- --check
|
||||
|
||||
- name: Cargo format libraries/persistent_store
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: fmt
|
||||
args: --manifest-path libraries/persistent_store/Cargo.toml --all -- --check
|
||||
|
||||
- name: Cargo format tools/heapviz
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
|
||||
26
.github/workflows/persistent_store_test.yml
vendored
Normal file
26
.github/workflows/persistent_store_test.yml
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
---
|
||||
name: Persistent store tests
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'libraries/peristent_store/**'
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
|
||||
jobs:
|
||||
persistent_store_test:
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Unit testing of Persistent store library (release mode)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --manifest-path libraries/persistent_store/Cargo.toml --release --features std
|
||||
|
||||
- name: Unit testing of Persistent store library (debug mode)
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --manifest-path libraries/persistent_store/Cargo.toml --features std
|
||||
11
libraries/persistent_store/Cargo.toml
Normal file
11
libraries/persistent_store/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "persistent_store"
|
||||
version = "0.1.0"
|
||||
authors = ["Julien Cretin <cretin@google.com>"]
|
||||
license = "Apache-2.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[features]
|
||||
std = []
|
||||
19
libraries/persistent_store/src/lib.rs
Normal file
19
libraries/persistent_store/src/lib.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright 2019 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
mod storage;
|
||||
|
||||
pub use self::storage::{Storage, StorageError, StorageIndex, StorageResult};
|
||||
95
libraries/persistent_store/src/storage.rs
Normal file
95
libraries/persistent_store/src/storage.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
// Copyright 2019 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/// Represents a byte position in a storage
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StorageIndex {
|
||||
pub page: usize,
|
||||
pub byte: usize,
|
||||
}
|
||||
|
||||
/// Represents a possible storage error
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum StorageError {
|
||||
/// Arguments are not correctly aligned
|
||||
NotAligned,
|
||||
|
||||
/// Arguments are out of bounds
|
||||
OutOfBounds,
|
||||
|
||||
/// Implementation-specific error
|
||||
CustomError,
|
||||
}
|
||||
|
||||
pub type StorageResult<T> = Result<T, StorageError>;
|
||||
|
||||
/// Abstracts a flash storage
|
||||
pub trait Storage {
|
||||
/// The size of a word in bytes
|
||||
fn word_size(&self) -> usize;
|
||||
|
||||
/// The size of a page in bytes
|
||||
fn page_size(&self) -> usize;
|
||||
|
||||
/// The number of pages in the storage
|
||||
fn num_pages(&self) -> usize;
|
||||
|
||||
/// Maximum number of times a word can be written between page erasures
|
||||
fn max_word_writes(&self) -> usize;
|
||||
|
||||
/// Maximum number of times a page can be erased
|
||||
fn max_page_erases(&self) -> usize;
|
||||
|
||||
/// Reads a byte slice from the storage
|
||||
///
|
||||
/// The `index` must designate `length` bytes in the storage.
|
||||
fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult<&[u8]>;
|
||||
|
||||
/// Writes a word slice to the storage
|
||||
///
|
||||
/// The following pre-conditions must hold:
|
||||
/// - The `index` must designate `value.len()` bytes in the storage.
|
||||
/// - Both `index` and `value.len()` must be word-aligned.
|
||||
/// - The written words should not have been written too many times since last page erasure.
|
||||
fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()>;
|
||||
|
||||
/// Erases a page of the storage
|
||||
///
|
||||
/// The `page` must be in the storage.
|
||||
fn erase_page(&mut self, page: usize) -> StorageResult<()>;
|
||||
}
|
||||
|
||||
impl StorageIndex {
|
||||
/// Whether a slice fits in a storage page
|
||||
fn is_valid(self, length: usize, storage: &impl Storage) -> bool {
|
||||
let page_size = storage.page_size();
|
||||
self.page < storage.num_pages() && length <= page_size && self.byte <= page_size - length
|
||||
}
|
||||
|
||||
/// Returns the range of a valid slice
|
||||
///
|
||||
/// The range starts at `self` with `length` bytes.
|
||||
pub fn range(
|
||||
self,
|
||||
length: usize,
|
||||
storage: &impl Storage,
|
||||
) -> StorageResult<core::ops::Range<usize>> {
|
||||
if self.is_valid(length, storage) {
|
||||
let start = self.page * storage.page_size() + self.byte;
|
||||
Ok(start..start + length)
|
||||
} else {
|
||||
Err(StorageError::OutOfBounds)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,9 @@ cd ../..
|
||||
cd libraries/crypto
|
||||
cargo fmt --all -- --check
|
||||
cd ../..
|
||||
cd libraries/persistent_store
|
||||
cargo fmt --all -- --check
|
||||
cd ../..
|
||||
cd tools/heapviz
|
||||
cargo fmt --all -- --check
|
||||
cd ../..
|
||||
@@ -84,6 +87,9 @@ then
|
||||
cd libraries/crypto
|
||||
RUSTFLAGS='-C target-feature=+aes' cargo test --release --features std,derive_debug
|
||||
cd ../..
|
||||
cd libraries/persistent_store
|
||||
cargo test --release --features std
|
||||
cd ../..
|
||||
cargo test --release --features std
|
||||
|
||||
echo "Running unit tests on the desktop (debug mode)..."
|
||||
@@ -93,6 +99,9 @@ then
|
||||
cd libraries/crypto
|
||||
RUSTFLAGS='-C target-feature=+aes' cargo test --features std,derive_debug
|
||||
cd ../..
|
||||
cd libraries/persistent_store
|
||||
cargo test --features std
|
||||
cd ../..
|
||||
cargo test --features std
|
||||
|
||||
echo "Running unit tests on the desktop (release mode + CTAP1)..."
|
||||
|
||||
Reference in New Issue
Block a user