Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/gpt Add -T timestamp for reproducible builds.



details:   https://anonhg.NetBSD.org/src/rev/90c08b78ae58
branches:  trunk
changeset: 821737:90c08b78ae58
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Feb 16 03:32:17 2017 +0000

description:
Add -T timestamp for reproducible builds.

diffstat:

 sbin/gpt/biosboot.c    |   6 +++---
 sbin/gpt/gpt.8         |  12 ++++++++++--
 sbin/gpt/gpt.c         |   6 ++++--
 sbin/gpt/gpt.h         |   2 +-
 sbin/gpt/gpt_private.h |   1 +
 sbin/gpt/gpt_uuid.c    |  47 +++++++++++++++++++++++++++++++++++++----------
 sbin/gpt/main.c        |  42 ++++++++++++++++++++++++++++++++++++++----
 7 files changed, 94 insertions(+), 22 deletions(-)

diffs (truncated from 314 to 300 lines):

diff -r e7e2eb205677 -r 90c08b78ae58 sbin/gpt/biosboot.c
--- a/sbin/gpt/biosboot.c       Thu Feb 16 02:37:32 2017 +0000
+++ b/sbin/gpt/biosboot.c       Thu Feb 16 03:32:17 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: biosboot.c,v 1.26 2016/06/09 19:04:43 christos Exp $ */
+/*     $NetBSD: biosboot.c,v 1.27 2017/02/16 03:32:17 christos Exp $ */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: biosboot.c,v 1.26 2016/06/09 19:04:43 christos Exp $");
+__RCSID("$NetBSD: biosboot.c,v 1.27 2017/02/16 03:32:17 christos Exp $");
 #endif
 
 #include <sys/stat.h>
@@ -298,7 +298,7 @@
                start = dkw.dkw_offset;
                size = dkw.dkw_size;
                ngpt = gpt_open(dkw.dkw_parent, gpt->flags, gpt->verbose,
-                   gpt->mediasz, gpt->secsz);
+                   gpt->mediasz, gpt->secsz, gpt->timestamp);
                if (ngpt == NULL)
                        goto cleanup;
        }
diff -r e7e2eb205677 -r 90c08b78ae58 sbin/gpt/gpt.8
--- a/sbin/gpt/gpt.8    Thu Feb 16 02:37:32 2017 +0000
+++ b/sbin/gpt/gpt.8    Thu Feb 16 03:32:17 2017 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.51 2017/01/27 10:21:16 abhinav Exp $
+.\" $NetBSD: gpt.8,v 1.52 2017/02/16 03:32:17 christos Exp $
 .\"
 .\" Copyright (c) 2002 Marcel Moolenaar
 .\" All rights reserved.
@@ -26,7 +26,7 @@
 .\"
 .\" $FreeBSD: src/sbin/gpt/gpt.8,v 1.17 2006/06/22 22:22:32 marcel Exp $
 .\"
-.Dd November 1, 2016
+.Dd February 15, 2017
 .Dt GPT 8
 .Os
 .Sh NAME
@@ -37,6 +37,7 @@
 .Op Fl nrqv
 .Op Fl m Ar mediasize
 .Op Fl s Ar sectorsize
+.Op Fl T Ar timestamp
 .Ar command
 .Op Ar command_options
 .Ar device
@@ -94,6 +95,13 @@
 from the kernel if possible) or
 .Dv 512
 for plain files.
+.It Fl T Ar timestamp
+Specify a timestamp to be used for uuid generation so that uuids
+are not random and can be consistent for reproducible builds.
+The timestamp can be a pathname, where the timestamps are derived from
+that file, a parseable date for parsedate(3) (this option is not
+yet available in the tools build), or an integer value interpreted
+as the number of seconds from the Epoch.
 .It Fl v
 Controls the verbosity level.
 The level increases with every occurrence of this option.
