Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appending symbolic links to builder #95

Closed
clux opened this issue Feb 10, 2017 · 3 comments · Fixed by #117
Closed

appending symbolic links to builder #95

clux opened this issue Feb 10, 2017 · 3 comments · Fixed by #117

Comments

@clux
Copy link

clux commented Feb 10, 2017

Hi,

I'm trying to basically tar up a directory as is by walking a tree and adding the files therein, but am noticing that the symlinks that exists in the folder are duplicated as an actual full dereferenced copy in the archive. Is there a sane way to add symlinked files? Been grepping the tests and docs, but not really gotten any wiser.

This is what I've got:

    // pipe builder -> encoder -> file
    let file = File::create(&tarball)?;
    let mut encoder = GzEncoder::new(file, Compression::Default); // encoder writes file
    let mut builder = tar::Builder::new(&mut encoder); // tar builder writes to encoder

    let files = WalkDir::new("OUTPUT")
        .min_depth(1)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| !e.path().is_dir()); // ignore directories (these are created anyway)

    for f in files {
        let pth = f.path().strip_prefix("OUTPUT").unwrap();
        debug!("-> {}", pth.display());
        let mut f = File::open(f.path())?;
        builder.append_file(pth, &mut f)?;
    }

which causes symlinks to be copied.

From the looks of it one can do something with an explicit Header and set_link_names and using builder.append(), when something is a symlink. So have tried doing a match on fs::symlink_metadata(pth), and use builder.append() when it was a link using:

                    let mut h = Header::new_gnu();
                    h.set_path(pth).unwrap();
                    h.set_size(0);
                    let data : &[u8] = &[];
                    let f = fs::read_link(pth)?;
                    h.set_link_name(f)?;
                    builder.append(&h, data)?;

and the builder lets me do this, but this creates a damaged archive that tar tvf complains about. Any help / documentation on adding links would be great. I just want to do the equivalent of tar czvf mytar.tar OUTPUT

@alexcrichton
Copy link
Owner

Yeah right now the "easiest" way to do this is as you've found by creating a header manually and then appending it.

We could consider adding a new append_foo function though which doesn't traverse symlinks.

@ids1024
Copy link
Contributor

ids1024 commented Jun 30, 2017

@alexcrichton Would it be acceptable to add this as state to the builder, with something like builder.follow_symlinks(false)?

@alexcrichton
Copy link
Owner

Certainly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants