Skip to content

Commit 8a40a4e

Browse files
authored
Merge pull request #670 from cgwalters/port-rustix
Port to rustix, drop nix
2 parents 7ae681f + feada73 commit 8a40a4e

File tree

7 files changed

+27
-33
lines changed

7 files changed

+27
-33
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ hex = "0.4.3"
3030
libc = "^0.2"
3131
libsystemd = ">= 0.3, < 0.8"
3232
log = "^0.4"
33-
nix = ">= 0.22.1, < 0.24.0"
3433
openat = "0.1.20"
3534
openat-ext = ">= 0.2.2, < 0.3.0"
3635
openssl = "^0.10"
3736
os-release = "0.1.0"
37+
rustix = { version = "0.38.34", features = ["process", "fs"] }
3838
serde = { version = "^1.0", features = ["derive"] }
3939
serde_json = "^1.0"
4040
tempfile = "^3.10"

src/cli/bootupctl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn running_in_systemd() -> bool {
142142

143143
/// Require root permission
144144
fn require_root_permission() -> Result<()> {
145-
if !nix::unistd::Uid::effective().is_root() {
145+
if !rustix::process::getuid().is_root() {
146146
anyhow::bail!("This command requires root privileges")
147147
}
148148
Ok(())

src/efi.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use anyhow::{bail, Context, Result};
1313
use fn_error_context::context;
1414
use openat_ext::OpenatDirExt;
1515
use os_release::OsRelease;
16+
use rustix::fd::BorrowedFd;
1617
use walkdir::WalkDir;
1718
use widestring::U16CString;
1819

@@ -82,9 +83,9 @@ impl Efi {
8283
if !mnt.exists() {
8384
continue;
8485
}
85-
let st = nix::sys::statfs::statfs(&mnt)
86-
.with_context(|| format!("statfs failed for {mnt:?}"))?;
87-
if st.filesystem_type() != nix::sys::statfs::MSDOS_SUPER_MAGIC {
86+
let st =
87+
rustix::fs::statfs(&mnt).with_context(|| format!("statfs failed for {mnt:?}"))?;
88+
if st.f_type != libc::MSDOS_SUPER_MAGIC {
8889
continue;
8990
}
9091
log::debug!("Reusing existing {mnt:?}");
@@ -454,10 +455,13 @@ impl Drop for Efi {
454455
}
455456

456457
fn validate_esp(dir: &openat::Dir) -> Result<()> {
457-
let stat = nix::sys::statfs::fstatfs(dir)?;
458-
let fstype = stat.filesystem_type();
459-
if fstype != nix::sys::statfs::MSDOS_SUPER_MAGIC {
460-
bail!("EFI mount is not a msdos filesystem, but is {:?}", fstype);
458+
let dir = unsafe { BorrowedFd::borrow_raw(dir.as_raw_fd()) };
459+
let stat = rustix::fs::fstatfs(&dir)?;
460+
if stat.f_type != libc::MSDOS_SUPER_MAGIC {
461+
bail!(
462+
"EFI mount is not a msdos filesystem, but is {:?}",
463+
stat.f_type
464+
);
461465
};
462466
Ok(())
463467
}

src/filesystem.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::process::Command;
55

66
use anyhow::{Context, Result};
77
use fn_error_context::context;
8+
use rustix::fd::BorrowedFd;
89
use serde::Deserialize;
910

1011
#[derive(Deserialize, Debug)]
@@ -24,12 +25,12 @@ pub(crate) struct Findmnt {
2425

2526
#[context("Inspecting filesystem {path:?}")]
2627
pub(crate) fn inspect_filesystem(root: &openat::Dir, path: &str) -> Result<Filesystem> {
27-
let rootfd = root.as_raw_fd();
28+
let rootfd = unsafe { BorrowedFd::borrow_raw(root.as_raw_fd()) };
2829
// SAFETY: This is unsafe just for the pre_exec, when we port to cap-std we can use cap-std-ext
2930
let o = unsafe {
3031
Command::new("findmnt")
3132
.args(["-J", "-v", "--output=SOURCE,FSTYPE,OPTIONS,UUID", path])
32-
.pre_exec(move || nix::unistd::fchdir(rootfd).map_err(Into::into))
33+
.pre_exec(move || rustix::process::fchdir(rootfd).map_err(Into::into))
3334
.output()?
3435
};
3536
let st = o.status;

src/filetree.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ use std::fmt::Display;
1717
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
1818
use std::os::unix::io::AsRawFd;
1919
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
20-
use std::os::unix::process::CommandExt;
21-
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
2220
use std::path::Path;
2321

2422
/// The prefix we apply to our temporary files.
@@ -274,20 +272,12 @@ pub(crate) struct ApplyUpdateOptions {
274272
// Let's just fork off a helper process for now.
275273
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
276274
pub(crate) fn syncfs(d: &openat::Dir) -> Result<()> {
277-
let d = d.sub_dir(".").expect("subdir");
278-
let mut c = std::process::Command::new("sync");
279-
let c = c.args(["-f", "."]);
280-
unsafe {
281-
c.pre_exec(move || {
282-
nix::unistd::fchdir(d.as_raw_fd()).expect("fchdir");
283-
Ok(())
284-
})
285-
};
286-
let r = c.status().context("syncfs failed")?;
287-
if !r.success() {
288-
bail!("syncfs failed");
289-
}
290-
Ok(())
275+
use rustix::fd::BorrowedFd;
276+
use rustix::fs::{Mode, OFlags};
277+
let d = unsafe { BorrowedFd::borrow_raw(d.as_raw_fd()) };
278+
let oflags = OFlags::RDONLY | OFlags::CLOEXEC | OFlags::DIRECTORY;
279+
let d = rustix::fs::openat(d, ".", oflags, Mode::empty())?;
280+
rustix::fs::syncfs(d).map_err(Into::into)
291281
}
292282

293283
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]

src/util.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ pub(crate) fn filenames(dir: &openat::Dir) -> Result<HashSet<String>> {
6767
}
6868

6969
pub(crate) fn ensure_writable_mount<P: AsRef<Path>>(p: P) -> Result<()> {
70-
use nix::sys::statvfs;
7170
let p = p.as_ref();
72-
let stat = statvfs::statvfs(p)?;
73-
if !stat.flags().contains(statvfs::FsFlags::ST_RDONLY) {
71+
let stat = rustix::fs::statvfs(p)?;
72+
if !stat.f_flag.contains(rustix::fs::StatVfsMountFlags::RDONLY) {
7473
return Ok(());
7574
}
7675
let status = std::process::Command::new("mount")

0 commit comments

Comments
 (0)