Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Allow to set timestamp for pkg create
Baptiste Daroussin committed 6 years ago
commit fb72299993e8323644e8bdff5d21eb27d042919c
parent e729f44
6 files changed +51 -20
modified libpkg/packing.c
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2011-2014 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2020 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011 Will Andrews <will@FreeBSD.org>
 * All rights reserved.
 * 
@@ -45,18 +45,37 @@ struct packing {
	struct archive *aread;
	struct archive *awrite;
	struct archive_entry_linkresolver *resolver;
+
	time_t timestamp;
};

int
-
packing_init(struct packing **pack, const char *path, pkg_formats format)
+
packing_init(struct packing **pack, const char *path, pkg_formats format,
+
    time_t timestamp)
{
	char archive_path[MAXPATHLEN];
	const char *ext;
+
	const char *source_date_epoch;
+
	char *endptr;
+
	time_t ts;

	assert(pack != NULL);

	*pack = xcalloc(1, sizeof(struct packing));

+
	if (timestamp == (time_t)-1) {
+
		if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) != NULL) {
+
			ts = (time_t)strtoimax(source_date_epoch, &endptr, 10);
+
			if (*endptr != '\0') {
+
				pkg_emit_error("Ignoring bad environment variable "
+
				    "SOURCE_DATE_EPOCH: %s", source_date_epoch);
+
			} else {
+
				(*pack)->timestamp = ts;
+
			}
+
	    }
+
	} else {
+
		(*pack)->timestamp = timestamp;
+
	}
+

	(*pack)->aread = archive_read_disk_new();
	archive_read_disk_set_standard_lookup((*pack)->aread);
	archive_read_disk_set_symlink_physical((*pack)->aread);
@@ -135,11 +154,9 @@ packing_append_file_attr(struct packing *pack, const char *filepath,
	int fd;
	int retcode = EPKG_OK;
	int ret;
-
	time_t source_time;
	struct stat st;
	struct archive_entry *entry, *sparse_entry;
	bool unset_timestamp;
-
	const char *source_date_epoch;
	char buf[32768];
	int len;

@@ -193,18 +210,11 @@ packing_append_file_attr(struct packing *pack, const char *filepath,
		archive_entry_unset_birthtime(entry);
	}

-
	if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) != NULL) {
-
		if (source_date_epoch[strspn(source_date_epoch, "0123456789")] != '\0') {
-
			pkg_emit_error("Bad environment variable "
-
			    "SOURCE_DATE_EPOCH: %s", source_date_epoch);
-
			retcode = EPKG_FATAL;
-
			goto cleanup;
-
		}
-
		source_time = strtoll(source_date_epoch, NULL, 10);
-
		archive_entry_set_atime(entry, source_time, 0);
-
		archive_entry_set_ctime(entry, source_time, 0);
-
		archive_entry_set_mtime(entry, source_time, 0);
-
		archive_entry_set_birthtime(entry, source_time, 0);
+
	if (pack->timestamp != (time_t) -1) {
+
		archive_entry_set_atime(entry, pack->timestamp, 0);
+
		archive_entry_set_ctime(entry, pack->timestamp, 0);
+
		archive_entry_set_mtime(entry, pack->timestamp, 0);
+
		archive_entry_set_birthtime(entry, pack->timestamp, 0);
	}

	archive_entry_linkify(pack->resolver, &entry, &sparse_entry);
modified libpkg/pkg.h.in
@@ -1706,6 +1706,7 @@ void pkg_create_free(struct pkg_create *);
bool pkg_create_set_format(struct pkg_create *, const char *);
void pkg_create_set_rootdir(struct pkg_create *, const char *);
void pkg_create_set_output_dir(struct pkg_create *, const char *);
+
void pkg_create_set_timestamp(struct pkg_create *, time_t);
int pkg_create(struct pkg_create *, const char *, const char *, bool);
int pkg_create_i(struct pkg_create *, struct pkg *, bool);
/* deprecated */
modified libpkg/pkg_create.c
@@ -190,7 +190,7 @@ pkg_create_archive(struct pkg *pkg, struct pkg_create *pc, unsigned required_fla
		return (NULL);
	}

-
	if (packing_init(&pkg_archive, pkg_path, pc->format) != EPKG_OK)
+
	if (packing_init(&pkg_archive, pkg_path, pc->format, pc->timestamp) != EPKG_OK)
		pkg_archive = NULL;

	free(pkg_path);
@@ -230,6 +230,7 @@ pkg_create_new(void)

	pc = xcalloc(1, sizeof(pc));
	pc->format = TXZ;
+
	pc->timestamp = (time_t) -1;

	return (pc);
}
@@ -270,6 +271,12 @@ pkg_create_set_output_dir(struct pkg_create *pc, const char *outdir)
	pc->outdir = outdir;
}

