From a58a5cf79588840cc87837b7f0113df00d4c3173 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Fri, 19 Nov 2021 13:48:38 +0100 Subject: [PATCH] move HumanByte to pbs-abi-types crate Originally-by: Dietmar Maurer Signed-off-by: Thomas Lamprecht --- pbs-api-types/src/human_byte.rs | 50 +++++++++++++++++++++++++++++ pbs-api-types/src/lib.rs | 3 ++ pbs-client/src/backup_writer.rs | 2 +- pbs-datastore/src/datastore.rs | 3 +- pbs-tools/Cargo.toml | 1 + pbs-tools/src/format.rs | 53 ++----------------------------- src/bin/proxmox-tape.rs | 2 +- src/server/email_notifications.rs | 3 +- 8 files changed, 60 insertions(+), 57 deletions(-) create mode 100644 pbs-api-types/src/human_byte.rs diff --git a/pbs-api-types/src/human_byte.rs b/pbs-api-types/src/human_byte.rs new file mode 100644 index 00000000..a82d8fe8 --- /dev/null +++ b/pbs-api-types/src/human_byte.rs @@ -0,0 +1,50 @@ +pub struct HumanByte { + b: usize, +} +impl std::fmt::Display for HumanByte { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.b < 1024 { + return write!(f, "{} B", self.b); + } + let kb: f64 = self.b as f64 / 1024.0; + if kb < 1024.0 { + return write!(f, "{:.2} KiB", kb); + } + let mb: f64 = kb / 1024.0; + if mb < 1024.0 { + return write!(f, "{:.2} MiB", mb); + } + let gb: f64 = mb / 1024.0; + if gb < 1024.0 { + return write!(f, "{:.2} GiB", gb); + } + let tb: f64 = gb / 1024.0; + if tb < 1024.0 { + return write!(f, "{:.2} TiB", tb); + } + let pb: f64 = tb / 1024.0; + return write!(f, "{:.2} PiB", pb); + } +} +impl From for HumanByte { + fn from(v: usize) -> Self { + HumanByte { b: v } + } +} +impl From for HumanByte { + fn from(v: u64) -> Self { + HumanByte { b: v as usize } + } +} + +#[test] +fn correct_byte_convert() { + fn convert(b: usize) -> String { + HumanByte::from(b).to_string() + } + assert_eq!(convert(1023), "1023 B"); + assert_eq!(convert(1 << 10), "1.00 KiB"); + assert_eq!(convert(1 << 20), "1.00 MiB"); + assert_eq!(convert((1 << 30) + 103 * (1 << 20)), "1.10 GiB"); + assert_eq!(convert((2 << 50) + 500 * (1 << 40)), "2.49 PiB"); +} diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs index 4247bba3..a28ddafd 100644 --- a/pbs-api-types/src/lib.rs +++ b/pbs-api-types/src/lib.rs @@ -39,6 +39,9 @@ pub use acl::*; mod datastore; pub use datastore::*; +mod human_byte; +pub use human_byte::HumanByte; + mod jobs; pub use jobs::*; diff --git a/pbs-client/src/backup_writer.rs b/pbs-client/src/backup_writer.rs index 531de2e8..e44474c3 100644 --- a/pbs-client/src/backup_writer.rs +++ b/pbs-client/src/backup_writer.rs @@ -14,8 +14,8 @@ use tokio_stream::wrappers::ReceiverStream; use proxmox::tools::digest_to_hex; +use pbs_api_types::HumanByte; use pbs_tools::crypt_config::CryptConfig; -use pbs_tools::format::HumanByte; use pbs_datastore::{CATALOG_NAME, PROXMOX_BACKUP_PROTOCOL_ID_V1}; use pbs_datastore::data_blob::{ChunkInfo, DataBlob, DataChunkBuilder}; use pbs_datastore::dynamic_index::DynamicIndexReader; diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs index 1ee67655..027f1db8 100644 --- a/pbs-datastore/src/datastore.rs +++ b/pbs-datastore/src/datastore.rs @@ -14,8 +14,7 @@ use proxmox_sys::process_locker::ProcessLockSharedGuard; use proxmox_sys::worker_task_context::WorkerTaskContext; use proxmox_sys::{task_log, task_warn}; -use pbs_api_types::{UPID, DataStoreConfig, Authid, GarbageCollectionStatus}; -use pbs_tools::format::HumanByte; +use pbs_api_types::{UPID, DataStoreConfig, Authid, GarbageCollectionStatus, HumanByte}; use pbs_tools::fs::{lock_dir_noblock, DirLockGuard}; use pbs_config::{open_backup_lockfile, BackupLockGuard}; diff --git a/pbs-tools/Cargo.toml b/pbs-tools/Cargo.toml index 3edfe48f..7a2b6c27 100644 --- a/pbs-tools/Cargo.toml +++ b/pbs-tools/Cargo.toml @@ -40,6 +40,7 @@ proxmox-lang = { version = "1" } proxmox-time = { version = "1" } pbs-buildcfg = { path = "../pbs-buildcfg" } +pbs-api-types = { path = "../pbs-api-types" } [dev-dependencies] tokio = { version = "1.6", features = [ "macros" ] } diff --git a/pbs-tools/src/format.rs b/pbs-tools/src/format.rs index feabd8f1..df5fe544 100644 --- a/pbs-tools/src/format.rs +++ b/pbs-tools/src/format.rs @@ -3,6 +3,8 @@ use std::borrow::Borrow; use anyhow::{Error}; use serde_json::Value; +use pbs_api_types::HumanByte; + pub fn strip_server_file_extension(name: &str) -> &str { if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") { &name[..name.len()-5] @@ -63,54 +65,3 @@ pub fn render_bytes_human_readable(value: &Value, _record: &Value) -> Result) -> std::fmt::Result { - if self.b < 1024 { - return write!(f, "{} B", self.b); - } - let kb: f64 = self.b as f64 / 1024.0; - if kb < 1024.0 { - return write!(f, "{:.2} KiB", kb); - } - let mb: f64 = kb / 1024.0; - if mb < 1024.0 { - return write!(f, "{:.2} MiB", mb); - } - let gb: f64 = mb / 1024.0; - if gb < 1024.0 { - return write!(f, "{:.2} GiB", gb); - } - let tb: f64 = gb / 1024.0; - if tb < 1024.0 { - return write!(f, "{:.2} TiB", tb); - } - let pb: f64 = tb / 1024.0; - return write!(f, "{:.2} PiB", pb); - } -} -impl From for HumanByte { - fn from(v: usize) -> Self { - HumanByte { b: v } - } -} -impl From for HumanByte { - fn from(v: u64) -> Self { - HumanByte { b: v as usize } - } -} - -#[test] -fn correct_byte_convert() { - fn convert(b: usize) -> String { - HumanByte::from(b).to_string() - } - assert_eq!(convert(1023), "1023 B"); - assert_eq!(convert(1<<10), "1.00 KiB"); - assert_eq!(convert(1<<20), "1.00 MiB"); - assert_eq!(convert((1<<30) + 103 * (1<<20)), "1.10 GiB"); - assert_eq!(convert((2<<50) + 500 * (1<<40)), "2.49 PiB"); -} diff --git a/src/bin/proxmox-tape.rs b/src/bin/proxmox-tape.rs index 25a39601..eadadfa7 100644 --- a/src/bin/proxmox-tape.rs +++ b/src/bin/proxmox-tape.rs @@ -12,7 +12,6 @@ use proxmox_time::strftime_local; use pbs_client::view_task_result; use pbs_tools::format::{ - HumanByte, render_epoch, render_bytes_human_readable, }; @@ -25,6 +24,7 @@ use pbs_api_types::{ Userid, Authid, DATASTORE_SCHEMA, DATASTORE_MAP_LIST_SCHEMA, DRIVE_NAME_SCHEMA, MEDIA_LABEL_SCHEMA, MEDIA_POOL_NAME_SCHEMA, TAPE_RESTORE_SNAPSHOT_SCHEMA, GROUP_FILTER_LIST_SCHEMA, GroupListItem, + HumanByte }; use pbs_tape::{ PROXMOX_BACKUP_CONTENT_HEADER_MAGIC_1_0, BlockReadError, MediaContentHeader, diff --git a/src/server/email_notifications.rs b/src/server/email_notifications.rs index b462bd4c..7c047630 100644 --- a/src/server/email_notifications.rs +++ b/src/server/email_notifications.rs @@ -7,10 +7,9 @@ use proxmox::tools::email::sendmail; use proxmox_lang::try_block; use proxmox_schema::{parse_property_string, ApiType}; -use pbs_tools::format::HumanByte; use pbs_api_types::{ User, TapeBackupJobSetup, SyncJobConfig, VerificationJobConfig, - APTUpdateInfo, GarbageCollectionStatus, + APTUpdateInfo, GarbageCollectionStatus, HumanByte, Userid, Notify, DatastoreNotify, DataStoreConfig, };