use crate::hasher::Hasher;
use crate::metadata::Metadata;
use smallvec::SmallVec;
use std::cell::RefCell;
use std::cmp::max;
use std::cmp::Ordering;
use std::io;
use std::path::Path;
#[derive(Debug, Clone)]
pub struct FileSet {
pub max_hardlinks: u64,
pub paths: SmallVec<[Box<Path>; 1]>,
}
impl FileSet {
pub fn new(path: Box<Path>, max_hardlinks: u64) -> Self {
let mut paths = SmallVec::new();
paths.push(path);
FileSet {
max_hardlinks,
paths,
}
}
pub fn push(&mut self, path: Box<Path>) {
self.paths.push(path);
}
pub fn links(&self) -> u64 {
max(self.max_hardlinks, self.paths.len() as u64)
}
}
#[derive(Debug)]
pub struct FileContent {
path: Box<Path>,
metadata: Metadata,
hashes: RefCell<Hasher>,
}
impl FileContent {
pub fn from_path(path: Box<Path>) -> Result<Self, io::Error> {
let m = Metadata::from_path(&path)?;
Ok(Self::new(path, m))
}
pub fn new(path: Box<Path>, metadata: Metadata) -> Self {
FileContent {
path,
metadata,
hashes: RefCell::new(Hasher::new()),
}
}
}
impl Eq for FileContent {}
impl PartialEq for FileContent {
fn eq(&self, other: &Self) -> bool {
self.partial_cmp(other)
.map_or(false, |o| o == Ordering::Equal)
}
}
impl Ord for FileContent {
fn cmp(&self, other: &Self) -> Ordering {
self.compare(other).unwrap_or(Ordering::Greater)
}
}
impl PartialOrd for FileContent {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.compare(other).ok()
}
}
impl FileContent {
fn compare(&self, other: &Self) -> io::Result<Ordering> {
if std::ptr::eq(self, other) {
return Ok(Ordering::Equal);
}
let cmp = self.metadata.cmp(&other.metadata);
if cmp != Ordering::Equal {
return Ok(cmp);
}
let mut hashes1 = self.hashes.borrow_mut();
let mut hashes2 = other.hashes.borrow_mut();
hashes1.compare(&mut hashes2, self.metadata.size, &self.path, &other.path)
}
}