pbs tape: rust fmt

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht
2022-04-06 17:00:29 +02:00
parent a527b54f84
commit b23adfd4ee
17 changed files with 863 additions and 811 deletions

View File

@ -12,54 +12,46 @@
/// - support tape alert flags
/// - support volume statistics
/// - read cartridge memory
use std::convert::TryInto;
use anyhow::{bail, Error};
use serde_json::Value;
use proxmox_schema::{api, ArraySchema, IntegerSchema, Schema, StringSchema};
use proxmox_router::cli::*;
use proxmox_router::RpcEnvironment;
use proxmox_schema::{api, ArraySchema, IntegerSchema, Schema, StringSchema};
use pbs_api_types::{
LTO_DRIVE_PATH_SCHEMA, DRIVE_NAME_SCHEMA, LtoTapeDrive,
};
use pbs_api_types::{LtoTapeDrive, DRIVE_NAME_SCHEMA, LTO_DRIVE_PATH_SCHEMA};
use pbs_config::drive::complete_drive_name;
use pbs_tape::{
sg_tape::SgTape,
linux_list_drives::{complete_drive_path, lto_tape_device_list, open_lto_tape_device},
sg_tape::SgTape,
};
pub const FILE_MARK_COUNT_SCHEMA: Schema =
IntegerSchema::new("File mark count.")
pub const FILE_MARK_COUNT_SCHEMA: Schema = IntegerSchema::new("File mark count.")
.minimum(1)
.maximum(i32::MAX as isize)
.schema();
pub const FILE_MARK_POSITION_SCHEMA: Schema =
IntegerSchema::new("File mark position (0 is BOT).")
pub const FILE_MARK_POSITION_SCHEMA: Schema = IntegerSchema::new("File mark position (0 is BOT).")
.minimum(0)
.maximum(i32::MAX as isize)
.schema();
pub const RECORD_COUNT_SCHEMA: Schema =
IntegerSchema::new("Record count.")
pub const RECORD_COUNT_SCHEMA: Schema = IntegerSchema::new("Record count.")
.minimum(1)
.maximum(i32::MAX as isize)
.schema();
pub const DRIVE_OPTION_SCHEMA: Schema = StringSchema::new(
"Lto Tape Driver Option, either numeric value or option name.")
.schema();
pub const DRIVE_OPTION_SCHEMA: Schema =
StringSchema::new("Lto Tape Driver Option, either numeric value or option name.").schema();
pub const DRIVE_OPTION_LIST_SCHEMA: Schema =
ArraySchema::new("Drive Option List.", &DRIVE_OPTION_SCHEMA)
.min_length(1)
.schema();
.min_length(1)
.schema();
fn get_tape_handle(param: &Value) -> Result<SgTape, Error> {
if let Some(name) = param["drive"].as_str() {
let (config, _digest) = pbs_config::drive::config()?;
let drive: LtoTapeDrive = config.lookup("lto", name)?;
@ -88,7 +80,9 @@ fn get_tape_handle(param: &Value) -> Result<SgTape, Error> {
let mut drive_names = Vec::new();
for (name, (section_type, _)) in config.sections.iter() {
if section_type != "lto" { continue; }
if section_type != "lto" {
continue;
}
drive_names.push(name);
}
@ -122,7 +116,6 @@ fn get_tape_handle(param: &Value) -> Result<SgTape, Error> {
/// Position the tape at the beginning of the count file (after
/// filemark count)
fn asf(count: u64, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.locate_file(count)?;
@ -130,7 +123,6 @@ fn asf(count: u64, param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -152,7 +144,6 @@ fn asf(count: u64, param: Value) -> Result<(), Error> {
///
/// The tape is positioned on the last block of the previous file.
fn bsf(count: usize, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.space_filemarks(-count.try_into()?)?;
@ -160,7 +151,6 @@ fn bsf(count: usize, param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -183,7 +173,6 @@ fn bsf(count: usize, param: Value) -> Result<(), Error> {
/// This leaves the tape positioned at the first block of the file
/// that is count - 1 files before the current file.
fn bsfm(count: usize, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.space_filemarks(-count.try_into()?)?;
@ -192,7 +181,6 @@ fn bsfm(count: usize, param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -212,7 +200,6 @@ fn bsfm(count: usize, param: Value) -> Result<(), Error> {
)]
/// Backward space records.
fn bsr(count: usize, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.space_blocks(-count.try_into()?)?;
@ -220,7 +207,6 @@ fn bsr(count: usize, param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -241,7 +227,6 @@ fn bsr(count: usize, param: Value) -> Result<(), Error> {
)]
/// Read Cartridge Memory
fn cartridge_memory(param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let mut handle = get_tape_handle(&param)?;
@ -292,11 +277,11 @@ fn cartridge_memory(param: Value) -> Result<(), Error> {
)]
/// Read Tape Alert Flags
fn tape_alert_flags(param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let mut handle = get_tape_handle(&param)?;
let result = handle.tape_alert_flags()
let result = handle
.tape_alert_flags()
.map(|flags| format!("{:?}", flags));
if output_format == "json-pretty" {
@ -337,14 +322,12 @@ fn tape_alert_flags(param: Value) -> Result<(), Error> {
)]
/// Eject drive media
fn eject(param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.eject()?;
Ok(())
}
#[api(
input: {
properties: {
@ -361,14 +344,12 @@ fn eject(param: Value) -> Result<(), Error> {
)]
/// Move to end of media
fn eod(param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.move_to_eom(false)?;
Ok(())
}
#[api(
input: {
properties: {
@ -391,7 +372,6 @@ fn eod(param: Value) -> Result<(), Error> {
)]
/// Erase media (from current position)
fn erase(fast: Option<bool>, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.erase_media(fast.unwrap_or(true))?;
@ -420,7 +400,6 @@ fn erase(fast: Option<bool>, param: Value) -> Result<(), Error> {
)]
/// Format media, single partition
fn format(fast: Option<bool>, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.format_media(fast.unwrap_or(true))?;
@ -448,7 +427,6 @@ fn format(fast: Option<bool>, param: Value) -> Result<(), Error> {
///
/// The tape is positioned on the first block of the next file.
fn fsf(count: usize, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.space_filemarks(count.try_into()?)?;
@ -478,7 +456,6 @@ fn fsf(count: usize, param: Value) -> Result<(), Error> {
/// This leaves the tape positioned at the last block of the file that
/// is count - 1 files past the current file.
fn fsfm(count: usize, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.space_filemarks(count.try_into()?)?;
@ -487,7 +464,6 @@ fn fsfm(count: usize, param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -507,7 +483,6 @@ fn fsfm(count: usize, param: Value) -> Result<(), Error> {
)]
/// Forward space records.
fn fsr(count: usize, param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.space_blocks(count.try_into()?)?;
@ -515,7 +490,6 @@ fn fsr(count: usize, param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -532,14 +506,12 @@ fn fsr(count: usize, param: Value) -> Result<(), Error> {
)]
/// Load media
fn load(param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.load()?;
Ok(())
}
#[api(
input: {
properties: {
@ -556,7 +528,6 @@ fn load(param: Value) -> Result<(), Error> {
)]
/// Lock the tape drive door
fn lock(param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.set_medium_removal(false)?;
@ -564,7 +535,6 @@ fn lock(param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -581,14 +551,12 @@ fn lock(param: Value) -> Result<(), Error> {
)]
/// Rewind the tape
fn rewind(param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.rewind()?;
Ok(())
}
#[api(
input: {
properties: {
@ -601,7 +569,6 @@ fn rewind(param: Value) -> Result<(), Error> {
)]
/// Scan for existing tape changer devices
fn scan(param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let list = lto_tape_device_list();
@ -621,7 +588,10 @@ fn scan(param: Value) -> Result<(), Error> {
}
for item in list.iter() {
println!("{} ({}/{}/{})", item.path, item.vendor, item.model, item.serial);
println!(
"{} ({}/{}/{})",
item.path, item.vendor, item.model, item.serial
);
}
Ok(())
@ -647,7 +617,6 @@ fn scan(param: Value) -> Result<(), Error> {
)]
/// Drive Status
fn status(param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let mut handle = get_tape_handle(&param)?;
@ -677,7 +646,6 @@ fn status(param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -694,7 +662,6 @@ fn status(param: Value) -> Result<(), Error> {
)]
/// Unlock the tape drive door
fn unlock(param: Value) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
handle.set_medium_removal(true)?;
@ -702,7 +669,6 @@ fn unlock(param: Value) -> Result<(), Error> {
Ok(())
}
#[api(
input: {
properties: {
@ -723,7 +689,6 @@ fn unlock(param: Value) -> Result<(), Error> {
)]
/// Volume Statistics
fn volume_statistics(param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let mut handle = get_tape_handle(&param)?;
@ -772,7 +737,6 @@ fn volume_statistics(param: Value) -> Result<(), Error> {
)]
/// Write count (default 1) EOF marks at current position.
fn weof(count: Option<usize>, param: Value) -> Result<(), Error> {
let count = count.unwrap_or(1);
let mut handle = get_tape_handle(&param)?;
@ -825,7 +789,6 @@ fn options(
defaults: Option<bool>,
param: Value,
) -> Result<(), Error> {
let mut handle = get_tape_handle(&param)?;
if let Some(true) = defaults {
@ -838,7 +801,6 @@ fn options(
}
fn main() -> Result<(), Error> {
let uid = nix::unistd::Uid::current();
let username = match nix::unistd::User::from_uid(uid)? {
@ -875,8 +837,7 @@ fn main() -> Result<(), Error> {
.insert("tape-alert-flags", std_cmd(&API_METHOD_TAPE_ALERT_FLAGS))
.insert("unlock", std_cmd(&API_METHOD_UNLOCK))
.insert("volume-statistics", std_cmd(&API_METHOD_VOLUME_STATISTICS))
.insert("weof", std_cmd(&API_METHOD_WEOF).arg_param(&["count"]))
;
.insert("weof", std_cmd(&API_METHOD_WEOF).arg_param(&["count"]));
let mut rpcenv = CliEnvironment::new();
rpcenv.set_auth_id(Some(format!("{}@pam", username)));

View File

@ -11,29 +11,25 @@
///
/// - list serial number for attached drives, so that it is possible
/// to associate drive numbers with drives.
use std::fs::File;
use anyhow::{bail, Error};
use serde_json::Value;
use proxmox_schema::api;
use proxmox_router::cli::*;
use proxmox_router::RpcEnvironment;
use proxmox_schema::api;
use pbs_api_types::{LtoTapeDrive, ScsiTapeChanger, CHANGER_NAME_SCHEMA, SCSI_CHANGER_PATH_SCHEMA};
use pbs_config::drive::complete_changer_name;
use pbs_api_types::{
SCSI_CHANGER_PATH_SCHEMA, CHANGER_NAME_SCHEMA, ScsiTapeChanger, LtoTapeDrive,
};
use pbs_tape::{
linux_list_drives::{complete_changer_path, linux_tape_changer_list},
sg_pt_changer,
sgutils2::scsi_inquiry,
ElementStatus,
sg_pt_changer,
linux_list_drives::{complete_changer_path, linux_tape_changer_list},
};
fn get_changer_handle(param: &Value) -> Result<File, Error> {
if let Some(name) = param["changer"].as_str() {
let (config, _digest) = pbs_config::drive::config()?;
let changer_config: ScsiTapeChanger = config.lookup("changer", name)?;
@ -83,10 +79,7 @@ fn get_changer_handle(param: &Value) -> Result<File, Error> {
},
)]
/// Inquiry
fn inquiry(
param: Value,
) -> Result<(), Error> {
fn inquiry(param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let result: Result<_, Error> = proxmox_lang::try_block!({
@ -113,7 +106,10 @@ fn inquiry(
let info = result?;
println!("Type: {} ({})", info.peripheral_type_text, info.peripheral_type);
println!(
"Type: {} ({})",
info.peripheral_type_text, info.peripheral_type
);
println!("Vendor: {}", info.vendor);
println!("Product: {}", info.product);
println!("Revision: {}", info.revision);
@ -136,10 +132,7 @@ fn inquiry(
},
)]
/// Inventory
fn inventory(
param: Value,
) -> Result<(), Error> {
fn inventory(param: Value) -> Result<(), Error> {
let mut file = get_changer_handle(&param)?;
sg_pt_changer::initialize_element_status(&mut file)?;
@ -170,12 +163,7 @@ fn inventory(
},
)]
/// Load
fn load(
param: Value,
slot: u64,
drivenum: Option<u64>,
) -> Result<(), Error> {
fn load(param: Value, slot: u64, drivenum: Option<u64>) -> Result<(), Error> {
let mut file = get_changer_handle(&param)?;
let drivenum = drivenum.unwrap_or(0);
@ -210,12 +198,7 @@ fn load(
},
)]
/// Unload
fn unload(
param: Value,
slot: Option<u64>,
drivenum: Option<u64>,
) -> Result<(), Error> {
fn unload(param: Value, slot: Option<u64>, drivenum: Option<u64>) -> Result<(), Error> {
let mut file = get_changer_handle(&param)?;
let drivenum = drivenum.unwrap_or(0);
@ -271,10 +254,7 @@ fn unload(
},
)]
/// Changer Status
fn status(
param: Value,
) -> Result<(), Error> {
fn status(param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let result: Result<_, Error> = proxmox_lang::try_block!({
@ -302,7 +282,10 @@ fn status(
let status = result?;
for (i, transport) in status.transports.iter().enumerate() {
println!("Transport Element (Griper) {:>3}: {:?}",i, transport.status);
println!(
"Transport Element (Griper) {:>3}: {:?}",
i, transport.status
);
}
for (i, drive) in status.drives.iter().enumerate() {
@ -312,7 +295,7 @@ fn status(
};
let serial_txt = match drive.drive_serial_number {
Some(ref serial) => format!(", Serial: {}", serial),
None => String::new(),
None => String::new(),
};
println!(
@ -323,9 +306,9 @@ fn status(
for (i, slot) in status.slots.iter().enumerate() {
if slot.import_export {
println!(" Import/Export {:>3}: {:?}", i+1, slot.status);
println!(" Import/Export {:>3}: {:?}", i + 1, slot.status);
} else {
println!(" Storage Element {:>3}: {:?}", i+1, slot.status);
println!(" Storage Element {:>3}: {:?}", i + 1, slot.status);
}
}
@ -355,12 +338,7 @@ fn status(
},
)]
/// Transfer
fn transfer(
param: Value,
from: u64,
to: u64,
) -> Result<(), Error> {
fn transfer(param: Value, from: u64, to: u64) -> Result<(), Error> {
let mut file = get_changer_handle(&param)?;
sg_pt_changer::transfer_medium(&mut file, from, to)?;
@ -380,7 +358,6 @@ fn transfer(
)]
/// Scan for existing tape changer devices
fn scan(param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let list = linux_tape_changer_list();
@ -400,14 +377,16 @@ fn scan(param: Value) -> Result<(), Error> {
}
for item in list.iter() {
println!("{} ({}/{}/{})", item.path, item.vendor, item.model, item.serial);
println!(
"{} ({}/{}/{})",
item.path, item.vendor, item.model, item.serial
);
}
Ok(())
}
fn main() -> Result<(), Error> {
let uid = nix::unistd::Uid::current();
let username = match nix::unistd::User::from_uid(uid)? {
@ -415,49 +394,47 @@ fn main() -> Result<(), Error> {
None => bail!("unable to get user name"),
};
let cmd_def = CliCommandMap::new()
.usage_skip_options(&["device", "changer", "output-format"])
.insert(
"inquiry",
CliCommand::new(&API_METHOD_INQUIRY)
.completion_cb("changer", complete_changer_name)
.completion_cb("device", complete_changer_path)
.completion_cb("device", complete_changer_path),
)
.insert(
"inventory",
CliCommand::new(&API_METHOD_INVENTORY)
.completion_cb("changer", complete_changer_name)
.completion_cb("device", complete_changer_path)
.completion_cb("device", complete_changer_path),
)
.insert(
"load",
CliCommand::new(&API_METHOD_LOAD)
.arg_param(&["slot"])
.completion_cb("changer", complete_changer_name)
.completion_cb("device", complete_changer_path)
.completion_cb("device", complete_changer_path),
)
.insert(
"unload",
CliCommand::new(&API_METHOD_UNLOAD)
.completion_cb("changer", complete_changer_name)
.completion_cb("device", complete_changer_path)
.completion_cb("device", complete_changer_path),
)
.insert("scan", CliCommand::new(&API_METHOD_SCAN))
.insert(
"status",
CliCommand::new(&API_METHOD_STATUS)
.completion_cb("changer", complete_changer_name)
.completion_cb("device", complete_changer_path)
.completion_cb("device", complete_changer_path),
)
.insert(
"transfer",
CliCommand::new(&API_METHOD_TRANSFER)
.arg_param(&["from", "to"])
.completion_cb("changer", complete_changer_name)
.completion_cb("device", complete_changer_path)
)
;
.completion_cb("device", complete_changer_path),
);
let mut rpcenv = CliEnvironment::new();
rpcenv.set_auth_id(Some(format!("{}@pam", username)));