Skip to content

Commit

Permalink
Chore: address 'cargo clippy' recommendations (#23)
Browse files Browse the repository at this point in the history
* build(agent): fix different Rust build manifests for dev and prod

* Cargo.toml is used for production builds: it has no 'dev-dependencies'
  and it adds 'rustls' feature to 'reqwest'
* Cargo-dev.toml is used for developmet: it has  'dev-dependencies' and
  does not need 'rustls'

* chore(agent): address 'cargo clippy' static analyzer findings

* chore(agent): update code style (rustfmt)
  • Loading branch information
marekful authored May 7, 2023
1 parent a217eb3 commit 75c1188
Show file tree
Hide file tree
Showing 17 changed files with 564 additions and 757 deletions.
734 changes: 290 additions & 444 deletions agent/Cargo-dev.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions agent/Cargo-dev.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webscp-agent"
version = "0.1.3"
version = "0.1.4"
authors = ["Marcell Fülöp <[email protected]>"]
repository = "https://github.com/marekful/webscp/agent"
edition = "2021"
Expand All @@ -11,7 +11,7 @@ publish = false
[dependencies]
ssh2 = "0.9.4"
serde = "1.0.156"
reqwest = { version = "0.11", features = ["blocking", "rustls-tls"] }
reqwest = { version = "0.11", features = ["blocking"] }
urlencoding = "2.1.2"
json = "0.12.4"
flate2 = "1.0.25"
Expand Down
4 changes: 2 additions & 2 deletions agent/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions agent/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webscp-agent"
version = "0.1.3"
version = "0.1.4"
authors = ["Marcell Fülöp <[email protected]>"]
repository = "https://github.com/marekful/webscp/agent"
edition = "2021"
Expand All @@ -11,7 +11,7 @@ publish = false
[dependencies]
ssh2 = "0.9.4"
serde = "1.0.156"
reqwest = { version = "0.11", features = ["blocking"] }
reqwest = { version = "0.11", features = ["blocking", "rustls-tls"] }
urlencoding = "2.1.2"
json = "0.12.4"
flate2 = "1.0.25"
Expand Down
96 changes: 46 additions & 50 deletions agent/src/cli/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use rocket::tokio::task;
use std::{
fs,
fs::File,
future::Future,
io::Error,
path::Path,
time::{Duration, Instant},
Expand Down Expand Up @@ -43,9 +42,9 @@ pub struct ProgressCounter {
impl Clone for ProgressCounter {
fn clone(&self) -> Self {
Self {
items_total: self.items_total.clone(),
items_added: self.items_added.clone(),
files_added: self.files_added.clone(),
items_total: self.items_total,
items_added: self.items_added,
files_added: self.files_added,
}
}
}
Expand All @@ -66,7 +65,7 @@ impl ArchiveWriter {
}
};

return if compress {
if compress {
let enc = GzEncoder::new(file, Compression::fast());
let writer = Builder::new(enc);
Ok(Self {
Expand Down Expand Up @@ -97,59 +96,57 @@ impl ArchiveWriter {
},
last_update_sent_at: Instant::now(),
})
};
}
}

pub fn crate_archive<'a>(
pub async fn crate_archive<'a>(
&'a mut self,
items: Vec<ArchiveItem>,
transfer: &'a Transfer,
) -> impl Future<Output = Result<(), ArchiveError>> + '_ {
async move {
self.progress.items_total = items.len();

// loop through submitted items adding each to the archive
for item in items.iter() {
let src_ = decode(&item.source).unwrap().into_owned();
let dst_ = decode(&item.destination).unwrap().into_owned();
let src = String::from(src_.replacen("/files", &self.source_base_path, 1));
let dst = String::from(dst_.trim_start_matches("/"));
self.progress.items_added += 1;

let res = match self.add_file_to_archive(src.clone(), dst, transfer) {
Ok(_) => Ok::<(), Error>(()),
Err(e) => {
return Err(ArchiveError {
code: 301,
message: e.to_string(),
});
}
};

if res.is_err() {
) -> Result<(), ArchiveError> {
self.progress.items_total = items.len();

// loop through submitted items adding each to the archive
for item in items.iter() {
let src_ = decode(&item.source).unwrap().into_owned();
let dst_ = decode(&item.destination).unwrap().into_owned();
let src = src_.replacen("/files", &self.source_base_path, 1);
let dst = String::from(dst_.trim_start_matches('/'));
self.progress.items_added += 1;

let res = match self.add_file_to_archive(src.clone(), dst, transfer) {
Ok(_) => Ok::<(), Error>(()),
Err(e) => {
return Err(ArchiveError {
code: 302,
message: res.unwrap_err().to_string(),
code: 301,
message: e.to_string(),
});
}
};

task::yield_now().await;

// send progress update
let msg = &format!(
"progress::{}::{}/{}/{}",
self.get_job_type(),
self.progress.items_added,
self.progress.items_total,
self.progress.files_added,
);
FilesApi::new()
.send_upload_status_update_async(transfer, msg)
.await;
if let Err(err) = res {
return Err(ArchiveError {
code: 302,
message: err.to_string(),
});
}

Ok(())
task::yield_now().await;

// send progress update
let msg = &format!(
"progress::{}::{}/{}/{}",
self.get_job_type(),
self.progress.items_added,
self.progress.items_total,
self.progress.files_added,
);
FilesApi::new()
.send_upload_status_update_async(transfer, msg)
.await;
}

Ok(())
}

fn remove_archive(&self) {
Expand Down Expand Up @@ -214,12 +211,11 @@ impl ArchiveWriter {
.append_path_with_name(src, path),
};

if res.is_err() {
if let Err(err) = res {
self.remove_archive();
// include problem file path in error message
let orig_err = res.unwrap_err();
let err_msg = format!("{} {}", orig_err, src_copy);
Err(Error::new(orig_err.kind(), err_msg))
let err_msg = format!("{} {}", err, src_copy);
Err(Error::new(err.kind(), err_msg))
} else {
self.progress.files_added += 1;

Expand Down
52 changes: 21 additions & 31 deletions agent/src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ pub struct ClientError {

impl From<Error> for ClientError {
fn from(err: Error) -> Self {
return ClientError {
ClientError {
code: 987,
message: err.to_string(),
http_code: Some(500),
};
}
}
}

Expand Down Expand Up @@ -77,7 +77,7 @@ impl Client<'_> {
let key_id: u64 = rand::random::<u64>();
let key_id_hex = format!("{:x}", key_id);

String::from(key_id_hex)
key_id_hex
}

pub fn exchange_keys(&self, secret: &str) -> i32 {
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Client<'_> {

pub fn get_remote_resource(&self, user_id: u32, token: &str, path: &str) -> i32 {
let sess = self.create_session(None).unwrap();
match Client::remote_get_resource(&sess, user_id, &token, &path) {
match Client::remote_get_resource(&sess, user_id, token, path) {
Ok(resources_result) => {
print!("{resources_result}");
}
Expand All @@ -146,9 +146,9 @@ impl Client<'_> {
0
}

pub fn remote_before_copy(&self, user_id: u32, token: &str, items: &String) -> i32 {
pub fn remote_before_copy(&self, user_id: u32, token: &str, items: &str) -> i32 {
let sess = self.create_session(None).unwrap();
match Client::_remote_before_copy(&sess, user_id, &token, &items) {
match Client::_remote_before_copy(&sess, user_id, token, items) {
Ok(result) => print!("{}", result),
Err(e) => {
Client::print_error_and_exit(e.code, e.message);
Expand All @@ -174,8 +174,8 @@ impl Client<'_> {
return result;
}

print!(
"{:.2}ms / {:.2}ms\n",
println!(
"{:.2}ms / {:.2}ms",
dur_sess.as_millis(),
dur_exec.as_millis()
);
Expand All @@ -195,12 +195,13 @@ impl Client<'_> {
let remote_path = format!("{}{}{}", DEFAULTS.temp_data_dir, archive_name, ".dst.tar");

// create argument list for uploader script
let mut script_args: Vec<&str> = Vec::new();
script_args.push(DEFAULTS.uploader_script_path);
script_args.push(&local_path);
script_args.push(&transfer.host);
script_args.push(&transfer.port);
script_args.push(&remote_path);
let script_args: Vec<&str> = vec![
DEFAULTS.uploader_script_path,
&local_path,
&transfer.host,
&transfer.port,
&remote_path,
];

// setup command for asynchronous execution
let mut cmd = Command::new("bash");
Expand Down Expand Up @@ -270,9 +271,7 @@ impl Client<'_> {
}

// remove local copy of archive
let mut rm_args: Vec<&str> = Vec::new();
rm_args.push("-f");
rm_args.push(local_path.as_str());
let rm_args: Vec<&str> = vec!["-f", &local_path];
let _rm_result = run_command_async(83, false, true, "rm", rm_args).await;

// abort process on any errors from command execution (including usr1 signal)
Expand All @@ -289,7 +288,7 @@ impl Client<'_> {
let port: i16 = transfer.port.to_string().parse::<i16>().unwrap();
let client = Client::new(&transfer.host, port);
match client.remote_extract_archive(
&archive_name,
archive_name,
&transfer.remote_path,
transfer.compress,
transfer.overwrite,
Expand Down Expand Up @@ -348,12 +347,7 @@ impl Client<'_> {
Err(e) => {
Client::print_error_and_exit(
132,
format!(
"503 Couldn't connect to {}:{}: {}",
self.host,
self.port,
e.to_string()
),
format!("503 Couldn't connect to {}:{}: {}", self.host, self.port, e),
);
return None;
}
Expand Down Expand Up @@ -405,7 +399,7 @@ impl Client<'_> {
// upload our public key
let mut upload = sess.channel_session().unwrap();
upload
.exec(&*format!(
.exec(&format!(
"echo -n \"{key}\" >> {}",
DEFAULTS.authorized_keys_file
))
Expand Down Expand Up @@ -481,7 +475,7 @@ impl Client<'_> {
let lock_file = digest(secret);
let mut result = sess.channel_session().unwrap();
result
.exec(&*format!("rm -f {}/{}", DEFAULTS.ssh_dir_path, lock_file))
.exec(&format!("rm -f {}/{}", DEFAULTS.ssh_dir_path, lock_file))
.unwrap();
let mut key = String::new();
result.read_to_string(&mut key).unwrap();
Expand Down Expand Up @@ -516,8 +510,6 @@ impl Client<'_> {
eprintln!("Couldn't write to file: {}", e);
exit(133);
}

()
}

fn remove_key_file(key_id: &str) {
Expand All @@ -531,8 +523,6 @@ impl Client<'_> {
exit(131);
}
}

()
}

fn get_agent_version(sess: &Session) -> String {
Expand Down Expand Up @@ -639,7 +629,7 @@ impl Client<'_> {
items: &str,
) -> Result<String, ClientError> {
let mut ch = sess.channel_session().unwrap();
ch.exec(&*format!(
ch.exec(&format!(
"{} {} {} {}",
Client::command(COMMAND_LOCAL_BEFORE_COPY),
user_id,
Expand Down
2 changes: 1 addition & 1 deletion agent/src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub fn command_local_before_copy(client: Client<'_>, args: Option<Vec<String>>)

match client.files_api.local_before_copy(user_id, token, items) {
Ok(response) => {
print!("{}", response.trim().to_string());
print!("{}", response.trim());
}
Err(e) => {
let mut msg = e.message;
Expand Down
14 changes: 5 additions & 9 deletions agent/src/cli/command_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@ pub fn run_command(
.output();

// return error if failed to execute command
if result.is_err() {
return Err(get_error(
command_id,
result.unwrap_err().to_string(),
"500".to_string(),
));
if let Err(err) = result {
return Err(get_error(command_id, err.to_string(), "500".to_string()));
}
let result = result.unwrap();
let stderr = String::from_utf8(result.stderr).unwrap_or("".to_string());
let stdout = String::from_utf8(result.stdout).unwrap_or("".to_string());
let code = result.status.code().unwrap();

// return error if the command's error output is not empty
if !allow_stderr && stderr.trim().len() > 0 {
if !allow_stderr && stderr.trim().is_empty() {
return Err(get_error(code, stderr.trim().to_string(), stderr));
}

Expand Down Expand Up @@ -81,7 +77,7 @@ pub async fn run_command_async(
let code = result.status.code().unwrap();

// return error if the command's error output is not empty
if !allow_stderr && stderr.trim().len() > 0 {
if !allow_stderr && !stderr.trim().is_empty() {
return Err(get_error(code, stderr.trim().to_string(), stderr));
}

Expand All @@ -101,7 +97,7 @@ fn get_command_args<'a>(
) -> (&'a str, Vec<&'a str>) {
let mut command_args: Vec<&str> = Vec::new();
let program;
if is_cli == true {
if is_cli {
// prepend command to the provided list of arguments and execute cli as the program
program = DEFAULTS.cli_executable_path;
command_args.push(command);
Expand Down
Loading

0 comments on commit 75c1188

Please sign in to comment.