diff --git a/native/src/base/logging.rs b/native/src/base/logging.rs index 3e779cb44..d89999b09 100644 --- a/native/src/base/logging.rs +++ b/native/src/base/logging.rs @@ -207,6 +207,10 @@ pub trait ResultExt { fn log_with_msg fmt::Result>(self, f: F) -> LoggedResult; } +pub trait ResultNoLog { + fn no_log(self) -> LoggedResult; +} + // Internal C++ bridging logging routines pub(crate) trait CxxResultExt { fn log_cxx(self) -> LoggedResult; @@ -223,6 +227,24 @@ trait LogImpl { ) -> LoggedResult; } +impl ResultNoLog for Result { + fn no_log(self) -> LoggedResult { + match self { + Ok(v) => Ok(v), + Err(_) => Err(LoggedError::default()), + } + } +} + +impl ResultNoLog for Option { + fn no_log(self) -> LoggedResult { + match self { + Some(v) => Ok(v), + None => Err(LoggedError::default()), + } + } +} + impl> CxxResultExt for R { fn log_cxx(self) -> LoggedResult { self.log_impl(LogLevel::ErrorCxx, None) diff --git a/native/src/core/resetprop/persist.rs b/native/src/core/resetprop/persist.rs index 4dd8e3b02..e2231bbf7 100644 --- a/native/src/core/resetprop/persist.rs +++ b/native/src/core/resetprop/persist.rs @@ -12,8 +12,8 @@ use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer}; use base::libc::{O_CLOEXEC, O_RDONLY}; use base::{ - clone_attr, cstr, debug, libc::mkstemp, Directory, FsPath, FsPathBuf, LibcReturn, LoggedError, - LoggedResult, MappedFile, Utf8CStr, Utf8CStrBufArr, WalkResult, + clone_attr, cstr, debug, libc::mkstemp, Directory, FsPath, FsPathBuf, LibcReturn, LoggedResult, + MappedFile, ResultNoLog, Utf8CStr, Utf8CStrBufArr, WalkResult, }; use crate::ffi::{prop_cb_exec, PropCb}; @@ -68,11 +68,8 @@ impl PropExt for PersistentProperties { } fn find(&mut self, name: &Utf8CStr) -> LoggedResult<&mut PersistentPropertyRecord> { - if let Ok(idx) = self.find_index(name) { - Ok(&mut self[idx]) - } else { - Err(LoggedError::default()) - } + let idx = self.find_index(name).no_log()?; + Ok(&mut self[idx]) } } @@ -85,7 +82,7 @@ fn file_get_prop(name: &Utf8CStr) -> LoggedResult { let path = FsPathBuf::new(&mut buf) .join(PERSIST_PROP_DIR!()) .join(name); - let mut file = path.open(O_RDONLY | O_CLOEXEC)?; + let mut file = path.open(O_RDONLY | O_CLOEXEC).no_log()?; debug!("resetprop: read prop from [{}]", path); let mut s = String::new(); file.read_to_string(&mut s)?; @@ -112,8 +109,8 @@ fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()> debug!("resetprop: write prop to [{}]", tmp); tmp.rename_to(path)? } else { + path.remove().no_log()?; debug!("resetprop: unlink [{}]", path); - path.remove()?; } Ok(()) } @@ -150,10 +147,11 @@ pub unsafe fn persist_get_prop(name: *const c_char, prop_cb: Pin<&mut PropCb>) { let name = unsafe { Utf8CStr::from_ptr(name)? }; if check_proto() { let mut props = proto_read_props()?; - if let Ok(PersistentPropertyRecord { + let prop = props.find(name)?; + if let PersistentPropertyRecord { name: Some(ref mut n), value: Some(ref mut v), - }) = props.find(name) + } = prop { prop_cb.exec(Utf8CStr::from_string(n), Utf8CStr::from_string(v)); } @@ -171,11 +169,11 @@ pub unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>) { fn inner(mut prop_cb: Pin<&mut PropCb>) -> LoggedResult<()> { if check_proto() { let mut props = proto_read_props()?; - props.iter_mut().for_each(|p| { + props.iter_mut().for_each(|prop| { if let PersistentPropertyRecord { name: Some(ref mut n), value: Some(ref mut v), - } = p + } = prop { prop_cb.exec(Utf8CStr::from_string(n), Utf8CStr::from_string(v)); } @@ -204,12 +202,9 @@ pub unsafe fn persist_delete_prop(name: *const c_char) -> bool { let name = unsafe { Utf8CStr::from_ptr(name)? }; if check_proto() { let mut props = proto_read_props()?; - if let Ok(idx) = props.find_index(name) { - props.remove(idx); - proto_write_props(&props) - } else { - Err(LoggedError::default()) - } + let idx = props.find_index(name).no_log()?; + props.remove(idx); + proto_write_props(&props) } else { file_set_prop(name, None) }