Skip to content

Instantly share code, notes, and snippets.

@dbnicholson
Created June 9, 2022 22:21
Show Gist options
  • Save dbnicholson/4c1898deae258b07d207aa7254ac51e0 to your computer and use it in GitHub Desktop.
Save dbnicholson/4c1898deae258b07d207aa7254ac51e0 to your computer and use it in GitHub Desktop.
Count ostree objects
#include <glib.h>
#include <gio/gio.h>
#include <ostree.h>
#include <locale.h>
#include <fcntl.h>
static gchar *opt_repo = NULL;
static gboolean opt_commits = FALSE;
static GOptionEntry entries[] = {
{ "repo", 0, 0, G_OPTION_ARG_STRING, &opt_repo, "Repo path", "PATH" },
{ "commits", 0, 0, G_OPTION_ARG_NONE, &opt_commits, "Commits only", NULL },
G_OPTION_ENTRY_NULL
};
int main(int argc, char *argv[])
{
g_autoptr(GError) error = NULL;
setlocale (LC_ALL, "");
g_set_prgname (argv[0]);
g_autoptr(GOptionContext) context = NULL;
context = g_option_context_new ("- count OSTree repo objects");
g_option_context_add_main_entries (context, entries, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_printerr ("error: option parsing failed: %s\n", error->message);
return 1;
}
g_autoptr(OstreeRepo) repo = NULL;
if (opt_repo != NULL)
{
repo = ostree_repo_open_at (AT_FDCWD, opt_repo, NULL, &error);
if (repo == NULL)
{
g_printerr ("error: cannot open repo \"%s\": %s", opt_repo,
error->message);
return 1;
}
}
else
{
repo = ostree_repo_new_default ();
if (!ostree_repo_open (repo, NULL, &error))
{
g_printerr ("error: cannot open default repo: %s", error->message);
return 1;
}
}
g_autoptr(GHashTable) objects = NULL;
if (opt_commits)
{
if (!ostree_repo_list_commit_objects_starting_with (repo, "", &objects,
NULL, &error))
{
g_printerr ("error: could not list commit objects: %s",
error->message);
return 1;
}
g_print ("Number of commits: %u\n", g_hash_table_size (objects));
}
else
{
if (!ostree_repo_list_objects (repo, OSTREE_REPO_LIST_OBJECTS_ALL,
&objects, NULL, &error))
{
g_printerr ("error: could not list repo objects: %s",
error->message);
return 1;
}
g_print ("Number of objects: %u\n", g_hash_table_size (objects));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment