Skip to content

Commit 547002d

Browse files
committed
Check ESP space to avoid errors
1 parent 68585a8 commit 547002d

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/filetree.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ impl FileTree {
207207
Ok(Self { children })
208208
}
209209

210+
/// Total size in bytes of all files in the tree (for space checks).
211+
#[cfg(any(
212+
target_arch = "x86_64",
213+
target_arch = "aarch64",
214+
target_arch = "riscv64"
215+
))]
216+
pub(crate) fn total_size(&self) -> u64 {
217+
self.children.values().map(|m| m.size).sum()
218+
}
219+
210220
/// Determine the changes *from* self to the updated tree
211221
#[cfg(any(
212222
target_arch = "x86_64",

src/util.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::collections::HashSet;
2+
use std::os::unix::io::AsRawFd;
23
use std::path::Path;
34
use std::process::Command;
45

56
use anyhow::{bail, Context, Result};
67
use openat_ext::OpenatDirExt;
8+
use rustix::fd::BorrowedFd;
79

810
/// Parse an environment variable as UTF-8
911
#[allow(dead_code)]
@@ -51,9 +53,18 @@ pub(crate) fn filenames(dir: &openat::Dir) -> Result<HashSet<String>> {
5153
Ok(ret)
5254
}
5355

56+
/// Return the available space in bytes on the filesystem containing the given directory.
57+
/// Uses f_bavail * f_frsize from fstatvfs to avoid partial updates when the partition is full.
58+
pub(crate) fn available_space_bytes(dir: &openat::Dir) -> Result<u64> {
59+
let fd = unsafe { BorrowedFd::borrow_raw(dir.as_raw_fd()) };
60+
let st = rustix::fs::fstatvfs(fd)?;
61+
Ok((st.f_bavail as u64) * (st.f_frsize as u64))
62+
}
63+
5464
pub(crate) fn ensure_writable_mount<P: AsRef<Path>>(p: P) -> Result<()> {
5565
let p = p.as_ref();
56-
let stat = rustix::fs::statvfs(p)?;
66+
let stat =
67+
rustix::fs::statvfs(p).map_err(|e| std::io::Error::from_raw_os_error(e.raw_os_error()))?;
5768
if !stat.f_flag.contains(rustix::fs::StatVfsMountFlags::RDONLY) {
5869
return Ok(());
5970
}

0 commit comments

Comments
 (0)