use easy_fuser::prelude::*;
use easy_fuser::templates::{DefaultFuseHandler, mirror_fs::*};
use std::fs::{self, File};
use std::io::{Read, Seek, SeekFrom, Write};
use std::time::Duration;
use tempfile::TempDir;
#[test]
fn test_mirror_fs_file_offsets() {
let mount_dir = TempDir::new().unwrap();
let source_dir = TempDir::new().unwrap();
let mntpoint = mount_dir.path().to_path_buf();
let source_path = source_dir.path().to_path_buf();
let mntpoint_clone = mntpoint.clone();
let handle = std::thread::spawn(move || {
let fs = MirrorFs::new(source_path.clone(), DefaultFuseHandler::new());
#[cfg(feature = "serial")]
mount(fs, &mntpoint_clone, &[]).unwrap();
#[cfg(not(feature = "serial"))]
mount(fs, &mntpoint_clone, &[], 4).unwrap();
});
std::thread::sleep(Duration::from_millis(50));
{
let test_file = mntpoint.join("offset_test.txt");
let mut file = File::create(&test_file).unwrap();
file.write_all(b"Hello, World!").unwrap();
file.sync_all().unwrap();
let mut file = File::open(&test_file).unwrap();
let mut buffer = [0u8; 5];
file.seek(SeekFrom::Start(0)).unwrap();
file.read_exact(&mut buffer).unwrap();
assert_eq!(&buffer, b"Hello");
file.seek(SeekFrom::Start(7)).unwrap();
file.read_exact(&mut buffer).unwrap();
assert_eq!(&buffer, b"World");
let mut file = fs::OpenOptions::new()
.read(true)
.write(true)
.open(&test_file)
.unwrap();
file.seek(SeekFrom::Start(7)).unwrap();
file.write_all(b"Rust!").unwrap();
file.sync_all().unwrap();
file.seek(SeekFrom::Start(0)).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
assert_eq!(content, "Hello, Rust!!");
file.seek(SeekFrom::Start(0)).unwrap();
file.seek(SeekFrom::Current(7)).unwrap();
file.read_exact(&mut buffer).unwrap();
assert_eq!(&buffer, b"Rust!");
file.seek(SeekFrom::End(-1)).unwrap();
file.read_exact(&mut buffer[0..1]).unwrap();
assert_eq!(buffer[0], b'!');
file.seek(SeekFrom::End(0)).unwrap();
file.write_all(b" Extended").unwrap();
file.sync_all().unwrap();
file.seek(SeekFrom::Start(0)).unwrap();
let mut extended_content = String::new();
file.read_to_string(&mut extended_content).unwrap();
assert_eq!(extended_content, "Hello, Rust!! Extended");
}
eprintln!("Unmounting filesystem...");
let _ = std::process::Command::new("fusermount")
.arg("-u")
.arg(&mntpoint)
.status();
handle.join().unwrap();
}