git: cc9a8a116d19 - main - pkg(7): replace usage of sbuf(9) with open_memstream(3)

Baptiste Daroussin bapt at FreeBSD.org
Thu Apr 29 04:44:57 UTC 2021


The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=cc9a8a116d19daf224222506441e91a3d329160e

commit cc9a8a116d19daf224222506441e91a3d329160e
Author:     Baptiste Daroussin <bapt at FreeBSD.org>
AuthorDate: 2021-04-27 02:38:55 +0000
Commit:     Baptiste Daroussin <bapt at FreeBSD.org>
CommitDate: 2021-04-29 04:41:59 +0000

    pkg(7): replace usage of sbuf(9) with open_memstream(3)
    
    open_memstream(3) is a standard way to obtain the same feature we do get
    by using sbuf(9) (aka dynamic size buffer), switching to using it makes
    pkg(7) more portable, and reduces its number of dependencies.
    
    Reviewed by:    manu
    Differential Revision:  https://reviews.freebsd.org/D30005
---
 usr.sbin/pkg/Makefile |  2 +-
 usr.sbin/pkg/config.c | 33 +++++++++++++++----------
 usr.sbin/pkg/pkg.c    | 67 +++++++++++++++++++++++++++------------------------
 3 files changed, 56 insertions(+), 46 deletions(-)

diff --git a/usr.sbin/pkg/Makefile b/usr.sbin/pkg/Makefile
index 980faafc6b6c..a71f0b2acb86 100644
--- a/usr.sbin/pkg/Makefile
+++ b/usr.sbin/pkg/Makefile
@@ -25,6 +25,6 @@ MAN=	pkg.7
 
 CFLAGS+=-I${SRCTOP}/contrib/libucl/include
 .PATH:	${SRCTOP}/contrib/libucl/include
-LIBADD=	archive fetch ucl sbuf crypto ssl util
+LIBADD=	archive fetch ucl crypto ssl util
 
 .include <bsd.prog.mk>
diff --git a/usr.sbin/pkg/config.c b/usr.sbin/pkg/config.c
index cf21a43b078d..08e206b93511 100644
--- a/usr.sbin/pkg/config.c
+++ b/usr.sbin/pkg/config.c
@@ -33,7 +33,6 @@ __FBSDID("$FreeBSD$");
 #include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/utsname.h>
-#include <sys/sbuf.h>
 #include <sys/sysctl.h>
 
 #include <dirent.h>
