cmdline: Fatally error if the timestamp in a commit is invalid
authorColin Walters <walters@verbum.org>
Fri, 4 Dec 2015 16:14:25 +0000 (11:14 -0500)
committerColin Walters <walters@verbum.org>
Fri, 4 Dec 2015 16:26:07 +0000 (11:26 -0500)
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.

src/ostree/ot-builtin-commit.c
src/ostree/ot-dump.c

index 6afed2079af788edc2bbe1fee8bc517b32cbdaf5..72b83bf779daadfed412f61400026ede4c1175fe 100644 (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)
         {
index dc2b3a0e3044b6eab1c197a70a1b95ef81ae187c..fd4c367c159e6f10544c0fce37a1f63ce32c7c82 100644 (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)))
     {