2023-02-02 18:22:21 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020-2022 Project CHIP Authors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2023-04-29 10:03:34 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
pub use file_psm::*;
|
2023-02-02 18:22:21 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
2023-04-29 10:03:34 +00:00
|
|
|
mod file_psm {
|
|
|
|
use std::fs;
|
2023-02-02 18:22:21 +00:00
|
|
|
use std::io::{Read, Write};
|
2023-04-29 10:03:34 +00:00
|
|
|
use std::path::PathBuf;
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 16:01:21 +00:00
|
|
|
use log::info;
|
|
|
|
|
2023-04-29 19:38:01 +03:00
|
|
|
use crate::error::{Error, ErrorCode};
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
pub struct FilePsm {
|
|
|
|
dir: PathBuf,
|
2023-02-02 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FilePsm {
|
2023-04-29 10:03:34 +00:00
|
|
|
pub fn new(dir: PathBuf) -> Result<Self, Error> {
|
|
|
|
fs::create_dir_all(&dir)?;
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
Ok(Self { dir })
|
2023-02-02 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
pub fn load<'a>(&self, key: &str, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error> {
|
|
|
|
let path = self.dir.join(key);
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
match fs::File::open(path) {
|
|
|
|
Ok(mut file) => {
|
|
|
|
let mut offset = 0;
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
loop {
|
|
|
|
if offset == buf.len() {
|
2023-04-29 19:38:01 +03:00
|
|
|
Err(ErrorCode::NoSpace)?;
|
2023-04-29 10:03:34 +00:00
|
|
|
}
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
let len = file.read(&mut buf[offset..])?;
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
if len == 0 {
|
|
|
|
break;
|
|
|
|
}
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
offset += len;
|
|
|
|
}
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 16:01:21 +00:00
|
|
|
let data = &buf[..offset];
|
|
|
|
|
|
|
|
info!("Key {}: loaded {} bytes {:?}", key, data.len(), data);
|
|
|
|
|
|
|
|
Ok(Some(data))
|
2023-04-29 10:03:34 +00:00
|
|
|
}
|
|
|
|
Err(_) => Ok(None),
|
|
|
|
}
|
2023-02-02 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
pub fn store(&self, key: &str, data: &[u8]) -> Result<(), Error> {
|
|
|
|
let path = self.dir.join(key);
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
let mut file = fs::File::create(path)?;
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
file.write_all(data)?;
|
2023-02-02 18:22:21 +00:00
|
|
|
|
2023-04-29 16:01:21 +00:00
|
|
|
info!("Key {}: stored {} bytes {:?}", key, data.len(), data);
|
|
|
|
|
2023-04-29 10:03:34 +00:00
|
|
|
Ok(())
|
2023-02-02 18:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|