From b6ca5e3ea163f66239f6a835874fe231b2a9286f Mon Sep 17 00:00:00 2001 From: tiann Date: Mon, 20 Feb 2023 10:00:36 +0800 Subject: [PATCH] Wait for ueventd to create loop device on Android --- src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 20b1929..ebe89ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,7 +118,10 @@ impl LoopControl { LOOP_CTL_GET_FREE as IoctlRequest, ) })?; - LoopDevice::open(&format!("{}{}", LOOP_PREFIX, dev_num)) + let dev = format!("{}{}", LOOP_PREFIX, dev_num); + #[cfg(target_os = "android")] + wait_for_device(&dev); + LoopDevice::open(&dev) } /// Add and opens a new loop device. @@ -512,3 +515,19 @@ fn ioctl_to_error(ret: i32) -> io::Result { Ok(ret) } } + +// Android doesn't use devtmpfs. Instead device nodes under /dev are +// created by userspace daemon ueventd. There could be a noticeable delay +// between LOOP_CTL_GET_FREE issued and loop device created, so we need to +// wait until it is created and then continue. +fn wait_for_device>(device: P) { + let start = std::time::Instant::now(); + let timeout = std::time::Duration::from_secs(2); + let quantum = std::time::Duration::from_millis(1); + while !device.as_ref().exists() { + if start.elapsed() > timeout { + break; + } + std::thread::sleep(quantum); + } +}