Skip to content

Commit

Permalink
Merge pull request #34 from ktwrd/housekeeping/clippy-2
Browse files Browse the repository at this point in the history
Housekeeping/clippy 2
  • Loading branch information
ktwrd authored Sep 18, 2024
2 parents 64040c2 + bf28450 commit 6926b0d
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 162 deletions.
7 changes: 3 additions & 4 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::os::unix::fs::PermissionsExt;

use log::{debug,
error,
info,
trace};
info};

use crate::{depends,
helper,
Expand Down Expand Up @@ -216,7 +215,7 @@ impl RunnerContext
let location = self.gameinfo_location();
if helper::file_exists(location.clone())
{
let perm = std::fs::Permissions::from_mode(0o644 as u32);
let perm = std::fs::Permissions::from_mode(0o644_u32);
if let Err(e) = std::fs::set_permissions(&location, perm.clone())
{
let xe = BeansError::GameInfoPermissionSetFail {
Expand Down Expand Up @@ -330,7 +329,7 @@ impl RunnerContext
{
if let Err(e) = std::fs::remove_file(&ln_location)
{
trace!(
debug!(
"[RunnerContext::prepare_symlink] failed to remove {}\n{:#?}",
ln_location,
e
Expand Down
10 changes: 3 additions & 7 deletions src/depends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn try_write_deps()
#[cfg(not(target_os = "windows"))]
if helper::file_exists(get_butler_location())
{
let p = std::fs::Permissions::from_mode(0744 as u32);
let p = std::fs::Permissions::from_mode(0o0744_u32);
if let Err(e) = std::fs::set_permissions(get_butler_location(), p)
{
sentry::capture_error(&e);
Expand Down Expand Up @@ -77,11 +77,7 @@ pub async fn try_install_vcredist() -> Result<(), BeansError>
Ok(v) =>
{
let x: std::io::Result<u32> = v.get_value("Installed");
match x
{
Ok(_) => false,
Err(_) => true
}
x.is_err()
}
Err(_) => true
}
Expand All @@ -100,7 +96,7 @@ pub async fn try_install_vcredist() -> Result<(), BeansError>
)
.await?;

if std::path::Path::new(&out_loc).exists() == false
if !std::path::Path::new(&out_loc).exists()
{
return Err(BeansError::FileNotFound {
location: out_loc.clone(),
Expand Down
227 changes: 108 additions & 119 deletions src/extract.rs
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 {
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 {
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,
Expand All @@ -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()
Expand Down
1 change: 0 additions & 1 deletion src/gui/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ impl DialogBuilder

ui.win.set_label(&self.title);
ui.btn_ok.emit(send_action, GUIAppStatus::Quit);

window_centre_screen(&mut ui.win);
ui.win.make_resizable(false);
ui.win.show();
Expand Down
Loading

0 comments on commit 6926b0d

Please sign in to comment.