Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 7 May 2011 10:27:23 +0000 (UTC)
From:      Kai Wang <kaiw@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org
Subject:   svn commit: r221588 - stable/8/usr.bin/ar
Message-ID:  <201105071027.p47ARN6Z092537@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kaiw
Date: Sat May  7 10:27:23 2011
New Revision: 221588
URL: http://svn.freebsd.org/changeset/base/221588

Log:
  MFC r213643 (kientzle):
  
  Add -D (deterministic) option to ar.
  When set, it forces all timestamps and owners to zero and
  modes to 0644.  Useful for producing libraries that are
  bitwise identical across multiple build runs.

Modified:
  stable/8/usr.bin/ar/ar.1
  stable/8/usr.bin/ar/ar.c
  stable/8/usr.bin/ar/ar.h
  stable/8/usr.bin/ar/write.c
Directory Properties:
  stable/8/usr.bin/ar/   (props changed)

Modified: stable/8/usr.bin/ar/ar.1
==============================================================================
--- stable/8/usr.bin/ar/ar.1	Sat May  7 10:23:07 2011	(r221587)
+++ stable/8/usr.bin/ar/ar.1	Sat May  7 10:27:23 2011	(r221588)
@@ -62,6 +62,7 @@
 .Op Fl a Ar position-after
 .Op Fl b Ar position-before
 .Op Fl c
+.Op Fl D
 .Op Fl i Ar position-before
 .Op Fl j
 .Op Fl s
@@ -179,6 +180,16 @@ from the archive specified by argument
 .Ar archive .
 The archive's symbol table, if present, is updated to reflect
 the new contents of the archive.
+.It Fl D
+When used in combination with the 
+.Fl r
+or
+.Fl q
+option, insert 0's instead of the real mtime, uid and gid values 
+and 0644 instead of file mode from the members named by arguments
+.Ar files ... .
+This ensures that checksums on the resulting archives are reproducible
+when member contents are identical.
 .It Fl f
 Synonymous with option
 .Fl T .

Modified: stable/8/usr.bin/ar/ar.c
==============================================================================
--- stable/8/usr.bin/ar/ar.c	Sat May  7 10:23:07 2011	(r221587)
+++ stable/8/usr.bin/ar/ar.c	Sat May  7 10:27:23 2011	(r221588)
@@ -154,7 +154,7 @@ main(int argc, char **argv)
 		}
 	}
 
-	while ((opt = getopt_long(argc, argv, "abCcdfijlMmopqrSsTtuVvxz",
+	while ((opt = getopt_long(argc, argv, "abCcdDfijlMmopqrSsTtuVvxz",
 	    longopts, NULL)) != -1) {
 		switch(opt) {
 		case 'a':
@@ -173,6 +173,9 @@ main(int argc, char **argv)
 		case 'd':
 			set_mode(bsdar, opt);
 			break;
+		case 'D':
+			bsdar->options |= AR_D;
+			break;
 		case 'f':
 		case 'T':
 			bsdar->options |= AR_TR;
@@ -269,6 +272,8 @@ main(int argc, char **argv)
 		only_mode(bsdar, "-c", "qr");
 	if (bsdar->options & AR_CC)
 		only_mode(bsdar, "-C", "x");
+	if (bsdar->options & AR_D)
+		only_mode(bsdar, "-D", "qr");
 	if (bsdar->options & AR_O)
 		only_mode(bsdar, "-o", "x");
 	if (bsdar->options & AR_SS)
@@ -356,9 +361,9 @@ bsdar_usage()
 	(void)fprintf(stderr, "\tar -m [-Tjsvz] archive file ...\n");
 	(void)fprintf(stderr, "\tar -m [-Tabijsvz] position archive file ...\n");
 	(void)fprintf(stderr, "\tar -p [-Tv] archive [file ...]\n");
-	(void)fprintf(stderr, "\tar -q [-Tcjsvz] archive file ...\n");
-	(void)fprintf(stderr, "\tar -r [-Tcjsuvz] archive file ...\n");
-	(void)fprintf(stderr, "\tar -r [-Tabcijsuvz] position archive file ...\n");
+	(void)fprintf(stderr, "\tar -q [-TcDjsvz] archive file ...\n");
+	(void)fprintf(stderr, "\tar -r [-TcDjsuvz] archive file ...\n");
+	(void)fprintf(stderr, "\tar -r [-TabcDijsuvz] position archive file ...\n");
 	(void)fprintf(stderr, "\tar -s [-jz] archive\n");
 	(void)fprintf(stderr, "\tar -t [-Tv] archive [file ...]\n");
 	(void)fprintf(stderr, "\tar -x [-CTouv] archive [file ...]\n");

Modified: stable/8/usr.bin/ar/ar.h
==============================================================================
--- stable/8/usr.bin/ar/ar.h	Sat May  7 10:23:07 2011	(r221587)
+++ stable/8/usr.bin/ar/ar.h	Sat May  7 10:27:23 2011	(r221588)
@@ -43,6 +43,7 @@
 #define AR_U	0x0200		/* only extract or update newer members.*/
 #define AR_V	0x0400		/* verbose mode */
 #define AR_Z	0x0800		/* gzip compression */
+#define AR_D	0x1000		/* insert dummy mode, mtime, uid and gid */
 
 #define DEF_BLKSZ 10240		/* default block size */
 

Modified: stable/8/usr.bin/ar/write.c
==============================================================================
--- stable/8/usr.bin/ar/write.c	Sat May  7 10:23:07 2011	(r221587)
+++ stable/8/usr.bin/ar/write.c	Sat May  7 10:27:23 2011	(r221588)
@@ -163,11 +163,24 @@ create_obj_from_file(struct bsdar *bsdar
 	if (mtime != 0 && bsdar->options & AR_U && sb.st_mtime <= mtime)
 		goto giveup;
 
-	obj->uid = sb.st_uid;
-	obj->gid = sb.st_gid;
-	obj->md = sb.st_mode;
+	/*
+	 * When option '-D' is specified, mtime and UID / GID from the file
+	 * will be replaced with 0, and file mode with 644. This ensures that 
+	 * checksums will match for two archives containing the exact same
+	 * files.
+	 */
+	if (bsdar->options & AR_D) {
+		obj->uid = 0;
+		obj->gid = 0;
+		obj->mtime = 0;
+		obj->md = S_IFREG | 0644;
+	} else {
+		obj->uid = sb.st_uid;
+		obj->gid = sb.st_gid;
+		obj->mtime = sb.st_mtime;
+		obj->md = sb.st_mode;
+	}
 	obj->size = sb.st_size;
-	obj->mtime = sb.st_mtime;
 	obj->dev = sb.st_dev;
 	obj->ino = sb.st_ino;
 
@@ -621,7 +634,8 @@ write_objs(struct bsdar *bsdar)
 	    bsdar->options & AR_S) {
 		entry = archive_entry_new();
 		archive_entry_copy_pathname(entry, "/");
-		archive_entry_set_mtime(entry, time(NULL), 0);
+		if ((bsdar->options & AR_D) == 0)
+			archive_entry_set_mtime(entry, time(NULL), 0);
 		archive_entry_set_size(entry, (bsdar->s_cnt + 1) *
 		    sizeof(uint32_t) + bsdar->s_sn_sz);
 		AC(archive_write_header(a, entry));



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201105071027.p47ARN6Z092537>