Skip to content

Commit

Permalink
Fix new clippies on linux (#89)
Browse files Browse the repository at this point in the history
* Fix new clippy errors related to using variables in formats statements on Linux
  • Loading branch information
andrewdavidmackenzie authored Dec 14, 2022
1 parent 5b88f88 commit a1e0255
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
1 change: 0 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ env:

jobs:
build:

runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand Down
4 changes: 2 additions & 2 deletions src/dmesg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::libproc::libproc::kmesg_buffer;

fn main() {
match kmesg_buffer::kmsgbuf() {
Ok(message) => print!("{}", message),
Err(e) => eprintln!("{}", e)
Ok(message) => print!("{message}"),
Err(e) => eprintln!("{e}")
}
}
14 changes: 7 additions & 7 deletions src/libproc/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::io::{BufRead, BufReader};
pub fn get_errno_with_message(return_code: i32) -> String {
let e = errno();
let code = e.0;
format!("return code = {}, errno = {}, message = '{}'", return_code, code, e)
format!("return code = {return_code}, errno = {code}, message = '{e}'")
}

/// Helper function that depending on the `ret` value:
Expand All @@ -25,7 +25,7 @@ pub fn check_errno(ret: i32, buf: &mut Vec<u8>) -> Result<String, String> {

match String::from_utf8(buf.to_vec()) {
Ok(return_value) => Ok(return_value),
Err(e) => Err(format!("Invalid UTF-8 sequence: {}", e))
Err(e) => Err(format!("Invalid UTF-8 sequence: {e}"))
}
}
}
Expand All @@ -35,10 +35,10 @@ pub fn check_errno(ret: i32, buf: &mut Vec<u8>) -> Result<String, String> {
/// This will be more useful when implementing more linux functions
pub fn procfile_field(filename: &str, field_name: &str) -> Result<String, String> {
const SEPARATOR: &str = ":";
let line_header = format!("{}{}", field_name, SEPARATOR);
let line_header = format!("{field_name}{SEPARATOR}");

// Open the file in read-only mode (ignoring errors).
let file = File::open(filename).map_err(|_| format!("Could not open /proc file '{}'", filename))?;
let file = File::open(filename).map_err(|_| format!("Could not open /proc file '{filename}'"))?;
let reader = BufReader::new(file);

// Read the file line by line using the lines() iterator from std::io::BufRead.
Expand All @@ -50,7 +50,7 @@ pub fn procfile_field(filename: &str, field_name: &str) -> Result<String, String
}
}

Err(format!("Could not find the field named '{}' in the /proc FS file name '{}'", field_name, filename))
Err(format!("Could not find the field named '{field_name}' in the /proc FS file name '{filename}'"))
}

