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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of an alternative Builder which impl io::Read #316

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
17 changes: 1 addition & 16 deletions src/builder.rs
Expand Up @@ -4,7 +4,7 @@ use std::io::prelude::*;
use std::path::Path;
use std::str;

use crate::header::{path2bytes, HeaderMode};
use crate::header::{path2bytes, HeaderMode, prepare_header};
use crate::{other, EntryType, Header};

/// A structure for building archives
Expand Down Expand Up @@ -533,21 +533,6 @@ fn append_dir(
append_fs(dst, path, &stat, &mut io::empty(), mode, None)
}

fn prepare_header(size: u64, entry_type: u8) -> Header {
let mut header = Header::new_gnu();
let name = b"././@LongLink";
header.as_gnu_mut().unwrap().name[..name.len()].clone_from_slice(&name[..]);
header.set_mode(0o644);
header.set_uid(0);
header.set_gid(0);
header.set_mtime(0);
// + 1 to be compliant with GNU tar
header.set_size(size + 1);
header.set_entry_type(EntryType::new(entry_type));
header.set_cksum();
header
}

fn prepare_header_path(dst: &mut dyn Write, header: &mut Header, path: &Path) -> io::Result<()> {
// Try to encode the path directly in the header, but if it ends up not
// working (probably because it's too long) then try to use the GNU-specific
Expand Down
15 changes: 15 additions & 0 deletions src/header.rs
Expand Up @@ -1640,3 +1640,18 @@ pub fn bytes2path(bytes: Cow<[u8]>) -> io::Result<Cow<Path>> {
fn invalid_utf8<T>(_: T) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, "Invalid utf-8")
}

pub(crate) fn prepare_header(size: u64, entry_type: u8) -> Header {
let mut header = Header::new_gnu();
let name = b"././@LongLink";
header.as_gnu_mut().unwrap().name[..name.len()].clone_from_slice(&name[..]);
header.set_mode(0o644);
header.set_uid(0);
header.set_gid(0);
header.set_mtime(0);
// + 1 to be compliant with GNU tar
header.set_size(size + 1);
header.set_entry_type(EntryType::new(entry_type));
header.set_cksum();
header
}
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -30,6 +30,7 @@ pub use crate::entry_type::EntryType;
pub use crate::header::GnuExtSparseHeader;
pub use crate::header::{GnuHeader, GnuSparseHeader, Header, HeaderMode, OldHeader, UstarHeader};
pub use crate::pax::{PaxExtension, PaxExtensions};
pub use crate::streamer::Streamer;

mod archive;
mod builder;
Expand All @@ -38,6 +39,7 @@ mod entry_type;
mod error;
mod header;
mod pax;
mod streamer;

fn other(msg: &str) -> Error {
Error::new(ErrorKind::Other, msg)
Expand Down