Fix some clippy lints

This commit is contained in:
Nathan Fisher 2023-04-18 17:48:40 -04:00
parent 6037836f6b
commit 641eff3378
9 changed files with 60 additions and 71 deletions

View File

@ -34,6 +34,7 @@ pub struct Creator {
} }
impl Creator { impl Creator {
#[allow(clippy::missing_panics_doc)]
pub fn new(path: &Path, specs: Specs) -> Result<Self, io::Error> { pub fn new(path: &Path, specs: Specs) -> Result<Self, io::Error> {
let d = env::current_dir()?; let d = env::current_dir()?;
env::set_current_dir(path)?; env::set_current_dir(path)?;
@ -74,7 +75,7 @@ impl Creator {
self.entries.is_empty() self.entries.is_empty()
} }
pub fn create(self, outdir: &Path, sender: Sender<Message>) -> Result<(), Box<dyn Error>> { pub fn create(self, outdir: &Path, sender: &Sender<Message>) -> Result<(), Box<dyn Error>> {
let d = env::current_dir()?; let d = env::current_dir()?;
let plist = Mutex::new(Plist::default()); let plist = Mutex::new(Plist::default());
let totalsize: AtomicUsize = 0.into(); let totalsize: AtomicUsize = 0.into();

View File

@ -66,21 +66,21 @@ impl Hooks {
} }
fn makeinfo() -> Result<Output, InstallError> { fn makeinfo() -> Result<Output, InstallError> {
Command::new("makewhatis").output().map_err(|e| e.into()) Command::new("makewhatis").output().map_err(Into::into)
} }
fn compile_schemas() -> Result<Output, InstallError> { fn compile_schemas() -> Result<Output, InstallError> {
Command::new("glib-compile-schemas") Command::new("glib-compile-schemas")
.arg("/usr/share/glib-2.0/schemas") .arg("/usr/share/glib-2.0/schemas")
.output() .output()
.map_err(|e| e.into()) .map_err(Into::into)
} }
fn install_info(path: &str) -> Result<Output, InstallError> { fn install_info(path: &str) -> Result<Output, InstallError> {
Command::new("install-info") Command::new("install-info")
.arg(path) .arg(path)
.output() .output()
.map_err(|e| e.into()) .map_err(Into::into)
} }
impl Pinstall { impl Pinstall {
@ -93,6 +93,6 @@ impl Pinstall {
.arg(self.script.to_str().unwrap_or("")) .arg(self.script.to_str().unwrap_or(""))
.env("HPK_ROOT", self.root.to_str().unwrap_or("")) .env("HPK_ROOT", self.root.to_str().unwrap_or(""))
.output() .output()
.map_err(|e| e.into()) .map_err(Into::into)
} }
} }

View File

@ -100,7 +100,7 @@ fn create(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
} }
Ok::<(), CreationError>(()) Ok::<(), CreationError>(())
}); });
creator.create(&outdir, sender)?; creator.create(&outdir, &sender)?;
match handle.join() { match handle.join() {
Ok(_) => { Ok(_) => {
println!("Package created successfully"); println!("Package created successfully");

View File

@ -1,5 +1,5 @@
#![allow(dead_code)] #![allow(dead_code)]
pub use error::InstallError; pub use error::Error;
use { use {
crate::{ crate::{
@ -80,13 +80,12 @@ impl<T: io::Read> Installer<T> {
self, self,
hooks: &mut Vec<Hooks>, hooks: &mut Vec<Hooks>,
sender: Sender<InstallMessage>, sender: Sender<InstallMessage>,
) -> Result<Package, InstallError> { ) -> Result<Package, Error> {
let reader = Decoder::new(self.reader)?; let reader = Decoder::new(self.reader)?;
let mut archive = Archive::read(reader)?; let mut archive = Archive::read(reader)?;
sender.send(InstallMessage::ArchiveRead)?; sender.send(InstallMessage::ArchiveRead)?;
let pr_node = match archive.pop("package.ron") { let Some(pr_node) = archive.pop("package.ron") else {
Some(node) => node, return Err(Error::MissingManifest);
None => return Err(InstallError::MissingManifest),
}; };
let mut buf = vec![]; let mut buf = vec![];
pr_node.write(&mut buf)?; pr_node.write(&mut buf)?;
@ -94,12 +93,12 @@ impl<T: io::Read> Installer<T> {
if let Some(ref users) = package.users { if let Some(ref users) = package.users {
users users
.iter() .iter()
.for_each(|u| hooks.push((u.clone(), Some(self.root.to_path_buf())).into())); .for_each(|u| hooks.push((u.clone(), Some(self.root.clone())).into()));
} }
if let Some(ref groups) = package.groups { if let Some(ref groups) = package.groups {
groups groups
.iter() .iter()
.for_each(|g| hooks.push((g.clone(), Some(self.root.to_path_buf())).into())); .for_each(|g| hooks.push((g.clone(), Some(self.root.clone())).into()));
} }
let mut db_pkgdir = crate::get_dbdir(Some(self.root.clone())); let mut db_pkgdir = crate::get_dbdir(Some(self.root.clone()));
db_pkgdir.push(&package.name); db_pkgdir.push(&package.name);
@ -120,11 +119,10 @@ impl<T: io::Read> Installer<T> {
pr_node.write(writer)?; pr_node.write(writer)?;
let root = &self.root; let root = &self.root;
let hooks = Mutex::new(hooks); let hooks = Mutex::new(hooks);
let s = sender.clone();
archive archive
.nodes .nodes
.par_iter() .par_iter()
.try_for_each_with(s, |sender, node| { .try_for_each_with(sender, |sender, node| {
let mut path = root.clone(); let mut path = root.clone();
let fpath = node.header.file_path()?; let fpath = node.header.file_path()?;
if let Some(s) = node.header.prefix() { if let Some(s) = node.header.prefix() {
@ -137,7 +135,7 @@ impl<T: io::Read> Installer<T> {
hooks hooks
.lock() .lock()
.unwrap() .unwrap()
.push(Hooks::Info(fpath.to_str().unwrap().to_string())) .push(Hooks::Info(fpath.to_str().unwrap().to_string()));
} else if s.contains("/share/glib-2.0/schemas") { } else if s.contains("/share/glib-2.0/schemas") {
let mut h = hooks.lock().unwrap(); let mut h = hooks.lock().unwrap();
if !h.contains(&Hooks::GlibSchema) { if !h.contains(&Hooks::GlibSchema) {
@ -157,14 +155,15 @@ impl<T: io::Read> Installer<T> {
mode: _, mode: _,
size: _, size: _,
} => path == &fpath, } => path == &fpath,
Entry::Link { path, target: _ } => path == &fpath, Entry::Link { path, target: _ } | Entry::Directory { path, mode: _ } => {
Entry::Directory { path, mode: _ } => path == &fpath, path == &fpath
}
}) })
.unwrap(); .unwrap();
path = path.join(&fpath); path = path.join(&fpath);
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
if !parent.exists() { if !parent.exists() {
fs::create_dir_all(&parent)?; fs::create_dir_all(parent)?;
} }
} }
if path.exists() { if path.exists() {
@ -176,24 +175,20 @@ impl<T: io::Read> Installer<T> {
} }
let msg = extract_entry(entry, node, path)?; let msg = extract_entry(entry, node, path)?;
sender.send(msg)?; sender.send(msg)?;
Ok::<(), InstallError>(()) Ok::<(), Error>(())
})?; })?;
Ok(package) Ok(package)
} }
} }
fn extract_entry( fn extract_entry(entry: &Entry, node: &Node, path: PathBuf) -> Result<InstallMessage, Error> {
entry: &Entry,
node: &Node,
path: PathBuf,
) -> Result<InstallMessage, InstallError> {
match entry { match entry {
Entry::Directory { path: _, mode } => { Entry::Directory { path: _, mode } => {
DirBuilder::new().mode(*mode).create(&path)?; DirBuilder::new().mode(*mode).create(&path)?;
Ok(InstallMessage::DirectoryCreated(path)) Ok(InstallMessage::DirectoryCreated(path))
} }
Entry::Link { path: _, target } => { Entry::Link { path: _, target } => {
os::unix::fs::symlink(&path, &target)?; os::unix::fs::symlink(&path, target)?;
Ok(InstallMessage::LinkCreated(Link { Ok(InstallMessage::LinkCreated(Link {
path, path,
target: target.clone(), target: target.clone(),
@ -215,7 +210,7 @@ fn extract_entry(
write!(sum, "{c:02x}")?; write!(sum, "{c:02x}")?;
} }
if sha256sum != &sum { if sha256sum != &sum {
return Err(InstallError::ChecksumMismatch); return Err(Error::ChecksumMismatch);
} }
let fd = File::options().mode(*mode).write(true).open(&path)?; let fd = File::options().mode(*mode).write(true).open(&path)?;
let mut writer = BufWriter::new(fd); let mut writer = BufWriter::new(fd);
@ -225,7 +220,7 @@ fn extract_entry(
} }
} }
fn pop_appstream(archive: &mut Archive, db_pkgdir: &Path) -> Result<(), InstallError> { fn pop_appstream(archive: &mut Archive, db_pkgdir: &Path) -> Result<(), Error> {
let appstream = archive.pop("appdata.xml"); let appstream = archive.pop("appdata.xml");
if let Some(node) = appstream { if let Some(node) = appstream {
let mut appdatafile = db_pkgdir.to_path_buf(); let mut appdatafile = db_pkgdir.to_path_buf();
@ -242,7 +237,7 @@ fn pop_pinstall(
hooks: &mut Vec<Hooks>, hooks: &mut Vec<Hooks>,
pkgname: &str, pkgname: &str,
root: &Path, root: &Path,
) -> Result<(), InstallError> { ) -> Result<(), Error> {
let pinstall = archive.pop("postinstall.sh"); let pinstall = archive.pop("postinstall.sh");
if let Some(node) = pinstall { if let Some(node) = pinstall {
let mut path: PathBuf = ["/", "tmp", "hpk", pkgname].iter().collect(); let mut path: PathBuf = ["/", "tmp", "hpk", pkgname].iter().collect();
@ -263,7 +258,6 @@ mod error {
use crate::{tar, Hooks}; use crate::{tar, Hooks};
use ron::error::SpannedError; use ron::error::SpannedError;
use std::{ use std::{
error::Error,
fmt, io, fmt, io,
string::FromUtf8Error, string::FromUtf8Error,
sync::{ sync::{
@ -273,7 +267,7 @@ mod error {
}; };
#[derive(Debug)] #[derive(Debug)]
pub enum InstallError { pub enum Error {
Fmt(fmt::Error), Fmt(fmt::Error),
IO(io::Error), IO(io::Error),
RonError(SpannedError), RonError(SpannedError),
@ -285,14 +279,14 @@ mod error {
Utf8, Utf8,
} }
impl fmt::Display for InstallError { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}") write!(f, "{self:?}")
} }
} }
impl Error for InstallError { impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self { match self {
Self::Fmt(e) => Some(e), Self::Fmt(e) => Some(e),
Self::IO(e) => Some(e), Self::IO(e) => Some(e),
@ -304,49 +298,49 @@ mod error {
} }
} }
impl From<fmt::Error> for InstallError { impl From<fmt::Error> for Error {
fn from(value: fmt::Error) -> Self { fn from(value: fmt::Error) -> Self {
Self::Fmt(value) Self::Fmt(value)
} }
} }
impl From<io::Error> for InstallError { impl From<io::Error> for Error {
fn from(value: io::Error) -> Self { fn from(value: io::Error) -> Self {
Self::IO(value) Self::IO(value)
} }
} }
impl From<PoisonError<Sender<InstallMessage>>> for InstallError { impl From<PoisonError<Sender<InstallMessage>>> for Error {
fn from(_value: PoisonError<Sender<InstallMessage>>) -> Self { fn from(_value: PoisonError<Sender<InstallMessage>>) -> Self {
Self::MutexError Self::MutexError
} }
} }
impl From<PoisonError<Vec<Hooks>>> for InstallError { impl From<PoisonError<Vec<Hooks>>> for Error {
fn from(_value: PoisonError<Vec<Hooks>>) -> Self { fn from(_value: PoisonError<Vec<Hooks>>) -> Self {
Self::MutexError Self::MutexError
} }
} }
impl From<SendError<InstallMessage>> for InstallError { impl From<SendError<InstallMessage>> for Error {
fn from(value: SendError<InstallMessage>) -> Self { fn from(value: SendError<InstallMessage>) -> Self {
Self::SendError(value) Self::SendError(value)
} }
} }
impl From<SpannedError> for InstallError { impl From<SpannedError> for Error {
fn from(value: SpannedError) -> Self { fn from(value: SpannedError) -> Self {
Self::RonError(value) Self::RonError(value)
} }
} }
impl From<FromUtf8Error> for InstallError { impl From<FromUtf8Error> for Error {
fn from(_value: FromUtf8Error) -> Self { fn from(_value: FromUtf8Error) -> Self {
Self::Utf8 Self::Utf8
} }
} }
impl From<crate::tar::Error> for InstallError { impl From<crate::tar::Error> for Error {
fn from(value: crate::tar::Error) -> Self { fn from(value: crate::tar::Error) -> Self {
Self::Tar(value) Self::Tar(value)
} }

View File

@ -6,7 +6,6 @@ use {
deku::DekuError, deku::DekuError,
sha2::{Digest, Sha256}, sha2::{Digest, Sha256},
std::{ std::{
error::Error,
ffi::OsStr, ffi::OsStr,
fmt::{self, Write}, fmt::{self, Write},
fs, fs,
@ -24,7 +23,7 @@ pub struct Item {
} }
impl Item { impl Item {
pub fn try_create(path: &Path) -> Result<Self, ItemError> { pub fn try_create(path: &Path) -> Result<Self, Error> {
let path = fix_path(path); let path = fix_path(path);
let meta = fs::metadata(&path)?; let meta = fs::metadata(&path)?;
let filename = format!("{}", path.display()); let filename = format!("{}", path.display());
@ -72,21 +71,21 @@ impl Item {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ItemError { pub enum Error {
Io(io::Error), Io(io::Error),
Fmt(fmt::Error), Fmt(fmt::Error),
Tar(TarError), Tar(TarError),
Deku(DekuError), Deku(DekuError),
} }
impl fmt::Display for ItemError { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}") write!(f, "{self:?}")
} }
} }
impl Error for ItemError { impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self { match self {
Self::Io(e) => Some(e), Self::Io(e) => Some(e),
Self::Fmt(e) => Some(e), Self::Fmt(e) => Some(e),
@ -96,25 +95,25 @@ impl Error for ItemError {
} }
} }
impl From<io::Error> for ItemError { impl From<io::Error> for Error {
fn from(value: io::Error) -> Self { fn from(value: io::Error) -> Self {
Self::Io(value) Self::Io(value)
} }
} }
impl From<fmt::Error> for ItemError { impl From<fmt::Error> for Error {
fn from(value: fmt::Error) -> Self { fn from(value: fmt::Error) -> Self {
Self::Fmt(value) Self::Fmt(value)
} }
} }
impl From<TarError> for ItemError { impl From<TarError> for Error {
fn from(value: TarError) -> Self { fn from(value: TarError) -> Self {
Self::Tar(value) Self::Tar(value)
} }
} }
impl From<DekuError> for ItemError { impl From<DekuError> for Error {
fn from(value: DekuError) -> Self { fn from(value: DekuError) -> Self {
Self::Deku(value) Self::Deku(value)
} }

View File

@ -17,8 +17,8 @@ pub use {
creator::{CreationError, Creator, Message}, creator::{CreationError, Creator, Message},
db::Database, db::Database,
hooks::{Hooks, Pinstall}, hooks::{Hooks, Pinstall},
installer::{InstallError, InstallMessage, Installer}, installer::{Error as InstallError, InstallMessage, Installer},
item::{Item, ItemError}, item::{Error as ItemError, Item},
package::{Arch, Dependency, Group, Package, Specs, User}, package::{Arch, Dependency, Group, Package, Specs, User},
plist::{Entry, Plist}, plist::{Entry, Plist},
repository::Repository, repository::Repository,

View File

@ -34,9 +34,9 @@ pub enum Message {
} }
impl Repository { impl Repository {
pub fn build(name: &str, url: &Url, sender: Sender<Message>) -> Result<Self, Box<dyn Error>> { pub fn build(name: &str, url: &Url, sender: &Sender<Message>) -> Result<Self, Box<dyn Error>> {
let url = url.join("packages.ron.zstd")?; let url = url.join("packages.ron.zstd")?;
let resp = ureq::get(&url.to_string()) let resp = ureq::get(url.as_ref())
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(10))
.call()?; .call()?;
if let Some(val) = resp.header("Content-Length") { if let Some(val) = resp.header("Content-Length") {
@ -57,7 +57,7 @@ impl Repository {
let packages = ron::de::from_bytes(&buf)?; let packages = ron::de::from_bytes(&buf)?;
Ok(Self { Ok(Self {
name: name.to_string(), name: name.to_string(),
base_url: url.clone(), base_url: url,
packages, packages,
}) })
} }

View File

@ -1,3 +1,4 @@
#![allow(clippy::similar_names)]
use crate::tar::Error; use crate::tar::Error;
use deku::prelude::*; use deku::prelude::*;
use std::{ use std::{
@ -131,9 +132,8 @@ impl Header {
for c in self.fname { for c in self.fname {
if c == 0 { if c == 0 {
break; break;
} else {
write!(s, "{}", char::from(c))?;
} }
write!(s, "{}", char::from(c))?;
} }
Ok(s) Ok(s)
} }
@ -153,9 +153,8 @@ impl Header {
for c in self.mode { for c in self.mode {
if c == 0 { if c == 0 {
break; break;
} else {
write!(s, "{}", char::from(c))?;
} }
write!(s, "{}", char::from(c))?;
} }
let mode = u32::from_str_radix(&s, 8)?; let mode = u32::from_str_radix(&s, 8)?;
Ok(mode) Ok(mode)
@ -166,9 +165,8 @@ impl Header {
for c in self.mode { for c in self.mode {
if c == 0 { if c == 0 {
break; break;
} else {
write!(s, "{}", char::from(c))?;
} }
write!(s, "{}", char::from(c))?;
} }
let uid = u32::from_str_radix(&s, 8)?; let uid = u32::from_str_radix(&s, 8)?;
Ok(uid) Ok(uid)
@ -179,9 +177,8 @@ impl Header {
for c in self.mode { for c in self.mode {
if c == 0 { if c == 0 {
break; break;
} else {
write!(s, "{}", char::from(c))?;
} }
write!(s, "{}", char::from(c))?;
} }
let gid = u32::from_str_radix(&s, 8)?; let gid = u32::from_str_radix(&s, 8)?;
Ok(gid) Ok(gid)
@ -192,9 +189,8 @@ impl Header {
for c in self.username { for c in self.username {
if c == 0 { if c == 0 {
break; break;
} else {
write!(s, "{}", char::from(c))?;
} }
write!(s, "{}", char::from(c))?;
} }
Ok(s) Ok(s)
} }
@ -204,9 +200,8 @@ impl Header {
for c in self.groupname { for c in self.groupname {
if c == 0 { if c == 0 {
break; break;
} else {
write!(s, "{}", char::from(c))?;
} }
write!(s, "{}", char::from(c))?;
} }
Ok(s) Ok(s)
} }
@ -239,9 +234,8 @@ impl Header {
for c in self.file_prefix { for c in self.file_prefix {
if c == 0 { if c == 0 {
break; break;
} else {
write!(s, "{}", char::from(c)).ok()?;
} }
write!(s, "{}", char::from(c)).ok()?;
} }
if s.is_empty() { if s.is_empty() {
None None

View File

@ -1,3 +1,4 @@
#![allow(clippy::similar_names)]
use crate::tar::{header::Owner, Error, FileType, Header}; use crate::tar::{header::Owner, Error, FileType, Header};
use deku::prelude::*; use deku::prelude::*;
use std::{ use std::{