1*01c18c64Ssparkzky extern crate libc;
2*01c18c64Ssparkzky use core::ffi::{c_char, c_void};
3*01c18c64Ssparkzky use libc::{mount, umount};
4*01c18c64Ssparkzky use nix::errno::Errno;
5*01c18c64Ssparkzky use std::fs;
6*01c18c64Ssparkzky use std::os::unix::fs::symlink;
7*01c18c64Ssparkzky use std::path::Path;
8*01c18c64Ssparkzky
main()9*01c18c64Ssparkzky fn main() {
10*01c18c64Ssparkzky mount_test_ramfs();
11*01c18c64Ssparkzky
12*01c18c64Ssparkzky let target = "/mnt/myramfs/target_file.txt";
13*01c18c64Ssparkzky let symlink_path = "/mnt/myramfs/another/symlink_file.txt";
14*01c18c64Ssparkzky let dir = "/mnt/myramfs/another";
15*01c18c64Ssparkzky
16*01c18c64Ssparkzky fs::write(target, "This is the content of the target file.")
17*01c18c64Ssparkzky .expect("Failed to create target file");
18*01c18c64Ssparkzky fs::create_dir(dir).expect("Failed to create target dir");
19*01c18c64Ssparkzky
20*01c18c64Ssparkzky assert!(Path::new(target).exists(), "Target file was not created");
21*01c18c64Ssparkzky assert!(Path::new(dir).exists(), "Target dir was not created");
22*01c18c64Ssparkzky
23*01c18c64Ssparkzky symlink(target, symlink_path).expect("Failed to create symlink");
24*01c18c64Ssparkzky
25*01c18c64Ssparkzky assert!(Path::new(symlink_path).exists(), "Symlink was not created");
26*01c18c64Ssparkzky
27*01c18c64Ssparkzky let symlink_content = fs::read_link(symlink_path).expect("Failed to read symlink");
28*01c18c64Ssparkzky assert_eq!(
29*01c18c64Ssparkzky symlink_content.display().to_string(),
30*01c18c64Ssparkzky target,
31*01c18c64Ssparkzky "Symlink points to the wrong target"
32*01c18c64Ssparkzky );
33*01c18c64Ssparkzky
34*01c18c64Ssparkzky fs::remove_file(symlink_path).expect("Failed to remove symlink");
35*01c18c64Ssparkzky fs::remove_file(target).expect("Failed to remove target file");
36*01c18c64Ssparkzky fs::remove_dir(dir).expect("Failed to remove test_dir");
37*01c18c64Ssparkzky
38*01c18c64Ssparkzky assert!(!Path::new(symlink_path).exists(), "Symlink was not deleted");
39*01c18c64Ssparkzky assert!(!Path::new(target).exists(), "Target file was not deleted");
40*01c18c64Ssparkzky assert!(!Path::new(dir).exists(), "Directory was not deleted");
41*01c18c64Ssparkzky
42*01c18c64Ssparkzky umount_test_ramfs();
43*01c18c64Ssparkzky
44*01c18c64Ssparkzky println!("All tests passed!");
45*01c18c64Ssparkzky }
46*01c18c64Ssparkzky
mount_test_ramfs()47*01c18c64Ssparkzky fn mount_test_ramfs() {
48*01c18c64Ssparkzky let path = Path::new("mnt/myramfs");
49*01c18c64Ssparkzky let dir = fs::create_dir_all(path);
50*01c18c64Ssparkzky assert!(dir.is_ok(), "mkdir /mnt/myramfs failed");
51*01c18c64Ssparkzky
52*01c18c64Ssparkzky let source = b"\0".as_ptr() as *const c_char;
53*01c18c64Ssparkzky let target = b"/mnt/myramfs\0".as_ptr() as *const c_char;
54*01c18c64Ssparkzky let fstype = b"ramfs\0".as_ptr() as *const c_char;
55*01c18c64Ssparkzky // let flags = MS_BIND;
56*01c18c64Ssparkzky let flags = 0;
57*01c18c64Ssparkzky let data = std::ptr::null() as *const c_void;
58*01c18c64Ssparkzky let result = unsafe { mount(source, target, fstype, flags, data) };
59*01c18c64Ssparkzky
60*01c18c64Ssparkzky assert_eq!(
61*01c18c64Ssparkzky result,
62*01c18c64Ssparkzky 0,
63*01c18c64Ssparkzky "Mount myramfs failed, errno: {}",
64*01c18c64Ssparkzky Errno::last().desc()
65*01c18c64Ssparkzky );
66*01c18c64Ssparkzky println!("Mount myramfs success!");
67*01c18c64Ssparkzky }
68*01c18c64Ssparkzky
umount_test_ramfs()69*01c18c64Ssparkzky fn umount_test_ramfs() {
70*01c18c64Ssparkzky let path = b"/mnt/myramfs\0".as_ptr() as *const c_char;
71*01c18c64Ssparkzky let result = unsafe { umount(path) };
72*01c18c64Ssparkzky if result != 0 {
73*01c18c64Ssparkzky let err = Errno::last();
74*01c18c64Ssparkzky println!("Errno: {}", err);
75*01c18c64Ssparkzky println!("Infomation: {}", err.desc());
76*01c18c64Ssparkzky }
77*01c18c64Ssparkzky assert_eq!(result, 0, "Umount myramfs failed");
78*01c18c64Ssparkzky }
79