Update to openat-ext 0.1.8, use new file writing API

This cleans up several cases.

Remove the utils path.
This commit is contained in:
Colin Walters 2020-10-27 16:37:00 +00:00 committed by OpenShift Merge Robot
parent 2c4f265587
commit af37608d9b
7 changed files with 90 additions and 108 deletions

25
rust/Cargo.lock generated
View File

@ -215,6 +215,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "drop_bomb"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bda8e21c04aca2ae33ffc2fd8c23134f3cac46db123ba97bd9d3f3b8a4a85e1"
[[package]] [[package]]
name = "dtoa" name = "dtoa"
version = "0.4.6" version = "0.4.6"
@ -590,15 +596,14 @@ dependencies = [
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.17.0" version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" checksum = "83450fe6a6142ddd95fb064b746083fc4ef1705fe81f64a64e1d4b39f54a1055"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"cc", "cc",
"cfg-if 0.1.10", "cfg-if 0.1.10",
"libc", "libc",
"void",
] ]
[[package]] [[package]]
@ -665,13 +670,15 @@ dependencies = [
[[package]] [[package]]
name = "openat-ext" name = "openat-ext"
version = "0.1.6" version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af1be1e54fbd54bae8b4497ddcc30f97ae57f18fa37fa2f35d48db7b0a8f0b59" checksum = "73e85ee792caf10bf54dc2a844b9f5cbc4a05095360fdeba67ffe687912d2b8b"
dependencies = [ dependencies = [
"drop_bomb",
"libc", "libc",
"nix 0.17.0", "nix 0.18.0",
"openat", "openat",
"rand",
] ]
[[package]] [[package]]
@ -1263,12 +1270,6 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.9.0+wasi-snapshot-preview1" version = "0.9.0+wasi-snapshot-preview1"

View File

@ -22,7 +22,7 @@ tempfile = "3.1.0"
clap = "2.33.3" clap = "2.33.3"
structopt = "0.3.20" structopt = "0.3.20"
openat = "0.1.19" openat = "0.1.19"
openat-ext = "0.1.6" openat-ext = "^0.1.8"
curl = "0.4.34" curl = "0.4.34"
rayon = "1.5.0" rayon = "1.5.0"
c_utf8 = "0.1.0" c_utf8 = "0.1.0"

View File

@ -18,7 +18,7 @@
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use std::io::prelude::*; use std::io::prelude::*;
use std::{io, path}; use std::path;
use openat_ext::OpenatDirExt; use openat_ext::OpenatDirExt;
use rayon::prelude::*; use rayon::prelude::*;
@ -84,20 +84,19 @@ fn write_wrappers(rootfs_dfd: &openat::Dir) -> Result<()> {
let destpath = format!("{}/{}", CLIWRAP_DESTDIR, name); let destpath = format!("{}/{}", CLIWRAP_DESTDIR, name);
rootfs_dfd.local_rename(bin, destpath.as_str())?; rootfs_dfd.local_rename(bin, destpath.as_str())?;
let f = rootfs_dfd.write_file(binpath, 0o755)?; rootfs_dfd.write_file_with(binpath, 0o755, |w| {
let mut f = io::BufWriter::new(f); write!(
write!( w,
f, "#!/bin/sh
"#!/bin/sh # Wrapper created by rpm-ostree to override
# Wrapper created by rpm-ostree to override # behavior of the underlying binary. For more
# behavior of the underlying binary. For more # information see `man rpm-ostree`. The real
# information see `man rpm-ostree`. The real # binary is now located at: {}
# binary is now located at: {} exec /usr/bin/rpm-ostree cliwrap $0 \"$@\"
exec /usr/bin/rpm-ostree cliwrap $0 \"$@\" ",
", binpath.to_str().unwrap()
binpath.to_str().unwrap() )
)?; })?;
f.flush()?;
Ok(()) Ok(())
}) })
} }

View File

