Skip to content

Commit

Permalink
--tries, using (potentially) multiple tries for downloading manifes…
Browse files Browse the repository at this point in the history
…t, optimizing `Error`
  • Loading branch information
JonathanHelianthicusDoe committed Oct 15, 2019
1 parent 1f7ac15 commit 1ed551b
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 45 deletions.
6 changes: 4 additions & 2 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::{crate_name, crate_version};
use reqwest::blocking as rb;
use std::{
io::{self, prelude::*},
num::NonZeroUsize,
path::Path,
process,
time,
Expand Down Expand Up @@ -39,6 +40,7 @@ pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
quiet: bool,
maybe_usernames: Option<U>,
detach: bool,
max_tries: NonZeroUsize,
) -> Result<(), Error> {
let mut children = Vec::new();
if let Some(usernames) = maybe_usernames {
Expand Down Expand Up @@ -146,7 +148,7 @@ pub fn enter_command_mode<'a, P: AsRef<Path>, U: Iterator<Item = &'a str>>(
Some("update") | Some("up") => {
check_children(quiet, &mut children)?;
if children.is_empty() {
update::update(config, client, quiet)?
update::update(config, client, quiet, max_tries)?
} else if children.len() == 1 {
println!(
"There's still a game instance running, can't update \
Expand Down Expand Up @@ -345,7 +347,7 @@ fn kill_instance(
if !quiet {
println!(
"Successfully killed {}'s instance with pid {},",
name, pid
name, pid,
);
let secs = uptime_sec % 60;
let minutes = (uptime_sec / 60) % 60;
Expand Down
32 changes: 24 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum Error {
BadPatchSize,
SeekError(PathBuf, io::Error),
PatchSanityCheckFail(u8),
FileRenameError(PathBuf, PathBuf, io::Error),
FileRenameError(PathBuf, PathBuf),
NotDir(PathBuf),
RemoveFileError(PathBuf, io::Error),
#[allow(dead_code)]
Expand All @@ -42,6 +42,8 @@ pub enum Error {
ThreadSpawnError(io::Error),
ThreadJoinError(io::Error),
ProcessKillError(u32, io::Error),
HashMismatch(PathBuf, [u8; 20]),
InvalidArgValue(&'static str),
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -116,11 +118,8 @@ impl fmt::Display for Error {
),
Self::PatchSanityCheckFail(i) =>
write!(f, "During patching, sanity check #{} failed", i),
Self::FileRenameError(from, to, ioe) => write!(
f,
"Error renaming file from {:?} to {:?}:\n\t{}",
from, to, ioe,
),
Self::FileRenameError(from, to) =>
write!(f, "Error renaming file from {:?} to {:?}", from, to),
Self::NotDir(path) => write!(f, "{:?} is not a directory", path),
Self::RemoveFileError(path, ioe) =>
write!(f, "Error removing file {:?}:\n\t{}", path, ioe),
Expand Down Expand Up @@ -152,9 +151,24 @@ impl fmt::Display for Error {
write!(f, "Error attempting to join thread:\n\t{}", ioe),
Self::ProcessKillError(pid, ioe) => write!(
f,
"Error killing child process with PID {}:\n\t{}",
"Error killing child process with pid {}:\n\t{}",
pid, ioe,
),
Self::HashMismatch(path, expected) => {
write!(
f,
"SHA1 hash of local file {:?} did not match manifest's \
hash of ",
path,
)?;
for b in expected.iter() {
write!(f, "{:02x}", b)?;
}

Ok(())
},
Self::InvalidArgValue(param) =>
write!(f, "Invalid value for the argument of {}", param),
}
}
}
Expand Down Expand Up @@ -186,7 +200,7 @@ impl Error {
Self::BadPatchSize => 20,
Self::SeekError(_, _) => 21,
Self::PatchSanityCheckFail(_) => 22,
Self::FileRenameError(_, _, _) => 23,
Self::FileRenameError(_, _) => 23,
Self::NotDir(_) => 24,
Self::RemoveFileError(_, _) => 25,
Self::MissingFile(_) => 26,
Expand All @@ -200,6 +214,8 @@ impl Error {
Self::ThreadSpawnError(_) => 34,
Self::ThreadJoinError(_) => 35,
Self::ProcessKillError(_, _) => 36,
Self::HashMismatch(_, _) => 37,
Self::InvalidArgValue(_) => 38,
}
}
}
29 changes: 27 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![forbid(unsafe_code)]
#![deny(clippy::all)]
#![deny(deprecated)]

mod command;
mod config;
Expand All @@ -19,7 +20,7 @@ use clap::{
};
use error::Error;
use reqwest::blocking as rb;
use std::process;
use std::{num::NonZeroUsize, process};

fn main() {
if let Err(e) = run() {
Expand Down Expand Up @@ -163,9 +164,32 @@ fn run() -> Result<(), Error> {
)
.takes_value(false),
)
.arg(
Arg::with_name("tries")
.short("t")
.long("tries")
.help(
"Positive integer number of times to try doing things \
involving the network. Defaults to 5.",
)
.long_help(
"Positive integer number of times to try doing things \
that involve interacting with the network. Defaults to \
5. Currently works for downloading files, including the \
manifest.",
)
.takes_value(true),
)
.get_matches();

let quiet = arg_matches.is_present("quiet");
let max_tries = if let Some(tries_str) = arg_matches.value_of("tries") {
tries_str
.parse()
.map_err(|_| Error::InvalidArgValue("--tries/-t"))?
} else {
NonZeroUsize::new(5).unwrap()
};

let (mut config, config_path) = config::get_config(
arg_matches.is_present("no-config"),
Expand All @@ -180,7 +204,7 @@ fn run() -> Result<(), Error> {
.map_err(Error::HttpClientCreateError)?;

if !arg_matches.is_present("no-auto-update") {
update::update(&config, &client, quiet)?;
update::update(&config, &client, quiet, max_tries)?;

if !quiet {
println!();
Expand All @@ -194,5 +218,6 @@ fn run() -> Result<(), Error> {
quiet,
arg_matches.values_of("username"),
arg_matches.is_present("detach"),
max_tries,
)
}
3 changes: 1 addition & 2 deletions src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ pub fn patch_file<P: AsRef<Path>, Q: AsRef<Path>>(

bsdiff_patch(patch_file_path, &target_file_path, &temp_file_path)?;

std::fs::rename(&temp_file_path, &target_file_path).map_err(|ioe| {
std::fs::rename(&temp_file_path, &target_file_path).map_err(|_| {
Error::FileRenameError(
temp_file_path.into(),
target_file_path.as_ref().to_path_buf(),
ioe,
)
})?;

Expand Down
Loading

0 comments on commit 1ed551b

Please sign in to comment.