ksud: fix issues found by clippy (#167)

These issues are mostly found by `cargo clippy -- -W clippy::pedantic`.
This commit is contained in:
skbeh
2023-02-03 09:45:07 +08:00
committed by GitHub
parent bea93f6ad7
commit 219ea1c458
13 changed files with 217 additions and 245 deletions

View File

@@ -1,23 +1,21 @@
use anyhow::Result;
#[cfg(target_os = "android")]
#[cfg(unix)]
use anyhow::{Context, Ok};
#[cfg(target_os = "android")]
#[cfg(unix)]
use retry::delay::NoDelay;
#[cfg(target_os = "android")]
#[cfg(unix)]
use sys_mount::{unmount, FilesystemType, Mount, MountFlags, Unmount, UnmountFlags};
#[allow(dead_code)]
pub struct AutoMountExt4 {
mnt: String,
#[cfg(target_os = "android")]
#[cfg(unix)]
mount: Option<Mount>,
auto_umount: bool,
}
#[allow(dead_code)]
impl AutoMountExt4 {
#[cfg(target_os = "android")]
#[cfg(unix)]
pub fn try_new(src: &str, mnt: &str, auto_umount: bool) -> Result<Self> {
let result = Mount::builder()
.fstype(FilesystemType::from("ext4"))
@@ -27,11 +25,11 @@ impl AutoMountExt4 {
Ok(Self {
mnt: mnt.to_string(),
mount: Some(mount),
auto_umount
auto_umount,
})
});
if let Err(e) = result {
println!("- Mount failed: {}, retry with system mount", e);
println!("- Mount failed: {e}, retry with system mount");
let result = std::process::Command::new("mount")
.arg("-t")
.arg("ext4")
@@ -46,7 +44,7 @@ impl AutoMountExt4 {
Ok(Self {
mnt: mnt.to_string(),
mount: None,
auto_umount
auto_umount,
})
}
} else {
@@ -54,41 +52,45 @@ impl AutoMountExt4 {
}
}
#[cfg(not(target_os = "android"))]
#[cfg(not(unix))]
pub fn try_new(_src: &str, _mnt: &str, _auto_umount: bool) -> Result<Self> {
unimplemented!()
}
#[cfg(target_os = "android")]
#[cfg(unix)]
pub fn umount(&self) -> Result<()> {
match self.mount {
Some(ref mount) => mount
if let Some(ref mount) = self.mount {
mount
.unmount(UnmountFlags::empty())
.map_err(|e| anyhow::anyhow!(e)),
None => {
let result = std::process::Command::new("umount").arg(&self.mnt).status();
if let Err(e) = result {
Err(anyhow::anyhow!("umount: {} failed: {e}", self.mnt))
} else {
Ok(())
}
.map_err(|e| anyhow::anyhow!(e))
} else {
let result = std::process::Command::new("umount").arg(&self.mnt).status();
if let Err(e) = result {
Err(anyhow::anyhow!("umount: {} failed: {e}", self.mnt))
} else {
Ok(())
}
}
}
}
#[cfg(target_os = "android")]
#[cfg(unix)]
impl Drop for AutoMountExt4 {
fn drop(&mut self) {
log::info!("AutoMountExt4 drop: {}, auto_umount: {}", self.mnt, self.auto_umount);
log::info!(
"AutoMountExt4 drop: {}, auto_umount: {}",
self.mnt,
self.auto_umount
);
if self.auto_umount {
let _ = self.umount();
}
}
}
#[cfg(target_os = "android")]
fn do_mount_image(src: &str, target: &str, autodrop: bool) -> Result<()> {
#[allow(dead_code)]
#[cfg(unix)]
fn mount_image(src: &str, target: &str, autodrop: bool) -> Result<()> {
if autodrop {
Mount::builder()
.fstype(FilesystemType::from("ext4"))
@@ -103,23 +105,24 @@ fn do_mount_image(src: &str, target: &str, autodrop: bool) -> Result<()> {
Ok(())
}
#[cfg(target_os = "android")]
#[allow(dead_code)]
#[cfg(unix)]
pub fn mount_ext4(src: &str, target: &str, autodrop: bool) -> Result<()> {
// umount target first.
let _ = umount_dir(target);
let result = retry::retry(NoDelay.take(3), || do_mount_image(src, target, autodrop));
let result = retry::retry(NoDelay.take(3), || mount_image(src, target, autodrop));
result
.map_err(|e| anyhow::anyhow!("mount partition: {src} -> {target} failed: {e}"))
.map(|_| ())
}
#[cfg(target_os = "android")]
#[cfg(unix)]
pub fn umount_dir(src: &str) -> Result<()> {
unmount(src, UnmountFlags::empty()).with_context(|| format!("Failed to umount {src}"))?;
Ok(())
}
#[cfg(target_os = "android")]
#[cfg(unix)]
pub fn mount_overlay(lowerdir: &str, mnt: &str) -> Result<()> {
Mount::builder()
.fstype(FilesystemType::from("overlay"))
@@ -130,17 +133,17 @@ pub fn mount_overlay(lowerdir: &str, mnt: &str) -> Result<()> {
.map_err(|e| anyhow::anyhow!("mount partition: {mnt} overlay failed: {e}"))
}
#[cfg(not(target_os = "android"))]
#[cfg(not(unix))]
pub fn mount_ext4(_src: &str, _target: &str, _autodrop: bool) -> Result<()> {
unimplemented!()
}
#[cfg(not(target_os = "android"))]
#[cfg(not(unix))]
pub fn umount_dir(_src: &str) -> Result<()> {
unimplemented!()
}
#[cfg(not(target_os = "android"))]
#[cfg(not(unix))]
pub fn mount_overlay(_lowerdir: &str, _mnt: &str) -> Result<()> {
unimplemented!()
}