From a1564c5218752f8e20cfdd68b3b554716bc8b6b2 Mon Sep 17 00:00:00 2001 From: cw Date: Fri, 11 Sep 2020 16:30:43 +0800 Subject: [PATCH 1/7] Add config writing capability Tested with short (less than 100 characters) and long data strings --- libconfig/src/lib.rs | 106 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index 4e5ff30..f07fb8f 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -3,7 +3,7 @@ extern crate alloc; use core::fmt; use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc}; -use core_io::{self as io, BufRead, BufReader, Read}; +use core_io::{self as io, BufRead, BufReader, Read, Write, Seek, ErrorKind, SeekFrom}; use libboard_zynq::sdio; pub mod sd_reader; @@ -66,6 +66,38 @@ fn parse_config<'a>( } Err(Error::KeyNotFoundError(key)) } +fn delete_old_entry<'a>( + key: &str, + file: fatfs::File, + mut file_tmp: fatfs::File, +) -> Result<'a, ()> { + let prefix = [key, "="].concat(); + let buf_reader = BufReader::new(file); + + for line in buf_reader.lines() { + let line = line?; + if !line.starts_with(&prefix) { + file_tmp.write_all(&[line.as_str(), "\n"].concat().as_bytes())?; + } + } + Ok(()) +} +fn rename_file<'a>(dir: &fatfs::Dir, old_file_name: &str, new_file_name: &str) -> Result<'a, ()>{ + { + let old_file = dir.open_file(old_file_name)?; + let mut new_file = dir.create_file(new_file_name)?; + new_file.truncate()?; + + for line in BufReader::new(old_file).lines() { + let line = line?; + new_file.write_all(&[line.as_str(), "\n"].concat().as_bytes())?; + } + } + + dir.remove(old_file_name)?; + + Ok(()) +} pub struct Config { fs: Option>>, @@ -112,4 +144,76 @@ impl Config { pub fn read_str<'b>(&self, key: &'b str) -> Result<'b, String> { Ok(String::from_utf8(self.read(key)?)?) } + + pub fn write_str<'b>(&mut self, key: &str, data: &str) -> Result<'b, ()>{ + if let Some(fs) = &self.fs { + let root_dir = fs.root_dir(); + + let config_key_bin = &["/CONFIG/", key, ".BIN"].concat(); + let config_txt_tmp = "/CONFIG.TMP"; + + if data.is_ascii() & (data.len() <= 100) { + match root_dir.create_file("/CONFIG.TXT") { + Ok(file) => { + let mut file_tmp = root_dir.create_file(config_txt_tmp)?; + file_tmp.truncate()?; + delete_old_entry(key, file, file_tmp)?; + }, + Err(e) => { + return Err(Error::IoError(e)); + } + }; + + rename_file(&root_dir, config_txt_tmp, "/CONFIG.TXT")?; + + let mut file = root_dir.open_file("/CONFIG.TXT")?; + file.seek(SeekFrom::End(0))?; + file.write_all(&["\n", key, "=", data, "\n"].concat().as_bytes())?; + + match root_dir.remove(config_key_bin) { + Ok(_) => {}, + Err(e) => match e.kind() { + ErrorKind::NotFound => {}, + _ => { + return Err(Error::IoError(e)); + } + } + }; + } else { + root_dir.create_dir("/CONFIG")?; + match root_dir.create_file(config_key_bin) { + Ok(mut file) => { + file.truncate()?; + file.write_all(&[data, "\n"].concat().as_bytes())?; + }, + Err(e) => { + return Err(Error::IoError(e)); + } + }; + + let mut need_to_rename = false; + match root_dir.open_file("/CONFIG.TXT") { + Ok(file) => { + need_to_rename = true; + let mut file_tmp = root_dir.create_file(config_txt_tmp)?; + file_tmp.truncate()?; + delete_old_entry(key, file, file_tmp)?; + }, + Err(e) => match e.kind() { + ErrorKind::NotFound => {}, + _ => { + return Err(Error::IoError(e)); + } + } + }; + if need_to_rename { // docs: ensure no ref to files + rename_file(&root_dir, config_txt_tmp, "/CONFIG.TXT")?; + } + } + + Ok(()) + } else { + Err(Error::NoConfig) + } + } } -- 2.42.0 From 822473e8e0a6b5833f02c11b5c899dc04cc66ecd Mon Sep 17 00:00:00 2001 From: cw Date: Fri, 11 Sep 2020 16:35:20 +0800 Subject: [PATCH 2/7] Add blank lines between functions --- libconfig/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index f07fb8f..863c1c8 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -66,6 +66,7 @@ fn parse_config<'a>( } Err(Error::KeyNotFoundError(key)) } + fn delete_old_entry<'a>( key: &str, file: fatfs::File, @@ -82,6 +83,7 @@ fn delete_old_entry<'a>( } Ok(()) } + fn rename_file<'a>(dir: &fatfs::Dir, old_file_name: &str, new_file_name: &str) -> Result<'a, ()>{ { let old_file = dir.open_file(old_file_name)?; @@ -206,7 +208,7 @@ impl Config { } } }; - if need_to_rename { // docs: ensure no ref to files + if need_to_rename { rename_file(&root_dir, config_txt_tmp, "/CONFIG.TXT")?; } } -- 2.42.0 From be2869bce569469e827e105fc0fd0ec43f0bd5f7 Mon Sep 17 00:00:00 2001 From: cw Date: Fri, 18 Sep 2020 17:30:16 +0800 Subject: [PATCH 3/7] Add remove and erase methods --- libconfig/src/lib.rs | 110 ++++++++++++++++++++++++++++++++----------- 1 file changed, 83 insertions(+), 27 deletions(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index 863c1c8..0378e5f 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -147,6 +147,87 @@ impl Config { Ok(String::from_utf8(self.read(key)?)?) } + pub fn erase<'b>(&mut self) -> Result<'b, ()>{ + if let Some(fs) = &self.fs { + let root_dir = fs.root_dir(); + + match root_dir.create_file("/CONFIG.TXT") { + Ok(file) => { + file.truncate()?; + }, + Err(e) => { + return Err(Error::IoError(e)); + } + }; + + let dir = root_dir.create_dir("/CONFIG")?; + for r in dir.iter() { + let entry = r?; + if entry.isfile() { + dir.remove(str::from_utf8(entry.short_file_name_as_bytes()).unwrap())?; + } + } + + Ok(()) + } else { + Err(Error::NoConfig) + } + } + pub fn remove_config_txt<'b>(&mut self, key: &str) -> Result<'b, ()>{ + if let Some(fs) = &self.fs { + let root_dir = fs.root_dir(); + + let config_txt_tmp = "/CONFIG.TMP"; + + let mut need_to_rename = false; + match root_dir.open_file("/CONFIG.TXT") { + Ok(file) => { + need_to_rename = true; + let mut file_tmp = root_dir.create_file(config_txt_tmp)?; + file_tmp.truncate()?; + delete_old_entry(key, file, file_tmp)?; + }, + Err(e) => match e.kind() { + ErrorKind::NotFound => {}, + _ => { + return Err(Error::IoError(e)); + } + } + }; + if need_to_rename { + rename_file(&root_dir, config_txt_tmp, "/CONFIG.TXT")?; + } + + Ok(()) + } else { + Err(Error::NoConfig) + } + } + pub fn remove_config_key_bin<'b>(&mut self, key: &str) -> Result<'b, ()>{ + if let Some(fs) = &self.fs { + let root_dir = fs.root_dir(); + + let config_key_bin = &["/CONFIG/", key, ".BIN"].concat(); + + match root_dir.remove(config_key_bin) { + Ok(_) => {}, + Err(e) => match e.kind() { + ErrorKind::NotFound => {}, + _ => { + return Err(Error::IoError(e)); + } + } + }; + + Ok(()) + } else { + Err(Error::NoConfig) + } + } + pub fn remove<'b>(&mut self, key: &str) -> Result<'b, ()>{ + self.remove_config_txt(key)?; + self.remove_config_key_bin(key)?; + } pub fn write_str<'b>(&mut self, key: &str, data: &str) -> Result<'b, ()>{ if let Some(fs) = &self.fs { let root_dir = fs.root_dir(); @@ -172,15 +253,7 @@ impl Config { file.seek(SeekFrom::End(0))?; file.write_all(&["\n", key, "=", data, "\n"].concat().as_bytes())?; - match root_dir.remove(config_key_bin) { - Ok(_) => {}, - Err(e) => match e.kind() { - ErrorKind::NotFound => {}, - _ => { - return Err(Error::IoError(e)); - } - } - }; + self.remove_config_key_bin(key)?; } else { root_dir.create_dir("/CONFIG")?; match root_dir.create_file(config_key_bin) { @@ -193,24 +266,7 @@ impl Config { } }; - let mut need_to_rename = false; - match root_dir.open_file("/CONFIG.TXT") { - Ok(file) => { - need_to_rename = true; - let mut file_tmp = root_dir.create_file(config_txt_tmp)?; - file_tmp.truncate()?; - delete_old_entry(key, file, file_tmp)?; - }, - Err(e) => match e.kind() { - ErrorKind::NotFound => {}, - _ => { - return Err(Error::IoError(e)); - } - } - }; - if need_to_rename { - rename_file(&root_dir, config_txt_tmp, "/CONFIG.TXT")?; - } + self.remove_config_txt(key)?; } Ok(()) -- 2.42.0 From e91549d1e867413dce2166391a78bf7902b80a0b Mon Sep 17 00:00:00 2001 From: cw Date: Tue, 22 Sep 2020 17:19:54 +0800 Subject: [PATCH 4/7] WIP: fix typos --- libconfig/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index 0378e5f..0b42072 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -2,7 +2,7 @@ extern crate alloc; use core::fmt; -use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc}; +use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc, str}; use core_io::{self as io, BufRead, BufReader, Read, Write, Seek, ErrorKind, SeekFrom}; use libboard_zynq::sdio; @@ -163,7 +163,7 @@ impl Config { let dir = root_dir.create_dir("/CONFIG")?; for r in dir.iter() { let entry = r?; - if entry.isfile() { + if entry.is_file() { dir.remove(str::from_utf8(entry.short_file_name_as_bytes()).unwrap())?; } } @@ -227,6 +227,7 @@ impl Config { pub fn remove<'b>(&mut self, key: &str) -> Result<'b, ()>{ self.remove_config_txt(key)?; self.remove_config_key_bin(key)?; + Ok(()) } pub fn write_str<'b>(&mut self, key: &str, data: &str) -> Result<'b, ()>{ if let Some(fs) = &self.fs { -- 2.42.0 From 0f6276e72277439cd646c9fe447b2c2f3172abbf Mon Sep 17 00:00:00 2001 From: cw Date: Tue, 22 Sep 2020 17:28:30 +0800 Subject: [PATCH 5/7] WIP: fix borrow --- libconfig/src/lib.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index 0b42072..7866e94 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -152,7 +152,7 @@ impl Config { let root_dir = fs.root_dir(); match root_dir.create_file("/CONFIG.TXT") { - Ok(file) => { + Ok(mut file) => { file.truncate()?; }, Err(e) => { @@ -230,6 +230,9 @@ impl Config { Ok(()) } pub fn write_str<'b>(&mut self, key: &str, data: &str) -> Result<'b, ()>{ + let call_remove_config_key_bin = false; + let call_remove_config_txt = false; + if let Some(fs) = &self.fs { let root_dir = fs.root_dir(); @@ -254,7 +257,7 @@ impl Config { file.seek(SeekFrom::End(0))?; file.write_all(&["\n", key, "=", data, "\n"].concat().as_bytes())?; - self.remove_config_key_bin(key)?; + call_remove_config_key_bin = true; } else { root_dir.create_dir("/CONFIG")?; match root_dir.create_file(config_key_bin) { @@ -267,12 +270,18 @@ impl Config { } }; - self.remove_config_txt(key)?; + call_remove_config_txt = true; } - - Ok(()) } else { Err(Error::NoConfig) } + + if call_remove_config_key_bin { + self.remove_config_key_bin(key)?; + } + if call_remove_config_txt { + self.remove_config_txt(key)?; + } + Ok(()) } } -- 2.42.0 From 9066a58d0092df2d1bf2cf8d6871025250df20c8 Mon Sep 17 00:00:00 2001 From: cw Date: Fri, 25 Sep 2020 15:53:46 +0800 Subject: [PATCH 6/7] WIP: Fix return statement --- libconfig/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index 7866e94..8075bdc 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -273,7 +273,7 @@ impl Config { call_remove_config_txt = true; } } else { - Err(Error::NoConfig) + return Err(Error::NoConfig); } if call_remove_config_key_bin { -- 2.42.0 From 4d961c1049602e144dd6214718ceb2612f95ea18 Mon Sep 17 00:00:00 2001 From: cw Date: Fri, 25 Sep 2020 15:55:08 +0800 Subject: [PATCH 7/7] WIP: Fix mutability --- libconfig/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libconfig/src/lib.rs b/libconfig/src/lib.rs index 8075bdc..aeabcbe 100644 --- a/libconfig/src/lib.rs +++ b/libconfig/src/lib.rs @@ -230,8 +230,8 @@ impl Config { Ok(()) } pub fn write_str<'b>(&mut self, key: &str, data: &str) -> Result<'b, ()>{ - let call_remove_config_key_bin = false; - let call_remove_config_txt = false; + let mut call_remove_config_key_bin = false; + let mut call_remove_config_txt = false; if let Some(fs) = &self.fs { let root_dir = fs.root_dir(); -- 2.42.0