-
Notifications
You must be signed in to change notification settings - Fork 185
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
Add contiguous mode to the Builder #234
Open
synek317
wants to merge
1
commit into
alexcrichton:main
Choose a base branch
from
synek317:contiguous_builder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::fs::{read_dir, File}; | ||
use std::io::Write; | ||
use std::iter::Peekable; | ||
use std::path::PathBuf; | ||
|
||
struct TarStream<I: Iterator<Item = PathBuf>> { | ||
files: Peekable<I>, | ||
} | ||
|
||
impl<I: Iterator<Item = PathBuf>> TarStream<I> { | ||
pub fn new(files: I) -> Self { | ||
Self { | ||
files: files.peekable(), | ||
} | ||
} | ||
|
||
pub fn read(&mut self, buf: &mut Vec<u8>) -> Option<()> { | ||
self.files.next().map(|path| { | ||
let new_path = PathBuf::from("archived").join(&path); | ||
let mut builder = tar::Builder::new(buf); | ||
|
||
builder.contiguous(true); | ||
builder.append_path_with_name(path, &new_path).unwrap(); | ||
|
||
if self.files.peek().is_none() { | ||
builder.finish().unwrap(); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
fn main() { | ||
let files = read_dir("examples") | ||
.unwrap() | ||
.map(|p| p.unwrap().path()) | ||
.filter(|p| p.is_file()); | ||
let mut buf = Vec::with_capacity(1024 * 1024 * 4); | ||
let mut tar_stream = TarStream::new(files); | ||
let mut output = File::create("examples.tar").unwrap(); | ||
|
||
while tar_stream.read(&mut buf).is_some() { | ||
output.write(&buf).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
finish()
is a public method, I think that it might be best if this check is folded into there? That can just keep the documentation, though, that it is not required to be called.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've introduced
finalize
since I wanted to give the user the possibility to write the 'footer' section, as you can see in the example.I could move the check to
finish
and then create public methodwrite_footer_section
. Then this:would have no effect and would need to be transformed into:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think though that this implementation, as-is, doesn't work. If you call
finish
it ignores theskip_footer_section
flag introduced here, which I think is a bug. I think it'd be fine to basically say thatfinish
is optional ifskip_footer_section
istrue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does work, please take a look at the example I've added to
examples/
or even tests. But I see your point, I will renamecontiguous
toskip_footer_section
and moveTo new public method
write_footer_section
. Would that work in your opinion?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes while it works I think it's too confusing. I think adding a separate method makes sense. That way there's a flag to disable auto-inclusion of the footer and in that scenario you can either create a
tar::Builder
at the end with the flag enabled or you can manually call the footer write.