reconnect flag

This commit is contained in:
Aiden McClelland
2022-01-05 14:23:12 -07:00
committed by Aiden McClelland
parent 48409c33d4
commit eaffa8e53c
5 changed files with 35 additions and 23 deletions

View File

@@ -128,13 +128,16 @@ pub fn target() -> Result<(), Error> {
Ok(())
}
// TODO: incorporate reconnect into this response as well
#[command(display(display_serializable))]
pub async fn list(
#[context] ctx: RpcContext,
) -> Result<BTreeMap<BackupTargetId, BackupTarget>, Error> {
let mut sql_handle = ctx.secret_store.acquire().await?;
let (disks, cifs) = tokio::try_join!(crate::disk::util::list(), cifs::list(&mut sql_handle),)?;
Ok(disks
let (disks_res, cifs) =
tokio::try_join!(crate::disk::util::list(), cifs::list(&mut sql_handle),)?;
Ok(disks_res
.disks
.into_iter()
.flat_map(|mut disk| {
std::mem::take(&mut disk.partitions)

View File

@@ -1,7 +1,7 @@
use clap::ArgMatches;
use rpc_toolkit::command;
use self::util::DiskInfo;
use self::util::DiskListResponse;
use crate::util::serde::{display_serializable, IoFormat};
use crate::Error;
@@ -17,7 +17,7 @@ pub fn disk() -> Result<(), Error> {
Ok(())
}
fn display_disk_info(info: Vec<DiskInfo>, matches: &ArgMatches<'_>) {
fn display_disk_info(info: DiskListResponse, matches: &ArgMatches<'_>) {
use prettytable::*;
if matches.is_present("format") {
@@ -32,7 +32,7 @@ fn display_disk_info(info: Vec<DiskInfo>, matches: &ArgMatches<'_>) {
"USED",
"EMBASSY OS VERSION"
]);
for disk in info {
for disk in info.disks {
let row = row![
disk.logicalname.display(),
"N/A",
@@ -76,6 +76,6 @@ pub async fn list(
#[allow(unused_variables)]
#[arg]
format: Option<IoFormat>,
) -> Result<Vec<DiskInfo>, Error> {
) -> Result<DiskListResponse, Error> {
crate::disk::util::list().await
}

View File

@@ -1,6 +1,5 @@
use std::num::ParseIntError;
use std::path::Path;
use std::time::Duration;
use color_eyre::eyre::eyre;
use tokio::io::AsyncWriteExt;
@@ -91,8 +90,9 @@ impl std::str::FromStr for Quirks {
}
#[instrument]
pub async fn update_quirks(quirks: &mut Quirks) -> Result<(), Error> {
pub async fn update_quirks(quirks: &mut Quirks) -> Result<Vec<String>, Error> {
let mut usb_devices = tokio::fs::read_dir("/sys/bus/usb/devices/").await?;
let mut to_reconnect = Vec::new();
while let Some(usb_device) = usb_devices.next_entry().await? {
if tokio::fs::metadata(usb_device.path().join("idVendor"))
.await
@@ -116,26 +116,25 @@ pub async fn update_quirks(quirks: &mut Quirks) -> Result<(), Error> {
quirk_file.write_all(quirks.to_string().as_bytes()).await?;
quirk_file.sync_all().await?;
drop(quirk_file);
tokio::time::sleep(Duration::from_secs(2)).await;
}
reconnect_usb(usb_device.path()).await?;
disconnect_usb(usb_device.path()).await?;
let (vendor_name, product_name) = tokio::try_join!(
tokio::fs::read_to_string(usb_device.path().join("manufacturer")),
tokio::fs::read_to_string(usb_device.path().join("product")),
)?;
to_reconnect.push(format!("{} {}", vendor_name, product_name));
}
Ok(())
Ok(to_reconnect)
}
#[instrument(skip(usb_device_path))]
pub async fn reconnect_usb(usb_device_path: impl AsRef<Path>) -> Result<(), Error> {
let authorized_path = usb_device_path.as_ref().join("authorized");
pub async fn disconnect_usb(usb_device_path: impl AsRef<Path>) -> Result<(), Error> {
let authorized_path = usb_device_path.as_ref().join("bConfigurationValue");
let mut authorized_file = tokio::fs::File::create(&authorized_path).await?;
authorized_file.write_all(b"0").await?;
authorized_file.sync_all().await?;
drop(authorized_file);
tokio::time::sleep(Duration::from_secs(2)).await;
let mut authorized_file = tokio::fs::File::create(&authorized_path).await?;
authorized_file.write_all(b"1").await?;
authorized_file.sync_all().await?;
tokio::time::sleep(Duration::from_secs(2)).await;
Ok(())
}

View File

@@ -24,6 +24,13 @@ use crate::util::serde::IoFormat;
use crate::util::{Invoke, Version};
use crate::{Error, ResultExt as _};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct DiskListResponse {
pub disks: Vec<DiskInfo>,
pub reconnect: Vec<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct DiskInfo {
@@ -232,9 +239,9 @@ pub async fn recovery_info(
}
#[instrument]
pub async fn list() -> Result<Vec<DiskInfo>, Error> {
pub async fn list() -> Result<DiskListResponse, Error> {
let mut quirks = fetch_quirks().await?;
update_quirks(&mut quirks).await?;
let reconnect = update_quirks(&mut quirks).await?;
save_quirks(&mut quirks).await?;
let disk_guids = pvscan().await?;
let disks = tokio_stream::wrappers::ReadDirStream::new(
@@ -366,7 +373,10 @@ pub async fn list() -> Result<Vec<DiskInfo>, Error> {
})
}
Ok(res)
Ok(DiskListResponse {
disks: res,
reconnect,
})
}
fn parse_pvscan_output(pvscan_output: &str) -> BTreeMap<PathBuf, Option<String>> {

View File

@@ -28,7 +28,7 @@ use crate::disk::main::DEFAULT_PASSWORD;
use crate::disk::mount::filesystem::block_dev::BlockDev;
use crate::disk::mount::filesystem::cifs::Cifs;
use crate::disk::mount::guard::TmpMountGuard;
use crate::disk::util::{pvscan, recovery_info, DiskInfo, EmbassyOsRecoveryInfo};
use crate::disk::util::{pvscan, recovery_info, DiskInfo, DiskListResponse, EmbassyOsRecoveryInfo};
use crate::hostname::PRODUCT_KEY_PATH;
use crate::id::Id;
use crate::init::init;
@@ -82,7 +82,7 @@ pub fn disk() -> Result<(), Error> {
}
#[command(rename = "list", rpc_only, metadata(authenticated = false))]
pub async fn list_disks() -> Result<Vec<DiskInfo>, Error> {
pub async fn list_disks() -> Result<DiskListResponse, Error> {
crate::disk::list(None).await
}