diff -r e7e2eb205677 -r 90c08b78ae58 sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c    Thu Feb 16 02:37:32 2017 +0000
+++ b/sbin/gpt/gpt.c    Thu Feb 16 03:32:17 2017 +0000
@@ -35,7 +35,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.69 2016/09/24 13:40:55 christos Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.70 2017/02/16 03:32:17 christos Exp $");
 #endif
 
 #include <sys/param.h>
@@ -461,7 +461,8 @@
 }
 
 gpt_t
-gpt_open(const char *dev, int flags, int verbose, off_t mediasz, u_int secsz)
+gpt_open(const char *dev, int flags, int verbose, off_t mediasz, u_int secsz,
+    time_t timestamp)
 {
        int mode, found;
        off_t devsz;
@@ -477,6 +478,7 @@
        gpt->verbose = verbose;
        gpt->mediasz = mediasz;
        gpt->secsz = secsz;
+       gpt->timestamp = timestamp;
 
        mode = (gpt->flags & GPT_READONLY) ? O_RDONLY : O_RDWR|O_EXCL;
                
diff -r e7e2eb205677 -r 90c08b78ae58 sbin/gpt/gpt.h
--- a/sbin/gpt/gpt.h    Thu Feb 16 02:37:32 2017 +0000
+++ b/sbin/gpt/gpt.h    Thu Feb 16 03:32:17 2017 +0000
@@ -74,7 +74,7 @@
 uint32_t crc32(const void *, size_t);
 void   gpt_close(gpt_t);
 int    gpt_gpt(gpt_t, off_t, int);
-gpt_t  gpt_open(const char *, int, int, off_t, u_int);
+gpt_t  gpt_open(const char *, int, int, off_t, u_int, time_t);
 #define GPT_READONLY   0x01
 #define GPT_MODIFIED   0x02
 #define GPT_QUIET      0x04
diff -r e7e2eb205677 -r 90c08b78ae58 sbin/gpt/gpt_private.h
--- a/sbin/gpt/gpt_private.h    Thu Feb 16 02:37:32 2017 +0000
+++ b/sbin/gpt/gpt_private.h    Thu Feb 16 03:32:17 2017 +0000
@@ -41,5 +41,6 @@
        struct map *tbl, *lbt, *gpt, *tpg;
        u_int secsz;
        off_t mediasz;
+       time_t timestamp;
        struct stat sb;
 };
diff -r e7e2eb205677 -r 90c08b78ae58 sbin/gpt/gpt_uuid.c
--- a/sbin/gpt/gpt_uuid.c       Thu Feb 16 02:37:32 2017 +0000
+++ b/sbin/gpt/gpt_uuid.c       Thu Feb 16 03:32:17 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gpt_uuid.c,v 1.13 2015/12/06 00:39:26 christos Exp $   */
+/*     $NetBSD: gpt_uuid.c,v 1.14 2017/02/16 03:32:17 christos Exp $   */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt_uuid.c,v 1.13 2015/12/06 00:39:26 christos Exp $");
+__RCSID("$NetBSD: gpt_uuid.c,v 1.14 2017/02/16 03:32:17 christos Exp $");
 #endif
 
 #include <err.h>
@@ -42,6 +42,7 @@
 
 #include "map.h"
 #include "gpt.h"
+#include "gpt_private.h"
 
 #if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H)
 #include <sys/endian.h>
@@ -247,13 +248,11 @@
                utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b));
 }
 
-int
-gpt_uuid_generate(gpt_t gpt, gpt_uuid_t t)
+static int
+gpt_uuid_random(gpt_t gpt, void *v, size_t n)
 {
-       struct dce_uuid u;
        int fd;
        uint8_t *p;
-       size_t n;
        ssize_t nread;
 
        /* Randomly generate the content.  */
@@ -262,7 +261,7 @@
                gpt_warn(gpt, "Can't open `/dev/urandom'");
                return -1;
        }
