Skip to content

Commit

Permalink
src/manifest: throw parser error for invalid slot hook combination
Browse files Browse the repository at this point in the history
When using an 'install' hook, the 'pre-install' and 'post-install' hooks
will not be called. To avoid confusion, prevent generating bundles
having both 'install' and 'pre-install' (or 'post-install') hook set for
the same slot.

Signed-off-by: Enrico Joerns <[email protected]>
Signed-off-by: Jan Luebbe <[email protected]>
  • Loading branch information
ejoerns committed Oct 30, 2024
1 parent 98b5ba8 commit 425bc36
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/manifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ G_GNUC_WARN_UNUSED_RESULT;
gboolean check_manifest_external(const RaucManifest *manifest, GError **error)
G_GNUC_WARN_UNUSED_RESULT;

/**
* Check a new manifest for consistency during bundle creation.
*
* This is used for checks that could otherwise break compatibility of new RAUC
* versions with old bundles.
*
* @param manifest Pointer to the manifest to check
* @param error return location for a GError, or NULL
*
* @return TRUE on success, FALSE if an error occurred
*/
gboolean check_manifest_create(const RaucManifest *mf, GError **error)
G_GNUC_WARN_UNUSED_RESULT;

/**
* Stores the manifest to memory.
*
Expand Down
6 changes: 6 additions & 0 deletions src/bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,12 @@ gboolean create_bundle(const gchar *bundlename, const gchar *contentdir, GError
g_print("%s\n", (gchar *)g_ptr_array_index(manifest->warnings, i));
}

res = check_manifest_create(manifest, &ierror);
if (!res) {
g_propagate_error(error, ierror);
goto out;
}

res = sync_manifest_with_contentdir(manifest, workdir, &ierror);
if (!res) {
g_propagate_error(error, ierror);
Expand Down
17 changes: 17 additions & 0 deletions src/manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,23 @@ gboolean check_manifest_external(const RaucManifest *mf, GError **error)
return res;
}

gboolean check_manifest_create(const RaucManifest *mf, GError **error)
{
for (GList *l = mf->images; l != NULL; l = l->next) {
RaucImage *image = l->data;

g_assert(image);

if (image->hooks.install && (image->hooks.pre_install || image->hooks.post_install)) {
g_set_error_literal(error, R_MANIFEST_ERROR, R_MANIFEST_CHECK_ERROR,
"An 'install' hook must not be combined with 'pre-install' or 'post-install' hooks");
return FALSE;
}
}

return TRUE;
}

static GKeyFile *prepare_manifest(const RaucManifest *mf)
{
g_autoptr(GKeyFile) key_file = NULL;
Expand Down
33 changes: 33 additions & 0 deletions test/manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,38 @@ hooks=doesnotexist\n\
g_assert_null(rm);
}

static void test_manifest_invalid_hook_combination(void)
{
const gchar *mffile = "\
[update]\n\
compatible=FooCorp Super BarBazzer\n\
version=2015.04-1\n\
\n\
[image.rootfs]\n\
filename=rootfs-default.ext4\n\
sha256=0815\n\
size=1\n\
hooks=install;pre-install\n\
";

g_autofree gchar *tmpdir = g_dir_make_tmp("rauc-XXXXXX", NULL);
g_assert_nonnull(tmpdir);

g_autofree gchar *manifestpath = write_tmp_file(tmpdir, "manifest.raucm", mffile, NULL);
g_assert_nonnull(manifestpath);

g_autoptr(RaucManifest) rm = NULL;
g_autoptr(GError) error = NULL;
gboolean res = load_manifest_file(manifestpath, &rm, &error);
g_assert_no_error(error);
g_assert_true(res);

res = check_manifest_create(rm, &error);
g_assert_error(error, R_MANIFEST_ERROR, R_MANIFEST_CHECK_ERROR);
g_assert_cmpstr("An 'install' hook must not be combined with 'pre-install' or 'post-install' hooks", ==, error->message);
g_assert_false(res);
}

static void test_manifest_missing_hook_name(void)
{
g_autofree gchar *tmpdir;
Expand Down Expand Up @@ -740,6 +772,7 @@ int main(int argc, char *argv[])
g_test_add_func("/manifest/load_meta", test_manifest_load_meta);
g_test_add_func("/manifest/load_details", test_manifest_load_details);
g_test_add_func("/manifest/invalid_hook_name", test_manifest_invalid_hook_name);
g_test_add_func("/manifest/invalid_hook_combination", test_manifest_invalid_hook_combination);
g_test_add_func("/manifest/missing_hook_name", test_manifest_missing_hook_name);
g_test_add_func("/manifest/missing_image_size", test_manifest_missing_image_size);
g_test_add_func("/manifest/invalid_data", test_invalid_data);
Expand Down

0 comments on commit 425bc36

Please sign in to comment.