#[cfg(target_os = "linux")]
Expand All @@ -59,14 +59,14 @@ pub fn procfile_field(filename: &str, field_name: &str) -> Result<String, String
pub fn parse_memory_string(line: &str) -> Result<u64, String> {
let parts: Vec<&str> = line.trim().split(' ').collect();
if parts.is_empty() {
return Err(format!("Could not parse Memory String: {}", line))
return Err(format!("Could not parse Memory String: {line}"))
}
let multiplier: u64 = if parts.len() == 2 {
match parts[1] {
"MB" => 1024 * 1024,
"kB" => 1024,
"B" => 1,
_ => return Err(format!("Could not parse units of Memory String: {}", line))
_ => return Err(format!("Could not parse units of Memory String: {line}"))
}
} else {
1
Expand Down
2 changes: 1 addition & 1 deletion src/libproc/pid_rusage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub fn pidrusage<T: PIDRUsage>(pid : i32) -> Result<T, String> {
/// ```
pub fn pidrusage<T: PIDRUsage>(pid : i32) -> Result<T, String> {
let mut pidrusage = T::default();
let vm_size = procfile_field(&format!("/proc/{}/status", pid), "VmSize")?;
let vm_size = procfile_field(&format!("/proc/{pid}/status"), "VmSize")?;
pidrusage.set_memory_used(parse_memory_string(&vm_size)?);

Ok(pidrusage)
Expand Down
12 changes: 6 additions & 6 deletions src/libproc/proc_pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub fn listpids(proc_types: ProcType) -> Result<Vec<u32>, String> {
ProcType::ProcAllPIDS => {
let mut pids = Vec::<u32>::new();

let proc_dir = fs::read_dir("/proc").map_err(|e| format!("Could not read '/proc': {}", e))?;
let proc_dir = fs::read_dir("/proc").map_err(|e| format!("Could not read '/proc': {e}"))?;

for entry in proc_dir {
let path = entry.map_err(|_| "Couldn't get /proc/ filename")?.path();
Expand Down Expand Up @@ -289,7 +289,7 @@ pub fn listpidspath(proc_types: ProcType, path: &str) -> Result<Vec<u32>, String
pub fn pidinfo<T: PIDInfo>(pid: i32, arg: u64) -> Result<T, String> {
let flavor = T::flavor() as i32;
let buffer_size = mem::size_of::<T>() as i32;
let mut pidinfo = unsafe { std::mem::zeroed() };
let mut pidinfo = unsafe { mem::zeroed() };
let buffer_ptr = &mut pidinfo as *mut _ as *mut c_void;
let ret: i32;

Expand Down Expand Up @@ -405,7 +405,7 @@ pub fn pidpath(pid: i32) -> Result<String, String> {
/// ```
#[cfg(target_os = "linux")]
pub fn pidpath(pid: i32) -> Result<String, String> {
let exe_path = CString::new(format!("/proc/{}/exe", pid))
let exe_path = CString::new(format!("/proc/{pid}/exe"))
.map_err(|_| "Could not create CString")?;
let mut buf: Vec<u8> = Vec::with_capacity(PATH_MAX as usize - 1);
let buffer_ptr = buf.as_mut_ptr() as *mut c_char;
Expand Down Expand Up @@ -499,7 +499,7 @@ pub fn name(pid: i32) -> Result<String, String> {

match String::from_utf8(namebuf) {
Ok(name) => Ok(name),
Err(e) => Err(format!("Invalid UTF-8 sequence: {}", e))
Err(e) => Err(format!("Invalid UTF-8 sequence: {e}"))
}
}
}
Expand All @@ -508,7 +508,7 @@ pub fn name(pid: i32) -> Result<String, String> {
/// Get the name of a Process using it's Pid
#[cfg(target_os = "linux")]
pub fn name(pid: i32) -> Result<String, String> {
helpers::procfile_field(&format!("/proc/{}/status", pid), "Name")
helpers::procfile_field(&format!("/proc/{pid}/status"), "Name")
}

/// Get information on all running processes.
Expand Down Expand Up @@ -600,7 +600,7 @@ pub fn pidcwd(_pid: pid_t) -> Result<PathBuf, String> {
/// }
/// ```
pub fn pidcwd(pid: pid_t) -> Result<PathBuf, String> {
fs::read_link(format!("/proc/{}/cwd", pid)).map_err(|e| {
fs::read_link(format!("/proc/{pid}/cwd")).map_err(|e| {
e.to_string()
})
}
Expand Down
24 changes: 12 additions & 12 deletions src/procinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,40 @@ fn getpid() -> i32 {

fn procinfo(pid: i32) {
match proc_pid::libversion() {
Ok((major, minor)) => println!("Libversion: {}.{}", major, minor),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
Ok((major, minor)) => println!("Libversion: {major}.{minor}"),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
}

println!("Pid: {}", pid);
println!("Pid: {pid}");

match proc_pid::pidpath(pid) {
Ok(path) => println!("Path: {}", path),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
Ok(path) => println!("Path: {path}"),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
}

match pidrusage::<RUsageInfoV0>(pid) {
Ok(resource_usage) => println!("Memory Used: {} Bytes", resource_usage.ri_resident_size),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
}

match proc_pid::name(pid) {
Ok(name) => println!("Name: {}", name),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
Ok(name) => println!("Name: {name}"),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
}

match proc_pid::regionfilename(pid, 0) {
Ok(regionfilename) => println!("Region Filename (at address 0): {}", regionfilename),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
Ok(regionfilename) => println!("Region Filename (at address 0): {regionfilename}"),
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
}

match proc_pid::listpids(proc_pid::ProcType::ProcAllPIDS) {
Ok(pids) => {
println!("There are currently {} processes active", pids.len());
for pid in pids {
println!("{}", pid);
println!("{pid}");
}
},
Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap()
Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
}
}

Expand Down

0 comments on commit a1e0255

Please sign in to comment.