Line data Source code
1 : //! Wrapper around nix::sys::statvfs::Statvfs that allows for mocking.
2 :
3 : use camino::Utf8Path;
4 :
5 : pub enum Statvfs {
6 : Real(nix::sys::statvfs::Statvfs),
7 : Mock(mock::Statvfs),
8 : }
9 :
10 : // NB: on macOS, the block count type of struct statvfs is u32.
11 : // The workaround seems to be to use the non-standard statfs64 call.
12 : // Sincce it should only be a problem on > 2TiB disks, let's ignore
13 : // the problem for now and upcast to u64.
14 : impl Statvfs {
15 0 : pub fn get(tenants_dir: &Utf8Path, mocked: Option<&mock::Behavior>) -> nix::Result<Self> {
16 0 : if let Some(mocked) = mocked {
17 0 : Ok(Statvfs::Mock(mock::get(tenants_dir, mocked)?))
18 : } else {
19 0 : Ok(Statvfs::Real(nix::sys::statvfs::statvfs(
20 0 : tenants_dir.as_std_path(),
21 0 : )?))
22 : }
23 0 : }
24 :
25 : // NB: allow() because the block count type is u32 on macOS.
26 : #[allow(clippy::useless_conversion, clippy::unnecessary_fallible_conversions)]
27 0 : pub fn blocks(&self) -> u64 {
28 0 : match self {
29 0 : Statvfs::Real(stat) => u64::try_from(stat.blocks()).unwrap(),
30 0 : Statvfs::Mock(stat) => stat.blocks,
31 : }
32 0 : }
33 :
34 : // NB: allow() because the block count type is u32 on macOS.
35 : #[allow(clippy::useless_conversion, clippy::unnecessary_fallible_conversions)]
36 0 : pub fn blocks_available(&self) -> u64 {
37 0 : match self {
38 0 : Statvfs::Real(stat) => u64::try_from(stat.blocks_available()).unwrap(),
39 0 : Statvfs::Mock(stat) => stat.blocks_available,
40 : }
41 0 : }
42 :
43 0 : pub fn fragment_size(&self) -> u64 {
44 0 : match self {
45 0 : Statvfs::Real(stat) => stat.fragment_size(),
46 0 : Statvfs::Mock(stat) => stat.fragment_size,
47 : }
48 0 : }
49 :
50 0 : pub fn block_size(&self) -> u64 {
51 0 : match self {
52 0 : Statvfs::Real(stat) => stat.block_size(),
53 0 : Statvfs::Mock(stat) => stat.block_size,
54 : }
55 0 : }
56 : }
57 :
58 : pub mod mock {
59 : use camino::Utf8Path;
60 : use regex::Regex;
61 : use tracing::log::info;
62 :
63 0 : #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
64 : #[serde(tag = "type")]
65 : pub enum Behavior {
66 : Success {
67 : blocksize: u64,
68 : total_blocks: u64,
69 : name_filter: Option<utils::serde_regex::Regex>,
70 : },
71 : Failure {
72 : mocked_error: MockedError,
73 : },
74 : }
75 :
76 0 : #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
77 : #[allow(clippy::upper_case_acronyms)]
78 : pub enum MockedError {
79 : EIO,
80 : }
81 :
82 : impl From<MockedError> for nix::Error {
83 0 : fn from(e: MockedError) -> Self {
84 0 : match e {
85 0 : MockedError::EIO => nix::Error::EIO,
86 0 : }
87 0 : }
88 : }
89 :
90 0 : pub fn get(tenants_dir: &Utf8Path, behavior: &Behavior) -> nix::Result<Statvfs> {
91 0 : info!("running mocked statvfs");
92 :
93 0 : match behavior {
94 : Behavior::Success {
95 0 : blocksize,
96 0 : total_blocks,
97 0 : ref name_filter,
98 0 : } => {
99 0 : let used_bytes = walk_dir_disk_usage(tenants_dir, name_filter.as_deref()).unwrap();
100 0 :
101 0 : // round it up to the nearest block multiple
102 0 : let used_blocks = (used_bytes + (blocksize - 1)) / blocksize;
103 0 :
104 0 : if used_blocks > *total_blocks {
105 0 : panic!(
106 0 : "mocking error: used_blocks > total_blocks: {used_blocks} > {total_blocks}"
107 0 : );
108 0 : }
109 0 :
110 0 : let avail_blocks = total_blocks - used_blocks;
111 0 :
112 0 : Ok(Statvfs {
113 0 : blocks: *total_blocks,
114 0 : blocks_available: avail_blocks,
115 0 : fragment_size: *blocksize,
116 0 : block_size: *blocksize,
117 0 : })
118 : }
119 0 : Behavior::Failure { mocked_error } => Err((*mocked_error).into()),
120 : }
121 0 : }
122 :
123 0 : fn walk_dir_disk_usage(path: &Utf8Path, name_filter: Option<&Regex>) -> anyhow::Result<u64> {
124 0 : let mut total = 0;
125 0 : for entry in walkdir::WalkDir::new(path) {
126 0 : let entry = entry?;
127 0 : if !entry.file_type().is_file() {
128 0 : continue;
129 0 : }
130 0 : if !name_filter
131 0 : .as_ref()
132 0 : .map(|filter| filter.is_match(entry.file_name().to_str().unwrap()))
133 0 : .unwrap_or(true)
134 : {
135 0 : continue;
136 0 : }
137 0 : let m = match entry.metadata() {
138 0 : Ok(m) => m,
139 0 : Err(e) if is_not_found(&e) => {
140 0 : // some temp file which got removed right as we are walking
141 0 : continue;
142 : }
143 0 : Err(e) => {
144 0 : return Err(anyhow::Error::new(e)
145 0 : .context(format!("get metadata of {:?}", entry.path())))
146 : }
147 : };
148 0 : total += m.len();
149 : }
150 0 : Ok(total)
151 0 : }
152 :
153 0 : fn is_not_found(e: &walkdir::Error) -> bool {
154 0 : let Some(io_error) = e.io_error() else {
155 0 : return false;
156 : };
157 0 : let kind = io_error.kind();
158 0 : matches!(kind, std::io::ErrorKind::NotFound)
159 0 : }
160 :
161 : pub struct Statvfs {
162 : pub blocks: u64,
163 : pub blocks_available: u64,
164 : pub fragment_size: u64,
165 : pub block_size: u64,
166 : }
167 : }
|