compose: Move opening passwd/group files into Rust

This was I think the last place where we were looking at the parent
directory of the primary treefile.

Closes: #1600
Approved by: jlebon
This commit is contained in:
Colin Walters 2018-10-05 21:15:48 +00:00 committed by Atomic Bot
parent f7f2cfeb61
commit 6846fe11fa
5 changed files with 58 additions and 24 deletions

View File

@ -134,6 +134,16 @@ pub extern "C" fn ror_treefile_get_add_file_fd(
fd.as_raw_fd()
}
#[no_mangle]
pub extern "C" fn ror_treefile_get_passwd_fd(tf: *mut Treefile) -> libc::c_int {
tf_from_raw(tf).externals.passwd.as_ref().map_or(-1, |fd| fd.as_raw_fd())
}
#[no_mangle]
pub extern "C" fn ror_treefile_get_group_fd(tf: *mut Treefile) -> libc::c_int {
tf_from_raw(tf).externals.group.as_ref().map_or(-1, |fd| fd.as_raw_fd())
}
#[no_mangle]
pub extern "C" fn ror_treefile_get_json_string(tf: *mut Treefile) -> *const libc::c_char {
tf_from_raw(tf).serialized.as_ptr()

View File

@ -36,6 +36,8 @@ const INCLUDE_MAXDEPTH: u32 = 50;
pub struct TreefileExternals {
pub postprocess_script: Option<fs::File>,
pub add_files: collections::HashMap<String, fs::File>,
pub passwd: Option<fs::File>,
pub group: Option<fs::File>,
}
pub struct Treefile {
@ -117,6 +119,20 @@ fn treefile_parse_stream<R: io::Read>(
Ok(treefile)
}
// If a passwd/group file is provided explicitly, load it as a fd
fn load_passwd_file<P: AsRef<Path>>(
basedir: P,
v: &Option<CheckPasswd>,
) -> io::Result<Option<fs::File>> {
if let &Some(ref v) = v {
let basedir = basedir.as_ref();
if let Some(ref path) = v.filename {
return Ok(Some(fs::File::open(basedir.join(path))?));
}
}
return Ok(None);
}
/// Given a treefile filename and an architecture, parse it and also
/// open its external files.
fn treefile_parse<P: AsRef<Path>>(
@ -151,11 +167,16 @@ fn treefile_parse<P: AsRef<Path>>(
add_files.insert(name.clone(), fs::File::open(filename.with_file_name(name))?);
}
}
let parent = filename.parent().unwrap();
let passwd = load_passwd_file(&parent, &tf.check_passwd)?;
let group = load_passwd_file(&parent, &tf.check_groups)?;
Ok(ConfigAndExternals {
config: tf,
externals: TreefileExternals {
postprocess_script,
add_files,
passwd,
group,
},
})
}
@ -239,6 +260,14 @@ fn treefile_merge_externals(dest: &mut TreefileExternals, src: &mut TreefileExte
for (k, v) in src.add_files.drain() {
dest.add_files.insert(k, v);
}
// passwd/group are basic values
if dest.passwd.is_none() {
dest.passwd = src.passwd.take();
}
if dest.group.is_none() {
dest.group = src.group.take();
}
}
/// Recursively parse a treefile, merging along the way.

View File

@ -1003,16 +1003,15 @@ impl_commit_tree (RpmOstreeTreeComposeContext *self,
cancellable, error))
return FALSE;
if (self->treefile)
if (self->treefile_rs)
{
g_autoptr(GFile) treefile_dirpath = g_file_get_parent (self->treefile_path);
if (!rpmostree_check_passwd (self->repo, self->rootfs_dfd, treefile_dirpath, self->treefile,
self->previous_checksum,
if (!rpmostree_check_passwd (self->repo, self->rootfs_dfd, self->treefile_rs,
self->treefile, self->previous_checksum,
cancellable, error))
return glnx_prefix_error (error, "Handling passwd db");
if (!rpmostree_check_groups (self->repo, self->rootfs_dfd, treefile_dirpath, self->treefile,
self->previous_checksum,
if (!rpmostree_check_groups (self->repo, self->rootfs_dfd, self->treefile_rs,
self->treefile, self->previous_checksum,
cancellable, error))
return glnx_prefix_error (error, "Handling group db");
}

View File

@ -33,6 +33,7 @@
#include "rpmostree-util.h"
#include "rpmostree-json-parsing.h"
#include "rpmostree-passwd-util.h"
#include "rpmostree-rust.h"
#include "libglnx.h"
@ -356,13 +357,12 @@ static gboolean
rpmostree_check_passwd_groups (gboolean passwd,
OstreeRepo *repo,
int rootfs_fd,
GFile *treefile_dirpath,
RORTreefile *treefile_rs,
JsonObject *treedata,
const char *previous_commit,
GCancellable *cancellable,
GError **error)
{
const char *direct = NULL;
const char *chk_type = "previous";
const char *commit_filepath = passwd ? "usr/lib/passwd" : "usr/lib/group";
const char *json_conf_name = passwd ? "check-passwd" : "check-groups";
@ -387,13 +387,7 @@ rpmostree_check_passwd_groups (gboolean passwd,
else if (g_str_equal (chk_type, "previous"))
; /* Handled below */
else if (g_str_equal (chk_type, "file"))
{
direct = _rpmostree_jsonutil_object_require_string_member (chk,
"filename",
error);
if (!direct)
return FALSE;
}
; /* Handled below */
else if (g_str_equal (chk_type, "data"))
{
JsonNode *ents_node = json_object_get_member (chk, "entries");
@ -493,9 +487,9 @@ rpmostree_check_passwd_groups (gboolean passwd,
}
else if (g_str_equal (chk_type, "file"))
{
old_path = g_file_resolve_relative_path (treefile_dirpath, direct);
old_contents = glnx_file_get_contents_utf8_at (AT_FDCWD, gs_file_get_path_cached (old_path), NULL,
cancellable, error);
int fd = passwd ? ror_treefile_get_passwd_fd (treefile_rs) :
ror_treefile_get_group_fd (treefile_rs);
old_contents = glnx_fd_readall_utf8 (fd, NULL, cancellable, error);
if (!old_contents)
return FALSE;
}
@ -690,13 +684,13 @@ rpmostree_check_passwd_groups (gboolean passwd,
gboolean
rpmostree_check_passwd (OstreeRepo *repo,
int rootfs_fd,
GFile *treefile_dirpath,
RORTreefile *treefile_rs,
JsonObject *treedata,
const char *previous_commit,
GCancellable *cancellable,
GError **error)
{
return rpmostree_check_passwd_groups (TRUE, repo, rootfs_fd, treefile_dirpath,
return rpmostree_check_passwd_groups (TRUE, repo, rootfs_fd, treefile_rs,
treedata, previous_commit,
cancellable, error);
}
@ -707,13 +701,13 @@ rpmostree_check_passwd (OstreeRepo *repo,
gboolean
rpmostree_check_groups (OstreeRepo *repo,
int rootfs_fd,
GFile *treefile_dirpath,
RORTreefile *treefile_rs,
JsonObject *treedata,
const char *previous_commit,
GCancellable *cancellable,
GError **error)
{
return rpmostree_check_passwd_groups (FALSE, repo, rootfs_fd, treefile_dirpath,
return rpmostree_check_passwd_groups (FALSE, repo, rootfs_fd, treefile_rs,
treedata, previous_commit,
cancellable, error);
}

View File

@ -24,10 +24,12 @@
#include <sys/types.h>
#include <sys/wait.h>
#include "rpmostree-rust.h"
gboolean
rpmostree_check_passwd (OstreeRepo *repo,
int rootfs_dfd,
GFile *treefile_path,
RORTreefile *treefile_rs,
JsonObject *treedata,
const char *previous_commit,
GCancellable *cancellable,
@ -36,7 +38,7 @@ rpmostree_check_passwd (OstreeRepo *repo,
gboolean
rpmostree_check_groups (OstreeRepo *repo,
int rootfs_dfd,
GFile *treefile_path,
RORTreefile *treefile_rs,
JsonObject *treedata,
const char *previous_commit,
GCancellable *cancellable,