In the below code I'm creating a temporary image file of given size and mounting it as a filesystem at a given directory. But the available space returned using fs4::available_space
doesn't match with temporary image file size created using dd
command.
use std::process::Command;
const TMP_DIR: &str = "/tmp/memory_check_test/";
fn init() {
let _ = Command::new("dd")
.args(["if=/dev/zero", "of=mem_check_file", "bs=1000", "count=100"])
.output()
.expect("failed to create empty file");
let _ = Command::new("mkfs.ext3")
.arg("mem_check_file")
.output()
.expect("failed to format mem_check_file with ext3 filesystem");
let _ = Command::new("mkdir")
.arg("-p")
.arg(TMP_DIR)
.output()
.expect(&format!("failed to create directory {}", TMP_DIR));
let _ = Command::new("mount")
.arg("-o")
.args(["loop", "mem_check_file", TMP_DIR])
.output()
.expect(&format!("failed to mount file to {} directory", TMP_DIR));
let _ = Command::new("chmod")
.arg("-R")
.args(["777", TMP_DIR])
.output()
.expect(&format!("failed to change permissions for {} directory", TMP_DIR));
}
fn teardown() {
let _ = Command::new("umount")
.arg(TMP_DIR)
.output()
.expect(&format!("failed to unmount {} directory", TMP_DIR));
let _ = Command::new("rmdir")
.arg(TMP_DIR)
.output()
.expect(&format!("failed to remove {} directory", TMP_DIR));
let _ = Command::new("rm")
.arg("mem_check_file")
.output()
.expect("failed to remove mem_check_file");
}
fn main() {
init();
let available_space = fs4::available_space(TMP_DIR).expect("Failed to retrieve available memory space");
println!("{}", available_space);
teardown();
assert_eq!(available_space, 100000);
}
Output
thread 'main' panicked at src/main.rs:57:5:
assertion `left == right` failed
left: 100000
right: 839506960384
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Cargo.toml
[package]
name = "mem_check"
version = "0.1.0"
edition = "2024"
[dependencies]
fs4 = "0.13.1"
In the below code I'm creating a temporary image file of given size and mounting it as a filesystem at a given directory. But the available space returned using fs4::available_space
doesn't match with temporary image file size created using dd
command.
use std::process::Command;
const TMP_DIR: &str = "/tmp/memory_check_test/";
fn init() {
let _ = Command::new("dd")
.args(["if=/dev/zero", "of=mem_check_file", "bs=1000", "count=100"])
.output()
.expect("failed to create empty file");
let _ = Command::new("mkfs.ext3")
.arg("mem_check_file")
.output()
.expect("failed to format mem_check_file with ext3 filesystem");
let _ = Command::new("mkdir")
.arg("-p")
.arg(TMP_DIR)
.output()
.expect(&format!("failed to create directory {}", TMP_DIR));
let _ = Command::new("mount")
.arg("-o")
.args(["loop", "mem_check_file", TMP_DIR])
.output()
.expect(&format!("failed to mount file to {} directory", TMP_DIR));
let _ = Command::new("chmod")
.arg("-R")
.args(["777", TMP_DIR])
.output()
.expect(&format!("failed to change permissions for {} directory", TMP_DIR));
}
fn teardown() {
let _ = Command::new("umount")
.arg(TMP_DIR)
.output()
.expect(&format!("failed to unmount {} directory", TMP_DIR));
let _ = Command::new("rmdir")
.arg(TMP_DIR)
.output()
.expect(&format!("failed to remove {} directory", TMP_DIR));
let _ = Command::new("rm")
.arg("mem_check_file")
.output()
.expect("failed to remove mem_check_file");
}
fn main() {
init();
let available_space = fs4::available_space(TMP_DIR).expect("Failed to retrieve available memory space");
println!("{}", available_space);
teardown();
assert_eq!(available_space, 100000);
}
Output
thread 'main' panicked at src/main.rs:57:5:
assertion `left == right` failed
left: 100000
right: 839506960384
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Cargo.toml
[package]
name = "mem_check"
version = "0.1.0"
edition = "2024"
[dependencies]
fs4 = "0.13.1"
Share
edited Mar 11 at 9:42
JensV
4,5892 gold badges22 silver badges48 bronze badges
asked Mar 11 at 8:04
HarryHarry
3,2581 gold badge24 silver badges46 bronze badges
4
|
1 Answer
Reset to default 1There's two issues here. You assume that if the command you run produces an error, your expect
function will be called which is not the case. The expect message is only produced if it's unable to run your command (for example if the command does not exist). If the command produces a non-zero exit status you need to check that yourself.
Since you don't really care about processing command output, you should rather use the following form:
let status = Command::new("...")
# any args
.status()
.expect("failed run run command")
println!("process finished with: {status}");
assert!(status.success());
This has the added benefit, that stdout and stderr are redirected automatically and become visible, aiding in troubleshooting.
Running the program as root - which is required for the mount
command to succeed - yields a value of 70656
in the end for me. Which is expected accounting for file system overhead and reserved space in the filesystem.
fdisk -l <disk>
I get a size of 97.5KiB. If I usedf
to show actual available space it's even lower. See e.g. unix.stackexchange/questions/40819/… – JensV Commented Mar 11 at 8:23sudo -E cargo run
, the assertion still fails, I mean huge difference between left and right values – Harry Commented Mar 11 at 9:06