add blobstore size lookup for sbot status

This commit is contained in:
glyph 2022-02-03 16:27:20 +02:00
parent 8f49fa55ad
commit 435e819648
1 changed files with 33 additions and 1 deletions

View File

@ -1,11 +1,31 @@
//! Data types and associated methods for monitoring and configuring go-sbot.
use std::{fs, fs::File, io::Write, process::Command, str};
use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str};
use serde::{Deserialize, Serialize};
use crate::error::PeachError;
/* HELPER FUNCTIONS */
// iterate over the given directory path to determine the size of the directory
fn dir_size(path: impl Into<PathBuf>) -> io::Result<u64> {
fn dir_size(mut dir: fs::ReadDir) -> io::Result<u64> {
dir.try_fold(0, |acc, file| {
let file = file?;
let size = match file.metadata()? {
data if data.is_dir() => dir_size(fs::read_dir(file.path())?)?,
data => data.len(),
};
Ok(acc + size)
})
}
dir_size(fs::read_dir(path.into())?)
}
/* SBOT-RELATED TYPES AND METHODS */
/// go-sbot process status.
#[derive(Debug, Serialize, Deserialize)]
pub struct SbotStatus {
@ -19,6 +39,8 @@ pub struct SbotStatus {
pub uptime: Option<String>,
/// Downtime for the process (if state is `inactive`).
pub downtime: Option<String>,
/// Size of the blobs directory in bytes.
pub blobstore: Option<u64>,
}
/// Default builder for `SbotStatus`.
@ -30,6 +52,7 @@ impl Default for SbotStatus {
memory: None,
uptime: None,
downtime: None,
blobstore: None,
}
}
}
@ -101,6 +124,15 @@ impl SbotStatus {
}
}
// determine path of user's home directory
let mut blobstore_path = dirs::home_dir().ok_or(PeachError::HomeDir)?;
// append the blobstore path
blobstore_path.push(".ssb-go/blobs/sha256");
// determine the size of the blobstore directory in bytes
status.blobstore = dir_size(blobstore_path).ok();
Ok(status)
}
}