Re: pkg_info -X: Add FILE_CKSUM (sha256)

Taylor R Campbell <[email protected]>
Newsgroups gmane.os.netbsd.devel.packages
Message-ID <[email protected]>
> Date: Fri, 16 Jan 2026 12:21:05 +0100
> From: Thomas Klausner <[email protected]>
> 
> I think it would be good to hide this behind an additional flag for
> now, so it has to be turned on - and that can be done when the SHA256
> will actually be used by something else.

The attached patch implements it as a flag `-H sha256'.

Without that flag, there is no change to behaviour.

Better?
pkgsumhash-v2.patch (text/plain, 4.2 KB)
diff --git a/pkgtools/pkg_install/files/info/info.h b/pkgtools/pkg_install/files/info/info.h
index 8718da209d69..c175cbb7b4a2 100644
--- a/pkgtools/pkg_install/files/info/info.h
+++ b/pkgtools/pkg_install/files/info/info.h
@@ -107,6 +107,11 @@ enum which {
     WHICH_LIST
 };
 
+enum summary_hash {
+    SUMMARY_HASH_NONE = 0,
+    SUMMARY_HASH_SHA256,
+};
+
 extern int Flags;
 extern enum which Which;
 extern Boolean File2Pkg;
@@ -114,6 +119,7 @@ extern Boolean Quiet;
 extern const char *InfoPrefix;
 extern const char *BuildInfoVariable;
 extern lpkg_head_t pkgs;
+extern enum summary_hash summary_hash;
 
 int CheckForPkg(const char *);
 int CheckForBestPkg(const char *);
diff --git a/pkgtools/pkg_install/files/info/main.c b/pkgtools/pkg_install/files/info/main.c
index 6827443f07fd..b6eddd90c077 100644
--- a/pkgtools/pkg_install/files/info/main.c
+++ b/pkgtools/pkg_install/files/info/main.c
@@ -41,7 +41,7 @@ __RCSID("$NetBSD: main.c,v 1.32 2018/04/25 12:20:53 joerg Exp $");
 #include "lib.h"
 #include "info.h"
 
-static const char Options[] = ".aBbcDde:E:fFhIiK:kLl:mNnpQ:qrRsSuvVX";
+static const char Options[] = ".aBbcDde:E:fFH:hIiK:kLl:mNnpQ:qrRsSuvVX";
 
 int     Flags = 0;
 enum which Which = WHICH_LIST;
@@ -50,13 +50,14 @@ Boolean Quiet = FALSE;
 const char   *InfoPrefix = "";
 const char   *BuildInfoVariable = "";
 lpkg_head_t pkgs;
+enum summary_hash summary_hash;
 
 static void
 usage(void)
 {
 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
 	    "usage: pkg_info [-BbcDdFfhIikLmNnpqRrSsVvX] [-E pkg-name] [-e pkg-name]",
-	    "                [-K pkg_dbdir] [-l prefix] pkg-name ...",
+	    "                [-H hash] [-K pkg_dbdir] [-l prefix] pkg-name ...",
 	    "       pkg_info [-a | -u] [flags]",
 	    "       pkg_info [-Q variable] pkg-name ...");
 	exit(1);
@@ -117,6 +118,13 @@ main(int argc, char **argv)
 			File2Pkg = 1;
 			break;
 
+		case 'H':
+			if (strcmp(optarg, "sha256") == 0)
+				summary_hash = SUMMARY_HASH_SHA256;
+			else
+				errx(EXIT_FAILURE, "unknown hash");
+			break;
+
 		case 'I':
 			Flags |= SHOW_INDEX;
 			break;
diff --git a/pkgtools/pkg_install/files/info/pkg_info.1 b/pkgtools/pkg_install/files/info/pkg_info.1
index 093734751612..452bc1dbd522 100644
--- a/pkgtools/pkg_install/files/info/pkg_info.1
+++ b/pkgtools/pkg_install/files/info/pkg_info.1
@@ -28,6 +28,7 @@
 .Op Fl BbcDdFfhIikLmNnpqRrSsVvX
 .Op Fl E Ar pkg-name
 .Op Fl e Ar pkg-name
+.Op Fl H Ar hash
 .Op Fl K Ar pkg_dbdir
 .Op Fl l Ar prefix
 .Ar pkg-name ...
@@ -133,6 +134,19 @@ See the
 section below for more information.
 .It Fl f
 Show the packing list instructions for each package.
+.It Fl H Ar hash
+When printing
+.Xr pkg_summary 5
+information with
+.Fl X ,
+include a
+.Li FILE_CKSUM
+line with the given
+.Ar hash
+of the package.
+.Pp
+Supported hashes:
+.Li sha256 .
 .It Fl h
 Print usage message and exit.
 .It Fl I
diff --git a/pkgtools/pkg_install/files/info/show.c b/pkgtools/pkg_install/files/info/show.c
index daf3bdba6a08..085628d67e96 100644
--- a/pkgtools/pkg_install/files/info/show.c
+++ b/pkgtools/pkg_install/files/info/show.c
@@ -60,6 +60,11 @@ __RCSID("$NetBSD: show.c,v 1.33 2012/02/21 18:32:14 wiz Exp $");
 #if HAVE_ERR_H
 #include <err.h>
 #endif
+#ifndef NETBSD
+#include <nbcompat/sha2.h>
+#else
+#include <sha2.h>
+#endif
 
 #include "defs.h"
 #include "lib.h"
@@ -377,16 +382,25 @@ show_summary(struct pkg_meta *meta, package_t *plist, const char *binpkgfile)
 		warnx("Build information missing");
 
 	if (binpkgfile != NULL && stat(binpkgfile, &st) == 0) {
-	    const char *base;
-
-	    base = strrchr(binpkgfile, '/');
-	    if (base == NULL)
-		base = binpkgfile;
-	    else
-		base++;
-	    printf("FILE_NAME=%s\n", base);
-	    printf("FILE_SIZE=%" MY_PRIu64 "\n", (uint64_t)st.st_size);
-	    /* XXX: DIGETS */
+		const char *base;
+
+		base = strrchr(binpkgfile, '/');
+		if (base == NULL)
+			base = binpkgfile;
+		else
+			base++;
+		printf("FILE_NAME=%s\n", base);
+		printf("FILE_SIZE=%" MY_PRIu64 "\n", (uint64_t)st.st_size);
+		switch (summary_hash) {
+		case SUMMARY_HASH_NONE:
+			break;
+		case SUMMARY_HASH_SHA256: {
+			char sha256[SHA256_DIGEST_STRING_LENGTH];
+			printf("FILE_CKSUM=sha256 %s\n",
+			    SHA256_File(binpkgfile, sha256));
+			break;
+		}
+		}
 	}
 
 	print_string_as_var("DESCRIPTION", meta->meta_desc);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.