summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2011-11-18 07:29:13 -0500
committerColin Walters <walters@verbum.org>2011-11-18 07:29:13 -0500
commit12f2d8917490b2af2cd0205b9ca5227379f39b6f (patch)
treee2393e278bc92e45e8a827bcf0795853681165bd
parent7f64d5cec745fdbc25c4151ef92032cee1587fa4 (diff)
downloadostree-12f2d8917490b2af2cd0205b9ca5227379f39b6f.tar.gz
core: Add checksum builtin
This necessitated reworking things so that builtins can specify no --repo is required.
-rw-r--r--Makefile-ostree.am1
-rw-r--r--src/ostree/main.c110
-rw-r--r--src/ostree/ot-builtin-checksum.c70
-rw-r--r--src/ostree/ot-builtins.h2
-rwxr-xr-xtests/t0000-basic.sh3
5 files changed, 157 insertions, 29 deletions
diff --git a/Makefile-ostree.am b/Makefile-ostree.am
index 0bb7cdec..aa04c946 100644
--- a/Makefile-ostree.am
+++ b/Makefile-ostree.am
@@ -22,6 +22,7 @@ bin_PROGRAMS += ostree
ostree_SOURCES = src/ostree/main.c \
src/ostree/ot-builtins.h \
src/ostree/ot-builtin-checkout.c \
+ src/ostree/ot-builtin-checksum.c \
src/ostree/ot-builtin-commit.c \
src/ostree/ot-builtin-compose.c \
src/ostree/ot-builtin-diff.c \
diff --git a/src/ostree/main.c b/src/ostree/main.c
index cf0e89c1..88dd3138 100644
--- a/src/ostree/main.c
+++ b/src/ostree/main.c
@@ -30,6 +30,7 @@
static OstreeBuiltin builtins[] = {
{ "checkout", ostree_builtin_checkout, 0 },
+ { "checksum", ostree_builtin_checksum, OSTREE_BUILTIN_FLAG_NO_REPO },
{ "diff", ostree_builtin_diff, 0 },
{ "init", ostree_builtin_init, 0 },
{ "commit", ostree_builtin_commit, 0 },
@@ -70,14 +71,42 @@ usage (char **argv, gboolean is_error)
return (is_error ? 1 : 0);
}
+static void
+prep_builtin_argv (const char *builtin,
+ int argc,
+ char **argv,
+ int *out_argc,
+ char ***out_argv)
+{
+ int i;
+ char **cmd_argv;
+
+ cmd_argv = g_new0 (char *, argc + 2);
+
+ cmd_argv[0] = (char*)builtin;
+ for (i = 0; i < argc; i++)
+ cmd_argv[i+1] = argv[i];
+ cmd_argv[i+1] = NULL;
+ *out_argc = argc+1;
+ *out_argv = cmd_argv;
+}
+
+static void
+set_unknown_command (char **argv, GError **error)
+{
+ usage (argv, TRUE);
+ g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "Unknown command");
+}
int
main (int argc,
char **argv)
{
OstreeBuiltin *builtin;
- const char *cmd;
- const char *repo;
+ GError *error = NULL;
+ int cmd_argc;
+ char **cmd_argv = NULL;
g_type_init ();
@@ -85,43 +114,66 @@ main (int argc,
builtin = builtins;
- if (argc < 3)
+ if (argc < 2)
return usage (argv, 1);
if (!g_str_has_prefix (argv[1], "--repo="))
- return usage (argv, 1);
- repo = argv[1] + strlen ("--repo=");
+ {
+ const char *cmd = argv[1];
+ gboolean found = FALSE;
- cmd = argv[2];
+ prep_builtin_argv (cmd, argc-2, argv+2, &cmd_argc, &cmd_argv);
+ while (builtin->name)
+ {
+ if (builtin->flags & OSTREE_BUILTIN_FLAG_NO_REPO
+ && strcmp (cmd, builtin->name) == 0)
+ {
+ found = TRUE;
+ if (!builtin->fn (cmd_argc, cmd_argv, NULL, &error))
+ goto out;
+ break;
+ }
+ builtin++;
+ }
- while (builtin->name)
+ if (!found)
+ set_unknown_command (argv, &error);
+ }
+ else
{
- GError *error = NULL;
- if (strcmp (cmd, builtin->name) == 0)
- {
- int i;
- int tmp_argc;
- char **tmp_argv;
+ const char *repo = argv[1] + strlen ("--repo=");
+ const char *cmd = argv[2];
+ gboolean found = FALSE;
+
+ if (argc < 3)
+ return usage (argv, 1);
- tmp_argc = argc - 2;
- tmp_argv = g_new0 (char *, tmp_argc + 1);
+ prep_builtin_argv (cmd, argc-3, argv+3, &cmd_argc, &cmd_argv);
- tmp_argv[0] = (char*)builtin->name;
- for (i = 0; i < tmp_argc; i++)
- tmp_argv[i+1] = argv[i+3];
- if (!builtin->fn (tmp_argc, tmp_argv, repo, &error))
+ while (builtin->name)
+ {
+ if (!(builtin->flags & OSTREE_BUILTIN_FLAG_NO_REPO)
+ && strcmp (cmd, builtin->name) == 0)
{
- g_free (tmp_argv);
- g_printerr ("%s\n", error->message);
- g_clear_error (&error);
- return 1;
+ found = TRUE;
+ if (!builtin->fn (cmd_argc, cmd_argv, repo, &error))
+ goto out;
+ break;
}
- g_free (tmp_argv);
- return 0;
+ builtin++;
}
- builtin++;
+
+ if (!found)
+ set_unknown_command (argv, &error);
}
-
- g_printerr ("Unknown command '%s'\n", cmd);
- return usage (argv, 1);
+
+ out:
+ g_free (cmd_argv);
+ if (error)
+ {
+ g_printerr ("%s\n", error->message);
+ g_clear_error (&error);
+ return 1;
+ }
+ return 0;
}
diff --git a/src/ostree/ot-builtin-checksum.c b/src/ostree/ot-builtin-checksum.c
new file mode 100644
index 00000000..3cf2cb7d
--- /dev/null
+++ b/src/ostree/ot-builtin-checksum.c
@@ -0,0 +1,70 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2011 Colin Walters <walters@verbum.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Colin Walters <walters@verbum.org>
+ */
+
+#include "config.h"
+
+#include "ot-builtins.h"
+#include "ostree.h"
+
+#include <glib/gi18n.h>
+
+static GOptionEntry options[] = {
+ { NULL }
+};
+
+gboolean
+ostree_builtin_checksum (int argc, char **argv, const char *repo_path, GError **error)
+{
+ GOptionContext *context;
+ gboolean ret = FALSE;
+ GChecksum *checksum = NULL;
+ GFile *f = NULL;
+
+ context = g_option_context_new ("FILENAME - Checksum a file or directory");
+ g_option_context_add_main_entries (context, options, NULL);
+
+ if (!g_option_context_parse (context, &argc, &argv, error))
+ goto out;
+
+ if (argc > 1)
+ f = ot_util_new_file_for_path (argv[1]);
+ else
+ {
+ g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "A filename must be given");
+ goto out;
+ }
+
+ if (!ostree_checksum_file (f, OSTREE_OBJECT_TYPE_FILE, &checksum, NULL, error))
+ goto out;
+
+ g_print ("%s\n", g_checksum_get_string (checksum));
+
+ ret = TRUE;
+ out:
+ if (checksum)
+ g_checksum_free (checksum);
+ g_clear_object (&f);
+ if (context)
+ g_option_context_free (context);
+ return ret;
+}
diff --git a/src/ostree/ot-builtins.h b/src/ostree/ot-builtins.h
index 8337cc82..bd985341 100644
--- a/src/ostree/ot-builtins.h
+++ b/src/ostree/ot-builtins.h
@@ -29,6 +29,7 @@ G_BEGIN_DECLS
typedef enum {
OSTREE_BUILTIN_FLAG_NONE = 0,
+ OSTREE_BUILTIN_FLAG_NO_REPO = 1,
} OstreeBuiltinFlags;
typedef struct {
@@ -38,6 +39,7 @@ typedef struct {
} OstreeBuiltin;
gboolean ostree_builtin_checkout (int argc, char **argv, const char *repo, GError **error);
+gboolean ostree_builtin_checksum (int argc, char **argv, const char *repo, GError **error);
gboolean ostree_builtin_commit (int argc, char **argv, const char *repo, GError **error);
gboolean ostree_builtin_compose (int argc, char **argv, const char *repo, GError **error);
gboolean ostree_builtin_diff (int argc, char **argv, const char *repo, GError **error);
diff --git a/tests/t0000-basic.sh b/tests/t0000-basic.sh
index 0fd53f22..df1c3d5d 100755
--- a/tests/t0000-basic.sh
+++ b/tests/t0000-basic.sh
@@ -23,6 +23,9 @@ echo "1..15"
. libtest.sh
+echo hello > afile
+assert_streq "$(ostree checksum afile)" e56457ac3d60e89083e3492c738588f28311ea44c347f57f12e8b7f35d518fe3
+
setup_test_repository "regular"
echo "ok setup"