xref: /DragonOS/user/apps/test-mount/src/main.rs (revision 01c18c64b14b4ebabd98fa92c587c26874275eb1)
11d37ca6dSDonkey Kane use core::ffi::{c_char, c_void};
2*01c18c64Ssparkzky use errno::errno;
31d37ca6dSDonkey Kane use libc::{mount, MS_BIND};
4b8ed3825SDonkey Kane use std::fs;
5b8ed3825SDonkey Kane use std::path::Path;
6911132c4SDonkey Kane use std::time;
7*01c18c64Ssparkzky 
main()81d37ca6dSDonkey Kane fn main() {
9b8ed3825SDonkey Kane     let path = Path::new("mnt/tmp");
10b8ed3825SDonkey Kane     let dir = fs::create_dir_all(path);
11b8ed3825SDonkey Kane     if dir.is_err() {
12b8ed3825SDonkey Kane         panic!("mkdir /mnt/tmp fail.");
13b8ed3825SDonkey Kane     }
14911132c4SDonkey Kane     let clock = time::Instant::now();
151d37ca6dSDonkey Kane     let source = b"\0".as_ptr() as *const c_char;
161d37ca6dSDonkey Kane     let target = b"/mnt/tmp\0".as_ptr() as *const c_char;
171d37ca6dSDonkey Kane     let fstype = b"ramfs\0".as_ptr() as *const c_char;
181d37ca6dSDonkey Kane     let flags = MS_BIND;
191d37ca6dSDonkey Kane     let data = std::ptr::null() as *const c_void;
201d37ca6dSDonkey Kane     let result = unsafe { mount(source, target, fstype, flags, data) };
21b8ed3825SDonkey Kane     let path = Path::new("mnt/tmp/tmp");
22b8ed3825SDonkey Kane     let dir = fs::create_dir_all(path);
23b8ed3825SDonkey Kane     if dir.is_err() {
24b8ed3825SDonkey Kane         panic!("mkdir /mnt/tmp/tmp fail.");
25b8ed3825SDonkey Kane     }
26b8ed3825SDonkey Kane     let target = b"/mnt/tmp/tmp\0".as_ptr() as *const c_char;
27b8ed3825SDonkey Kane     let result = unsafe { mount(source, target, fstype, flags, data) };
281d37ca6dSDonkey Kane     if result == 0 {
291d37ca6dSDonkey Kane         println!("Mount successful");
301d37ca6dSDonkey Kane     } else {
31*01c18c64Ssparkzky         let err = errno();
32*01c18c64Ssparkzky         println!("Mount failed with error code: {}", err.0);
331d37ca6dSDonkey Kane     }
34911132c4SDonkey Kane     let dur = clock.elapsed();
35911132c4SDonkey Kane     println!("mount costing time: {} ns", dur.as_nanos());
361d37ca6dSDonkey Kane }
37