From 3c49fb2324cef3d436e74cd7eeeb444eadc54166 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 12 Dec 2023 01:45:27 -0500 Subject: [PATCH] Add test for hardlink nodes and ensure it passes --- src/node.rs | 45 ++++++++++++++++++++++++++++++++++++--------- src/special.rs | 2 +- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/node.rs b/src/node.rs index 69c96cb..b703a86 100644 --- a/src/node.rs +++ b/src/node.rs @@ -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() { { diff --git a/src/special.rs b/src/special.rs index c4c4d9d..f1abed3 100644 --- a/src/special.rs +++ b/src/special.rs @@ -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,