From 9f80512a11926c5ec3f869ad5e220b3b350eec9a Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 8 Jul 2020 12:18:41 +0200 Subject: Remove unused imports --- src/auth.rs | 2 -- src/device/mod.rs | 2 -- src/device/pro.rs | 2 -- src/device/storage.rs | 2 -- src/lib.rs | 2 -- src/otp.rs | 2 -- src/pws.rs | 3 --- 7 files changed, 15 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 6748ca1..fe2f7f2 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -8,8 +8,6 @@ use std::ops; use std::os::raw::c_char; use std::os::raw::c_int; -use nitrokey_sys; - use crate::config::{Config, RawConfig}; use crate::device::{Device, DeviceWrapper, Pro, Storage}; use crate::error::Error; diff --git a/src/device/mod.rs b/src/device/mod.rs index 067fdf6..5082537 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -10,8 +10,6 @@ use std::ffi; use std::fmt; use std::str; -use nitrokey_sys; - use crate::auth::Authenticate; use crate::config::{Config, RawConfig}; use crate::error::{CommunicationError, Error, LibraryError}; diff --git a/src/device/pro.rs b/src/device/pro.rs index 591b730..0d5443e 100644 --- a/src/device/pro.rs +++ b/src/device/pro.rs @@ -1,8 +1,6 @@ // Copyright (C) 2018-2019 Robin Krahl // SPDX-License-Identifier: MIT -use nitrokey_sys; - use crate::device::{Device, Model, Status}; use crate::error::Error; use crate::otp::GenerateOtp; diff --git a/src/device/storage.rs b/src/device/storage.rs index 5669a91..a18d94f 100644 --- a/src/device/storage.rs +++ b/src/device/storage.rs @@ -5,8 +5,6 @@ use std::convert::TryFrom as _; use std::fmt; use std::ops; -use nitrokey_sys; - use crate::device::{Device, FirmwareVersion, Model, SerialNumber, Status}; use crate::error::{CommandError, Error}; use crate::otp::GenerateOtp; diff --git a/src/lib.rs b/src/lib.rs index 92247d7..3fa3ae3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,8 +133,6 @@ use std::marker; use std::ptr::NonNull; use std::sync; -use nitrokey_sys; - pub use crate::auth::{Admin, Authenticate, User}; pub use crate::config::Config; pub use crate::device::{ diff --git a/src/otp.rs b/src/otp.rs index 4667aff..1d5f507 100644 --- a/src/otp.rs +++ b/src/otp.rs @@ -3,8 +3,6 @@ use std::ffi::CString; -use nitrokey_sys; - use crate::error::Error; use crate::util::{get_command_result, get_cstring, result_from_string}; diff --git a/src/pws.rs b/src/pws.rs index 3398deb..93294c5 100644 --- a/src/pws.rs +++ b/src/pws.rs @@ -1,9 +1,6 @@ // Copyright (C) 2018-2019 Robin Krahl // SPDX-License-Identifier: MIT -use libc; -use nitrokey_sys; - use crate::device::{Device, DeviceWrapper, Pro, Storage}; use crate::error::{CommandError, Error}; use crate::util::{get_command_result, get_cstring, get_last_error, result_from_string}; -- cgit v1.2.3 From cf460cbb5e616d12f5e6b1f64acddf3ec0e7b087 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 8 Jul 2020 12:25:31 +0200 Subject: Use find(…) instead of skip_while(…).next() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch replaces calls to skip_while(…).next() for an iter::Iterator with a call to find(…), as suggested by clippy. --- src/device/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/device/mod.rs b/src/device/mod.rs index 5082537..c84faa1 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -223,9 +223,8 @@ fn get_hidapi_serial_number(serial_number: &str) -> Option { return None; } - let iter = serial_number.char_indices().rev(); - let first_non_null = iter.skip_while(|(_, c)| *c == '0').next(); - if let Some((i, _)) = first_non_null { + let mut iter = serial_number.char_indices().rev(); + if let Some((i, _)) = iter.find(|(_, c)| *c != '0') { let substr = if len - i < 8 { // The last eight characters contain at least one non-zero character --> use them serial_number.split_at(len - 8).1 -- cgit v1.2.3 From 98232c7bc262dd3902b8f3e299196ab9c83fb355 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 8 Jul 2020 12:08:26 +0200 Subject: Remove sync::PoisonError from Error::PoisonError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the Error::PoisonError contained the sync::PoisonError that caused the error. This is problematic as sync::PoisonError does not implement Send, making it impossible to use the Error enum with the anyhow crate. At the same time, storing the sync::PoisonError is not very useful. If a user wants to access the poisoned lock, they can call the force_take function. Therefore we remove the sync::PoisonError value from the Error:: PoisonError variant. This also allows us to simplify the From> and From> implementations as we no longer need to know the type of the mutex that caused the error. For more information, see this thread: https://lists.sr.ht/~ireas/nitrokey-rs-dev/%3C68ed0f3f-d98f-63bc-04d2-81b6d6cde560%40posteo.net%3E --- CHANGELOG.md | 4 ++++ src/error.rs | 16 ++++++++-------- src/lib.rs | 11 ++++------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2dd8a7..dddccb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ Copyright (C) 2019-2020 Robin Krahl SPDX-License-Identifier: CC0-1.0 --> +# Unreleased +- Refactor the `Error` enum so that it is `Send`, `Sync` and `'static`: + - Remove the `sync::PoisonError` from the `PoisonError` variant. + # v0.6.0 (2020-02-03) - Add `String` value to the `Error::UnexpectedError` variant. - Always store serial numbers as structs: diff --git a/src/error.rs b/src/error.rs index 7bea3f2..e0698a2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,7 +21,7 @@ pub enum Error { /// A library usage error. LibraryError(LibraryError), /// An error that occurred due to a poisoned lock. - PoisonError(sync::PoisonError>), + PoisonError, /// An error that occurred during random number generation. RandError(Box), /// An error that is caused by an unexpected value returned by libnitrokey. @@ -72,14 +72,14 @@ impl From for Error { } } -impl From>> for Error { - fn from(error: sync::PoisonError>) -> Self { - Error::PoisonError(error) +impl From> for Error { + fn from(_error: sync::PoisonError) -> Self { + Error::PoisonError } } -impl From>> for Error { - fn from(error: sync::TryLockError>) -> Self { +impl From> for Error { + fn from(error: sync::TryLockError) -> Self { match error { sync::TryLockError::Poisoned(err) => err.into(), sync::TryLockError::WouldBlock => Error::ConcurrentAccessError, @@ -100,7 +100,7 @@ impl error::Error for Error { Error::CommunicationError(ref err) => Some(err), Error::ConcurrentAccessError => None, Error::LibraryError(ref err) => Some(err), - Error::PoisonError(ref err) => Some(err), + Error::PoisonError => None, Error::RandError(ref err) => Some(err.as_ref()), Error::UnexpectedError(_) => None, Error::UnknownError(_) => None, @@ -117,7 +117,7 @@ impl fmt::Display for Error { Error::CommunicationError(ref err) => write!(f, "Communication error: {}", err), Error::ConcurrentAccessError => write!(f, "Internal error: concurrent access"), Error::LibraryError(ref err) => write!(f, "Library error: {}", err), - Error::PoisonError(_) => write!(f, "Internal error: poisoned lock"), + Error::PoisonError => write!(f, "Internal error: poisoned lock"), Error::RandError(ref err) => write!(f, "RNG error: {}", err), Error::UnexpectedError(ref s) => write!(f, "An unexpected error occurred: {}", s), Error::UnknownError(ref err) => write!(f, "Unknown error: {}", err), diff --git a/src/lib.rs b/src/lib.rs index 3fa3ae3..5b72d63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -470,13 +470,10 @@ pub fn take() -> Result, Error> { /// [`ConcurrentAccessError`]: struct.Error.html#variant.ConcurrentAccessError /// [`Manager`]: struct.Manager.html pub fn force_take() -> Result, Error> { - match take() { - Ok(guard) => Ok(guard), - Err(err) => match err { - Error::PoisonError(err) => Ok(err.into_inner()), - err => Err(err), - }, - } + MANAGER.try_lock().or_else(|err| match err { + sync::TryLockError::Poisoned(err) => Ok(err.into_inner()), + sync::TryLockError::WouldBlock => Err(Error::ConcurrentAccessError), + }) } /// List all connected Nitrokey devices. -- cgit v1.2.3 From 3f402dc13530ce4de167bae843200cfbd72ed69b Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 8 Jul 2020 22:22:10 +0200 Subject: Remove Error::RandError variant Since we update rand_os to version 0.2 in commit 6c138eaa850c745b97b7e48a201db0cbaad8e1e0, the random number generation can no longer fail. Therefore the Error::RandError variant is no longer needed. As we did not want to break the public API, we still kept the RandError variant. This patch removes the RandError variant for good. --- CHANGELOG.md | 1 + src/error.rs | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dddccb1..651a50e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ SPDX-License-Identifier: CC0-1.0 # Unreleased - Refactor the `Error` enum so that it is `Send`, `Sync` and `'static`: - Remove the `sync::PoisonError` from the `PoisonError` variant. + - Remove `Error::RandError` variant. # v0.6.0 (2020-02-03) - Add `String` value to the `Error::UnexpectedError` variant. diff --git a/src/error.rs b/src/error.rs index e0698a2..995741b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -22,8 +22,6 @@ pub enum Error { LibraryError(LibraryError), /// An error that occurred due to a poisoned lock. PoisonError, - /// An error that occurred during random number generation. - RandError(Box), /// An error that is caused by an unexpected value returned by libnitrokey. UnexpectedError(String), /// An unknown error returned by libnitrokey. @@ -101,7 +99,6 @@ impl error::Error for Error { Error::ConcurrentAccessError => None, Error::LibraryError(ref err) => Some(err), Error::PoisonError => None, - Error::RandError(ref err) => Some(err.as_ref()), Error::UnexpectedError(_) => None, Error::UnknownError(_) => None, Error::UnsupportedModelError => None, @@ -118,7 +115,6 @@ impl fmt::Display for Error { Error::ConcurrentAccessError => write!(f, "Internal error: concurrent access"), Error::LibraryError(ref err) => write!(f, "Library error: {}", err), Error::PoisonError => write!(f, "Internal error: poisoned lock"), - Error::RandError(ref err) => write!(f, "RNG error: {}", err), Error::UnexpectedError(ref s) => write!(f, "An unexpected error occurred: {}", s), Error::UnknownError(ref err) => write!(f, "Unknown error: {}", err), Error::UnsupportedModelError => write!(f, "Unsupported Nitrokey model"), -- cgit v1.2.3 From f1e83eb81879412e1de9898238ba58289bb6cb24 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 8 Jul 2020 22:58:41 +0200 Subject: Ensure Error trait implementations The anyhow crate requires that error types are error::Error, Send, Sync and 'static. This patch implements a simple static assertion that our Error type implements these traits. --- src/error.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/error.rs b/src/error.rs index 995741b..1aa1793 100644 --- a/src/error.rs +++ b/src/error.rs @@ -267,3 +267,22 @@ impl fmt::Display for LibraryError { }) } } + +// build our own static assertion that Error implements error::Error, Send, Sync, 'static + +struct Helper(T); + +trait Assert { + fn assert() -> bool; +} + +impl Assert for Helper { + fn assert() -> bool { + true + } +} + +#[allow(unused)] +fn assert_error_impl() { + let _ = Helper::::assert(); +} -- cgit v1.2.3 From 8f72f038cab8e61fb162c12c4bd4cbaa243faa2d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 14 Jul 2020 21:52:06 +0200 Subject: Release v0.7.0 This release changes the Error enum to implement Send, Sync and 'static for compatibility with error handling crates like anyhow. --- CHANGELOG.md | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 651a50e..736620d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Copyright (C) 2019-2020 Robin Krahl SPDX-License-Identifier: CC0-1.0 --> -# Unreleased +# v0.7.0 (2020-07-14) - Refactor the `Error` enum so that it is `Send`, `Sync` and `'static`: - Remove the `sync::PoisonError` from the `PoisonError` variant. - Remove `Error::RandError` variant. diff --git a/Cargo.toml b/Cargo.toml index f2d859d..00c41ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ [package] name = "nitrokey" -version = "0.6.0" +version = "0.7.0" authors = ["Robin Krahl "] edition = "2018" homepage = "https://code.ireas.org/nitrokey-rs/" -- cgit v1.2.3