Skip to content

Commit

Permalink
compose+rust: Parse includes via Rust too
Browse files Browse the repository at this point in the history
This follows up to #1576
AKA commit 2e56784 - we now process
treefile inheritance in Rust code.  Previously for elements which
reference external files (`postprocess-script` and `add-files`)
we'd hardcoded things to only look in the first context dir.

Now we open file descriptors in the Rust side for these "externals"
as we're parsing, and load them C side.  Hence we'll correctly handle
a `postprocess-script` from an included config.

Other advantages are that the include handling was ugly un-typesafe C code
with no unit tests, now it's memory safe Rust with unit tests.

The downside here is I ended up spelling out the list of fields
again - there's probably a way to unify this via macros but
for now I think this is OK.

Closes: #1574
Approved by: jlebon
  • Loading branch information
cgwalters authored and rh-atomic-bot committed Oct 4, 2018
1 parent 638fab0 commit e3be475
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 259 deletions.
2 changes: 1 addition & 1 deletion Makefile-lib.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ librpmostree_1_la_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/libglnx -I$(srcdir)/src/libp
-fvisibility=hidden '-D_RPMOSTREE_EXTERN=__attribute((visibility("default"))) extern' \
$(PKGDEP_RPMOSTREE_CFLAGS)
librpmostree_1_la_LDFLAGS = $(AM_LDFLAGS) -version-number 1:0:0 -Bsymbolic-functions
librpmostree_1_la_LIBADD = $(PKGDEP_RPMOSTREE_LIBS) librpmostreepriv.la
librpmostree_1_la_LIBADD = $(PKGDEP_RPMOSTREE_LIBS) librpmostreepriv.la $(librpmostree_rust_path)

# bundled libdnf
INTROSPECTION_SCANNER_ENV = env LD_LIBRARY_PATH=$(top_builddir)/libdnf-build/libdnf
Expand Down
38 changes: 37 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ extern crate serde_json;
extern crate serde_yaml;

use std::ffi::{CStr, OsStr};
use std::io::Seek;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::{FromRawFd, IntoRawFd};
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
use std::{io, ptr};

mod glibutils;
Expand Down Expand Up @@ -94,6 +95,41 @@ pub extern "C" fn ror_treefile_new(
)
}

#[no_mangle]
pub extern "C" fn ror_treefile_get_dfd(tf: *mut Treefile) -> libc::c_int {
assert!(!tf.is_null());
let tf = unsafe { &mut *tf };
tf.primary_dfd.as_raw_fd()
}

#[no_mangle]
pub extern "C" fn ror_treefile_get_postprocess_script_fd(tf: *mut Treefile) -> libc::c_int {
assert!(!tf.is_null());
let tf = unsafe { &mut *tf };
if let Some(ref mut fd) = tf.externals.postprocess_script.as_ref() {
// We always seek to the start
fd.seek(io::SeekFrom::Start(0)).unwrap();
fd.as_raw_fd()
} else {
-1
}
}

#[no_mangle]
pub extern "C" fn ror_treefile_get_add_file_fd(
tf: *mut Treefile,
filename: *const libc::c_char,
) -> libc::c_int {
assert!(!tf.is_null());
let tf = unsafe { &mut *tf };
let filename = OsStr::from_bytes(bytes_from_nonnull(filename));
let filename = filename.to_string_lossy().into_owned();
let mut fd = tf.externals.add_files.get(&filename).expect("add-file");
// We always seek to the start
fd.seek(io::SeekFrom::Start(0)).unwrap();
fd.as_raw_fd()
}

#[no_mangle]
pub extern "C" fn ror_treefile_to_json(
tf: *mut Treefile,
Expand Down
Loading

0 comments on commit e3be475

Please sign in to comment.