+
void
+
pkg_create_set_timestamp(struct pkg_create *pc, time_t timestamp)
+
{
+
	pc->timestamp = timestamp;
+
}
+

static int
hash_file(struct pkg_create *pc, struct pkg *pkg)
{
modified libpkg/pkg_repo_create.c
@@ -825,7 +825,7 @@ pkg_repo_pack_db(const char *name, const char *archive, char *path,
	sig = NULL;
	pub = NULL;

-
	if (packing_init(&pack, archive, meta->packing_format) != EPKG_OK)
+
	if (packing_init(&pack, archive, meta->packing_format, (time_t)-1) != EPKG_OK)
		return (EPKG_FATAL);

	if (rsa != NULL) {
modified libpkg/private/pkg.h
@@ -743,7 +743,7 @@ int pkg_jobs_resolv(struct pkg_jobs *jobs);

struct packing;

-
int packing_init(struct packing **pack, const char *path, pkg_formats format);
+
int packing_init(struct packing **pack, const char *path, pkg_formats format, time_t timestamp);
int packing_append_file_attr(struct packing *pack, const char *filepath,
     const char *newpath, const char *uname, const char *gname, mode_t perm,
     u_long fflags);
modified src/create.c
@@ -173,9 +173,11 @@ exec_create(int argc, char **argv)
	const char	*metadatadir = NULL;
	const char	*manifest = NULL;
	char		*plist = NULL;
+
	char	*endptr;
	pkg_formats	 fmt;
	int		 ch;
	bool		 hash = false;
+
	time_t		 ts = (time_t)-1;


	/* POLA: pkg create is quiet by default, unless
@@ -197,11 +199,12 @@ exec_create(int argc, char **argv)
		{ "out-dir",	required_argument,	NULL,	'o' },
		{ "plist",	required_argument,	NULL,	'p' },
		{ "quiet",	no_argument,		NULL,	'q' },
+
		{ "timestamp",	required_argument,	NULL,	't' },
		{ "verbose",	no_argument,		NULL,	'v' },
		{ NULL,		0,			NULL,	0   },
	};

-
	while ((ch = getopt_long(argc, argv, "+aghxf:r:m:M:o:p:qv", longopts, NULL)) != -1) {
+
	while ((ch = getopt_long(argc, argv, "+aghxf:r:m:M:o:p:qvt:", longopts, NULL)) != -1) {
		switch (ch) {
		case 'a':
			match = MATCH_ALL;
@@ -233,6 +236,14 @@ exec_create(int argc, char **argv)
		case 'r':
			rootdir = optarg;
			break;
+
		case 't':
+
			endptr = NULL;
+
			ts = (time_t)strtoimax(optarg, &endptr, 10);
+
			if (*endptr != '\0') {
+
				warnx("Invalid timestamp %s", optarg);
+
				return (EX_USAGE);
+
			}
+
			break;
		case 'v':
			quiet = false;
			break;
@@ -273,6 +284,8 @@ exec_create(int argc, char **argv)

	pkg_create_set_rootdir(pc, rootdir);
	pkg_create_set_output_dir(pc, outdir);
+
	if (ts != (time_t)-1)
+
		pkg_create_set_timestamp(pc, ts);

	if (metadatadir == NULL && manifest == NULL)
		return (pkg_create_matches(argc, argv, match, pc) == EPKG_OK ? EX_OK : EX_SOFTWARE);