From ccce46eba44f9b97cc1af7665f34b4aac35aea1e Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 5 Aug 2019 14:14:09 +0200 Subject: [PATCH] formatting fixup make fmt (cargo fmt --all) Signed-off-by: Wolfgang Bumiller --- proxmox-sys/src/linux/procfs.rs | 84 ++++++++++++++++++++------------- proxmox-tools/src/fs.rs | 58 ++++++++++------------- 2 files changed, 78 insertions(+), 64 deletions(-) diff --git a/proxmox-sys/src/linux/procfs.rs b/proxmox-sys/src/linux/procfs.rs index e142cc6c..e3aba536 100644 --- a/proxmox-sys/src/linux/procfs.rs +++ b/proxmox-sys/src/linux/procfs.rs @@ -13,7 +13,9 @@ use proxmox_tools::fs::file_read_firstline; /// POSIX sysconf call pub fn sysconf(name: i32) -> i64 { - extern { fn sysconf(name: i32) -> i64; } + extern "C" { + fn sysconf(name: i32) -> i64; + } unsafe { sysconf(name) } } @@ -44,7 +46,10 @@ pub fn read_proc_pid_stat(pid: libc::pid_t) -> Result { if let Some(cap) = REGEX.captures(&statstr) { if pid != cap["pid"].parse::().unwrap() { - bail!("unable to read pid stat for process '{}' - got wrong pid", pid); + bail!( + "unable to read pid stat for process '{}' - got wrong pid", + pid + ); } return Ok(ProcFsPidStat { @@ -168,7 +173,9 @@ pub struct ProcFsCPUInfo { static CPU_INFO: Option = None; pub fn read_cpuinfo() -> Result { - if let Some(cpu_info) = &CPU_INFO { return Ok(cpu_info.clone()); } + if let Some(cpu_info) = &CPU_INFO { + return Ok(cpu_info.clone()); + } let path = "/proc/cpuinfo"; let file = OpenOptions::new().read(true).open(&path)?; @@ -185,21 +192,21 @@ pub fn read_cpuinfo() -> Result { let mut socket_ids = HashSet::new(); for line in BufReader::new(&file).lines() { let content = line?; - if content.is_empty() { continue; } + if content.is_empty() { + continue; + } let mut content_iter = content.split(":"); match (content_iter.next(), content_iter.next()) { - (Some(key), Some(value)) => { - match key.trim_end() { - "processor" => cpuinfo.cpus += 1, - "model name" => cpuinfo.model = value.trim().to_string(), - "cpu MHz" => cpuinfo.mhz = value.trim().parse::()?, - "flags" => cpuinfo.hvm = value.contains(" vmx ") || value.contains(" svm "), - "physical id" => { - let id = value.trim().parse::()?; - socket_ids.insert(id); - }, - _ => continue, + (Some(key), Some(value)) => match key.trim_end() { + "processor" => cpuinfo.cpus += 1, + "model name" => cpuinfo.model = value.trim().to_string(), + "cpu MHz" => cpuinfo.mhz = value.trim().parse::()?, + "flags" => cpuinfo.hvm = value.contains(" vmx ") || value.contains(" svm "), + "physical id" => { + let id = value.trim().parse::()?; + socket_ids.insert(id); } + _ => continue, }, _ => bail!("Error while parsing '{}'", path), } @@ -223,12 +230,11 @@ pub fn read_memory_usage() -> Result { let ps = 4096; match (values.next(), values.next(), values.next()) { - (Some(Ok(size)), Some(Ok(resident)), Some(Ok(shared))) => - Ok(ProcFsMemUsage { - size: size * ps, - resident: resident * ps, - shared: shared * ps, - }), + (Some(Ok(size)), Some(Ok(resident)), Some(Ok(shared))) => Ok(ProcFsMemUsage { + size: size * ps, + resident: resident * ps, + shared: shared * ps, + }), _ => bail!("Error while parsing '{}'", path), } } @@ -251,11 +257,11 @@ pub fn read_proc_net_dev() -> Result, Error> { match (iter.next(), iter.next(), iter.skip(7).next()) { (Some(device), Some(receive), Some(send)) => { result.push(ProcFsNetDev { - device: device[..device.len()-1].to_string(), + device: device[..device.len() - 1].to_string(), receive: receive.parse::()?, send: send.parse::()?, }); - }, + } _ => bail!("Error while parsing '{}'", path), } } @@ -303,14 +309,20 @@ pub fn read_proc_net_route() -> Result, Error> { let mut result = Vec::new(); for line in BufReader::new(&file).lines().skip(1) { let content = line?; - if content.is_empty() { continue; } + if content.is_empty() { + continue; + } let mut iter = content.split_whitespace(); - let mut next = || iter.next() - .ok_or(format_err!("Error while parsing '{}'", path)); + let mut next = || { + iter.next() + .ok_or(format_err!("Error while parsing '{}'", path)) + }; let (iface, dest, gateway) = (next()?, next()?, next()?); - for _ in 0..3 { next()?; } + for _ in 0..3 { + next()?; + } let (metric, mask, mtu) = (next()?, next()?, next()?); result.push(ProcFsNetRoute { @@ -379,16 +391,24 @@ pub fn read_proc_net_ipv6_route() -> Result, Error> { let mut result = Vec::new(); for line in BufReader::new(&file).lines() { let content = line?; - if content.is_empty() { continue; } + if content.is_empty() { + continue; + } let mut iter = content.split_whitespace(); - let mut next = || iter.next() - .ok_or_else(|| format_err!("Error while parsing '{}'", path)); + let mut next = || { + iter.next() + .ok_or_else(|| format_err!("Error while parsing '{}'", path)) + }; let (dest, prefix) = (next()?, next()?); - for _ in 0..2 { next()?; } + for _ in 0..2 { + next()?; + } let (nexthop, metric) = (next()?, next()?); - for _ in 0..3 { next()?; } + for _ in 0..3 { + next()?; + } let iface = next()?; result.push(ProcFsNetIPv6Route { diff --git a/proxmox-tools/src/fs.rs b/proxmox-tools/src/fs.rs index fe6e0cda..aad6dc88 100644 --- a/proxmox-tools/src/fs.rs +++ b/proxmox-tools/src/fs.rs @@ -14,24 +14,16 @@ use super::try_block; /// /// This basically call ``std::fs::read``, but provides more elaborate /// error messages including the path. -pub fn file_get_contents>( - path: P, -) -> Result, Error> { - +pub fn file_get_contents>(path: P) -> Result, Error> { let path = path.as_ref(); - std::fs::read(path) - .map_err(|err| format_err!("unable to read {:?} - {}", path, err)) + std::fs::read(path).map_err(|err| format_err!("unable to read {:?} - {}", path, err)) } /// Read .json file into a ``Value`` /// /// The optional ``default`` is used when the file does not exist. -pub fn file_get_json>( - path: P, - default: Option, -) -> Result { - +pub fn file_get_json>(path: P, default: Option) -> Result { let path = path.as_ref(); let raw = match std::fs::read(path) { @@ -50,14 +42,12 @@ pub fn file_get_json>( let data = String::from_utf8(raw)?; let json = serde_json::from_str(&data)?; Ok(json) - }).map_err(|err: Error| format_err!("unable to parse json from {:?} - {}", path, err)) + }) + .map_err(|err: Error| format_err!("unable to parse json from {:?} - {}", path, err)) } /// Read the first line of a file as String -pub fn file_read_firstline>( - path: P, -) -> Result { - +pub fn file_read_firstline>(path: P) -> Result { let path = path.as_ref(); try_block!({ @@ -70,7 +60,8 @@ pub fn file_read_firstline>( let _ = reader.read_line(&mut line)?; Ok(line) - }).map_err(|err: Error| format_err!("unable to read {:?} - {}", path, err)) + }) + .map_err(|err: Error| format_err!("unable to read {:?} - {}", path, err)) } /// Atomically write a file @@ -105,9 +96,8 @@ pub fn file_set_contents_full>( let tmp_path = tmp_path.as_path(); - let mode : stat::Mode = perm.unwrap_or(stat::Mode::from( - stat::Mode::S_IRUSR | stat::Mode::S_IWUSR | - stat::Mode::S_IRGRP | stat::Mode::S_IROTH + let mode: stat::Mode = perm.unwrap_or(stat::Mode::from( + stat::Mode::S_IRUSR | stat::Mode::S_IWUSR | stat::Mode::S_IRGRP | stat::Mode::S_IROTH, )); if perm != None { @@ -143,14 +133,17 @@ pub fn file_set_contents_full>( pub fn fchown( fd: RawFd, owner: Option, - group: Option + group: Option, ) -> Result<(), Error> { - // According to the POSIX specification, -1 is used to indicate that owner and group // are not to be changed. Since uid_t and gid_t are unsigned types, we have to wrap // around to get -1 (copied fron nix crate). - let uid = owner.map(Into::into).unwrap_or((0 as libc::uid_t).wrapping_sub(1)); - let gid = group.map(Into::into).unwrap_or((0 as libc::gid_t).wrapping_sub(1)); + let uid = owner + .map(Into::into) + .unwrap_or((0 as libc::uid_t).wrapping_sub(1)); + let gid = group + .map(Into::into) + .unwrap_or((0 as libc::gid_t).wrapping_sub(1)); let res = unsafe { libc::fchown(fd, uid, gid) }; nix::errno::Errno::result(res)?; @@ -166,17 +159,16 @@ pub fn create_dir_chown>( perm: Option, owner: Option, group: Option, -) -> Result<(), nix::Error> -{ - let mode : stat::Mode = perm.unwrap_or(stat::Mode::from_bits_truncate(0o770)); +) -> Result<(), nix::Error> { + let mode: stat::Mode = perm.unwrap_or(stat::Mode::from_bits_truncate(0o770)); let path = path.as_ref(); match nix::unistd::mkdir(path, mode) { - Ok(()) => {}, + Ok(()) => {} Err(nix::Error::Sys(nix::errno::Errno::EEXIST)) => { return Ok(()); - }, + } err => return err, } @@ -198,7 +190,7 @@ pub fn image_size(path: &Path) -> Result { let file_type = metadata.file_type(); if file_type.is_block_device() { - let mut size : u64 = 0; + let mut size: u64 = 0; let res = unsafe { blkgetsize64(file.as_raw_fd(), &mut size) }; if let Err(err) = res { @@ -208,7 +200,9 @@ pub fn image_size(path: &Path) -> Result { } else if file_type.is_file() { Ok(metadata.len()) } else { - bail!("image size failed - got unexpected file type {:?}", file_type); + bail!( + "image size failed - got unexpected file type {:?}", + file_type + ); } } -