From 2712ab4cc2db08bf7d8e77ba0e550189198ee36e Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Fri, 24 Feb 2023 19:21:52 +0300 Subject: [PATCH] Add Windows support for storage monitor (#28) --- Cargo.lock | 13 ++++++++++++- client/storage-monitor/Cargo.toml | 2 +- client/storage-monitor/src/lib.rs | 13 +++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b5a32bd072340..c58c7a74bf11e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2679,6 +2679,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "fs4" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea55201cc351fdb478217c0fb641b59813da9b4efe4c414a9d8f989a657d149" +dependencies = [ + "libc", + "rustix 0.35.13", + "winapi", +] + [[package]] name = "fs_extra" version = "1.2.0" @@ -9297,9 +9308,9 @@ name = "sc-storage-monitor" version = "0.1.0" dependencies = [ "clap 4.0.32", + "fs4", "futures", "log", - "nix 0.26.1", "sc-client-db", "sc-utils", "sp-core", diff --git a/client/storage-monitor/Cargo.toml b/client/storage-monitor/Cargo.toml index 2ba24f9e2e718..52f34a2967654 100644 --- a/client/storage-monitor/Cargo.toml +++ b/client/storage-monitor/Cargo.toml @@ -12,7 +12,7 @@ homepage = "https://substrate.io" clap = { version = "4.0.9", features = ["derive", "string"] } futures = "0.3.21" log = "0.4.17" -nix = { version = "0.26.1", features = ["fs"] } +fs4 = "0.6.3" sc-client-db = { version = "0.10.0-dev", default-features = false, path = "../db" } sc-utils = { version = "4.0.0-dev", path = "../utils" } sp-core = { version = "7.0.0", path = "../../primitives/core" } diff --git a/client/storage-monitor/src/lib.rs b/client/storage-monitor/src/lib.rs index 162fcfe34a52b..3b1772ed98704 100644 --- a/client/storage-monitor/src/lib.rs +++ b/client/storage-monitor/src/lib.rs @@ -17,10 +17,11 @@ // along with this program. If not, see . use clap::Args; -use nix::{errno::Errno, sys::statvfs::statvfs}; +use fs4::statvfs; use sc_client_db::DatabaseSource; use sp_core::traits::SpawnEssentialNamed; use std::{ + io, path::{Path, PathBuf}, time::Duration, }; @@ -31,7 +32,7 @@ const LOG_TARGET: &str = "storage-monitor"; #[derive(Debug, thiserror::Error)] pub enum Error { #[error("IO Error")] - IOError(#[from] Errno), + IOError(#[from] io::Error), #[error("Out of storage space: available {0}MB, required {1}MB")] StorageOutOfSpace(u64, u64), } @@ -117,12 +118,8 @@ impl StorageMonitorService { /// Returns free space in MB, or error if statvfs failed. fn free_space(path: &Path) -> Result { - statvfs(path) - .map(|stats| { - u64::from(stats.blocks_available()).saturating_mul(u64::from(stats.block_size())) / - 1_000_000 - }) - .map_err(Error::from) + let amount = statvfs(path)?; + Ok(amount.available_space() / (1024 * 1024)) } /// Checks if the amount of free space for given `path` is above given `threshold`.