1 use core::ffi::{c_char, c_void}; 2 use errno::errno; 3 use libc::{mount, MS_BIND}; 4 use std::fs; 5 use std::path::Path; 6 use std::time; 7 8 fn main() { 9 let path = Path::new("mnt/tmp"); 10 let dir = fs::create_dir_all(path); 11 if dir.is_err() { 12 panic!("mkdir /mnt/tmp fail."); 13 } 14 let clock = time::Instant::now(); 15 let source = b"\0".as_ptr() as *const c_char; 16 let target = b"/mnt/tmp\0".as_ptr() as *const c_char; 17 let fstype = b"ramfs\0".as_ptr() as *const c_char; 18 let flags = MS_BIND; 19 let data = std::ptr::null() as *const c_void; 20 let result = unsafe { mount(source, target, fstype, flags, data) }; 21 let path = Path::new("mnt/tmp/tmp"); 22 let dir = fs::create_dir_all(path); 23 if dir.is_err() { 24 panic!("mkdir /mnt/tmp/tmp fail."); 25 } 26 let target = b"/mnt/tmp/tmp\0".as_ptr() as *const c_char; 27 let result = unsafe { mount(source, target, fstype, flags, data) }; 28 if result == 0 { 29 println!("Mount successful"); 30 } else { 31 let err = errno(); 32 println!("Mount failed with error code: {}", err.0); 33 } 34 let dur = clock.elapsed(); 35 println!("mount costing time: {} ns", dur.as_nanos()); 36 } 37