Fixed clippy lints

This commit is contained in:
Nathan Fisher 2023-02-20 23:00:35 -05:00
parent 5a7c2008b5
commit 102678015b
35 changed files with 118 additions and 148 deletions

View File

@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
//! Minimal bitflags type implementation allowing to check if a set of flags
//! contains a specific flag. The type must implement Copy and Bitand with u32.
use core::ops::BitAnd;

View File

@ -47,7 +47,7 @@ impl Cmd for Base32 {
} else if index > 0 {
println!();
}
let contents = get_contents(&file)?;
let contents = get_contents(file)?;
if matches.get_flag("DECODE") {
decode_base32(contents, matches.get_flag("IGNORE"))?;
} else {

View File

@ -46,7 +46,7 @@ impl Cmd for Base64 {
} else if index > 0 {
println!();
}
let contents = get_contents(&file)?;
let contents = get_contents(file)?;
if matches.get_flag("DECODE") {
decode_base64(contents, matches.get_flag("IGNORE"))?;
} else {

View File

@ -348,7 +348,7 @@ fn completions(prefix: &str, matches: &ArgMatches, usr: bool) -> Result<(), io::
})?;
}
if matches.get_flag("zsh") || matches.get_flag("all") {
let mut outdir = basedir.clone();
let mut outdir = basedir;
outdir.push("zsh");
outdir.push("site-functions");
COMMANDS.iter().try_for_each(|cmd| {

View File

@ -59,15 +59,13 @@ impl Cmd for Chmod {
};
if let Err(e) = action.apply() {
if !matches.get_flag("quiet") {
return Err(e.into());
return Err(e);
}
}
if matches.get_flag("recursive") {
if action.path.is_dir() {
if let Err(e) = action.recurse() {
if !matches.get_flag("quiet") {
return Err(e.into());
}
if matches.get_flag("recursive") && action.path.is_dir() {
if let Err(e) = action.recurse() {
if !matches.get_flag("quiet") {
return Err(e);
}
}
}
@ -136,7 +134,7 @@ impl Action<'_> {
Ok(())
}
fn into_child(&self, entry: DirEntry) -> Self {
fn get_child(&self, entry: DirEntry) -> Self {
Self {
path: entry.path().to_path_buf(),
feedback: self.feedback,
@ -148,7 +146,7 @@ impl Action<'_> {
let walker = WalkDir::new(&self.path).max_open(1);
for entry in walker {
let entry = entry?;
let action = self.into_child(entry);
let action = self.get_child(entry);
action.apply()?;
}
Ok(())

View File

@ -56,12 +56,11 @@ impl Cmd for Chgrp {
feedback,
};
if let Some(r) = recurse {
if action.path.is_dir() {
if action.path.is_dir()
&& action.path.is_symlink()
&& r.traversal != Traversal::NoLinks
{
action.recurse(recurse)?;
} else if action.path.is_symlink() {
if r.traversal != Traversal::NoLinks {
action.recurse(recurse)?;
}
}
}
action.apply()?;
@ -127,7 +126,7 @@ impl Action<'_> {
println!("{} retained as {}", &self.path.display(), &self.group.name);
}
fn into_child(&self, entry: DirEntry) -> Result<Self, Box<dyn Error>> {
fn as_child(&self, entry: DirEntry) -> Result<Self, Box<dyn Error>> {
let path = entry.path().to_path_buf();
Ok(Self {
path,
@ -140,13 +139,10 @@ impl Action<'_> {
let walker = WalkDir::new(&self.path)
.max_open(1)
.same_file_system(recurse.map_or(false, |x| x.same_filesystem))
.follow_links(recurse.map_or(false, |x| match x.traversal {
Traversal::FullLinks => true,
_ => false,
}));
.follow_links(recurse.map_or(false, |x| matches!(x.traversal, Traversal::FullLinks)));
for entry in walker {
let entry = entry?;
let action = self.into_child(entry)?;
let action = self.as_child(entry)?;
action.apply()?;
}
Ok(())

View File

@ -68,12 +68,11 @@ impl Cmd for Chown {
feedback,
};
if let Some(r) = recurse {
if action.path.is_dir() {
if action.path.is_dir()
&& action.path.is_symlink()
&& r.traversal != Traversal::NoLinks
{
action.recurse(recurse)?;
} else if action.path.is_symlink() {
if r.traversal != Traversal::NoLinks {
action.recurse(recurse)?;
}
}
}
action.apply()?;
@ -119,6 +118,7 @@ fn args() -> [Arg; 8] {
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(clippy::enum_variant_names)]
enum Traversal {
CliLinks,
FullLinks,
@ -245,7 +245,7 @@ impl Action<'_> {
}
}
fn into_child(&self, entry: DirEntry) -> Result<Self, Box<dyn Error>> {
fn as_child(&self, entry: DirEntry) -> Result<Self, Box<dyn Error>> {
let path = entry.path().to_path_buf();
Ok(Self {
path,
@ -259,13 +259,10 @@ impl Action<'_> {
let walker = WalkDir::new(&self.path)
.max_open(1)
.same_file_system(recurse.map_or(false, |x| x.same_filesystem))
.follow_links(recurse.map_or(false, |x| match x.traversal {
Traversal::FullLinks => true,
_ => false,
}));
.follow_links(recurse.map_or(false, |x| matches!(x.traversal, Traversal::FullLinks)));
for entry in walker {
let entry = entry?;
let action = self.into_child(entry)?;
let action = self.as_child(entry)?;
action.apply()?;
}
Ok(())

View File

@ -59,7 +59,7 @@ impl Cmd for Chroot {
} else {
env::set_current_dir("/")?;
}
return Err(command.exec().into());
Err(command.exec().into())
}
fn path(&self) -> Option<shitbox::Path> {

View File

@ -44,8 +44,7 @@ impl Cmd for Df {
if let Some(files) = matches.get_many::<String>("file") {
for f in files {
let entry = MntEntries::new("/proc/mounts")?
.filter(|e| e.dir.as_str() == f)
.next()
.find(|e| e.dir.as_str() == f)
.ok_or(io::Error::new(io::ErrorKind::Other, "no such filesystem"))?;
print_stats(&entry.fsname, &entry.dir, units)?;
}
@ -93,8 +92,8 @@ enum Units {
impl fmt::Display for Units {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Posix => write!(f, "512-blocks"),
Self::Kilo => write!(f, " 1K-blocks"),
Self::Posix => write!(f, "512-blocks"),
Self::Kilo => write!(f, " 1K-blocks"),
Self::Natural => write!(f, " Size"),
}
}
@ -109,8 +108,8 @@ fn print_stats(fs: &str, mntpt: &str, units: Units) -> Result<(), Box<dyn Error>
Units::Kilo => st.f_frsize / 1024,
Units::Natural => st.f_frsize,
};
let total = st.f_blocks * bs as u64;
let avail = st.f_bfree * bs as u64;
let total = st.f_blocks * bs;
let avail = st.f_bfree * bs;
let used = total - avail;
let mut capacity = if total > 0 { (used * 100) / total } else { 0 };
if used * 100 != capacity * (used + avail) {

View File

@ -38,7 +38,7 @@ impl Cmd for Expand {
])
}
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

View File

@ -64,14 +64,10 @@ impl Cmd for Ln {
} else {
SymTarget::ToLink
};
if files.len() > 1 {
if !dest.is_dir() {
return Err(io::Error::new(
io::ErrorKind::Other,
"destination must be a directory",
)
.into());
}
if files.len() > 1 && !dest.is_dir() {
return Err(
io::Error::new(io::ErrorKind::Other, "destination must be a directory").into(),
);
}
for f in files {
let source = PathBuf::from(f);
@ -115,13 +111,11 @@ fn do_link(
let name = source.file_name().unwrap();
dest.push(name);
}
if dest.exists() {
if force {
unistd::unlink(
dest.to_str()
.ok_or(io::Error::new(io::ErrorKind::Other, "invalid utf8"))?,
)?;
}
if dest.exists() && force {
unistd::unlink(
dest.to_str()
.ok_or(io::Error::new(io::ErrorKind::Other, "invalid utf8"))?,
)?;
}
if symbolic {
os::unix::fs::symlink(&source, &dest)?;

View File

@ -62,7 +62,7 @@ impl Cmd for MkTemp {
let fname = cname.to_str()?;
println!("{fname}");
if matches.get_flag("dryrun") {
fs::remove_dir(&fname)?;
fs::remove_dir(fname)?;
}
} else {
let raw_fd = unsafe { libc::mkstemp(ptr) };
@ -73,7 +73,7 @@ impl Cmd for MkTemp {
let fname = cname.to_str()?;
println!("{fname}");
if matches.get_flag("dryrun") {
fs::remove_file(&fname)?;
fs::remove_file(fname)?;
}
}
Ok(())

View File

@ -43,7 +43,7 @@ impl Cmd for Realpath {
process::exit(1);
}
} else {
match env::current_dir().and_then(|d| fs::canonicalize(d)) {
match env::current_dir().and_then(fs::canonicalize) {
Ok(p) => println!("{}", p.display()),
Err(e) => {
if matches.get_flag("quiet") {

View File

@ -42,7 +42,7 @@ impl Cmd for Rev {
}?;
stdout.reset()?;
}
rev_file(&file)?;
rev_file(file)?;
}
}
Ok(())

View File

@ -83,7 +83,7 @@ impl Cmd for Rm {
for act in actions.items {
if let Err(e) = act.apply() {
if !act.force {
return Err(e.into());
return Err(e);
}
}
}
@ -147,7 +147,7 @@ impl Action {
Ok(None) => return Ok(()),
Err(e) => {
if !self.force {
return Err(e.into());
return Err(e);
} else {
return Ok(());
}
@ -156,7 +156,7 @@ impl Action {
} else {
match File::open(&self.path)
.and_then(|fd| fd.metadata())
.and_then(|meta| Ok(FileType::from(meta)))
.map(FileType::from)
{
Ok(ft) => ft,
Err(e) => {
@ -220,7 +220,7 @@ impl Action {
} else {
File::open(path)
.and_then(|fd| fd.metadata())
.and_then(|meta| Ok(FileType::from(meta)))?
.map(FileType::from)?
};
eprint!("rm: remove {ft} '{}'? ", &self.path);
let mut reply = String::new();
@ -277,10 +277,7 @@ impl AllActions {
let mut reply = String::new();
let stdin = io::stdin();
let _read = stdin.read_line(&mut reply);
match reply.trim_end() {
"y" | "Y" | "yes" | "Yes" => true,
_ => false,
}
matches!(reply.trim_end(), "y" | "Y" | "yes" | "Yes")
} else {
true
}

View File

@ -114,7 +114,7 @@ fn get_values<'a>(
}
if flags.contains(Flags::Max) {
f.max = 0;
contents.lines().into_iter().for_each(|line| {
contents.lines().for_each(|line| {
let max = line.chars().count();
f.max = cmp::max(max, f.max);
});

View File

@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc, clippy::too_many_lines)]
use core::fmt;
use std::{error, ffi::c_long};

View File

@ -54,10 +54,10 @@ impl Cmd for B2sum {
let line = line?;
let mut split = line.split_whitespace();
let sum = split.next().ok_or::<io::Error>(
io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(),
io::Error::new(io::ErrorKind::Other, "invalid checksum file"),
)?;
let file = split.next().ok_or::<io::Error>(
io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(),
io::Error::new(io::ErrorKind::Other, "invalid checksum file"),
)?;
let mut buf = vec![];
let mut fd = File::open(file)?;

View File

@ -348,7 +348,7 @@ fn completions(prefix: &str, matches: &ArgMatches, usr: bool) -> Result<(), io::
})?;
}
if matches.get_flag("zsh") || matches.get_flag("all") {
let mut outdir = basedir.clone();
let mut outdir = basedir;
outdir.push("zsh");
outdir.push("site-functions");
COMMANDS.iter().try_for_each(|cmd| {

View File

@ -47,10 +47,10 @@ pub fn check_sums(file: &str, hashtype: HashType, erred: &mut usize) -> Result<(
let line = line?;
let mut split = line.split_whitespace();
let sum = split.next().ok_or::<io::Error>(
io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(),
io::Error::new(io::ErrorKind::Other, "invalid checksum file"),
)?;
let file = split.next().ok_or::<io::Error>(
io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(),
io::Error::new(io::ErrorKind::Other, "invalid checksum file"),
)?;
let s = match hashtype {
HashType::Md5 => compute_hash(file, Md5::new())?,

View File

@ -1,5 +1,7 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
use errno::Errno;
use sc::*;
use sc::syscall;
use std::{error::Error, ffi::CString};
mod mntent;
@ -12,7 +14,7 @@ pub use self::{
mntflags::{Flags as MntFlags, ParseFlagsError},
};
/// Wraps the Linux SYS_mount syscall in a nicer interface
/// Wraps the Linux `SYS_mount` syscall in a nicer interface
pub fn mount(
dev: &str,
mountpoint: &str,

View File

@ -1,3 +1,4 @@
#![allow(clippy::must_use_candidate)]
//! A Rust reimplementation of libc's mntent, for parsing /etc/fstab or
//! /proc/mounts
use super::{MntEntries, MntFlags};
@ -52,7 +53,7 @@ impl MntEntry {
if self.fstype != "swap" {
return false;
}
let dev = self.device().map(|x| x.to_str().map(|x| x.to_string()));
let dev = self.device().map(|x| x.to_str().map(ToString::to_string));
let Ok(Some(dev)) = dev else { return false };
let Ok(fd) = File::open("/proc/swaps") else {
return false;
@ -64,7 +65,7 @@ impl MntEntry {
};
let swap = line.split_whitespace().next();
if let Some(swap) = swap {
if swap == &dev {
if swap == dev {
return true;
}
}
@ -179,13 +180,10 @@ fn dev_from_uuid(s: &str) -> io::Result<PathBuf> {
for dev in devs {
let tags = dev.tags();
for tag in tags {
match tag.typ() {
TagType::Superblock(SuperblockTag::Uuid) => {
if uuid.to_lowercase().as_str().trim() == tag.value().trim() {
return dev.name().canonicalize();
}
if let TagType::Superblock(SuperblockTag::Uuid) = tag.typ() {
if uuid.to_lowercase().as_str().trim() == tag.value().trim() {
return dev.name().canonicalize();
}
_ => {}
}
}
}
@ -202,13 +200,10 @@ fn dev_from_label(s: &str) -> io::Result<PathBuf> {
for dev in devs {
let tags = dev.tags();
for tag in tags {
match tag.typ() {
TagType::Superblock(SuperblockTag::Label) => {
if label == tag.value() {
return dev.name().canonicalize();
}
if let TagType::Superblock(SuperblockTag::Label) = tag.typ() {
if label == tag.value() {
return dev.name().canonicalize();
}
_ => {}
}
}
}
@ -225,13 +220,10 @@ fn dev_from_partuuid(s: &str) -> io::Result<PathBuf> {
for dev in devs {
let tags = dev.tags();
for tag in tags {
match tag.typ() {
TagType::Partition(PartitionTag::PartEntryUuid) => {
if partlabel == tag.value() {
return dev.name().canonicalize();
}
if let TagType::Partition(PartitionTag::PartEntryUuid) = tag.typ() {
if partlabel == tag.value() {
return dev.name().canonicalize();
}
_ => {}
}
}
}
@ -248,13 +240,10 @@ fn dev_from_partlabel(s: &str) -> io::Result<PathBuf> {
for dev in devs {
let tags = dev.tags();
for tag in tags {
match tag.typ() {
TagType::Partition(PartitionTag::PartEntryName) => {
if partlabel == tag.value() {
return dev.name().canonicalize();
}
if let TagType::Partition(PartitionTag::PartEntryName) = tag.typ() {
if partlabel == tag.value() {
return dev.name().canonicalize();
}
_ => {}
}
}
}

View File

@ -5,6 +5,7 @@ use std::{
str::FromStr,
};
#[repr(u32)]
pub enum Flags {
ReadOnly = 1,
NoSuid = 2,

View File

@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc, clippy::must_use_candidate)]
//! Wraps certain libc functions around groups and users
use std::{
error::Error,

View File

@ -1,5 +1,6 @@
//! Common arguments to be re-used across multiple commands. Using these args
//! instead of implementing from scratch increases consistency across applets
#![allow(clippy::must_use_candidate)]
use clap::{Arg, ArgAction};
pub fn verbose() -> Arg {

View File

@ -1,4 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
use std::{env, path::PathBuf, string::ToString};
pub mod args;

View File

@ -1,14 +1,13 @@
use errno::Errno;
use sc::*;
use sc::syscall;
use std::{
error::Error,
ffi::{c_long, CString},
ffi::CString,
fs::File,
mem,
os::fd::AsRawFd,
};
#[inline(always)]
pub fn mknod(path: &str, mode: u32, dev: u64) -> Result<(), Box<dyn Error>> {
let ret = unsafe {
syscall!(
@ -26,7 +25,6 @@ pub fn mknod(path: &str, mode: u32, dev: u64) -> Result<(), Box<dyn Error>> {
}
}
#[inline(always)]
pub fn mkfifo(path: &str, mode: u32) -> Result<(), Box<dyn Error>> {
let ret = unsafe {
syscall!(
@ -44,7 +42,6 @@ pub fn mkfifo(path: &str, mode: u32) -> Result<(), Box<dyn Error>> {
}
}
#[inline(always)]
pub fn statfs(dir: &str) -> Result<libc::statvfs, Box<dyn Error>> {
let mut buf = mem::MaybeUninit::<libc::statvfs>::uninit();
let fd = File::open(dir)?;
@ -54,6 +51,6 @@ pub fn statfs(dir: &str) -> Result<libc::statvfs, Box<dyn Error>> {
Ok(buf)
} else {
let e = unsafe { *libc::__errno_location() };
Err(Errno::from(e as c_long).into())
Err(Errno::from(i64::from(e)).into())
}
}

View File

@ -1,9 +1,10 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
use errno::Errno;
use libc::utsname;
use sc::syscall;
use std::{error::Error, ffi::CString, fs::File, os::fd::AsRawFd, u8};
use std::{error::Error, ffi::CString, fs::File, os::fd::AsRawFd, ptr, u8};
#[inline(always)]
fn new_utsname() -> utsname {
utsname {
sysname: [0; 65],
@ -15,11 +16,11 @@ fn new_utsname() -> utsname {
}
}
#[inline(always)]
#[allow(clippy::cast_sign_loss)]
pub fn gethostname() -> Result<String, Box<dyn Error>> {
let mut uts = new_utsname();
unsafe {
let ret = syscall!(UNAME, &mut uts as *mut utsname);
let ret = syscall!(UNAME, ptr::addr_of_mut!(uts));
if ret != 0 {
return Err(Errno::from(ret).into());
}
@ -30,10 +31,9 @@ pub fn gethostname() -> Result<String, Box<dyn Error>> {
.map(|x| *x as u8)
.take_while(|x| *x != 0)
.collect();
String::from_utf8(name).map_err(|e| e.into())
String::from_utf8(name).map_err(Into::into)
}
#[inline(always)]
pub fn sethostname(name: &str) -> Result<(), Errno> {
let ret = unsafe { syscall!(SETHOSTNAME, name.as_ptr(), name.len()) };
if ret == 0 {
@ -43,7 +43,6 @@ pub fn sethostname(name: &str) -> Result<(), Errno> {
}
}
#[inline(always)]
pub fn link(source: &str, dest: &str) -> Result<(), Box<dyn Error>> {
let ret = unsafe {
syscall!(
@ -62,7 +61,6 @@ pub fn link(source: &str, dest: &str) -> Result<(), Box<dyn Error>> {
}
}
#[inline(always)]
pub fn unlink(name: &str) -> Result<(), Box<dyn Error>> {
let ret = unsafe { syscall!(UNLINKAT, libc::AT_FDCWD, CString::new(name)?.as_ptr(), 0) };
if ret == 0 {
@ -72,7 +70,7 @@ pub fn unlink(name: &str) -> Result<(), Box<dyn Error>> {
}
}
#[inline(always)]
#[allow(clippy::cast_sign_loss)]
pub fn readlink(name: &str) -> Result<String, Box<dyn Error>> {
let mut buf = Vec::<i8>::with_capacity(1024);
let ret = unsafe {
@ -90,13 +88,12 @@ pub fn readlink(name: &str) -> Result<String, Box<dyn Error>> {
.map(|x| *x as u8)
.take_while(|x| *x != 0)
.collect();
String::from_utf8(path).map_err(|e| e.into())
String::from_utf8(path).map_err(Into::into)
} else {
Err(Errno::from(ret).into())
}
}
#[inline(always)]
pub fn fdatasync(fd: &File) -> Result<(), Errno> {
let ret = unsafe { syscall!(FDATASYNC, fd.as_raw_fd()) };
if ret == 0 {
@ -106,7 +103,6 @@ pub fn fdatasync(fd: &File) -> Result<(), Errno> {
}
}
#[inline(always)]
pub fn syncfs(fd: &File) -> Result<(), Errno> {
let ret = unsafe { syscall!(SYNCFS, fd.as_raw_fd()) };
if ret == 0 {
@ -116,7 +112,6 @@ pub fn syncfs(fd: &File) -> Result<(), Errno> {
}
}
#[inline(always)]
pub fn fsync(fd: &File) -> Result<(), Errno> {
let ret = unsafe { syscall!(FSYNC, fd.as_raw_fd()) };
if ret == 0 {
@ -126,12 +121,10 @@ pub fn fsync(fd: &File) -> Result<(), Errno> {
}
}
#[inline(always)]
pub fn sync() {
unsafe { syscall!(SYNC) };
}
#[inline(always)]
pub fn symlink(source: &str, dest: &str) -> Result<(), Box<dyn Error>> {
let ret = unsafe {
syscall!(

View File

@ -1,3 +1,4 @@
#![allow(clippy::must_use_candidate)]
use bitflags::BitFlags;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
@ -80,7 +81,9 @@ impl BitOrAssign<Bit> for u32 {
impl Bit {
pub fn as_char(&self, mode: u32) -> char {
if mode & *self != 0 {
if mode & *self == 0 {
'-'
} else {
match self {
Self::Suid | Self::Sgid => 's',
Self::Sticky => 't',
@ -91,8 +94,6 @@ impl Bit {
Self::OExec if mode.contains(Self::Sticky) => 't',
Self::UExec | Self::GExec | Self::OExec => 'x',
}
} else {
'-'
}
}
}

View File

@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
//! Functions for parsing and managing permissions
mod bit;
mod parser;

View File

@ -348,7 +348,7 @@ fn completions(prefix: &str, matches: &ArgMatches, usr: bool) -> Result<(), io::
})?;
}
if matches.get_flag("zsh") || matches.get_flag("all") {
let mut outdir = basedir.clone();
let mut outdir = basedir;
outdir.push("zsh");
outdir.push("site-functions");
COMMANDS.iter().try_for_each(|cmd| {

View File

@ -1,4 +1,4 @@
use clap::{Arg, ArgAction, ArgMatches, Command};
use clap::{ArgMatches, Command};
use shitbox::Cmd;
#[derive(Debug, Default)]
@ -9,7 +9,7 @@ impl Cmd for Mount {
Command::new("mount")
}
fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
fn run(&self, _matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

View File

@ -8,8 +8,8 @@ use std::{
io::{self, Read, Seek, SeekFrom, Write},
};
const SWAP_MAGIC1: &'static str = "SWAPSPACE2";
const SWAP_MAGIC2: &'static str = "SWAP_SPACE";
const SWAP_MAGIC1: &str = "SWAPSPACE2";
const SWAP_MAGIC2: &str = "SWAP_SPACE";
const SWAP_MAGIC_LENGTH: u64 = 10;
const SWAP_LABEL_LENGTH: u64 = 16;
const SWAP_LABEL_OFFSET: u64 = 1024 + 4 + 4 + 4 + 16;
@ -61,7 +61,7 @@ impl Cmd for Swaplabel {
return Err(io::Error::new(io::ErrorKind::Other, "label is too long").into());
}
let label = CString::new(label.as_str())?;
fd.write(label.as_bytes())?;
fd.write_all(label.as_bytes())?;
} else {
let mut label = [0; SWAP_LABEL_LENGTH as usize];
fd.read_exact(&mut label)?;

View File

@ -47,7 +47,7 @@ impl Cmd for Swapoff {
}
}
} else if let Some(devices) = matches.get_many::<String>("device") {
for d in devices {
'devloop: for d in devices {
let fd = File::open("/proc/swaps")?;
let reader = BufReader::new(fd);
for line in reader.lines() {
@ -61,7 +61,7 @@ impl Cmd for Swapoff {
if matches.get_flag("verbose") {
println!("swapoff {d}");
}
return Ok(());
continue 'devloop;
}
}
let msg = format!("{d} is not swapped");

View File

@ -100,10 +100,8 @@ impl Cmd for Umount {
if x.fstype == t {
ret = false;
}
} else {
if &x.fstype != t {
ret = false;
}
} else if &x.fstype != t {
ret = false;
}
}
}
@ -126,14 +124,12 @@ impl Cmd for Umount {
println!("umount: {} unmounted", &p.dir);
}
}
} else {
if let Some(spec) = matches.get_many::<String>("spec") {
for s in spec {
let mntpt = get_mntpt(s)?;
mount::umount(&mntpt, flags as u32)?;
if matches.get_flag("verbose") {
println!("umount: {mntpt} unmounted");
}
} else if let Some(spec) = matches.get_many::<String>("spec") {
for s in spec {
let mntpt = get_mntpt(s)?;
mount::umount(&mntpt, flags as u32)?;
if matches.get_flag("verbose") {
println!("umount: {mntpt} unmounted");
}
}
}
@ -149,7 +145,7 @@ fn get_mntpt(spec: &str) -> io::Result<String> {
let entries = MntEntries::new("/proc/mounts")?;
for e in entries {
if e.fsname == spec {
return Ok(e.dir.clone());
return Ok(e.dir);
} else if e.dir == spec {
return Ok(spec.to_string());
}