Skip to content

Commit

Permalink
lib: Add an API to construct a MutableTree from a commit
Browse files Browse the repository at this point in the history
This is nicer than having the caller parse the commit
object, or indirect via the `OstreeRepoFile*` object of the root.

Will be used in ostree-rs-ext around tar parsing.
  • Loading branch information
cgwalters committed Oct 1, 2021
1 parent 5bf4b1d commit 868776a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions apidoc/ostree-sections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ OstreeLzmaDecompressorClass
<FILE>ostree-mutable-tree</FILE>
OstreeMutableTree
ostree_mutable_tree_new
ostree_mutable_tree_new_from_commit
ostree_mutable_tree_new_from_checksum
ostree_mutable_tree_check_error
ostree_mutable_tree_set_metadata_checksum
Expand Down
1 change: 1 addition & 0 deletions src/libostree/libostree-devel.sym
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
LIBOSTREE_2021.5 {
global:
ostree_sepolicy_new_from_commit;
ostree_mutable_tree_new_from_commit;
} LIBOSTREE_2021.4;

/* Stub section for the stable release *after* this development one; don't
Expand Down
36 changes: 36 additions & 0 deletions src/libostree/ostree-mutable-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,39 @@ ostree_mutable_tree_new_from_checksum (OstreeRepo *repo,
out->metadata_checksum = g_strdup (metadata_checksum);
return out;
}

/**
* ostree_mutable_tree_new_from_commit:
* @repo: The repo which contains the objects refered by the checksums.
* @rev: ref or SHA-256 checksum
*
* Creates a new OstreeMutableTree with the contents taken from the given commit.
* The data will be loaded from the repo lazily as needed.
*
* Returns: (transfer full): A new tree
* Since: 2021.5
*/
OstreeMutableTree *
ostree_mutable_tree_new_from_commit (OstreeRepo *repo,
const char *rev,
GError **error)
{
g_autofree char *commit = NULL;
if (!ostree_repo_resolve_rev (repo, rev, FALSE, &commit, error))
return NULL;
g_autoptr(GVariant) commit_v = NULL;
if (!ostree_repo_load_commit (repo, commit, &commit_v, NULL, error))
return NULL;

g_autoptr(GVariant) contents_checksum_v = NULL;
g_autoptr(GVariant) metadata_checksum_v = NULL;
char contents_checksum[OSTREE_SHA256_STRING_LEN + 1];
char metadata_checksum[OSTREE_SHA256_STRING_LEN + 1];
g_variant_get_child (commit_v, 6, "@ay", &contents_checksum_v);
ostree_checksum_inplace_from_bytes (g_variant_get_data (contents_checksum_v),
contents_checksum);
g_variant_get_child (commit_v, 7, "@ay", &metadata_checksum_v);
ostree_checksum_inplace_from_bytes (g_variant_get_data (metadata_checksum_v),
metadata_checksum);
return ostree_mutable_tree_new_from_checksum (repo, contents_checksum, metadata_checksum);
}
6 changes: 6 additions & 0 deletions src/libostree/ostree-mutable-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ GType ostree_mutable_tree_get_type (void) G_GNUC_CONST;
_OSTREE_PUBLIC
OstreeMutableTree *ostree_mutable_tree_new (void);

_OSTREE_PUBLIC
OstreeMutableTree *
ostree_mutable_tree_new_from_commit (OstreeRepo *repo,
const char *rev,
GError **error);

_OSTREE_PUBLIC
OstreeMutableTree * ostree_mutable_tree_new_from_checksum (OstreeRepo *repo,
const char *contents_checksum,
Expand Down
12 changes: 3 additions & 9 deletions src/ostree/ot-builtin-commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,20 +638,14 @@ ostree_builtin_commit (int argc, char **argv, OstreeCommandInvocation *invocatio

if (opt_base)
{
g_autofree char *base_commit = NULL;
g_autoptr(GFile) base_root = NULL;
if (!ostree_repo_read_commit (repo, opt_base, &base_root, &base_commit, cancellable, error))
mtree = ostree_mutable_tree_new_from_commit (repo, opt_base, error);
if (!mtree)
goto out;
OstreeRepoFile *rootf = (OstreeRepoFile*) base_root;

mtree = ostree_mutable_tree_new_from_checksum (repo,
ostree_repo_file_tree_get_contents_checksum (rootf),
ostree_repo_file_tree_get_metadata_checksum (rootf));

if (opt_selinux_policy_from_base)
{
g_assert (modifier);
if (!ostree_repo_commit_modifier_set_sepolicy_from_commit (modifier, repo, base_commit, cancellable, error))
if (!ostree_repo_commit_modifier_set_sepolicy_from_commit (modifier, repo, opt_base, cancellable, error))
goto out;
/* Don't try to handle it twice */
opt_selinux_policy_from_base = FALSE;
Expand Down

0 comments on commit 868776a

Please sign in to comment.