Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 May 2021 07:50:29 GMT
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 63d098d8d8d6 - stable/13 - pkg(7): replace usage of sbuf(9) with open_memstream(3)
Message-ID:  <202105050750.1457oTTb083244@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by bapt:

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

commit 63d098d8d8d6bb12e7e2652ebb338e526906d83a
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2021-04-27 02:38:55 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2021-05-05 07:50:02 +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
    
    (cherry picked from commit cc9a8a116d19daf224222506441e91a3d329160e)
---
 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 70c745900931..97983ea1f58a 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>
@@ -216,7 +215,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;
@@ -227,39 +228,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;
 		}
 
@@ -334,7 +340,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);
 }



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