@ -12,8 +12,6 @@ use std::io;
use std::io::{BufRead, Write}; use std::io::{BufRead, Write};
use std::path::Path; use std::path::Path;
use crate::utils;
// rpm-ostree uses /home → /var/home by default as generated by our // rpm-ostree uses /home → /var/home by default as generated by our
// rootfs; we don't expect people to change this. Let's be nice // rootfs; we don't expect people to change this. Let's be nice
// and also fixup the $HOME entries generated by `useradd` so // and also fixup the $HOME entries generated by `useradd` so
@ -26,21 +24,19 @@ use crate::utils;
fn postprocess_useradd(rootfs_dfd: &openat::Dir) -> Result<()> { fn postprocess_useradd(rootfs_dfd: &openat::Dir) -> Result<()> {
let path = Path::new("usr/etc/default/useradd"); let path = Path::new("usr/etc/default/useradd");
if let Some(f) = rootfs_dfd.open_file_optional(path)? { if let Some(f) = rootfs_dfd.open_file_optional(path)? {
let f = io::BufReader::new(f); rootfs_dfd.write_file_with(&path, 0o644, |bufw| -> Result<_> {
let tmp_path = utils::parent_dir(path).unwrap().join("useradd.tmp"); let f = io::BufReader::new(&f);
let o = rootfs_dfd.write_file(&tmp_path, 0o644)?; for line in f.lines() {
let mut bufw = io::BufWriter::new(&o); let line = line?;
for line in f.lines() { if !line.starts_with("HOME=") {
let line = line?; bufw.write(line.as_bytes())?;
if !line.starts_with("HOME=") { } else {
bufw.write(line.as_bytes())?; bufw.write("HOME=/var/home".as_bytes())?;
} else { }
bufw.write("HOME=/var/home".as_bytes())?; bufw.write("\n".as_bytes())?;
} }
bufw.write("\n".as_bytes())?; Ok(())
} })?;
bufw.flush()?;
rootfs_dfd.local_rename(&tmp_path, path)?;
} }
Ok(()) Ok(())
} }
@ -49,18 +45,19 @@ fn postprocess_useradd(rootfs_dfd: &openat::Dir) -> Result<()> {
// enabled; let's just do this rather than trying to propagate the // enabled; let's just do this rather than trying to propagate the
// preset everywhere. // preset everywhere.
fn postprocess_presets(rootfs_dfd: &openat::Dir) -> Result<()> { fn postprocess_presets(rootfs_dfd: &openat::Dir) -> Result<()> {
let mut o = rootfs_dfd.write_file( rootfs_dfd.write_file_with(
"usr/lib/systemd/system-preset/40-rpm-ostree-auto.preset", "usr/lib/systemd/system-preset/40-rpm-ostree-auto.preset",
0o644, 0o644,
)?; |w| {
o.write( w.write(
r###"# Written by rpm-ostree compose tree r###"# Written by rpm-ostree compose tree
enable ostree-remount.service enable ostree-remount.service
enable ostree-finalize-staged.path enable ostree-finalize-staged.path
"### "###
.as_bytes(), .as_bytes(),
)
},
)?; )?;
o.flush()?;
Ok(()) Ok(())
} }
@ -71,26 +68,22 @@ enable ostree-finalize-staged.path
fn postprocess_subs_dist(rootfs_dfd: &openat::Dir) -> Result<()> { fn postprocess_subs_dist(rootfs_dfd: &openat::Dir) -> Result<()> {
let path = Path::new("usr/etc/selinux/targeted/contexts/files/file_contexts.subs_dist"); let path = Path::new("usr/etc/selinux/targeted/contexts/files/file_contexts.subs_dist");
if let Some(f) = rootfs_dfd.open_file_optional(path)? { if let Some(f) = rootfs_dfd.open_file_optional(path)? {
let f = io::BufReader::new(f); rootfs_dfd.write_file_with(&path, 0o644, |w| -> Result<()> {
let tmp_path = utils::parent_dir(path) let f = io::BufReader::new(&f);
.unwrap() for line in f.lines() {
.join("file_contexts.subs_dist.tmp"); let line = line?;
let o = rootfs_dfd.write_file(&tmp_path, 0o644)?; if line.starts_with("/var/home ") {
let mut bufw = io::BufWriter::new(&o); w.write(b"# https://github.com/projectatomic/rpm-ostree/pull/1754\n")?;
for line in f.lines() { w.write(b"# ")?;
let line = line?; }
if line.starts_with("/var/home ") { w.write(line.as_bytes())?;
bufw.write(b"# https://github.com/projectatomic/rpm-ostree/pull/1754\n")?; w.write(b"\n")?;
bufw.write(b"# ")?;
} }
bufw.write(line.as_bytes())?; w.write(b"# https://github.com/projectatomic/rpm-ostree/pull/1754\n")?;
bufw.write(b"\n")?; w.write(b"/home /var/home")?;
} w.write(b"\n")?;
bufw.write(b"# https://github.com/projectatomic/rpm-ostree/pull/1754\n")?; Ok(())
bufw.write(b"/home /var/home")?; })?;
bufw.write(b"\n")?;
bufw.flush()?;
rootfs_dfd.local_rename(&tmp_path, path)?;
} }
Ok(()) Ok(())
} }

View File

@ -9,6 +9,7 @@
use anyhow::Result; use anyhow::Result;
use chrono::prelude::*; use chrono::prelude::*;
use openat_ext::OpenatDirExt;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use serde_json; use serde_json;
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
@ -230,7 +231,7 @@ mod ffi {
rpmmd_repos: *mut glib_sys::GPtrArray, rpmmd_repos: *mut glib_sys::GPtrArray,
gerror: *mut *mut glib_sys::GError, gerror: *mut *mut glib_sys::GError,
) -> libc::c_int { ) -> libc::c_int {
let filename = ffi_view_os_str(filename); let filename = Path::new(ffi_view_os_str(filename));
let packages: Vec<*mut DnfPackage> = ffi_ptr_array_to_vec(packages); let packages: Vec<*mut DnfPackage> = ffi_ptr_array_to_vec(packages);
let rpmmd_repos: Vec<*mut DnfRepo> = ffi_ptr_array_to_vec(rpmmd_repos); let rpmmd_repos: Vec<*mut DnfRepo> = ffi_ptr_array_to_vec(rpmmd_repos);
@ -292,9 +293,14 @@ mod ffi {
} }
int_glib_error( int_glib_error(
utils::write_file(filename, |w| { || -> Result<()> {
Ok(serde_json::to_writer_pretty(w, &lockfile)?) let lockfile_dir = openat::Dir::open(filename.parent().unwrap_or(Path::new("/")))?;
}), let basename = filename.file_name().expect("filename");
lockfile_dir.write_file_with(basename, 0o644, |w| -> Result<()> {
Ok(serde_json::to_writer_pretty(w, &lockfile)?)
})?;
Ok(())
}(),
gerror, gerror,
) )
} }