-       for (p = (void *)&u, n = sizeof u; n > 0; p += nread, n -= (size_t)nread) {
+       for (p = v;  n > 0; p += nread, n -= (size_t)nread) {
                nread = read(fd, p, n);
                if (nread < 0) {
                        gpt_warn(gpt, "Can't read `/dev/urandom'");
@@ -278,6 +277,37 @@
                }
        }
        (void)close(fd);
+       return 0;
+out:
+       (void)close(fd);
+       return -1;
+}
+
+static int 
+gpt_uuid_tstamp(gpt_t gpt, void *v, size_t l)
+{
+       uint8_t *p;
+       // Perhaps use SHA?
+       uint32_t x = (uint32_t)gpt->timestamp;
+
+       for (p = v; l > 0; p += sizeof(x), l -= sizeof(x))
+               memcpy(p, &x, sizeof(x));
+
+       return 0;
+}
+
+int
+gpt_uuid_generate(gpt_t gpt, gpt_uuid_t t)
+{
+       int rv;
+       struct dce_uuid u;
+       if (gpt->timestamp == 0)
+               rv = gpt_uuid_random(gpt, &u, sizeof(u));
+       else
+               rv = gpt_uuid_tstamp(gpt, &u, sizeof(u));
+
+       if (rv == -1)
+               return -1;
 
        /* Set the version number to 4.  */
        u.time_hi_and_version &= (uint16_t)~0xf000;
@@ -288,8 +318,5 @@
        u.clock_seq_hi_and_reserved |= 0x80;
 
        gpt_dce_to_uuid(&u, t);
-       close(fd);
        return 0;
-out:
-       return -1;
 }
diff -r e7e2eb205677 -r 90c08b78ae58 sbin/gpt/main.c
--- a/sbin/gpt/main.c   Thu Feb 16 02:37:32 2017 +0000
+++ b/sbin/gpt/main.c   Thu Feb 16 03:32:17 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.8 2017/02/12 16:54:06 aymeric Exp $ */
+/*     $NetBSD: main.c,v 1.9 2017/02/16 03:32:17 christos Exp $        */
 
 /*-
  * Copyright (c) 2002 Marcel Moolenaar
@@ -34,13 +34,18 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: main.c,v 1.8 2017/02/12 16:54:06 aymeric Exp $");
+__RCSID("$NetBSD: main.c,v 1.9 2017/02/16 03:32:17 christos Exp $");
 #endif
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <err.h>
+#include <errno.h>
+#include <sys/stat.h>
+#ifndef NBTOOL_CONFIG_H
+#include <util.h>
+#endif
 
 #include "map.h"
 #include "gpt.h"
@@ -126,6 +131,31 @@
                setprogname(pfx);
 }
 
+static time_t
+get_tstamp(const char *b)
+{
+       struct stat st;
+       char *eb;
+       long long l;
+#ifndef HAVE_NBTOOL_CONFIG_H
+       time_t when;
+#endif
+
+       if (stat(b, &st) != -1)
+               return (time_t)st.st_mtime;
+
+#ifndef HAVE_NBTOOL_CONFIG_H
+       errno = 0;
+       if ((when = parsedate(b, NULL, NULL)) != -1 || errno == 0)
+               return when;
+#endif
+       errno = 0;
+       l = strtoll(b, &eb, 0);
+       if (b == eb || *eb || errno)
+               errx(EXIT_FAILURE, "Can't parse timestamp `%s'", b);
+       return (time_t)l;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -135,6 +165,7 @@
        off_t mediasz = 0;
        int flags = 0;
        int verbose = 0;
+       time_t timestamp = 0;
        gpt_t gpt;
 
        setprogname(argv[0]);
@@ -152,7 +183,7 @@
 #endif
 
        /* Get the generic options */
-       while ((ch = getopt(argc, argv, GETOPT_BE_POSIX "m:nqrs:v")) != -1) {
+       while ((ch = getopt(argc, argv, GETOPT_BE_POSIX "m:nqrs:T:v")) != -1) {
                switch(ch) {
                case 'm':
                        if (mediasz > 0)
@@ -174,6 +205,9 @@
                        if (gpt_uint_get(NULL, &secsz) == -1)
                                usage();
                        break;
+               case 'T':



Home | Main Index | Thread Index | Old Index