Skip to content

Commit

Permalink
Merge pull request #670 from cgwalters/port-rustix
Browse files Browse the repository at this point in the history
Port to rustix, drop nix
  • Loading branch information
cgwalters committed Jun 20, 2024
2 parents 7ae681f + feada73 commit 8a40a4e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ hex = "0.4.3"
libc = "^0.2"
libsystemd = ">= 0.3, < 0.8"
log = "^0.4"
nix = ">= 0.22.1, < 0.24.0"
openat = "0.1.20"
openat-ext = ">= 0.2.2, < 0.3.0"
openssl = "^0.10"
os-release = "0.1.0"
rustix = { version = "0.38.34", features = ["process", "fs"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
tempfile = "^3.10"
Expand Down
2 changes: 1 addition & 1 deletion src/cli/bootupctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn running_in_systemd() -> bool {

/// Require root permission
fn require_root_permission() -> Result<()> {
if !nix::unistd::Uid::effective().is_root() {
if !rustix::process::getuid().is_root() {
anyhow::bail!("This command requires root privileges")
}
Ok(())
Expand Down
18 changes: 11 additions & 7 deletions src/efi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use anyhow::{bail, Context, Result};
use fn_error_context::context;
use openat_ext::OpenatDirExt;
use os_release::OsRelease;
use rustix::fd::BorrowedFd;
use walkdir::WalkDir;
use widestring::U16CString;

Expand Down Expand Up @@ -82,9 +83,9 @@ impl Efi {
if !mnt.exists() {
continue;
}
let st = nix::sys::statfs::statfs(&mnt)
.with_context(|| format!("statfs failed for {mnt:?}"))?;
if st.filesystem_type() != nix::sys::statfs::MSDOS_SUPER_MAGIC {
let st =
rustix::fs::statfs(&mnt).with_context(|| format!("statfs failed for {mnt:?}"))?;
if st.f_type != libc::MSDOS_SUPER_MAGIC {
continue;
}
log::debug!("Reusing existing {mnt:?}");
Expand Down Expand Up @@ -454,10 +455,13 @@ impl Drop for Efi {
}

fn validate_esp(dir: &openat::Dir) -> Result<()> {
let stat = nix::sys::statfs::fstatfs(dir)?;
let fstype = stat.filesystem_type();
if fstype != nix::sys::statfs::MSDOS_SUPER_MAGIC {
bail!("EFI mount is not a msdos filesystem, but is {:?}", fstype);
let dir = unsafe { BorrowedFd::borrow_raw(dir.as_raw_fd()) };
let stat = rustix::fs::fstatfs(&dir)?;
if stat.f_type != libc::MSDOS_SUPER_MAGIC {
bail!(
"EFI mount is not a msdos filesystem, but is {:?}",
stat.f_type
);
};
Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::process::Command;

use anyhow::{Context, Result};
use fn_error_context::context;
use rustix::fd::BorrowedFd;
use serde::Deserialize;

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

#[context("Inspecting filesystem {path:?}")]
pub(crate) fn inspect_filesystem(root: &openat::Dir, path: &str) -> Result<Filesystem> {
let rootfd = root.as_raw_fd();
let rootfd = unsafe { BorrowedFd::borrow_raw(root.as_raw_fd()) };
// SAFETY: This is unsafe just for the pre_exec, when we port to cap-std we can use cap-std-ext
let o = unsafe {
Command::new("findmnt")
.args(["-J", "-v", "--output=SOURCE,FSTYPE,OPTIONS,UUID", path])
.pre_exec(move || nix::unistd::fchdir(rootfd).map_err(Into::into))
.pre_exec(move || rustix::process::fchdir(rootfd).map_err(Into::into))
.output()?
};
let st = o.status;
Expand Down
22 changes: 6 additions & 16 deletions src/filetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use std::fmt::Display;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use std::os::unix::io::AsRawFd;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use std::os::unix::process::CommandExt;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use std::path::Path;

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

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
Expand Down
5 changes: 2 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ pub(crate) fn filenames(dir: &openat::Dir) -> Result<HashSet<String>> {
}

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

0 comments on commit 8a40a4e

Please sign in to comment.