View File

@ -949,6 +949,7 @@ impl TreeComposeConfig {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use openat_ext::OpenatDirExt;
use tempfile; use tempfile;
static ARCH_X86_64: &str = "x86_64"; static ARCH_X86_64: &str = "x86_64";
@ -1172,10 +1173,7 @@ automatic_version_prefix: bar
basearch: Option<&'b str>, basearch: Option<&'b str>,
) -> Result<Box<Treefile>> { ) -> Result<Box<Treefile>> {
let tf_path = workdir.join("treefile.yaml"); let tf_path = workdir.join("treefile.yaml");
utils::write_file(&tf_path, |b| { std::fs::write(&tf_path, contents)?;
b.write_all(contents.as_bytes())?;
Ok(())
})?;
Ok(Treefile::new_boxed( Ok(Treefile::new_boxed(
tf_path.as_path(), tf_path.as_path(),
basearch, basearch,
@ -1215,14 +1213,15 @@ rojig:
#[test] #[test]
fn test_treefile_includes() -> Result<()> { fn test_treefile_includes() -> Result<()> {
let workdir = tempfile::tempdir()?; let workdir = tempfile::tempdir()?;
utils::write_file(workdir.path().join("foo.yaml"), |b| { let workdir_d = openat::Dir::open(workdir.path())?;
let foo = r#" workdir_d.write_file_contents(
packages: "foo.yaml",
- fooinclude 0o644,
"#; r#"
b.write_all(foo.as_bytes())?; packages:
Ok(()) - fooinclude
})?; "#,
)?;
let mut buf = VALID_PRELUDE.to_string(); let mut buf = VALID_PRELUDE.to_string();
buf.push_str( buf.push_str(
r#" r#"
@ -1237,14 +1236,15 @@ include: foo.yaml
#[test] #[test]
fn test_treefile_arch_includes() -> Result<()> { fn test_treefile_arch_includes() -> Result<()> {
let workdir = tempfile::tempdir()?; let workdir = tempfile::tempdir()?;
utils::write_file(workdir.path().join("foo-x86_64.yaml"), |b| { let workdir_d = openat::Dir::open(workdir.path())?;
let foo = r#" workdir_d.write_file_contents(
"foo-x86_64.yaml",
0o644,
r#"
packages: packages:
- foo-x86_64-include - foo-x86_64-include
"#; "#,
b.write_all(foo.as_bytes())?; )?;
Ok(())
})?;
let mut buf = VALID_PRELUDE.to_string(); let mut buf = VALID_PRELUDE.to_string();
buf.push_str( buf.push_str(
r#" r#"

View File

@ -104,23 +104,6 @@ pub fn create_file<P: AsRef<Path>>(filename: P) -> Result<fs::File> {
})?); })?);
} }
/// Open file for writing, passes a Writer to a closure, and closes the file, with O_TMPFILE
/// semantics.
pub fn write_file<P, F>(filename: P, f: F) -> Result<()>
where
P: AsRef<Path>,
F: Fn(&mut io::BufWriter<&mut fs::File>) -> Result<()>,
{
// XXX: enhance with tempfile + linkat + rename dance
let mut file = create_file(filename)?;
{
let mut w = io::BufWriter::new(&mut file);
f(&mut w)?;
}
file.sync_all()?;
Ok(())
}
// Surprising we need a wrapper for this... parent() returns a slice of its buffer, so doesn't // Surprising we need a wrapper for this... parent() returns a slice of its buffer, so doesn't
// handle going up relative paths well: https://github.com/rust-lang/rust/issues/36861 // handle going up relative paths well: https://github.com/rust-lang/rust/issues/36861
pub fn parent_dir(filename: &Path) -> Option<&Path> { pub fn parent_dir(filename: &Path) -> Option<&Path> {