From ce0455f7c490bab2a9940f0cec1a4b1e797ddfe2 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Tue, 25 Mar 2008 07:02:56 +0200 Subject: [PATCH] Refactor status-fd duplicate output code into a new function Based on a patch by Ian Jackson . --- ChangeLog | 14 ++++++++++++++ TODO | 1 - lib/dbmodify.c | 18 +----------------- lib/dpkg-db.h | 6 ------ lib/dpkg.h | 9 +++++++++ lib/log.c | 33 +++++++++++++++++++++++++++++++++ src/configure.c | 18 +----------------- src/errors.c | 18 +----------------- 8 files changed, 59 insertions(+), 58 deletions(-) diff --git a/ChangeLog b/ChangeLog index 57eef74d..562fdda8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2008-03-25 Ian Jackson , + Guillem Jover + + * lib/dbmodify.c (status_pipes): Move definitions to ... + * lib/log.c: ... here. + * lib/dpkg-db.h (struct pipef, status_pipes): Move declarations to ... + * lib/dpkg.h: ... here. + (statusfd_send): New function protoype. + * lib/log.c: Include and . + (statusfd_send): New function. + * lib/dbmodify.c: Use statusfd_send instead of duplicate code. + * src/configure.c: Likewise. + * src/errors.c: Likewise. + 2008-03-25 Guillem Jover * lib/dbmodify.c (log_file, log_message): Move to ... diff --git a/TODO b/TODO index 4fb96932..ec45fb29 100644 --- a/TODO +++ b/TODO @@ -20,7 +20,6 @@ lenny ------ * Code cleanup: - - Coalesce status_pipe handling. - blankfoo embedded code. - static variables inside functions. - Coalesce admindir / infodir / foodir generation. diff --git a/lib/dbmodify.c b/lib/dbmodify.c index 6abc4fe9..8be4042a 100644 --- a/lib/dbmodify.c +++ b/lib/dbmodify.c @@ -234,30 +234,14 @@ void modstatdb_shutdown(void) { free(updatefnbuf); } -struct pipef *status_pipes= NULL; - void modstatdb_note(struct pkginfo *pkg) { assert(cstatus >= msdbrw_write); onerr_abort++; - if (status_pipes) { - static struct varbuf *status= NULL; - struct pipef *pipef= status_pipes; - int r; - if (status == NULL) { - status = nfmalloc(sizeof(struct varbuf)); - varbufinit(status); - } else - varbufreset(status); - r= varbufprintf(status, "status: %s: %s\n", pkg->name, statusinfos[pkg->status].name); - while (pipef) { - write(pipef->fd, status->buf, r); - pipef= pipef->next; - } - } log_message("status %s %s %s", statusinfos[pkg->status].name, pkg->name, versiondescribe(&pkg->installed.version, vdew_nonambig)); + statusfd_send("status: %s: %s", pkg->name, statusinfos[pkg->status].name); varbufreset(&uvb); varbufrecord(&uvb, pkg, &pkg->installed); diff --git a/lib/dpkg-db.h b/lib/dpkg-db.h index 565d629f..de7764fb 100644 --- a/lib/dpkg-db.h +++ b/lib/dpkg-db.h @@ -166,12 +166,6 @@ enum modstatdb_rw { msdbrw_noavail= 0100, }; -struct pipef { - int fd; - struct pipef *next; -}; -extern struct pipef *status_pipes; - enum modstatdb_rw modstatdb_init(const char *admindir, enum modstatdb_rw reqrwflags); void modstatdb_note(struct pkginfo *pkg); void modstatdb_checkpoint(void); diff --git a/lib/dpkg.h b/lib/dpkg.h index d6c06f9d..b3dd47a7 100644 --- a/lib/dpkg.h +++ b/lib/dpkg.h @@ -212,6 +212,15 @@ void warningf(const char *fmt, ...) PRINTFFORMAT(1, 2); extern const char *log_file; void log_message(const char *fmt, ...) PRINTFFORMAT(1, 2); +/* FIXME: pipef and status_pipes should not be publicly exposed. */ +struct pipef { + int fd; + struct pipef *next; +}; +extern struct pipef *status_pipes; + +void statusfd_send(const char *fmt, ...); + /*** cleanup.c ***/ void cu_closefile(int argc, void **argv); diff --git a/lib/log.c b/lib/log.c index 2e607b74..23cd828b 100644 --- a/lib/log.c +++ b/lib/log.c @@ -21,8 +21,10 @@ #include +#include #include #include +#include #include #include @@ -72,3 +74,34 @@ log_message(const char *fmt, ...) fprintf(logfd, "%s %s\n", time_str, log->buf); } +struct pipef *status_pipes = NULL; + +void +statusfd_send(const char *fmt, ...) +{ + static struct varbuf vb; + struct pipef *pipef; + const char *p; + int r, l; + va_list al; + + if (!status_pipes) + return; + + va_start(al, fmt); + varbufreset(&vb); + varbufvprintf(&vb, fmt, al); + varbufaddc(&vb, '\n'); + va_end(al); + + for (pipef = status_pipes; pipef; pipef = pipef->next) { + for (p = vb.buf, l = vb.used; l; p += r, l -= r) { + r = write(pipef->fd, vb.buf, vb.used); + if (r < 0) + ohshite("unable to write to status fd %d", + pipef->fd); + assert(r && r <= l); + } + } +} + diff --git a/src/configure.c b/src/configure.c index 9c73f8dc..8f447fe1 100644 --- a/src/configure.c +++ b/src/configure.c @@ -531,25 +531,9 @@ static enum conffopt promptconfaction(const char* cfgfile, const char* realold, if (!(what&cfof_prompt)) return what; - /* if there is a status pipe, send conffile-prompt there */ - if (status_pipes) { - static struct varbuf *status= NULL; - struct pipef *pipef= status_pipes; - int r; - if (status == NULL) { - status = nfmalloc(sizeof(struct varbuf)); - varbufinit(status); - } else - varbufreset(status); - - r= varbufprintf(status, "status: %s : %s : '%s' '%s' %i %i \n", + statusfd_send("status: %s : %s : '%s' '%s' %i %i ", cfgfile, "conffile-prompt", realold, realnew, useredited, distedited); - while (pipef) { - write(pipef->fd, status->buf, r); - pipef= pipef->next; - } - } do { /* Flush the terminal's input in case the user diff --git a/src/errors.c b/src/errors.c index ab2c9d27..83daa0f0 100644 --- a/src/errors.c +++ b/src/errors.c @@ -56,23 +56,7 @@ void print_error_perpackage(const char *emsg, const char *arg) { fprintf(stderr, _("%s: error processing %s (--%s):\n %s\n"), DPKG, arg, cipaction->olong, emsg); - if (status_pipes) { - static struct varbuf *status= NULL; - struct pipef *pipef= status_pipes; - int r; - if (status == NULL) { - status = nfmalloc(sizeof(struct varbuf)); - varbufinit(status); - } else - varbufreset(status); - - r= varbufprintf(status, "status: %s : %s : %s\n", arg, "error",emsg); - while (pipef) { - write(pipef->fd, status->buf, r); - pipef= pipef->next; - } - } - + statusfd_send("status: %s : %s : %s", arg, "error", emsg); nr= malloc(sizeof(struct error_report)); if (!nr) { -- 2.39.5