From 6a59939d9a9caa63e0cea660ac04c3d29975a058 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 13 Sep 2023 18:09:16 -0700 Subject: [PATCH] Remove for_all_file --- native/src/base/files.rs | 21 --------------------- native/src/core/resetprop/persist.rs | 23 ++++++++++++++--------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/native/src/base/files.rs b/native/src/base/files.rs index cd61c1bdb..b9220d04f 100644 --- a/native/src/base/files.rs +++ b/native/src/base/files.rs @@ -295,27 +295,6 @@ impl Directory { })?; Ok(()) } - - pub fn for_all_file io::Result>( - &mut self, - mut f: F, - ) -> io::Result { - use WalkResult::*; - loop { - match self.read()? { - None => return Ok(Continue), - Some(ref e) => { - if e.is_dir() { - return Ok(Continue); - } - match f(e)? { - Abort | Skip => return Ok(Continue), - Continue => {} - } - } - } - } - } } impl Directory { diff --git a/native/src/core/resetprop/persist.rs b/native/src/core/resetprop/persist.rs index 7abc09bd7..27cc7079d 100644 --- a/native/src/core/resetprop/persist.rs +++ b/native/src/core/resetprop/persist.rs @@ -86,7 +86,7 @@ fn file_get_prop(name: &Utf8CStr) -> LoggedResult { .join(PERSIST_PROP_DIR!()) .join(name); let mut file = path.open(O_RDONLY | O_CLOEXEC)?; - debug!("resetprop: read prop from [{}]\n", path); + debug!("resetprop: read prop from [{}]", path); let mut s = String::new(); file.read_to_string(&mut s)?; Ok(s) @@ -99,7 +99,9 @@ fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()> .join(name); if let Some(value) = value { let mut buf = Utf8CStrArr::default(); - let mut tmp = FsPathBuf::new(&mut buf).join(concat!(PERSIST_PROP_DIR!(), ".prop.XXXXXX")); + let mut tmp = FsPathBuf::new(&mut buf) + .join(PERSIST_PROP_DIR!()) + .join("prop.XXXXXX"); { let mut f = unsafe { let fd = mkstemp(tmp.as_mut_ptr()).check_os_err()?; @@ -107,10 +109,10 @@ fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()> }; f.write_all(value.as_bytes())?; } - debug!("resetprop: write prop to [{}]\n", tmp); + debug!("resetprop: write prop to [{}]", tmp); tmp.rename_to(path)? } else { - debug!("resetprop: unlink [{}]\n", path); + debug!("resetprop: unlink [{}]", path); path.remove()?; } Ok(()) @@ -180,13 +182,16 @@ pub unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>) { }); } else { let mut dir = Directory::open(cstr!(PERSIST_PROP_DIR!()))?; - dir.for_all_file(|f| { - if let Ok(name) = Utf8CStr::from_bytes(f.d_name().to_bytes()) { - if let Ok(mut value) = file_get_prop(name) { - prop_cb.exec(name, Utf8CStr::from_string(&mut value)); + dir.pre_order_walk(|e| { + if e.is_file() { + if let Ok(name) = Utf8CStr::from_cstr(e.d_name()) { + if let Ok(mut value) = file_get_prop(name) { + prop_cb.exec(name, Utf8CStr::from_string(&mut value)); + } } } - Ok(WalkResult::Continue) + // Do not traverse recursively + Ok(WalkResult::Skip) })?; } Ok(())