aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBaptiste Daroussin <bapt@FreeBSD.org>2021-04-27 02:38:55 +0000
committerBaptiste Daroussin <bapt@FreeBSD.org>2021-05-05 08:20:29 +0000
commit527a1eda9db8fada53b1434f71f12546fbaf16a4 (patch)
treec5d7482d0d9beed9221d2bcb94f89e995d7edb4d
parent1c6e6d014ae45650cfbc961198423e8dd172cc39 (diff)
downloadsrc-527a1eda9db8fada53b1434f71f12546fbaf16a4.tar.gz
src-527a1eda9db8fada53b1434f71f12546fbaf16a4.zip
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)
-rw-r--r--usr.sbin/pkg/Makefile1
-rw-r--r--usr.sbin/pkg/config.c33
-rw-r--r--usr.sbin/pkg/pkg.c67
3 files changed, 56 insertions, 45 deletions
diff --git a/usr.sbin/pkg/Makefile b/usr.sbin/pkg/Makefile
index ebfb71c1699b..725367ad4485 100644
--- a/usr.sbin/pkg/Makefile
+++ b/usr.sbin/pkg/Makefile
@@ -26,5 +26,6 @@ MAN= pkg.7
CFLAGS+=-I${SRCTOP}/contrib/libucl/include
.PATH: ${SRCTOP}/contrib/libucl/include
LIBADD= archive fetch ucl sbuf crypto ssl
+LIBADD= archive fetch ucl crypto ssl
.include <bsd.prog.mk>
diff --git a/usr.sbin/pkg/config.c b/usr.sbin/pkg/config.c
index 3b411859e9c4..f29bd586f338 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>
@@ -212,7 +211,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;
@@ -223,39 +224,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;
}
@@ -330,7 +336,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 c499fb28c42d..da3498438fa1 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);
}