bin/show: Add --print-sizes option to show sizes metadata

Use the new `ostree_commit_get_object_sizes()` API to read the
`ostree.sizes` commit metadata and print a summary.
This commit is contained in:
Dan Nicholson committed 2020-01-20 20:46:31 -07:00
1 parent 260bcd1193
commit 97c831dd5f
5 files changed
+140

No files matched your search

+69
View File
@@ -33,6 +33,7 @@ static gboolean opt_print_related;
static char* opt_print_variant_type;
static char* opt_print_metadata_key;
static char* opt_print_detached_metadata_key;
static gboolean opt_print_sizes;
static gboolean opt_raw;
static gboolean opt_no_byteswap;
static char *opt_gpg_homedir;
@@ -48,6 +49,7 @@ static GOptionEntry options[] = {
{ "print-variant-type", 0, 0, G_OPTION_ARG_STRING, &opt_print_variant_type, "Memory map OBJECT (in this case a filename) to the GVariant type string", "TYPE" },
{ "print-metadata-key", 0, 0, G_OPTION_ARG_STRING, &opt_print_metadata_key, "Print string value of metadata key", "KEY" },
{ "print-detached-metadata-key", 0, 0, G_OPTION_ARG_STRING, &opt_print_detached_metadata_key, "Print string value of detached metadata key", "KEY" },
{ "print-sizes", 0, 0, G_OPTION_ARG_NONE, &opt_print_sizes, "Show the commit size metadata", NULL },
{ "raw", 0, 0, G_OPTION_ARG_NONE, &opt_raw, "Show raw variant data" },
{ "no-byteswap", 'B', 0, G_OPTION_ARG_NONE, &opt_no_byteswap, "Do not automatically convert variant data from big endian" },
{ "gpg-homedir", 0, 0, G_OPTION_ARG_FILENAME, &opt_gpg_homedir, "GPG Homedir to use when looking for keyrings", "HOMEDIR"},
@@ -146,6 +148,65 @@ do_print_metadata_key (OstreeRepo *repo,
return TRUE;
}
static gboolean
do_print_sizes (OstreeRepo *repo,
const char *rev,
GError **error)
{
g_autoptr(GVariant) commit = NULL;
if (!ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, rev,
&commit, error))
{
g_prefix_error (error, "Failed to read commit: ");
return FALSE;
}
g_autoptr(GPtrArray) sizes = NULL;
if (!ostree_commit_get_object_sizes (commit, &sizes, error))
return FALSE;
gint64 new_archived = 0;
gint64 new_unpacked = 0;
gsize new_objects = 0;
gint64 archived = 0;
gint64 unpacked = 0;
gsize objects = 0;
for (guint i = 0; i < sizes->len; i++)
{
OstreeCommitSizesEntry *entry = sizes->pdata[i];
archived += entry->archived;
unpacked += entry->unpacked;
objects++;
gboolean exists;
if (!ostree_repo_has_object (repo, entry->objtype, entry->checksum,
&exists, NULL, error))
return FALSE;
if (!exists)
{
/* Object not in local repo */
new_archived += entry->archived;
new_unpacked += entry->unpacked;
new_objects++;
}
}
g_autofree char *new_archived_str = g_format_size (new_archived);
g_autofree char *archived_str = g_format_size (archived);
g_autofree char *new_unpacked_str = g_format_size (new_unpacked);
g_autofree char *unpacked_str = g_format_size (unpacked);
g_print ("Compressed size (needed/total): %s/%s\n"
"Unpacked size (needed/total): %s/%s\n"
"Number of objects (needed/total): %" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT "\n",
new_archived_str, archived_str,
new_unpacked_str, unpacked_str,
new_objects, objects);
return TRUE;
}
static gboolean
print_object (OstreeRepo *repo,
OstreeObjectType objtype,
@@ -279,6 +340,14 @@ ostree_builtin_show (int argc, char **argv, OstreeCommandInvocation *invocation,
if (!do_print_variant_generic (G_VARIANT_TYPE (opt_print_variant_type), rev, error))
return FALSE;
}
else if (opt_print_sizes)
{
if (!ostree_repo_resolve_rev (repo, rev, FALSE, &resolved_rev, error))
return FALSE;
if (!do_print_sizes (repo, resolved_rev, error))
return FALSE;
}
else
{
gboolean found = FALSE;