Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit ec01d70

Browse files
authoredJan 6, 2022
Merge pull request #201 from cgwalters/rustix
Use rustix instead of nix
2 parents 88a6f40 + b3974f5 commit ec01d70

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed
 

Diff for: ‎lib/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ hex = "0.4.3"
2424
indicatif = "0.16.0"
2525
once_cell = "1.9"
2626
libc = "0.2.92"
27-
nix = "0.23"
27+
rustix = "0.31.3"
2828
oci-spec = "0.5.0"
2929
openat = "0.1.20"
3030
openat-ext = "0.2.0"

Diff for: ‎lib/src/cmdext.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
use std::os::unix::prelude::{CommandExt, RawFd};
1+
use rustix::fd::{FromRawFd, IntoRawFd};
2+
use rustix::io::OwnedFd;
3+
use std::os::unix::prelude::CommandExt;
4+
use std::sync::Arc;
25

36
pub(crate) trait CommandRedirectionExt {
47
/// Pass a file descriptor into the target process.
5-
/// IMPORTANT: `fd` must be valid (i.e. cannot be closed) until after [`std::Process::Command::spawn`] or equivalent is invoked.
6-
fn take_fd_n(&mut self, fd: i32, target: i32) -> &mut Self;
8+
fn take_fd_n(&mut self, fd: Arc<OwnedFd>, target: i32) -> &mut Self;
79
}
810

911
#[allow(unsafe_code)]
1012
impl CommandRedirectionExt for std::process::Command {
11-
fn take_fd_n(&mut self, fd: i32, target: i32) -> &mut Self {
13+
fn take_fd_n(&mut self, fd: Arc<OwnedFd>, target: i32) -> &mut Self {
1214
unsafe {
1315
self.pre_exec(move || {
14-
nix::unistd::dup2(fd, target as RawFd)
15-
.map(|_r| ())
16-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)))
16+
let target = rustix::io::OwnedFd::from_raw_fd(target);
17+
rustix::io::dup2(&*fd, &target)?;
18+
// Intentionally leak into the child.
19+
let _ = target.into_raw_fd();
20+
Ok(())
1721
});
1822
}
1923
self

Diff for: ‎lib/src/container/ociwriter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ impl<'a> OciWriter<'a> {
169169

170170
#[context("Writing OCI")]
171171
pub(crate) fn complete(self) -> Result<()> {
172-
let utsname = nix::sys::utsname::uname();
173-
let machine = utsname.machine();
172+
let uname = rustix::process::uname();
173+
let machine = uname.machine().to_str().unwrap();
174174
let arch = MACHINE_TO_OCI.get(machine).unwrap_or(&machine);
175175
let arch = oci_image::Arch::from(*arch);
176176

Diff for: ‎lib/src/tar/write.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ use anyhow::{anyhow, Context};
1313
use camino::{Utf8Component, Utf8Path, Utf8PathBuf};
1414
use ostree::gio;
1515
use ostree::prelude::FileExt;
16+
use rustix::fd::FromFd;
1617
use std::collections::BTreeMap;
1718
use std::convert::TryInto;
1819
use std::io::{BufWriter, Write};
19-
use std::os::unix::prelude::AsRawFd;
2020
use std::path::Path;
2121
use std::process::Stdio;
22+
use std::sync::Arc;
2223
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite};
2324
use tracing::instrument;
2425

@@ -197,13 +198,14 @@ pub async fn write_tar(
197198
};
198199
let mut c = std::process::Command::new("ostree");
199200
let repofd = repo.dfd_as_file()?;
201+
let repofd = Arc::new(rustix::io::OwnedFd::from_into_fd(repofd));
200202
{
201203
let c = c
202204
.stdin(Stdio::piped())
203205
.stdout(Stdio::piped())
204206
.stderr(Stdio::piped())
205207
.args(&["commit"]);
206-
c.take_fd_n(repofd.as_raw_fd(), 3);
208+
c.take_fd_n(repofd.clone(), 3);
207209
c.arg("--repo=/proc/self/fd/3");
208210
if let Some(sepolicy) = sepolicy.as_ref() {
209211
c.arg("--selinux-policy");

0 commit comments

Comments
 (0)
This repository has been archived.