Skip to content

Commit

Permalink
[WIP] hardlinks - blocked on tar-rs support
Browse files Browse the repository at this point in the history
  • Loading branch information
the8472 committed Mar 18, 2021
1 parent d1817ef commit 47986e9
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ extern crate tar;
extern crate nix;

use std::io::*;
use std::path::Path;
use std::path::{Path, PathBuf};
use clap::{Arg, App};
use platter_walk::*;
use std::fs::File;
use tar::{Builder, Header, HeaderMode};
use tar::{Builder, Header, HeaderMode, EntryType};
use std::os::unix::io::{FromRawFd, AsRawFd};
use std::collections::HashMap;
use std::os::linux::fs::MetadataExt;

#[derive(Debug, Error)]
enum CliError {
Expand Down Expand Up @@ -86,6 +88,7 @@ fn process_args() -> std::result::Result<(), CliError> {
}

let mut builder = Builder::new(BufWriter::new(out));
let mut hardlinks: HashMap<(u64, u64), PathBuf> = HashMap::new();

loop {
match reap.next() {
Expand All @@ -96,6 +99,22 @@ fn process_args() -> std::result::Result<(), CliError> {
Some(Ok(mut reader)) => {
let mut p = reader.path().to_owned();
let meta = reader.metadata();

if meta.file_type().is_file() && meta.st_nlink() > 1 {
let val = hardlinks.entry((meta.st_dev(), meta.st_ino())).or_insert(p.clone());
if val != &p {
// hardlinked file we already visited
let mut header = Header::new_gnu();
header.set_metadata_in_mode(&meta, HeaderMode::Deterministic);
header.set_entry_type(EntryType::hard_link());
header.set_link_name(val).unwrap();
header.set_cksum();
// &p, std::io::empty()

continue;
}
}

//writeln!(std::io::stderr(), "before strip {}", p.to_string_lossy())?;
for path in &starting_points {
if p.starts_with(path) {
Expand Down

0 comments on commit 47986e9

Please sign in to comment.