Add test for hardlink nodes and ensure it passes

This commit is contained in:
Nathan Fisher 2023-12-12 01:45:27 -05:00
parent ff1afa6453
commit 3c49fb2324
2 changed files with 37 additions and 10 deletions

View File

@ -228,17 +228,15 @@ impl Node {
};
let n_path = if path.starts_with('/') {
PathBuf::from(&path)
} else if let Ok(mut p) = env::current_dir() {
p.push(&path);
p
} else {
if let Ok(mut p) = env::current_dir() {
p.push(&path);
p
} else {
PathBuf::from(&format!("./{path}"))
}
PathBuf::from(&format!("./{path}"))
};
if let Some(p) = n_path.parent() {
if !p.exists() {
self.mkdir(&p)?;
self.mkdir(p)?;
}
}
match self.filetype {
@ -280,7 +278,7 @@ impl Node {
} else {
t.to_string()
};
fs::hard_link(&target, &path)?;
fs::hard_link(target, &path)?;
}
FileType::SoftLink(ref t) => {
symlink(&self.name, t)?;
@ -310,7 +308,7 @@ impl Node {
#[cfg(test)]
mod tests {
use super::*;
use std::{fmt::Write, fs::remove_file};
use std::{ffi::CString, fmt::Write, fs::remove_file};
static LI: &[u8] = include_bytes!("../test/li.txt");
@ -348,6 +346,35 @@ mod tests {
assert_eq!(tgt, "li.txt");
}
#[test]
fn from_hardlink_path() {
let _res = remove_file("test/lorem.txt");
fs::hard_link("test/li.txt", "test/lorem.txt").unwrap();
let links = Mutex::new(HashMap::new());
let _node0 = Node::from_path("test/li.txt", Algorithm::Sha256, &links).unwrap();
let node1 = Node::from_path("test/lorem.txt", Algorithm::Sha256, &links).unwrap();
match node1.filetype {
FileType::HardLink(s) => assert_eq!(s, "test/li.txt"),
_ => panic!(),
}
}
// TODO: Discover why this test blocks
/*#[test]
fn from_fifo_path() {
let _res = remove_file("test/fifo");
let fname = CString::new("test/fifo").unwrap();
unsafe {
libc::mkfifo(fname.as_ptr(), 0o644);
}
let links = Mutex::new(HashMap::new());
let node = Node::from_path("test/fifo", Algorithm::Skip, &links).unwrap();
let FileType::Fifo = node.filetype else {
eprintln!("Incorrect filetype: {:?}", node.filetype);
panic!();
};
}*/
#[test]
fn load_store_file() {
{

View File

@ -41,7 +41,7 @@ impl Special {
major |= (rdev & 0x00000000000fff00) >> 8;
major |= (rdev & 0xfffff00000000000) >> 32;
let mut minor = 0;
minor |= (rdev & 0x00000000000000ff) >> 0;
minor |= rdev & 0x00000000000000ff;
minor |= (rdev & 0x00000ffffff00000) >> 12;
Self {
major: major as u32,