-
Notifications
You must be signed in to change notification settings - Fork 1
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
Housekeeping/clippy 2 #34
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bd17b92
bwah
toastxc 0a7695e
converted to matches macro
toastxc 0dfd776
reduced indents
toastxc 5951f27
removed more indentation
toastxc 802cced
oops...
toastxc d4b7a4d
final clippy changes
toastxc 04a7421
Merge branch 'develop' into housekeeping/clippy-2-katefix
ktwrd 747f89f
Merge branch 'develop' into housekeeping/clippy-2
ktwrd 81ce7cd
[extract::unpack_tarball] Fix progress bar being cleared in for loop
ktwrd bf28450
[extract] Remove unused function
ktwrd 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,160 +1,149 @@ | ||
use std::{backtrace::Backtrace, | ||
fs::File, | ||
io::Read}; | ||
fs::File}; | ||
|
||
use indicatif::{ProgressBar, | ||
ProgressStyle}; | ||
use log::{debug, error, info}; | ||
use log::{info, | ||
debug, | ||
error}; | ||
use zstd::stream::read::Decoder as ZstdDecoder; | ||
|
||
use crate::BeansError; | ||
|
||
fn unpack_tarball_getfile(tarball_location: String, output_directory: String) -> Result<File, BeansError> | ||
{ | ||
match File::open(&tarball_location) { | ||
Ok(x) => Ok(x), | ||
Err(e) => { | ||
Err(BeansError::TarExtractFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error: e, | ||
backtrace: Backtrace::capture(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
pub fn unpack_tarball( | ||
tarball_location: String, | ||
output_directory: String, | ||
show_progress: bool | ||
) -> Result<(), BeansError> | ||
{ | ||
let mut tarball = open_tarball_file(tarball_location.clone(), output_directory.clone())?; | ||
if show_progress | ||
let mut tarball = unpack_tarball_getfile(tarball_location.clone(), output_directory.clone())?; | ||
let mut archive = tar::Archive::new(&tarball); | ||
|
||
if !show_progress | ||
{ | ||
let mut archive_entries_instance = tar::Archive::new(&tarball); | ||
let archive_entries = match archive_entries_instance.entries() | ||
if let Err(e) = archive.unpack(&output_directory) | ||
{ | ||
Ok(v) => v, | ||
Err(e) => | ||
{ | ||
return Err(BeansError::TarExtractFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error: e, | ||
backtrace: Backtrace::capture() | ||
}); | ||
} | ||
}; | ||
let archive_entry_count = (&archive_entries.count()).clone() as u64; | ||
info!("Extracting {} files", archive_entry_count); | ||
return Err(BeansError::TarExtractFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error: e, | ||
backtrace: Backtrace::capture() | ||
}); | ||
} | ||
return Ok(()); | ||
}; | ||
|
||
let archive_entries = match archive.entries() | ||
{ | ||
Ok(v) => v, | ||
Err(e) => | ||
{ | ||
return Err(BeansError::TarExtractFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error: e, | ||
backtrace: Backtrace::capture() | ||
}); | ||
} | ||
}; | ||
let archive_entry_count = archive_entries.count() as u64; | ||
info!("Extracting {} files", archive_entry_count); | ||
|
||
tarball = unpack_tarball_getfile(tarball_location.clone(), output_directory.clone())?; | ||
archive = tar::Archive::new(&tarball); | ||
archive.set_preserve_permissions(false); | ||
|
||
let pb = ProgressBar::new(archive_entry_count); | ||
pb.set_style(ProgressStyle::with_template("{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} ({eta})") | ||
let pb = ProgressBar::new(archive_entry_count); | ||
pb.set_style(ProgressStyle::with_template("{msg}\n{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} ({eta})") | ||
.unwrap() | ||
.with_key("eta", |state: &indicatif::ProgressState, w: &mut dyn std::fmt::Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap()) | ||
.progress_chars("#>-")); | ||
pb.set_message("Extracting files"); | ||
pb.set_message("Extracting files"); | ||
|
||
// re-open the file, since tar::Archive::new will not work with a re-used file. | ||
tarball = open_tarball_file(tarball_location.clone(), output_directory.clone())?; | ||
let mut archive_inner = tar::Archive::new(&tarball); | ||
archive_inner.set_preserve_permissions(false); | ||
let mut idx: u64 = 0; | ||
match archive_inner.entries() | ||
let entries = match archive.entries() | ||
{ | ||
Ok(a) => a, | ||
Err(error) => | ||
{ | ||
Ok(etrs) => | ||
pb.finish_and_clear(); | ||
return Err(BeansError::TarExtractFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error, | ||
backtrace: Backtrace::capture() | ||
}); | ||
} | ||
}; | ||
|
||
for (size, entry) in entries.enumerate() | ||
{ | ||
match entry | ||
{ | ||
Ok(mut x) => | ||
{ | ||
for (size, entry) in etrs.enumerate() | ||
pb.set_message("Extracting files"); | ||
let mut filename = String::new(); | ||
|
||
if let Ok(Some(p)) = x.link_name() | ||
{ | ||
idx += 1; | ||
match entry | ||
if let Some(s) = p.to_str() | ||
{ | ||
Ok(mut x) => | ||
{ | ||
let ln = x.link_name(); | ||
pb.set_message("Extracting files"); | ||
let mut filename = String::new(); | ||
if let Ok(n) = ln | ||
{ | ||
if let Some(p) = n | ||
{ | ||
if let Some(s) = p.to_str() | ||
{ | ||
pb.set_message(format!("{:}", s)); | ||
filename = String::from(s); | ||
} | ||
} | ||
} | ||
|
||
if let Err(e) = x.unpack_in(&output_directory) | ||
{ | ||
pb.finish_and_clear(); | ||
debug!("[{idx:}] error={:#?}", e); | ||
debug!("[{idx:}] entry.path={:#?}", x.path()); | ||
debug!("[{idx:}] entry.link_name={:#?}", x.link_name()); | ||
debug!("[{idx:}] entry.size={:#?}", x.size()); | ||
debug!("[{idx:}] size={size:}"); | ||
error!("[extract::unpack_tarball] Failed to unpack file {filename} ({e:})"); | ||
let error = BeansError::TarUnpackItemFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
link_name: filename, | ||
error: e, | ||
backtrace: Backtrace::capture(), | ||
}; | ||
debug!("[{idx:}] {:#?}", error); | ||
return Err(error); | ||
} | ||
pb.inc(1); | ||
} | ||
Err(e) => | ||
{ | ||
pb.finish_and_clear(); | ||
debug!("[{idx:}] error={:#?}", e); | ||
debug!("[extract::unpack_tarball] idx: {idx:}, size={size:}"); | ||
error!("[extract::unpack_tarball] Failed to unpack entry ({e:})"); | ||
return Err(BeansError::TarExtractFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error: e, | ||
backtrace: Backtrace::capture() | ||
}); | ||
} | ||
}; | ||
pb.set_message(s.to_string()); | ||
filename = String::from(s); | ||
} | ||
} | ||
if let Err(error) = x.unpack_in(&output_directory) | ||
{ | ||
pb.finish_and_clear(); | ||
debug!("error={:#?}", error); | ||
debug!("entry.path={:#?}", x.path()); | ||
debug!("entry.link_name={:#?}", x.link_name()); | ||
debug!("entry.size={:#?}", x.size()); | ||
debug!("size={size:}"); | ||
error!("[extract::unpack_tarball] Failed to unpack file {filename} ({error:})"); | ||
return Err(BeansError::TarUnpackItemFailure { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
link_name: filename, | ||
error, | ||
backtrace: Backtrace::capture() | ||
}); | ||
} | ||
pb.inc(1); | ||
} | ||
Err(e) => | ||
Err(error) => | ||
{ | ||
pb.finish_and_clear(); | ||
debug!("{:#?}", e); | ||
error!("[extract::unpack_tarball] Failed to extract tarball entries (src: {tarball_location:}, dest: {output_directory:}, error: {e:})"); | ||
debug!("[extract::unpack_tarball] size={size:}, error={:#?}", error); | ||
error!("[extract::unpack_tarball] Failed to unpack entry ({error:})"); | ||
return Err(BeansError::TarExtractFailure { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error: e, | ||
error, | ||
backtrace: Backtrace::capture() | ||
}); | ||
} | ||
}; | ||
pb.finish(); | ||
debug!("[extract::unpack_tarball] Total entries extracted: {idx:}"); | ||
} | ||
else | ||
{ | ||
if let Err(e) = tar::Archive::new(&tarball).unpack(&output_directory) | ||
{ | ||
debug!("{:#?}", e); | ||
error!("[extract::unpack_tarball] Failed to unpack {tarball_location} to directory {output_directory} ({e:}"); | ||
return Err(BeansError::TarExtractFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error: e, | ||
backtrace: Backtrace::capture() | ||
}); | ||
} | ||
} | ||
pb.finish(); | ||
Ok(()) | ||
} | ||
fn open_tarball_file(tarball_location: String, output_directory: String) -> Result<File, BeansError> | ||
{ | ||
match File::open(&tarball_location) | ||
{ | ||
Ok(x) => Ok(x), | ||
Err(e) => Err(BeansError::TarExtractFailure { | ||
src_file: tarball_location, | ||
target_dir: output_directory, | ||
error: e, | ||
backtrace: Backtrace::capture() | ||
}) | ||
} | ||
} | ||
|
||
pub fn decompress_zstd( | ||
zstd_location: String, | ||
output_file: String, | ||
|
@@ -169,7 +158,7 @@ pub fn decompress_zstd( | |
let decoder = ZstdDecoder::new(zstd_file)?; | ||
// estimate extracted size as x2 since idk how to get the decompressed size with | ||
// zstd | ||
let pb_decompress = ProgressBar::new((zstd_file_length.clone() * 2) as u64); | ||
let pb_decompress = ProgressBar::new(*zstd_file_length * 2); | ||
pb_decompress | ||
.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})") | ||
.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
Oops, something went wrong.
Oops, something went wrong.
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.
pb.finish();
should be called beforereturn Err(