Remove nix and bitflags dependencies
This commit is contained in:
Родитель
217accc870
Коммит
a0c3a92d2f
|
@ -4,9 +4,6 @@ version = "0.1.0"
|
|||
authors = ["Kyle Machulis <kyle@nonpolynomial.com>", "J.C. Jones <jc@mozilla.org>", "Tim Taubert <ttaubert@mozilla.com>"]
|
||||
build = "build.rs"
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
nix = "^0.7"
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
libudev = "^0.2"
|
||||
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
#[macro_use]
|
||||
extern crate nix;
|
||||
|
||||
#[cfg(any(target_os = "linux"))]
|
||||
extern crate libudev;
|
||||
|
||||
|
|
|
@ -1,28 +1,29 @@
|
|||
use std::path::PathBuf;
|
||||
extern crate libc;
|
||||
|
||||
use std::ffi::{CString, OsString};
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::os::unix::io::RawFd;
|
||||
|
||||
use std::os::unix::prelude::*;
|
||||
|
||||
use ::consts::CID_BROADCAST;
|
||||
use ::platform::hidraw;
|
||||
use ::platform::util::from_nix_result;
|
||||
use ::platform::util::{from_unix_result, to_io_err};
|
||||
|
||||
use u2fprotocol::{U2FDevice};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Device {
|
||||
path: PathBuf,
|
||||
fd: RawFd,
|
||||
path: OsString,
|
||||
fd: libc::c_int,
|
||||
cid: [u8; 4],
|
||||
}
|
||||
|
||||
impl Device {
|
||||
pub fn new(path: PathBuf) -> io::Result<Self> {
|
||||
let opts = ::nix::fcntl::O_RDWR;
|
||||
let mode = ::nix::sys::stat::Mode::empty();
|
||||
let fd = from_nix_result(::nix::fcntl::open(&path, opts, mode))?;
|
||||
assert!(fd > 0);
|
||||
|
||||
pub fn new(path: OsString) -> io::Result<Self> {
|
||||
let cstr = CString::new(path.as_bytes()).map_err(to_io_err)?;
|
||||
let fd = unsafe { libc::open(cstr.as_ptr(), libc::O_RDWR) };
|
||||
let fd = from_unix_result(fd)?;
|
||||
Ok(Self { path, fd, cid: CID_BROADCAST })
|
||||
}
|
||||
|
||||
|
@ -34,7 +35,7 @@ impl Device {
|
|||
impl Drop for Device {
|
||||
fn drop(&mut self) {
|
||||
// Close the fd, ignore any errors.
|
||||
let _ = ::nix::unistd::close(self.fd);
|
||||
let _ = unsafe { libc::close(self.fd) };
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,14 +46,18 @@ impl PartialEq for Device {
|
|||
}
|
||||
|
||||
impl Read for Device {
|
||||
fn read(&mut self, bytes: &mut [u8]) -> io::Result<usize> {
|
||||
from_nix_result(::nix::unistd::read(self.fd, bytes))
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let bufp = buf.as_mut_ptr() as *mut libc::c_void;
|
||||
let rv = unsafe { libc::read(self.fd, bufp, buf.len()) };
|
||||
from_unix_result(rv as usize)
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Device {
|
||||
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
|
||||
from_nix_result(::nix::unistd::write(self.fd, bytes))
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
let bufp = buf.as_ptr() as *const libc::c_void;
|
||||
let rv = unsafe { libc::write(self.fd, bufp, buf.len()) };
|
||||
from_unix_result(rv as usize)
|
||||
}
|
||||
|
||||
// USB HID writes don't buffer, so this will be a nop.
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use rand::{thread_rng, Rng};
|
||||
use std::collections::hash_map::ValuesMut;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::ffi::OsString;
|
||||
|
||||
use ::platform::device::Device;
|
||||
use ::platform::monitor::Event;
|
||||
|
||||
pub struct DeviceMap {
|
||||
map: HashMap<PathBuf, Device>
|
||||
map: HashMap<OsString, Device>
|
||||
}
|
||||
|
||||
impl DeviceMap {
|
||||
|
@ -15,7 +15,7 @@ impl DeviceMap {
|
|||
Self { map: HashMap::new() }
|
||||
}
|
||||
|
||||
pub fn values_mut(&mut self) -> ValuesMut<PathBuf, Device> {
|
||||
pub fn values_mut(&mut self) -> ValuesMut<OsString, Device> {
|
||||
self.map.values_mut()
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ impl DeviceMap {
|
|||
}
|
||||
}
|
||||
|
||||
fn add(&mut self, path: PathBuf) {
|
||||
fn add(&mut self, path: OsString) {
|
||||
if self.map.contains_key(&path) {
|
||||
return;
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ impl DeviceMap {
|
|||
}
|
||||
}
|
||||
|
||||
fn remove(&mut self, path: PathBuf) {
|
||||
fn remove(&mut self, path: OsString) {
|
||||
// Ignore errors.
|
||||
let _ = self.map.remove(&path);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
extern crate libc;
|
||||
|
||||
use std::io;
|
||||
use std::mem;
|
||||
use std::os::unix::io::RawFd;
|
||||
|
||||
use ::consts::{FIDO_USAGE_PAGE, FIDO_USAGE_U2FHID};
|
||||
use ::platform::util::from_nix_result;
|
||||
use ::platform::util::from_unix_result;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(C)]
|
||||
|
@ -17,9 +20,33 @@ impl ReportDescriptor {
|
|||
}
|
||||
}
|
||||
|
||||
const NRBITS: u32 = 8;
|
||||
const TYPEBITS: u32 = 8;
|
||||
|
||||
const READ: u8 = 2;
|
||||
const SIZEBITS: u8 = 14;
|
||||
|
||||
const NRSHIFT: u32 = 0;
|
||||
const TYPESHIFT: u32 = NRSHIFT + NRBITS as u32;
|
||||
const SIZESHIFT: u32 = TYPESHIFT + TYPEBITS as u32;
|
||||
const DIRSHIFT: u32 = SIZESHIFT + SIZEBITS as u32;
|
||||
|
||||
macro_rules! ioctl {
|
||||
($dir:expr, $name:ident, $ioty:expr, $nr:expr; $ty:ty) => (
|
||||
pub unsafe fn $name(fd: libc::c_int, val: *mut $ty) -> io::Result<libc::c_int> {
|
||||
let size = mem::size_of::<$ty>();
|
||||
let ioc = (($dir as u32) << DIRSHIFT) |
|
||||
(($ioty as u32) << TYPESHIFT) |
|
||||
(($nr as u32) << NRSHIFT) |
|
||||
((size as u32) << SIZESHIFT);
|
||||
from_unix_result(libc::ioctl(fd, ioc as libc::c_ulong, val))
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// https://github.com/torvalds/linux/blob/master/include/uapi/linux/hidraw.h
|
||||
ioctl!(read hidiocgrdescsize with b'H', 0x01; ::libc::c_int);
|
||||
ioctl!(read hidiocgrdesc with b'H', 0x02; /*struct*/ ReportDescriptor);
|
||||
ioctl!(READ, hidiocgrdescsize, b'H', 0x01; ::libc::c_int);
|
||||
ioctl!(READ, hidiocgrdesc, b'H', 0x02; /*struct*/ ReportDescriptor);
|
||||
|
||||
enum Data {
|
||||
UsagePage { data: u32 },
|
||||
|
@ -90,8 +117,8 @@ pub fn is_u2f_device(fd: RawFd) -> bool {
|
|||
|
||||
fn read_report_descriptor(fd: RawFd) -> io::Result<ReportDescriptor> {
|
||||
let mut desc = ReportDescriptor { size: 0, value: [0; 4096] };
|
||||
from_nix_result(unsafe { hidiocgrdescsize(fd, &mut desc.size) })?;
|
||||
from_nix_result(unsafe { hidiocgrdesc(fd, &mut desc) })?;
|
||||
let _ = unsafe { hidiocgrdescsize(fd, &mut desc.size)? };
|
||||
let _ = unsafe { hidiocgrdesc(fd, &mut desc)? };
|
||||
Ok(desc)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,9 +3,9 @@ use libudev::EventType;
|
|||
|
||||
use libc::{c_int, c_short, c_ulong};
|
||||
|
||||
use std::ffi::OsString;
|
||||
use std::io;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::mpsc::{channel, Receiver, TryIter};
|
||||
|
||||
use runloop::RunLoop;
|
||||
|
@ -31,13 +31,15 @@ fn poll(fds: &mut Vec<::libc::pollfd>) -> io::Result<()> {
|
|||
}
|
||||
|
||||
pub enum Event {
|
||||
Add(PathBuf),
|
||||
Remove(PathBuf)
|
||||
Add(OsString),
|
||||
Remove(OsString)
|
||||
}
|
||||
|
||||
impl Event {
|
||||
fn from_udev(event: libudev::Event) -> Option<Self> {
|
||||
let path = event.device().devnode().map(|dn| dn.to_path_buf());
|
||||
let path = event.device().devnode().map(|dn| {
|
||||
dn.to_owned().into_os_string()
|
||||
});
|
||||
|
||||
match (event.event_type(), path) {
|
||||
(EventType::Add, Some(path)) => Some(Event::Add(path)),
|
||||
|
@ -65,7 +67,9 @@ impl Monitor {
|
|||
|
||||
// Iterate all existing devices.
|
||||
for dev in enumerator.scan_devices()? {
|
||||
if let Some(path) = dev.devnode().map(|p| p.to_owned()) {
|
||||
if let Some(path) = dev.devnode().map(|p| {
|
||||
p.to_owned().into_os_string()
|
||||
}) {
|
||||
tx.send(Event::Add(path)).map_err(to_io_err)?;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +1,37 @@
|
|||
extern crate libc;
|
||||
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
|
||||
pub fn from_nix_error(err: ::nix::Error) -> io::Error {
|
||||
io::Error::from_raw_os_error(err.errno() as i32)
|
||||
pub trait Signed {
|
||||
fn is_negative(&self) -> bool;
|
||||
}
|
||||
|
||||
pub fn from_nix_result<T>(res: ::nix::Result<T>) -> io::Result<T> {
|
||||
match res {
|
||||
Ok(r) => Ok(r),
|
||||
Err(err) => Err(from_nix_error(err)),
|
||||
impl Signed for i32 {
|
||||
fn is_negative(&self) -> bool {
|
||||
*self < (0 as i32)
|
||||
}
|
||||
}
|
||||
|
||||
impl Signed for usize {
|
||||
fn is_negative(&self) -> bool {
|
||||
(*self as isize) < (0 as isize)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_unix_result<T: Signed>(rv: T) -> io::Result<T> {
|
||||
if rv.is_negative() {
|
||||
let errno = unsafe { *libc::__errno_location() };
|
||||
Err(io::Error::from_raw_os_error(errno))
|
||||
} else {
|
||||
Ok(rv)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn io_err(msg: &str) -> io::Error {
|
||||
io::Error::new(io::ErrorKind::Other, msg)
|
||||
io::Error::new(io::ErrorKind::Other, msg)
|
||||
}
|
||||
|
||||
pub fn to_io_err<T: Error>(err: T) -> io::Error {
|
||||
io_err(err.description())
|
||||
io_err(err.description())
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче