diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index d08e6db..a9fbc31 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -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) -> io::Result { + fn dir_size(mut dir: fs::ReadDir) -> io::Result { + 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, /// Downtime for the process (if state is `inactive`). pub downtime: Option, + /// Size of the blobs directory in bytes. + pub blobstore: Option, } /// 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) } }