mirror of
https://github.com/ostreedev/ostree.git
synced 2024-10-26 08:55:19 +03:00
core: Add checksum builtin
This necessitated reworking things so that builtins can specify no --repo is required.
This commit is contained in:
parent
7f64d5cec7
commit
12f2d89174
@ -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 \
|
||||
|
@ -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=");
|
||||
|
||||
cmd = argv[2];
|
||||
{
|
||||
const char *cmd = argv[1];
|
||||
gboolean found = FALSE;
|
||||
|
||||
prep_builtin_argv (cmd, argc-2, argv+2, &cmd_argc, &cmd_argv);
|
||||
while (builtin->name)
|
||||
{
|
||||
GError *error = NULL;
|
||||
if (strcmp (cmd, builtin->name) == 0)
|
||||
if (builtin->flags & OSTREE_BUILTIN_FLAG_NO_REPO
|
||||
&& strcmp (cmd, builtin->name) == 0)
|
||||
{
|
||||
int i;
|
||||
int tmp_argc;
|
||||
char **tmp_argv;
|
||||
|
||||
tmp_argc = argc - 2;
|
||||
tmp_argv = g_new0 (char *, tmp_argc + 1);
|
||||
|
||||
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))
|
||||
{
|
||||
g_free (tmp_argv);
|
||||
g_printerr ("%s\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return 1;
|
||||
}
|
||||
g_free (tmp_argv);
|
||||
return 0;
|
||||
found = TRUE;
|
||||
if (!builtin->fn (cmd_argc, cmd_argv, NULL, &error))
|
||||
goto out;
|
||||
break;
|
||||
}
|
||||
builtin++;
|
||||
}
|
||||
|
||||
g_printerr ("Unknown command '%s'\n", cmd);
|
||||
if (!found)
|
||||
set_unknown_command (argv, &error);
|
||||
}
|
||||
else
|
||||
{
|
||||
const char *repo = argv[1] + strlen ("--repo=");
|
||||
const char *cmd = argv[2];
|
||||
gboolean found = FALSE;
|
||||
|
||||
if (argc < 3)
|
||||
return usage (argv, 1);
|
||||
|
||||
prep_builtin_argv (cmd, argc-3, argv+3, &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, repo, &error))
|
||||
goto out;
|
||||
break;
|
||||
}
|
||||
builtin++;
|
||||
}
|
||||
|
||||
if (!found)
|
||||
set_unknown_command (argv, &error);
|
||||
}
|
||||
|
||||
out:
|
||||
g_free (cmd_argv);
|
||||
if (error)
|
||||
{
|
||||
g_printerr ("%s\n", error->message);
|
||||
g_clear_error (&error);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
70
src/ostree/ot-builtin-checksum.c
Normal file
70
src/ostree/ot-builtin-checksum.c
Normal file
@ -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;
|
||||
}
|
@ -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);
|
||||
|
@ -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"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user