Skip to content

Commit 5cfcdf9

Browse files
committed
WIP: rust: Add sysusers code
See #49
1 parent e58ee20 commit 5cfcdf9

29 files changed

+1467
-103
lines changed

Makefile-rpm-ostree.am

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ rpm_ostree_SOURCES = src/app/main.c \
3939
src/app/rpmostree-pkg-builtins.c \
4040
src/app/rpmostree-builtin-status.c \
4141
src/app/rpmostree-builtin-ex.c \
42+
src/app/rpmostree-builtin-testimpl.c \
4243
src/app/rpmostree-builtin-container.c \
4344
src/app/rpmostree-ex-builtin-commit2rojig.c \
4445
src/app/rpmostree-ex-builtin-rojig2commit.c \

docs/manual/treefile.md

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ It supports the following parameters:
8888

8989
Note this does not alter the RPM database, so `rpm -V` will complain.
9090

91+
* `sysusers`: boolean, optional: Defaults to `false`. Enable generation of
92+
systemd sysusers.d entries based on `useradd` invocations. If enabled,
93+
this overrides `preserve-passwd`. It also obsoletes `check-passwd`.
94+
9195
* `preserve-passwd`: boolean, optional: Defaults to `true`. If enabled,
9296
and `check-passwd` has a type other than file, copy the `/etc/passwd` (and
9397
`/usr/lib/passwd`) files from the previous commit if they exist. If

rust/Cargo.lock

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

rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ glib-sys = "0.7.0"
1414
gio-sys = "0.7.0"
1515
glib = "0.6.1"
1616
tempfile = "3.0.3"
17+
clap = "~2.28"
1718
openat = "0.1.15"
1819
curl = "0.4.14"
1920
rayon = "1.0"

rust/src/ffiutil.rs

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ pub fn ffi_view_nullable_str<'a>(s: *const libc::c_char) -> Option<&'a str> {
6767
}
6868
}
6969

70+
/// Like ffi_view_nullable_str(), but also panics if value is NULL
71+
#[allow(dead_code)]
72+
pub fn ffi_view_str<'a>(s: *const libc::c_char) -> &'a str {
73+
ffi_view_nullable_str(s).expect("ffi_view_str: non-NULL value")
74+
}
75+
76+
7077
/// Given a NUL-terminated C string, copy it to an owned
7178
/// String. Will panic if the C string is not valid UTF-8.
7279
pub fn ffi_new_string(s: *const libc::c_char) -> String {

rust/src/lib.rs

+45
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
extern crate c_utf8;
20+
extern crate clap;
2021
extern crate curl;
2122
#[macro_use]
2223
extern crate failure;
@@ -50,3 +51,47 @@ pub use journal::*;
5051
mod utils;
5152
pub use utils::*;
5253
mod openat_utils;
54+
mod sysusers;
55+
pub use sysusers::*;
56+
57+
fn testimpl_run(argv: &Vec<String>) -> failure::Fallible<()> {
58+
use clap;
59+
use openat;
60+
let app = clap::App::new("rpmostree-testimpl")
61+
.subcommand(clap::SubCommand::with_name("sysusers-post")
62+
.arg(clap::Arg::with_name("treefile").help("Treefile").required(true).takes_value(true))
63+
.arg(clap::Arg::with_name("dir").help("Directory").required(true).takes_value(true)));
64+
let matches = app.get_matches_from(argv);
65+
if let Some(matches) = matches.subcommand_matches("sysusers-post") {
66+
let d = openat::Dir::open(matches.value_of("dir").unwrap())?;
67+
use tempfile;
68+
let workdir = tempfile::tempdir()?;
69+
// Hack opening the treefile here
70+
let tf =
71+
Treefile::new_boxed(std::path::Path::new(matches.value_of("treefile").unwrap()), None, openat::Dir::open(workdir.path())?)?;
72+
sysusers::post_useradd(&d, &tf)?;
73+
}
74+
Ok(())
75+
}
76+
77+
/// Entrypoint for testing internal functions; not a stable API.
78+
fn testimpl_main(argv: &Vec<String>) {
79+
if let Err(ref e) = testimpl_run(argv) {
80+
eprintln!("error: {}", e);
81+
std::process::exit(1)
82+
}
83+
}
84+
85+
mod ffi {
86+
use super::*;
87+
use glib;
88+
use libc;
89+
90+
#[no_mangle]
91+
pub extern "C" fn ror_testimpl_main(argv: *mut *mut libc::c_char) -> libc::c_int {
92+
let v: Vec<String> = unsafe { glib::translate::FromGlibPtrContainer::from_glib_none(argv) };
93+
testimpl_main(&v);
94+
0
95+
}
96+
}
97+
pub use self::ffi::*;

rust/src/openat_utils.rs

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ pub(crate) trait OpenatDirExt {
2626
// and Rust has an elegant way to map that with Option<>. Every other
2727
// error I usually just want to propagate back up.
2828
fn open_file_optional<P: openat::AsPath>(&self, p: P) -> io::Result<Option<fs::File>>;
29+
30+
// On modern filesystems the directory entry contains the type; if available,
31+
// return it. Otherwise invoke stat().
32+
fn get_file_type(&self, e: &openat::Entry) -> io::Result<openat::SimpleType>;
2933
}
3034

3135
impl OpenatDirExt for openat::Dir {
@@ -41,5 +45,13 @@ impl OpenatDirExt for openat::Dir {
4145
}
4246
}
4347
}
48+
49+
fn get_file_type(&self, e: &openat::Entry) -> io::Result<openat::SimpleType> {
50+
if let Some(ftype) = e.simple_type() {
51+
Ok(ftype)
52+
} else {
53+
Ok(self.metadata(e.file_name())?.simple_type())
54+
}
55+
}
4456
}
4557

0 commit comments

Comments
 (0)