cmdline: Fatally error if the timestamp in a commit is invalid

Previously we were just ignoring this, which hid a bug in
an earlier commit that generated them.

Also change the `commit` program to use both APIs - this
involves extra code, but not too much.

This way, reverting the fix with this on top caused the test suite to
fail.  Adding an active test for this would need a custom test program
using the C API, or adding a cmdline flag to the client, neither of
which quite seemed worth it.
This commit is contained in:
Colin Walters 2015-12-04 11:14:25 -05:00
parent f88a9733ce
commit 8ba90a3341
2 changed files with 25 additions and 11 deletions

View File

@ -497,6 +497,11 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
GDateTime *now = g_date_time_new_now_utc ();
timestamp = g_date_time_to_unix (now);
g_date_time_unref (now);
if (!ostree_repo_write_commit (repo, parent, opt_subject, opt_body, metadata,
OSTREE_REPO_FILE (root),
&commit_checksum, cancellable, error))
goto out;
}
else
{
@ -508,13 +513,13 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
goto out;
}
timestamp = ts.tv_sec;
}
if (!ostree_repo_write_commit_with_time (repo, parent, opt_subject, opt_body, metadata,
OSTREE_REPO_FILE (root),
timestamp,
&commit_checksum, cancellable, error))
goto out;
if (!ostree_repo_write_commit_with_time (repo, parent, opt_subject, opt_body, metadata,
OSTREE_REPO_FILE (root),
timestamp,
&commit_checksum, cancellable, error))
goto out;
}
if (detached_metadata)
{

View File

@ -24,6 +24,8 @@
#include "config.h"
#include <err.h>
#include "ot-dump.h"
#include "otutil.h"
#include "ot-admin-functions.h"
@ -47,14 +49,19 @@ ot_dump_variant (GVariant *variant)
}
static gchar *
format_timestamp (guint64 timestamp)
format_timestamp (guint64 timestamp,
GError **error)
{
GDateTime *dt;
gchar *str;
dt = g_date_time_new_from_unix_utc (timestamp);
if (dt == NULL)
return NULL;
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
"Invalid timestamp: %" G_GUINT64_FORMAT, timestamp);
return NULL;
}
str = g_date_time_format (dt, "%Y-%m-%d %H:%M:%S +0000");
g_date_time_unref (dt);
@ -94,15 +101,17 @@ dump_commit (GVariant *variant,
guint64 timestamp;
g_autofree char *str = NULL;
g_autofree char *version = NULL;
g_autoptr(GError) local_error = NULL;
/* See OSTREE_COMMIT_GVARIANT_FORMAT */
g_variant_get (variant, "(a{sv}aya(say)&s&stayay)", NULL, NULL, NULL,
&subject, &body, &timestamp, NULL, NULL);
timestamp = GUINT64_FROM_BE (timestamp);
str = format_timestamp (timestamp);
if (str)
g_print ("Date: %s\n", str);
str = format_timestamp (timestamp, &local_error);
if (!str)
errx (1, "Failed to read commit: %s", local_error->message);
g_print ("Date: %s\n", str);
if ((version = ot_admin_checksum_version (variant)))
{