@@ -214,7 +213,9 @@ boolstr_to_bool(const char *str)
 static void
 config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
 {
-	struct sbuf *buf = sbuf_new_auto();
+	FILE *buffp;
+	char *buf = NULL;
+	size_t bufsz = 0;
 	const ucl_object_t *cur, *seq, *tmp;
 	ucl_object_iter_t it = NULL, itseq = NULL, it_obj = NULL;
 	struct config_entry *temp_config;
@@ -225,39 +226,44 @@ config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
 
 	/* Temporary config for configs that may be disabled. */
 	temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
+	buffp = open_memstream(&buf, &bufsz);
+	if (buffp == NULL)
+		err(EXIT_FAILURE, "open_memstream()");
 
 	while ((cur = ucl_iterate_object(obj, &it, true))) {
 		key = ucl_object_key(cur);
 		if (key == NULL)
 			continue;
-		sbuf_clear(buf);
+		if (buf != NULL)
+			memset(buf, 0, bufsz);
+		rewind(buffp);
 
 		if (conftype == CONFFILE_PKG) {
 			for (j = 0; j < strlen(key); ++j)
-				sbuf_putc(buf, toupper(key[j]));
-			sbuf_finish(buf);
+				fputc(toupper(key[j]), buffp);
+			fflush(buffp);
 		} else if (conftype == CONFFILE_REPO) {
 			if (strcasecmp(key, "url") == 0)
-				sbuf_cpy(buf, "PACKAGESITE");
+				fputs("PACKAGESITE", buffp);
 			else if (strcasecmp(key, "mirror_type") == 0)
-				sbuf_cpy(buf, "MIRROR_TYPE");
+				fputs("MIRROR_TYPE", buffp);
 			else if (strcasecmp(key, "signature_type") == 0)
-				sbuf_cpy(buf, "SIGNATURE_TYPE");
+				fputs("SIGNATURE_TYPE", buffp);
 			else if (strcasecmp(key, "fingerprints") == 0)
-				sbuf_cpy(buf, "FINGERPRINTS");
+				fputs("FINGERPRINTS", buffp);
 			else if (strcasecmp(key, "pubkey") == 0)
-				sbuf_cpy(buf, "PUBKEY");
+				fputs("PUBKEY", buffp);
 			else if (strcasecmp(key, "enabled") == 0) {
 				if ((cur->type != UCL_BOOLEAN) ||
 				    !ucl_object_toboolean(cur))
 					goto cleanup;
 			} else
 				continue;
-			sbuf_finish(buf);
+			fflush(buffp);
 		}
 
 		for (i = 0; i < CONFIG_SIZE; i++) {
-			if (strcmp(sbuf_data(buf), c[i].key) == 0)
+			if (strcmp(buf, c[i].key) == 0)
 				break;
 		}
 
@@ -332,7 +338,8 @@ config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
 
 cleanup:
 	free(temp_config);
-	sbuf_delete(buf);
+	fclose(buffp);
+	free(buf);
 }
 
 /*-
diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c
index 04232672ac39..8193dc79a430 100644
--- a/usr.sbin/pkg/pkg.c
+++ b/usr.sbin/pkg/pkg.c
@@ -33,7 +33,6 @@ __FBSDID("$FreeBSD$");
 #include <sys/param.h>
 #include <sys/queue.h>
 #include <sys/types.h>
-#include <sys/sbuf.h>
 #include <sys/wait.h>
 
 #include <archive.h>
@@ -595,7 +594,9 @@ static struct pubkey *
 read_pubkey(int fd)
 {
 	struct pubkey *pk;
-	struct sbuf *sig;
+	char *sigb;
+	size_t sigsz;
+	FILE *sig;
 	char buf[4096];
 	int r;
 
@@ -604,18 +605,22 @@ read_pubkey(int fd)
 		return (NULL);
 	}
 
-	sig = sbuf_new_auto();
+	sigsz = 0;
+	sigb = NULL;
+	sig = open_memstream(&sigb, &sigsz);
+	if (sig == NULL)
+		err(EXIT_FAILURE, "open_memstream()");
 
 	while ((r = read(fd, buf, sizeof(buf))) >0) {
-		sbuf_bcat(sig, buf, r);
+		fwrite(buf, 1, r, sig);
 	}
 
-	sbuf_finish(sig);
+	fclose(sig);
 	pk = calloc(1, sizeof(struct pubkey));
-	pk->siglen = sbuf_len(sig);
+	pk->siglen = sigsz;
 	pk->sig = calloc(1, pk->siglen);
-	memcpy(pk->sig, sbuf_data(sig), pk->siglen);
-	sbuf_delete(sig);
+	memcpy(pk->sig, sigb, pk->siglen);
+	free(sigb);
 
 	return (pk);
 }
@@ -624,16 +629,17 @@ static struct sig_cert *
 parse_cert(int fd) {
 	int my_fd;
 	struct sig_cert *sc;
-	FILE *fp;
-	struct sbuf *buf, *sig, *cert;
+	FILE *fp, *sigfp, *certfp, *tmpfp;
 	char *line;
-	size_t linecap;
+	char *sig, *cert;
+	size_t linecap, sigsz, certsz;
 	ssize_t linelen;
 
-	buf = NULL;
 	sc = NULL;
 	line = NULL;
 	linecap = 0;
+	sig = cert = NULL;
+	sigfp = certfp = tmpfp = NULL;
 
 	if (lseek(fd, 0, 0) == -1) {
 		warn("lseek");
@@ -652,41 +658,38 @@ parse_cert(int fd) {
 		return (NULL);
 	}
 
-	sig = sbuf_new_auto();
-	cert = sbuf_new_auto();
+	sigsz = certsz = 0;
+	sigfp = open_memstream(&sig, &sigsz);
+	if (sigfp == NULL)
+		err(EXIT_FAILURE, "open_memstream()");
+	certfp = open_memstream(&cert, &certsz);
+	if (certfp == NULL)
+		err(EXIT_FAILURE, "open_memstream()");
 
 	while ((linelen = getline(&line, &linecap, fp)) > 0) {
 		if (strcmp(line, "SIGNATURE\n") == 0) {
-			buf = sig;
+			tmpfp = sigfp;
 			continue;
 		} else if (strcmp(line, "CERT\n") == 0) {
-			buf = cert;
+			tmpfp = certfp;
 			continue;
 		} else if (strcmp(line, "END\n") == 0) {
 			break;
 		}
-		if (buf != NULL)
-			sbuf_bcat(buf, line, linelen);
+		if (tmpfp != NULL)
+			fwrite(line, 1, linelen, tmpfp);
 	}
 
 	fclose(fp);
-
-	/* Trim out unrelated trailing newline */
-	sbuf_setpos(sig, sbuf_len(sig) - 1);
-
-	sbuf_finish(sig);
-	sbuf_finish(cert);
+	fclose(sigfp);
+	fclose(certfp);
 
 	sc = calloc(1, sizeof(struct sig_cert));
-	sc->siglen = sbuf_len(sig);
-	sc->sig = calloc(1, sc->siglen);
-	memcpy(sc->sig, sbuf_data(sig), sc->siglen);
-
-	sc->certlen = sbuf_len(cert);
-	sc->cert = strdup(sbuf_data(cert));
+	sc->siglen = sigsz -1; /* Trim out unrelated trailing newline */
+	sc->sig = sig;
 
-	sbuf_delete(sig);
-	sbuf_delete(cert);
+	sc->certlen = certsz;
+	sc->cert = cert;
 
 	return (sc);
 }


More information about the dev-commits-src-all mailing list