Resolve user-supplied paths, so that paths from /dev/input/by-id/ can be used directly

Resolve user-supplied paths in the `input-device-paths` configuration
array to resolved, absolute paths, so that users can directly use
paths from the `/dev/input/by-id/` in the configuration file.
This commit is contained in:
Smith Dhumbumroong 2023-11-14 01:21:37 +07:00
parent 8c96b40465
commit 1f92594bd6

View file

@ -7,6 +7,7 @@ use std::ffi::OsStr;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use std::collections::HashSet;
use std::fs::canonicalize;
use tokio::fs;
use tokio::sync::mpsc::{self, Receiver, Sender};
@ -19,7 +20,8 @@ pub struct Monitor {
impl Monitor {
pub fn new(input_device_paths: &HashSet<String>) -> Self {
let (sender, receiver) = mpsc::channel(1);
tokio::spawn(monitor(sender, input_device_paths.clone()));
let absolute_input_device_paths = canonicalize_input_device_paths(input_device_paths);
tokio::spawn(monitor(sender, absolute_input_device_paths));
Self { receiver }
}
@ -98,6 +100,30 @@ async fn monitor(sender: Sender<Result<Interceptor, Error>>, input_device_paths:
}
}
fn canonicalize_input_device_paths(input_device_paths: &HashSet<String>) -> HashSet<String> {
let mut absolute_paths = HashSet::new();
for path in input_device_paths {
match canonicalize(path) {
Ok(abs_path) => {
match abs_path.into_os_string().into_string() {
Ok(ap) => {
absolute_paths.insert(ap);
},
Err(err) => {
tracing::error!("Failed to convert absolute path into string {:?}", err);
continue;
},
}
},
Err(err) => {
tracing::error!("Failed to canonicalize a path: {}", err);
continue;
},
}
}
return absolute_paths;
}
fn register_input_device(input_device_paths: &HashSet<String>, input_device_path: PathBuf) -> bool {
if input_device_paths.len() > 0 {
match input_device_path.into_os_string().into_string() {