mirror of
https://github.com/opnsense/src.git
synced 2026-06-10 17:22:46 -04:00
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 cc9a8a116d)
This commit is contained in:
parent
d39a1652d4
commit
63d098d8d8
3 changed files with 56 additions and 46 deletions
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/*-
|
||||
|
|
|
|||
|
|
@ -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->siglen = sigsz -1; /* Trim out unrelated trailing newline */
|
||||
sc->sig = sig;
|
||||
|
||||
sc->certlen = sbuf_len(cert);
|
||||
sc->cert = strdup(sbuf_data(cert));
|
||||
|
||||
sbuf_delete(sig);
|
||||
sbuf_delete(cert);
|
||||
sc->certlen = certsz;
|
||||
sc->cert = cert;
|
||||
|
||||
return (sc);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue