From 19d481dbf79337924cb0a389cb2e073e80ce5495 Mon Sep 17 00:00:00 2001
From: root <root@localhost.localdomain>
Date: Wed, 6 Dec 2023 20:53:59 +0800
Subject: [PATCH 1/1] loopdev

---
 Cargo.lock                | 1 +
 exts/devmaster/Cargo.toml | 5 ++++-
 libs/device/Cargo.toml    | 6 ++++--
 libs/device/src/device.rs | 9 +++++----
 libs/device/src/utils.rs  | 9 +++++++--
 5 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index f103489..616a69c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -378,6 +378,7 @@ version = "0.5.1"
 dependencies = [
  "basic",
  "bitflags",
+ "device",
  "event",
  "fnmatch-sys",
  "libc",
diff --git a/exts/devmaster/Cargo.toml b/exts/devmaster/Cargo.toml
index c52e988..f9f2961 100644
--- a/exts/devmaster/Cargo.toml
+++ b/exts/devmaster/Cargo.toml
@@ -34,7 +34,7 @@ basic = { path = "../../libs/basic", default-features = false, features = [
     "argv",
 ] }
 blkid_rs = { path = "../../libs/blkid_rs" }
-device = { path = "../../libs/device" }
+device = { path = "../../libs/device", default-features = false }
 event = { path = "../../libs/event" }
 input_event_codes_rs = { path = "../../libs/input_event_codes_rs" }
 kmod_rs = { path = "../../libs/kmod_rs" }
@@ -78,3 +78,6 @@ fnmatch-sys = "1.0.0"
 
 [build-dependencies]
 basic = { path = "../../libs/basic", features = ["cargo"] }
+
+[dev-dependencies]
+device = { path = "../../libs/device", features = ["loopdev"] }
diff --git a/libs/device/Cargo.toml b/libs/device/Cargo.toml
index ecb9955..1737c17 100644
--- a/libs/device/Cargo.toml
+++ b/libs/device/Cargo.toml
@@ -20,8 +20,7 @@ log = { path = "../log" }
 # third libraries
 bitflags = "1.3.2"
 libc = { default-features = false, version = "0.2.140" }
-# only used in test case
-loopdev = "0.4.0"
+loopdev = { version = "0.4.0", optional = true } # only used in test case
 nix = { default-features = false, version = "0.24", features = [
     "ioctl",
     "user",
@@ -32,3 +31,6 @@ nix = { default-features = false, version = "0.24", features = [
 ] }
 snafu = { default-features = false, version = "0.7" }
 fnmatch-sys = "1.0.0"
+
+[dev-dependencies]
+device = { path = ".", features = ["loopdev"] }
diff --git a/libs/device/src/device.rs b/libs/device/src/device.rs
index d1ab230..c11de5b 100644
--- a/libs/device/src/device.rs
+++ b/libs/device/src/device.rs
@@ -2845,16 +2845,17 @@ impl PartialEq for Device {
 
 #[cfg(test)]
 mod tests {
-    use std::fs::OpenOptions;
-    use std::panic::catch_unwind;
-
     use crate::{
         device::*,
         device_enumerator::{DeviceEnumerationType, DeviceEnumerator},
-        utils::LoopDev,
     };
     use basic::IN_SET;
     use libc::S_IFBLK;
+    use std::fs::OpenOptions;
+    use std::panic::catch_unwind;
+
+    #[cfg(feature = "loopdev")]
+    use crate::utils::LoopDev;
 
     fn compare(dev1: &Device, dev2: &Device) -> bool {
         let syspath_1 = dev1.get_syspath().unwrap();
diff --git a/libs/device/src/utils.rs b/libs/device/src/utils.rs
index df750ed..2b814fa 100644
--- a/libs/device/src/utils.rs
+++ b/libs/device/src/utils.rs
@@ -11,12 +11,14 @@
 // See the Mulan PSL v2 for more details.
 
 //! utilities for device operation
+use crate::{error::*, Device};
 use nix::errno::Errno;
+use std::{cmp::Ordering, fmt::Debug, fs::DirEntry, path::Path};
 
-use crate::{error::*, Device};
+#[cfg(feature = "loopdev")]
 use loopdev::*;
+#[cfg(feature = "loopdev")]
 use std::path::PathBuf;
-use std::{cmp::Ordering, fmt::Debug, fs::DirEntry, path::Path};
 
 /// compare sound device
 pub(crate) fn sound_device_compare(devpath_a: &str, devpath_b: &str) -> Ordering {
@@ -106,11 +108,13 @@ pub(crate) fn readlink_value<P: AsRef<Path> + Debug>(path: P) -> Result<String,
 }
 
 /// loop device
+#[cfg(feature = "loopdev")]
 pub struct LoopDev {
     tmpfile: String,
     lodev: LoopDevice,
 }
 
+#[cfg(feature = "loopdev")]
 impl LoopDev {
     /// create a temporate file with specific size
     #[allow(dead_code)]
@@ -194,6 +198,7 @@ impl LoopDev {
     }
 }
 
+#[cfg(feature = "loopdev")]
 impl Drop for LoopDev {
     fn drop(&mut self) {
         let _ = self.lodev.detach();
-- 
2.33.0

