use crate::backend::{OpenIntent, OpenOptions};
use crate::path::SmbPath;
pub(crate) fn path(name: &str) -> SmbPath {
name.parse().expect("a valid path")
}
pub(crate) fn opts(intent: OpenIntent) -> OpenOptions {
OpenOptions {
write: true,
intent,
..OpenOptions::default()
}
}
pub(crate) fn dir_opts(intent: OpenIntent) -> OpenOptions {
OpenOptions {
directory: true,
intent,
..OpenOptions::default()
}
}
#[cfg(all(unix, feature = "s3"))]
const CHILD: &str = "SMB_SERVER_TEST_CHILD";
#[cfg(all(unix, feature = "s3"))]
pub(crate) fn alone_in_child(module: &str, name: &str) -> bool {
if std::env::var_os(CHILD).is_some() {
return true;
}
let module = module.split_once("::").unwrap().1;
let out = std::process::Command::new(std::env::current_exe().unwrap())
.args([&format!("{module}::{name}"), "--exact", "--test-threads=1"])
.env(CHILD, "1")
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
out.status.success() && stdout.contains("1 passed"),
"{stdout}{}",
String::from_utf8_lossy(&out.stderr)
);
false
}
#[cfg(all(unix, feature = "s3"))]
pub(crate) fn open_descriptors() -> usize {
(0..4096)
.filter(|&fd| unsafe { libc::fcntl(fd, libc::F_GETFD) } != -1)
.count()
}
#[cfg(all(unix, feature = "s3"))]
pub(crate) fn exhaust_descriptors() -> Vec<std::fs::File> {
let mut taken = Vec::new();
while let Ok(file) = std::fs::File::open("/dev/null") {
taken.push(file);
}
taken
}
#[cfg(all(unix, feature = "s3"))]
pub(crate) fn limit_descriptors(headroom: usize) {
let mut limit = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
unsafe {
assert_eq!(libc::getrlimit(libc::RLIMIT_NOFILE, &mut limit), 0);
limit.rlim_cur = (open_descriptors() + headroom) as libc::rlim_t;
assert_eq!(libc::setrlimit(libc::RLIMIT_NOFILE, &limit), 0);
}
}