oxedyne/fe2o3/fe2o3_sys/src/room.rs
3.5 KiB, 1 run
created by r1870400018:76767, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The room left on a filesystem, and the bytes a directory tree takes on it. |
| 2 | //! |
| 3 | //! [`room`] reads `statvfs` for the bytes an unprivileged writer may still use |
| 4 | //! (`f_bavail`, which leaves out the blocks reserved for root, so it is the figure an |
| 5 | //! application meets before it is told the disk is full) beside the filesystem's size. |
| 6 | //! [`bytes_under`] totals the regular files under a directory, which is what a copy of |
| 7 | //! that directory will write. A service that must keep room for a copy of its own store |
| 8 | //! (a nightly backup to the same disk, say) weighs the two before it accepts new bytes. |
| 9 | |
| 10 | use oxedyne_fe2o3_core::prelude::*; |
| 11 | |
| 12 | use std::{ |
| 13 | fs, |
| 14 | path::Path, |
| 15 | }; |
| 16 | |
| 17 | /// One reading of a filesystem's room, in bytes. |
| 18 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 19 | pub struct Room { |
| 20 | pub avail: u64, // Bytes an unprivileged writer may still use. |
| 21 | pub total: u64, // The filesystem's size. |
| 22 | } |
| 23 | |
| 24 | /// The room on the filesystem holding `path`. |
| 25 | pub fn room(path: &Path) -> Outcome<Room> { |
| 26 | let st = match nix::sys::statvfs::statvfs(path) { |
| 27 | Ok(s) => s, |
| 28 | Err(e) => return Err(err!(e, |
| 29 | "Reading the free room on the filesystem holding {:?}.", path; |
| 30 | IO, File, Read)), |
| 31 | }; |
| 32 | let frag = st.fragment_size() as u64; |
| 33 | Ok(Room { |
| 34 | avail: (st.blocks_available() as u64).saturating_mul(frag), |
| 35 | total: (st.blocks() as u64).saturating_mul(frag), |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | /// The bytes the regular files under `dir` take, by their lengths, walking every |
| 40 | /// directory below it. Symbolic links are not followed, so a link out of the tree |
| 41 | /// is neither counted nor walked. |
| 42 | pub fn bytes_under(dir: &Path) -> Outcome<u64> { |
| 43 | let mut sum = 0u64; |
| 44 | let mut stack = vec![dir.to_path_buf()]; |
| 45 | while let Some(d) = stack.pop() { |
| 46 | let rd = match fs::read_dir(&d) { |
| 47 | Ok(r) => r, |
| 48 | Err(e) => return Err(err!(e, |
| 49 | "Listing {:?} to total the bytes under {:?}.", d, dir; IO, File, Read)), |
| 50 | }; |
| 51 | for ent in rd { |
| 52 | let ent = match ent { |
| 53 | Ok(x) => x, |
| 54 | Err(e) => return Err(err!(e, |
| 55 | "Reading an entry of {:?}.", d; IO, File, Read)), |
| 56 | }; |
| 57 | let ft = match ent.file_type() { |
| 58 | Ok(t) => t, |
| 59 | Err(e) => return Err(err!(e, |
| 60 | "Reading the type of {:?}.", ent.path(); IO, File, Read)), |
| 61 | }; |
| 62 | if ft.is_dir() { |
| 63 | stack.push(ent.path()); |
| 64 | } else if ft.is_file() { |
| 65 | let md = match ent.metadata() { |
| 66 | Ok(m) => m, |
| 67 | Err(e) => return Err(err!(e, |
| 68 | "Reading the length of {:?}.", ent.path(); IO, File, Read)), |
| 69 | }; |
| 70 | sum = sum.saturating_add(md.len()); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | Ok(sum) |
| 75 | } |
| 76 | |
| 77 | #[cfg(test)] |
| 78 | mod tests { |
| 79 | use super::*; |
| 80 | |
| 81 | #[test] |
| 82 | fn a_tree_is_totalled_and_the_disk_has_room() -> Outcome<()> { |
| 83 | let base = std::env::temp_dir().join(fmt!("fe2o3_sys_room_{}", std::process::id())); |
| 84 | let sub = base.join("a").join("b"); |
| 85 | res!(fs::create_dir_all(&sub)); |
| 86 | res!(fs::write(base.join("one"), vec![0u8; 100])); |
| 87 | res!(fs::write(sub.join("two"), vec![0u8; 23])); |
| 88 | let n = res!(bytes_under(&base)); |
| 89 | let r = res!(room(&base)); |
| 90 | res!(fs::remove_dir_all(&base)); |
| 91 | assert_eq!(n, 123, "100 + 23 bytes under the tree"); |
| 92 | assert!(r.total > 0 && r.avail <= r.total, "{:?}", r); |
| 93 | Ok(()) |
| 94 | } |
| 95 | } |